1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
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 */
63 /* extreme statistics */
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;
72 int32_t rte_service_init(void)
74 if (rte_service_library_initialized) {
75 printf("service library init() called, init flag %d\n",
76 rte_service_library_initialized);
80 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
81 sizeof(struct rte_service_spec_impl),
84 printf("error allocating rte services array\n");
88 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
89 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
91 printf("error allocating core states array\n");
97 struct rte_config *cfg = rte_eal_get_configuration();
98 for (i = 0; i < RTE_MAX_LCORE; i++) {
99 if (lcore_config[i].core_role == ROLE_SERVICE) {
100 if ((unsigned int)i == cfg->master_lcore)
102 rte_service_lcore_add(i);
107 rte_service_library_initialized = 1;
111 rte_free(rte_services);
113 rte_free(lcore_states);
117 void rte_service_finalize(void)
119 if (!rte_service_library_initialized)
123 rte_free(rte_services);
126 rte_free(lcore_states);
128 rte_service_library_initialized = 0;
131 /* returns 1 if service is registered and has not been unregistered
132 * Returns 0 if service never registered, or has been unregistered
135 service_valid(uint32_t id)
137 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
140 /* validate ID and retrieve service pointer, or return error value */
141 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
142 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
144 service = &rte_services[id]; \
147 /* returns 1 if statistics should be collected for service
148 * Returns 0 if statistics should not be collected for service
151 service_stats_enabled(struct rte_service_spec_impl *impl)
153 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
157 service_mt_safe(struct rte_service_spec_impl *s)
159 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
162 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enabled)
164 struct rte_service_spec_impl *s;
165 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
168 s->internal_flags |= SERVICE_F_STATS_ENABLED;
170 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
175 int32_t 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;
194 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id)
200 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
201 if (service_valid(i) &&
202 strcmp(name, rte_services[i].spec.name) == 0) {
212 rte_service_get_name(uint32_t id)
214 struct rte_service_spec_impl *s;
215 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
220 rte_service_probe_capability(uint32_t id, uint32_t capability)
222 struct rte_service_spec_impl *s;
223 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
224 return !!(s->spec.capabilities & capability);
228 rte_service_component_register(const struct rte_service_spec *spec,
232 int32_t free_slot = -1;
234 if (spec->callback == NULL || strlen(spec->name) == 0)
237 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
238 if (!service_valid(i)) {
244 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
247 struct rte_service_spec_impl *s = &rte_services[free_slot];
249 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
261 rte_service_component_unregister(uint32_t id)
264 struct rte_service_spec_impl *s;
265 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
270 s->internal_flags &= ~(SERVICE_F_REGISTERED);
272 /* clear the run-bit in all cores */
273 for (i = 0; i < RTE_MAX_LCORE; i++)
274 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
276 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
282 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
284 struct rte_service_spec_impl *s;
285 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
288 s->comp_runstate = RUNSTATE_RUNNING;
290 s->comp_runstate = RUNSTATE_STOPPED;
297 rte_service_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);
303 s->app_runstate = RUNSTATE_RUNNING;
305 s->app_runstate = RUNSTATE_STOPPED;
312 rte_service_runstate_get(uint32_t id)
314 struct rte_service_spec_impl *s;
315 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
318 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
319 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
321 return (s->app_runstate == RUNSTATE_RUNNING) &&
322 (s->comp_runstate == RUNSTATE_RUNNING) &&
323 (check_disabled | lcore_mapped);
327 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
328 struct core_state *cs, uint32_t service_idx)
330 void *userdata = s->spec.callback_userdata;
332 if (service_stats_enabled(s)) {
333 uint64_t start = rte_rdtsc();
334 s->spec.callback(userdata);
335 uint64_t end = rte_rdtsc();
336 s->cycles_spent += end - start;
337 cs->calls_per_service[service_idx]++;
340 s->spec.callback(userdata);
344 static inline int32_t
345 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask)
347 if (!service_valid(i))
349 struct rte_service_spec_impl *s = &rte_services[i];
350 if (s->comp_runstate != RUNSTATE_RUNNING ||
351 s->app_runstate != RUNSTATE_RUNNING ||
352 !(service_mask & (UINT64_C(1) << i)))
355 /* check do we need cmpset, if MT safe or <= 1 core
356 * mapped, atomic ops are not required.
358 const int use_atomics = (service_mt_safe(s) == 0) &&
359 (rte_atomic32_read(&s->num_mapped_cores) > 1);
361 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
364 rte_service_runner_do_callback(s, cs, i);
365 rte_atomic32_clear(&s->execute_lock);
367 rte_service_runner_do_callback(s, cs, i);
372 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
373 uint32_t serialize_mt_unsafe)
375 /* run service on calling core, using all-ones as the service mask */
376 if (!service_valid(id))
379 struct core_state *cs = &lcore_states[rte_lcore_id()];
380 struct rte_service_spec_impl *s = &rte_services[id];
382 /* Atomically add this core to the mapped cores first, then examine if
383 * we can run the service. This avoids a race condition between
384 * checking the value, and atomically adding to the mapped count.
386 if (serialize_mt_unsafe)
387 rte_atomic32_inc(&s->num_mapped_cores);
389 if (service_mt_safe(s) == 0 &&
390 rte_atomic32_read(&s->num_mapped_cores) > 1) {
391 if (serialize_mt_unsafe)
392 rte_atomic32_dec(&s->num_mapped_cores);
396 int ret = service_run(id, cs, UINT64_MAX);
398 if (serialize_mt_unsafe)
399 rte_atomic32_dec(&s->num_mapped_cores);
405 rte_service_runner_func(void *arg)
409 const int lcore = rte_lcore_id();
410 struct core_state *cs = &lcore_states[lcore];
412 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
413 const uint64_t service_mask = cs->service_mask;
415 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
416 /* return value ignored as no change to code flow */
417 service_run(i, cs, service_mask);
423 lcore_config[lcore].state = WAIT;
429 rte_service_lcore_count(void)
433 for (i = 0; i < RTE_MAX_LCORE; i++)
434 count += lcore_states[i].is_service_core;
439 rte_service_lcore_list(uint32_t array[], uint32_t n)
441 uint32_t count = rte_service_lcore_count();
450 for (i = 0; i < RTE_MAX_LCORE; i++) {
451 struct core_state *cs = &lcore_states[i];
452 if (cs->is_service_core) {
462 rte_service_lcore_count_services(uint32_t lcore)
464 if (lcore >= RTE_MAX_LCORE)
467 struct core_state *cs = &lcore_states[lcore];
468 if (!cs->is_service_core)
471 return __builtin_popcountll(cs->service_mask);
475 rte_service_start_with_defaults(void)
477 /* create a default mapping from cores to services, then start the
478 * services to make them transparent to unaware applications.
482 uint32_t count = rte_service_get_count();
484 int32_t lcore_iter = 0;
485 uint32_t ids[RTE_MAX_LCORE] = {0};
486 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
488 if (lcore_count == 0)
491 for (i = 0; (int)i < lcore_count; i++)
492 rte_service_lcore_start(ids[i]);
494 for (i = 0; i < count; i++) {
495 /* do 1:1 core mapping here, with each service getting
496 * assigned a single core by default. Adding multiple services
497 * should multiplex to a single core, or 1:1 if there are the
498 * same amount of services as service-cores
500 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
505 if (lcore_iter >= lcore_count)
508 ret = rte_service_runstate_set(i, 1);
517 service_update(struct rte_service_spec *service, uint32_t lcore,
518 uint32_t *set, uint32_t *enabled)
523 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
524 if ((struct rte_service_spec *)&rte_services[i] == service &&
531 if (sid == -1 || lcore >= RTE_MAX_LCORE)
534 if (!lcore_states[lcore].is_service_core)
537 uint64_t sid_mask = UINT64_C(1) << sid;
539 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
542 if (*set && !lcore_mapped) {
543 lcore_states[lcore].service_mask |= sid_mask;
544 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
546 if (!*set && lcore_mapped) {
547 lcore_states[lcore].service_mask &= ~(sid_mask);
548 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
553 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
561 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
563 struct rte_service_spec_impl *s;
564 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
565 uint32_t on = enabled > 0;
566 return service_update(&s->spec, lcore, &on, 0);
570 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
572 struct rte_service_spec_impl *s;
573 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
575 int ret = service_update(&s->spec, lcore, 0, &enabled);
582 set_lcore_state(uint32_t lcore, int32_t state)
584 /* mark core state in hugepage backed config */
585 struct rte_config *cfg = rte_eal_get_configuration();
586 cfg->lcore_role[lcore] = state;
588 /* mark state in process local lcore_config */
589 lcore_config[lcore].core_role = state;
591 /* update per-lcore optimized state tracking */
592 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
595 int32_t rte_service_lcore_reset_all(void)
597 /* loop over cores, reset all to mask 0 */
599 for (i = 0; i < RTE_MAX_LCORE; i++) {
600 if (lcore_states[i].is_service_core) {
601 lcore_states[i].service_mask = 0;
602 set_lcore_state(i, ROLE_RTE);
603 lcore_states[i].runstate = RUNSTATE_STOPPED;
606 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
607 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
615 rte_service_lcore_add(uint32_t lcore)
617 if (lcore >= RTE_MAX_LCORE)
619 if (lcore_states[lcore].is_service_core)
622 set_lcore_state(lcore, ROLE_SERVICE);
624 /* ensure that after adding a core the mask and state are defaults */
625 lcore_states[lcore].service_mask = 0;
626 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
630 return rte_eal_wait_lcore(lcore);
634 rte_service_lcore_del(uint32_t lcore)
636 if (lcore >= RTE_MAX_LCORE)
639 struct core_state *cs = &lcore_states[lcore];
640 if (!cs->is_service_core)
643 if (cs->runstate != RUNSTATE_STOPPED)
646 set_lcore_state(lcore, ROLE_RTE);
653 rte_service_lcore_start(uint32_t lcore)
655 if (lcore >= RTE_MAX_LCORE)
658 struct core_state *cs = &lcore_states[lcore];
659 if (!cs->is_service_core)
662 if (cs->runstate == RUNSTATE_RUNNING)
665 /* set core to run state first, and then launch otherwise it will
666 * return immediately as runstate keeps it in the service poll loop
668 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
670 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
671 /* returns -EBUSY if the core is already launched, 0 on success */
676 rte_service_lcore_stop(uint32_t lcore)
678 if (lcore >= RTE_MAX_LCORE)
681 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
685 uint64_t service_mask = lcore_states[lcore].service_mask;
686 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
687 int32_t enabled = service_mask & (UINT64_C(1) << i);
688 int32_t service_running = rte_service_runstate_get(i);
689 int32_t only_core = (1 ==
690 rte_atomic32_read(&rte_services[i].num_mapped_cores));
692 /* if the core is mapped, and the service is running, and this
693 * is the only core that is mapped, the service would cease to
694 * run if this core stopped, so fail instead.
696 if (enabled && service_running && only_core)
700 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
706 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint32_t *attr_value)
708 struct rte_service_spec_impl *s;
709 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
715 case RTE_SERVICE_ATTR_CYCLES:
716 *attr_value = s->cycles_spent;
718 case RTE_SERVICE_ATTR_CALL_COUNT:
719 *attr_value = s->calls;
727 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
728 uint64_t all_cycles, uint32_t reset)
730 /* avoid divide by zero */
744 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
745 PRIu64"\tavg: %"PRIu64"\n",
746 s->spec.name, service_stats_enabled(s), s->calls,
747 s->cycles_spent, s->cycles_spent / calls);
751 rte_service_attr_reset_all(uint32_t id)
753 struct rte_service_spec_impl *s;
754 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
757 rte_service_dump_one(NULL, s, 0, reset);
762 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
765 struct core_state *cs = &lcore_states[lcore];
767 fprintf(f, "%02d\t", lcore);
768 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
769 if (!service_valid(i))
771 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
773 cs->calls_per_service[i] = 0;
778 int32_t rte_service_dump(FILE *f, uint32_t id)
781 int print_one = (id != UINT32_MAX);
783 uint64_t total_cycles = 0;
785 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
786 if (!service_valid(i))
788 total_cycles += rte_services[i].cycles_spent;
791 /* print only the specified service */
793 struct rte_service_spec_impl *s;
794 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
795 fprintf(f, "Service %s Summary\n", s->spec.name);
797 rte_service_dump_one(f, s, total_cycles, reset);
801 /* print all services, as UINT32_MAX was passed as id */
802 fprintf(f, "Services Summary\n");
803 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
804 if (!service_valid(i))
807 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
810 fprintf(f, "Service Cores Summary\n");
811 for (i = 0; i < RTE_MAX_LCORE; i++) {
812 if (lcore_config[i].core_role != ROLE_SERVICE)
816 service_dump_calls_per_lcore(f, i, reset);