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 #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 rte_atomic32_t num_mapped_cores;
55 uint64_t cycles_spent;
56 } __rte_cache_aligned;
58 /* the internal values of a service core */
60 /* map of services IDs are run on this core */
61 uint64_t service_mask;
62 uint8_t runstate; /* running or stopped */
63 uint8_t is_service_core; /* set if core is currently a service core */
64 uint8_t service_active_on_lcore[RTE_SERVICE_NUM_MAX];
66 uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
67 } __rte_cache_aligned;
69 static uint32_t rte_service_count;
70 static struct rte_service_spec_impl *rte_services;
71 static struct core_state *lcore_states;
72 static uint32_t rte_service_library_initialized;
75 rte_service_init(void)
77 if (rte_service_library_initialized) {
79 "service library init() called, init flag %d\n",
80 rte_service_library_initialized);
84 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
85 sizeof(struct rte_service_spec_impl),
88 RTE_LOG(ERR, EAL, "error allocating rte services array\n");
92 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
93 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
95 RTE_LOG(ERR, EAL, "error allocating core states array\n");
101 struct rte_config *cfg = rte_eal_get_configuration();
102 for (i = 0; i < RTE_MAX_LCORE; i++) {
103 if (lcore_config[i].core_role == ROLE_SERVICE) {
104 if ((unsigned int)i == cfg->master_lcore)
106 rte_service_lcore_add(i);
111 rte_service_library_initialized = 1;
114 rte_free(rte_services);
115 rte_free(lcore_states);
120 rte_service_finalize(void)
122 if (!rte_service_library_initialized)
125 rte_service_lcore_reset_all();
126 rte_eal_mp_wait_lcore();
128 rte_free(rte_services);
129 rte_free(lcore_states);
131 rte_service_library_initialized = 0;
134 /* returns 1 if service is registered and has not been unregistered
135 * Returns 0 if service never registered, or has been unregistered
138 service_valid(uint32_t id)
140 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
143 static struct rte_service_spec_impl *
144 service_get(uint32_t id)
146 return &rte_services[id];
149 /* validate ID and retrieve service pointer, or return error value */
150 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
151 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
153 service = &rte_services[id]; \
156 /* returns 1 if statistics should be collected for service
157 * Returns 0 if statistics should not be collected for service
160 service_stats_enabled(struct rte_service_spec_impl *impl)
162 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
166 service_mt_safe(struct rte_service_spec_impl *s)
168 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
172 rte_service_set_stats_enable(uint32_t id, int32_t enabled)
174 struct rte_service_spec_impl *s;
175 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
178 s->internal_flags |= SERVICE_F_STATS_ENABLED;
180 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
186 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
188 struct rte_service_spec_impl *s;
189 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
192 s->internal_flags |= SERVICE_F_START_CHECK;
194 s->internal_flags &= ~(SERVICE_F_START_CHECK);
200 rte_service_get_count(void)
202 return rte_service_count;
206 rte_service_get_by_name(const char *name, uint32_t *service_id)
212 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
213 if (service_valid(i) &&
214 strcmp(name, rte_services[i].spec.name) == 0) {
224 rte_service_get_name(uint32_t id)
226 struct rte_service_spec_impl *s;
227 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
232 rte_service_probe_capability(uint32_t id, uint32_t capability)
234 struct rte_service_spec_impl *s;
235 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
236 return !!(s->spec.capabilities & capability);
240 rte_service_component_register(const struct rte_service_spec *spec,
244 int32_t free_slot = -1;
246 if (spec->callback == NULL || strlen(spec->name) == 0)
249 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
250 if (!service_valid(i)) {
256 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
259 struct rte_service_spec_impl *s = &rte_services[free_slot];
261 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
273 rte_service_component_unregister(uint32_t id)
276 struct rte_service_spec_impl *s;
277 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
282 s->internal_flags &= ~(SERVICE_F_REGISTERED);
284 /* clear the run-bit in all cores */
285 for (i = 0; i < RTE_MAX_LCORE; i++)
286 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
288 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
294 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
296 struct rte_service_spec_impl *s;
297 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
300 s->comp_runstate = RUNSTATE_RUNNING;
302 s->comp_runstate = RUNSTATE_STOPPED;
309 rte_service_runstate_set(uint32_t id, uint32_t runstate)
311 struct rte_service_spec_impl *s;
312 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
315 s->app_runstate = RUNSTATE_RUNNING;
317 s->app_runstate = RUNSTATE_STOPPED;
324 rte_service_runstate_get(uint32_t id)
326 struct rte_service_spec_impl *s;
327 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
330 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
331 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
333 return (s->app_runstate == RUNSTATE_RUNNING) &&
334 (s->comp_runstate == RUNSTATE_RUNNING) &&
335 (check_disabled | lcore_mapped);
339 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
340 struct core_state *cs, uint32_t service_idx)
342 void *userdata = s->spec.callback_userdata;
344 if (service_stats_enabled(s)) {
345 uint64_t start = rte_rdtsc();
346 s->spec.callback(userdata);
347 uint64_t end = rte_rdtsc();
348 s->cycles_spent += end - start;
349 cs->calls_per_service[service_idx]++;
352 s->spec.callback(userdata);
356 /* Expects the service 's' is valid. */
358 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask,
359 struct rte_service_spec_impl *s)
364 if (s->comp_runstate != RUNSTATE_RUNNING ||
365 s->app_runstate != RUNSTATE_RUNNING ||
366 !(service_mask & (UINT64_C(1) << i))) {
367 cs->service_active_on_lcore[i] = 0;
371 cs->service_active_on_lcore[i] = 1;
373 /* check do we need cmpset, if MT safe or <= 1 core
374 * mapped, atomic ops are not required.
376 const int use_atomics = (service_mt_safe(s) == 0) &&
377 (rte_atomic32_read(&s->num_mapped_cores) > 1);
379 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
382 rte_service_runner_do_callback(s, cs, i);
383 rte_atomic32_clear(&s->execute_lock);
385 rte_service_runner_do_callback(s, cs, i);
391 rte_service_may_be_active(uint32_t id)
393 uint32_t ids[RTE_MAX_LCORE] = {0};
394 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
397 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))
400 for (i = 0; i < lcore_count; i++) {
401 if (lcore_states[i].service_active_on_lcore[id])
409 rte_service_run_iter_on_app_lcore(uint32_t id, uint32_t serialize_mt_unsafe)
411 struct core_state *cs = &lcore_states[rte_lcore_id()];
412 struct rte_service_spec_impl *s;
414 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
416 /* Atomically add this core to the mapped cores first, then examine if
417 * we can run the service. This avoids a race condition between
418 * checking the value, and atomically adding to the mapped count.
420 if (serialize_mt_unsafe)
421 rte_atomic32_inc(&s->num_mapped_cores);
423 if (service_mt_safe(s) == 0 &&
424 rte_atomic32_read(&s->num_mapped_cores) > 1) {
425 if (serialize_mt_unsafe)
426 rte_atomic32_dec(&s->num_mapped_cores);
430 int ret = service_run(id, cs, UINT64_MAX, s);
432 if (serialize_mt_unsafe)
433 rte_atomic32_dec(&s->num_mapped_cores);
439 rte_service_runner_func(void *arg)
443 const int lcore = rte_lcore_id();
444 struct core_state *cs = &lcore_states[lcore];
446 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
447 const uint64_t service_mask = cs->service_mask;
449 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
450 if (!service_valid(i))
452 /* return value ignored as no change to code flow */
453 service_run(i, cs, service_mask, service_get(i));
461 lcore_config[lcore].state = WAIT;
467 rte_service_lcore_count(void)
471 for (i = 0; i < RTE_MAX_LCORE; i++)
472 count += lcore_states[i].is_service_core;
477 rte_service_lcore_list(uint32_t array[], uint32_t n)
479 uint32_t count = rte_service_lcore_count();
488 for (i = 0; i < RTE_MAX_LCORE; i++) {
489 struct core_state *cs = &lcore_states[i];
490 if (cs->is_service_core) {
500 rte_service_lcore_count_services(uint32_t lcore)
502 if (lcore >= RTE_MAX_LCORE)
505 struct core_state *cs = &lcore_states[lcore];
506 if (!cs->is_service_core)
509 return __builtin_popcountll(cs->service_mask);
513 rte_service_start_with_defaults(void)
515 /* create a default mapping from cores to services, then start the
516 * services to make them transparent to unaware applications.
520 uint32_t count = rte_service_get_count();
522 int32_t lcore_iter = 0;
523 uint32_t ids[RTE_MAX_LCORE] = {0};
524 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
526 if (lcore_count == 0)
529 for (i = 0; (int)i < lcore_count; i++)
530 rte_service_lcore_start(ids[i]);
532 for (i = 0; i < count; i++) {
533 /* do 1:1 core mapping here, with each service getting
534 * assigned a single core by default. Adding multiple services
535 * should multiplex to a single core, or 1:1 if there are the
536 * same amount of services as service-cores
538 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
543 if (lcore_iter >= lcore_count)
546 ret = rte_service_runstate_set(i, 1);
555 service_update(struct rte_service_spec *service, uint32_t lcore,
556 uint32_t *set, uint32_t *enabled)
561 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
562 if ((struct rte_service_spec *)&rte_services[i] == service &&
569 if (sid == -1 || lcore >= RTE_MAX_LCORE)
572 if (!lcore_states[lcore].is_service_core)
575 uint64_t sid_mask = UINT64_C(1) << sid;
577 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
580 if (*set && !lcore_mapped) {
581 lcore_states[lcore].service_mask |= sid_mask;
582 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
584 if (!*set && lcore_mapped) {
585 lcore_states[lcore].service_mask &= ~(sid_mask);
586 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
591 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
599 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
601 struct rte_service_spec_impl *s;
602 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
603 uint32_t on = enabled > 0;
604 return service_update(&s->spec, lcore, &on, 0);
608 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
610 struct rte_service_spec_impl *s;
611 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
613 int ret = service_update(&s->spec, 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 lcore_states[i].runstate = RUNSTATE_STOPPED;
645 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
646 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
654 rte_service_lcore_add(uint32_t lcore)
656 if (lcore >= RTE_MAX_LCORE)
658 if (lcore_states[lcore].is_service_core)
661 set_lcore_state(lcore, ROLE_SERVICE);
663 /* ensure that after adding a core the mask and state are defaults */
664 lcore_states[lcore].service_mask = 0;
665 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
669 return rte_eal_wait_lcore(lcore);
673 rte_service_lcore_del(uint32_t lcore)
675 if (lcore >= RTE_MAX_LCORE)
678 struct core_state *cs = &lcore_states[lcore];
679 if (!cs->is_service_core)
682 if (cs->runstate != RUNSTATE_STOPPED)
685 set_lcore_state(lcore, ROLE_RTE);
692 rte_service_lcore_start(uint32_t lcore)
694 if (lcore >= RTE_MAX_LCORE)
697 struct core_state *cs = &lcore_states[lcore];
698 if (!cs->is_service_core)
701 if (cs->runstate == RUNSTATE_RUNNING)
704 /* set core to run state first, and then launch otherwise it will
705 * return immediately as runstate keeps it in the service poll loop
707 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
709 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
710 /* returns -EBUSY if the core is already launched, 0 on success */
715 rte_service_lcore_stop(uint32_t lcore)
717 if (lcore >= RTE_MAX_LCORE)
720 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
724 uint64_t service_mask = lcore_states[lcore].service_mask;
725 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
726 int32_t enabled = service_mask & (UINT64_C(1) << i);
727 int32_t service_running = rte_service_runstate_get(i);
728 int32_t only_core = (1 ==
729 rte_atomic32_read(&rte_services[i].num_mapped_cores));
731 /* if the core is mapped, and the service is running, and this
732 * is the only core that is mapped, the service would cease to
733 * run if this core stopped, so fail instead.
735 if (enabled && service_running && only_core)
739 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
745 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint64_t *attr_value)
747 struct rte_service_spec_impl *s;
748 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
754 case RTE_SERVICE_ATTR_CYCLES:
755 *attr_value = s->cycles_spent;
757 case RTE_SERVICE_ATTR_CALL_COUNT:
758 *attr_value = s->calls;
766 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
767 uint64_t *attr_value)
769 struct core_state *cs;
771 if (lcore >= RTE_MAX_LCORE || !attr_value)
774 cs = &lcore_states[lcore];
775 if (!cs->is_service_core)
779 case RTE_SERVICE_LCORE_ATTR_LOOPS:
780 *attr_value = cs->loops;
788 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
789 uint64_t all_cycles, uint32_t reset)
791 /* avoid divide by zero */
808 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
809 PRIu64"\tavg: %"PRIu64"\n",
810 s->spec.name, service_stats_enabled(s), s->calls,
811 s->cycles_spent, s->cycles_spent / calls);
815 rte_service_attr_reset_all(uint32_t id)
817 struct rte_service_spec_impl *s;
818 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
821 rte_service_dump_one(NULL, s, 0, reset);
826 rte_service_lcore_attr_reset_all(uint32_t lcore)
828 struct core_state *cs;
830 if (lcore >= RTE_MAX_LCORE)
833 cs = &lcore_states[lcore];
834 if (!cs->is_service_core)
843 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
846 struct core_state *cs = &lcore_states[lcore];
848 fprintf(f, "%02d\t", lcore);
849 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
850 if (!service_valid(i))
852 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
854 cs->calls_per_service[i] = 0;
860 rte_service_dump(FILE *f, uint32_t id)
863 int print_one = (id != UINT32_MAX);
865 uint64_t total_cycles = 0;
867 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
868 if (!service_valid(i))
870 total_cycles += rte_services[i].cycles_spent;
873 /* print only the specified service */
875 struct rte_service_spec_impl *s;
876 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
877 fprintf(f, "Service %s Summary\n", s->spec.name);
879 rte_service_dump_one(f, s, total_cycles, reset);
883 /* print all services, as UINT32_MAX was passed as id */
884 fprintf(f, "Services Summary\n");
885 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
886 if (!service_valid(i))
889 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
892 fprintf(f, "Service Cores Summary\n");
893 for (i = 0; i < RTE_MAX_LCORE; i++) {
894 if (lcore_config[i].core_role != ROLE_SERVICE)
898 service_dump_calls_per_lcore(f, i, reset);