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>
24 #include "eal_private.h"
26 #define RTE_SERVICE_NUM_MAX 64
28 #define SERVICE_F_REGISTERED (1 << 0)
29 #define SERVICE_F_STATS_ENABLED (1 << 1)
30 #define SERVICE_F_START_CHECK (1 << 2)
32 /* runstates for services and lcores, denoting if they are active or not */
33 #define RUNSTATE_STOPPED 0
34 #define RUNSTATE_RUNNING 1
36 /* internal representation of a service */
37 struct rte_service_spec_impl {
38 /* public part of the struct */
39 struct rte_service_spec spec;
41 /* atomic lock that when set indicates a service core is currently
42 * running this service callback. When not set, a core may take the
43 * lock and then run the service callback.
45 rte_atomic32_t execute_lock;
47 /* API set/get-able variables */
50 uint8_t internal_flags;
52 /* per service statistics */
53 /* Indicates how many cores the service is mapped to run on.
54 * It does not indicate the number of cores the service is running
57 rte_atomic32_t num_mapped_cores;
59 uint64_t cycles_spent;
60 } __rte_cache_aligned;
62 /* the internal values of a service core */
64 /* map of services IDs are run on this core */
65 uint64_t service_mask;
66 uint8_t runstate; /* running or stopped */
67 uint8_t is_service_core; /* set if core is currently a service core */
68 uint8_t service_active_on_lcore[RTE_SERVICE_NUM_MAX];
70 uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
71 } __rte_cache_aligned;
73 static uint32_t rte_service_count;
74 static struct rte_service_spec_impl *rte_services;
75 static struct core_state *lcore_states;
76 static uint32_t rte_service_library_initialized;
79 rte_service_init(void)
81 if (rte_service_library_initialized) {
83 "service library init() called, init flag %d\n",
84 rte_service_library_initialized);
88 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
89 sizeof(struct rte_service_spec_impl),
92 RTE_LOG(ERR, EAL, "error allocating rte services array\n");
96 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
97 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
99 RTE_LOG(ERR, EAL, "error allocating core states array\n");
105 struct rte_config *cfg = rte_eal_get_configuration();
106 for (i = 0; i < RTE_MAX_LCORE; i++) {
107 if (lcore_config[i].core_role == ROLE_SERVICE) {
108 if ((unsigned int)i == cfg->master_lcore)
110 rte_service_lcore_add(i);
115 rte_service_library_initialized = 1;
118 rte_free(rte_services);
119 rte_free(lcore_states);
124 rte_service_finalize(void)
126 if (!rte_service_library_initialized)
129 rte_service_lcore_reset_all();
130 rte_eal_mp_wait_lcore();
132 rte_free(rte_services);
133 rte_free(lcore_states);
135 rte_service_library_initialized = 0;
138 /* returns 1 if service is registered and has not been unregistered
139 * Returns 0 if service never registered, or has been unregistered
142 service_valid(uint32_t id)
144 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
147 static struct rte_service_spec_impl *
148 service_get(uint32_t id)
150 return &rte_services[id];
153 /* validate ID and retrieve service pointer, or return error value */
154 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
155 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
157 service = &rte_services[id]; \
160 /* returns 1 if statistics should be collected for service
161 * Returns 0 if statistics should not be collected for service
164 service_stats_enabled(struct rte_service_spec_impl *impl)
166 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
170 service_mt_safe(struct rte_service_spec_impl *s)
172 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
176 rte_service_set_stats_enable(uint32_t id, int32_t enabled)
178 struct rte_service_spec_impl *s;
179 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
182 s->internal_flags |= SERVICE_F_STATS_ENABLED;
184 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
190 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
192 struct rte_service_spec_impl *s;
193 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
196 s->internal_flags |= SERVICE_F_START_CHECK;
198 s->internal_flags &= ~(SERVICE_F_START_CHECK);
204 rte_service_get_count(void)
206 return rte_service_count;
210 rte_service_get_by_name(const char *name, uint32_t *service_id)
216 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
217 if (service_valid(i) &&
218 strcmp(name, rte_services[i].spec.name) == 0) {
228 rte_service_get_name(uint32_t id)
230 struct rte_service_spec_impl *s;
231 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
236 rte_service_probe_capability(uint32_t id, uint32_t capability)
238 struct rte_service_spec_impl *s;
239 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
240 return !!(s->spec.capabilities & capability);
244 rte_service_component_register(const struct rte_service_spec *spec,
248 int32_t free_slot = -1;
250 if (spec->callback == NULL || strlen(spec->name) == 0)
253 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
254 if (!service_valid(i)) {
260 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
263 struct rte_service_spec_impl *s = &rte_services[free_slot];
265 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);
286 s->internal_flags &= ~(SERVICE_F_REGISTERED);
288 /* clear the run-bit in all cores */
289 for (i = 0; i < RTE_MAX_LCORE; i++)
290 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
292 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
298 rte_service_component_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->comp_runstate = RUNSTATE_RUNNING;
306 s->comp_runstate = RUNSTATE_STOPPED;
313 rte_service_runstate_set(uint32_t id, uint32_t runstate)
315 struct rte_service_spec_impl *s;
316 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
319 s->app_runstate = RUNSTATE_RUNNING;
321 s->app_runstate = RUNSTATE_STOPPED;
328 rte_service_runstate_get(uint32_t id)
330 struct rte_service_spec_impl *s;
331 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
334 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
335 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
337 return (s->app_runstate == RUNSTATE_RUNNING) &&
338 (s->comp_runstate == RUNSTATE_RUNNING) &&
339 (check_disabled | lcore_mapped);
343 service_runner_do_callback(struct rte_service_spec_impl *s,
344 struct core_state *cs, uint32_t service_idx)
346 void *userdata = s->spec.callback_userdata;
348 if (service_stats_enabled(s)) {
349 uint64_t start = rte_rdtsc();
350 s->spec.callback(userdata);
351 uint64_t end = rte_rdtsc();
352 s->cycles_spent += end - start;
353 cs->calls_per_service[service_idx]++;
356 s->spec.callback(userdata);
360 /* Expects the service 's' is valid. */
362 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask,
363 struct rte_service_spec_impl *s, uint32_t serialize_mt_unsafe)
368 if (s->comp_runstate != RUNSTATE_RUNNING ||
369 s->app_runstate != RUNSTATE_RUNNING ||
370 !(service_mask & (UINT64_C(1) << i))) {
371 cs->service_active_on_lcore[i] = 0;
375 cs->service_active_on_lcore[i] = 1;
377 if ((service_mt_safe(s) == 0) && (serialize_mt_unsafe == 1)) {
378 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
381 service_runner_do_callback(s, cs, i);
382 rte_atomic32_clear(&s->execute_lock);
384 service_runner_do_callback(s, cs, i);
390 rte_service_may_be_active(uint32_t id)
392 uint32_t ids[RTE_MAX_LCORE] = {0};
393 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
396 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))
399 for (i = 0; i < lcore_count; i++) {
400 if (lcore_states[i].service_active_on_lcore[id])
408 rte_service_run_iter_on_app_lcore(uint32_t id, uint32_t serialize_mt_unsafe)
410 struct core_state *cs = &lcore_states[rte_lcore_id()];
411 struct rte_service_spec_impl *s;
413 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
415 /* Increment num_mapped_cores to reflect that this core is
416 * now mapped capable of running the service.
418 rte_atomic32_inc(&s->num_mapped_cores);
420 int ret = service_run(id, cs, UINT64_MAX, s, serialize_mt_unsafe);
422 rte_atomic32_dec(&s->num_mapped_cores);
428 service_runner_func(void *arg)
432 const int lcore = rte_lcore_id();
433 struct core_state *cs = &lcore_states[lcore];
435 while (cs->runstate == RUNSTATE_RUNNING) {
436 const uint64_t service_mask = cs->service_mask;
438 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
439 if (!service_valid(i))
441 /* return value ignored as no change to code flow */
442 service_run(i, cs, service_mask, service_get(i), 1);
450 lcore_config[lcore].state = WAIT;
456 rte_service_lcore_count(void)
460 for (i = 0; i < RTE_MAX_LCORE; i++)
461 count += lcore_states[i].is_service_core;
466 rte_service_lcore_list(uint32_t array[], uint32_t n)
468 uint32_t count = rte_service_lcore_count();
477 for (i = 0; i < RTE_MAX_LCORE; i++) {
478 struct core_state *cs = &lcore_states[i];
479 if (cs->is_service_core) {
489 rte_service_lcore_count_services(uint32_t lcore)
491 if (lcore >= RTE_MAX_LCORE)
494 struct core_state *cs = &lcore_states[lcore];
495 if (!cs->is_service_core)
498 return __builtin_popcountll(cs->service_mask);
502 rte_service_start_with_defaults(void)
504 /* create a default mapping from cores to services, then start the
505 * services to make them transparent to unaware applications.
509 uint32_t count = rte_service_get_count();
511 int32_t lcore_iter = 0;
512 uint32_t ids[RTE_MAX_LCORE] = {0};
513 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
515 if (lcore_count == 0)
518 for (i = 0; (int)i < lcore_count; i++)
519 rte_service_lcore_start(ids[i]);
521 for (i = 0; i < count; i++) {
522 /* do 1:1 core mapping here, with each service getting
523 * assigned a single core by default. Adding multiple services
524 * should multiplex to a single core, or 1:1 if there are the
525 * same amount of services as service-cores
527 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
532 if (lcore_iter >= lcore_count)
535 ret = rte_service_runstate_set(i, 1);
544 service_update(uint32_t sid, uint32_t lcore, uint32_t *set, uint32_t *enabled)
546 /* validate ID, or return error value */
547 if (sid >= RTE_SERVICE_NUM_MAX || !service_valid(sid) ||
548 lcore >= RTE_MAX_LCORE || !lcore_states[lcore].is_service_core)
551 uint64_t sid_mask = UINT64_C(1) << sid;
553 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
556 if (*set && !lcore_mapped) {
557 lcore_states[lcore].service_mask |= sid_mask;
558 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
560 if (!*set && lcore_mapped) {
561 lcore_states[lcore].service_mask &= ~(sid_mask);
562 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
567 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
575 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
577 uint32_t on = enabled > 0;
578 return service_update(id, lcore, &on, 0);
582 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
585 int ret = service_update(id, lcore, 0, &enabled);
592 set_lcore_state(uint32_t lcore, int32_t state)
594 /* mark core state in hugepage backed config */
595 struct rte_config *cfg = rte_eal_get_configuration();
596 cfg->lcore_role[lcore] = state;
598 /* mark state in process local lcore_config */
599 lcore_config[lcore].core_role = state;
601 /* update per-lcore optimized state tracking */
602 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
606 rte_service_lcore_reset_all(void)
608 /* loop over cores, reset all to mask 0 */
610 for (i = 0; i < RTE_MAX_LCORE; i++) {
611 if (lcore_states[i].is_service_core) {
612 lcore_states[i].service_mask = 0;
613 set_lcore_state(i, ROLE_RTE);
614 lcore_states[i].runstate = RUNSTATE_STOPPED;
617 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
618 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
626 rte_service_lcore_add(uint32_t lcore)
628 if (lcore >= RTE_MAX_LCORE)
630 if (lcore_states[lcore].is_service_core)
633 set_lcore_state(lcore, ROLE_SERVICE);
635 /* ensure that after adding a core the mask and state are defaults */
636 lcore_states[lcore].service_mask = 0;
637 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
641 return rte_eal_wait_lcore(lcore);
645 rte_service_lcore_del(uint32_t lcore)
647 if (lcore >= RTE_MAX_LCORE)
650 struct core_state *cs = &lcore_states[lcore];
651 if (!cs->is_service_core)
654 if (cs->runstate != RUNSTATE_STOPPED)
657 set_lcore_state(lcore, ROLE_RTE);
664 rte_service_lcore_start(uint32_t lcore)
666 if (lcore >= RTE_MAX_LCORE)
669 struct core_state *cs = &lcore_states[lcore];
670 if (!cs->is_service_core)
673 if (cs->runstate == RUNSTATE_RUNNING)
676 /* set core to run state first, and then launch otherwise it will
677 * return immediately as runstate keeps it in the service poll loop
679 cs->runstate = RUNSTATE_RUNNING;
681 int ret = rte_eal_remote_launch(service_runner_func, 0, lcore);
682 /* returns -EBUSY if the core is already launched, 0 on success */
687 rte_service_lcore_stop(uint32_t lcore)
689 if (lcore >= RTE_MAX_LCORE)
692 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
696 uint64_t service_mask = lcore_states[lcore].service_mask;
697 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
698 int32_t enabled = service_mask & (UINT64_C(1) << i);
699 int32_t service_running = rte_service_runstate_get(i);
700 int32_t only_core = (1 ==
701 rte_atomic32_read(&rte_services[i].num_mapped_cores));
703 /* if the core is mapped, and the service is running, and this
704 * is the only core that is mapped, the service would cease to
705 * run if this core stopped, so fail instead.
707 if (enabled && service_running && only_core)
711 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
717 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint64_t *attr_value)
719 struct rte_service_spec_impl *s;
720 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
726 case RTE_SERVICE_ATTR_CYCLES:
727 *attr_value = s->cycles_spent;
729 case RTE_SERVICE_ATTR_CALL_COUNT:
730 *attr_value = s->calls;
738 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
739 uint64_t *attr_value)
741 struct core_state *cs;
743 if (lcore >= RTE_MAX_LCORE || !attr_value)
746 cs = &lcore_states[lcore];
747 if (!cs->is_service_core)
751 case RTE_SERVICE_LCORE_ATTR_LOOPS:
752 *attr_value = cs->loops;
760 service_dump_one(FILE *f, struct rte_service_spec_impl *s, uint32_t reset)
762 /* avoid divide by zero */
776 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
777 PRIu64"\tavg: %"PRIu64"\n",
778 s->spec.name, service_stats_enabled(s), s->calls,
779 s->cycles_spent, s->cycles_spent / calls);
783 rte_service_attr_reset_all(uint32_t id)
785 struct rte_service_spec_impl *s;
786 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
789 service_dump_one(NULL, s, reset);
794 rte_service_lcore_attr_reset_all(uint32_t lcore)
796 struct core_state *cs;
798 if (lcore >= RTE_MAX_LCORE)
801 cs = &lcore_states[lcore];
802 if (!cs->is_service_core)
811 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
814 struct core_state *cs = &lcore_states[lcore];
816 fprintf(f, "%02d\t", lcore);
817 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
818 if (!service_valid(i))
820 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
822 cs->calls_per_service[i] = 0;
828 rte_service_dump(FILE *f, uint32_t id)
831 int print_one = (id != UINT32_MAX);
833 /* print only the specified service */
835 struct rte_service_spec_impl *s;
836 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
837 fprintf(f, "Service %s Summary\n", s->spec.name);
839 service_dump_one(f, s, reset);
843 /* print all services, as UINT32_MAX was passed as id */
844 fprintf(f, "Services Summary\n");
845 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
846 if (!service_valid(i))
849 service_dump_one(f, &rte_services[i], reset);
852 fprintf(f, "Service Cores Summary\n");
853 for (i = 0; i < RTE_MAX_LCORE; i++) {
854 if (lcore_config[i].core_role != ROLE_SERVICE)
858 service_dump_calls_per_lcore(f, i, reset);