1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
12 #include <rte_compat.h>
13 #include <rte_service.h>
14 #include "include/rte_service_component.h"
17 #include <rte_lcore.h>
18 #include <rte_common.h>
19 #include <rte_debug.h>
20 #include <rte_cycles.h>
21 #include <rte_atomic.h>
22 #include <rte_memory.h>
23 #include <rte_malloc.h>
25 #define RTE_SERVICE_NUM_MAX 64
27 #define SERVICE_F_REGISTERED (1 << 0)
28 #define SERVICE_F_STATS_ENABLED (1 << 1)
29 #define SERVICE_F_START_CHECK (1 << 2)
31 /* runstates for services and lcores, denoting if they are active or not */
32 #define RUNSTATE_STOPPED 0
33 #define RUNSTATE_RUNNING 1
35 /* internal representation of a service */
36 struct rte_service_spec_impl {
37 /* public part of the struct */
38 struct rte_service_spec spec;
40 /* atomic lock that when set indicates a service core is currently
41 * running this service callback. When not set, a core may take the
42 * lock and then run the service callback.
44 rte_atomic32_t execute_lock;
46 /* API set/get-able variables */
49 uint8_t internal_flags;
51 /* per service statistics */
52 rte_atomic32_t num_mapped_cores;
54 uint64_t cycles_spent;
55 uint8_t active_on_lcore[RTE_MAX_LCORE];
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 */
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;
74 int32_t rte_service_init(void)
76 if (rte_service_library_initialized) {
77 printf("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 printf("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 printf("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;
113 rte_free(rte_services);
115 rte_free(lcore_states);
120 rte_service_finalize(void)
122 if (!rte_service_library_initialized)
126 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 /* validate ID and retrieve service pointer, or return error value */
144 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
145 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
147 service = &rte_services[id]; \
150 /* returns 1 if statistics should be collected for service
151 * Returns 0 if statistics should not be collected for service
154 service_stats_enabled(struct rte_service_spec_impl *impl)
156 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
160 service_mt_safe(struct rte_service_spec_impl *s)
162 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
166 rte_service_set_stats_enable(uint32_t id, int32_t enabled)
168 struct rte_service_spec_impl *s;
169 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
172 s->internal_flags |= SERVICE_F_STATS_ENABLED;
174 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
180 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
182 struct rte_service_spec_impl *s;
183 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
186 s->internal_flags |= SERVICE_F_START_CHECK;
188 s->internal_flags &= ~(SERVICE_F_START_CHECK);
194 rte_service_get_count(void)
196 return rte_service_count;
200 rte_service_get_by_name(const char *name, uint32_t *service_id)
206 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
207 if (service_valid(i) &&
208 strcmp(name, rte_services[i].spec.name) == 0) {
218 rte_service_get_name(uint32_t id)
220 struct rte_service_spec_impl *s;
221 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
226 rte_service_probe_capability(uint32_t id, uint32_t capability)
228 struct rte_service_spec_impl *s;
229 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
230 return !!(s->spec.capabilities & capability);
234 rte_service_component_register(const struct rte_service_spec *spec,
238 int32_t free_slot = -1;
240 if (spec->callback == NULL || strlen(spec->name) == 0)
243 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
244 if (!service_valid(i)) {
250 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
253 struct rte_service_spec_impl *s = &rte_services[free_slot];
255 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
267 rte_service_component_unregister(uint32_t id)
270 struct rte_service_spec_impl *s;
271 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
276 s->internal_flags &= ~(SERVICE_F_REGISTERED);
278 /* clear the run-bit in all cores */
279 for (i = 0; i < RTE_MAX_LCORE; i++)
280 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
282 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
288 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
290 struct rte_service_spec_impl *s;
291 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
294 s->comp_runstate = RUNSTATE_RUNNING;
296 s->comp_runstate = RUNSTATE_STOPPED;
303 rte_service_runstate_set(uint32_t id, uint32_t runstate)
305 struct rte_service_spec_impl *s;
306 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
309 s->app_runstate = RUNSTATE_RUNNING;
311 s->app_runstate = RUNSTATE_STOPPED;
318 rte_service_runstate_get(uint32_t id)
320 struct rte_service_spec_impl *s;
321 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
324 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
325 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
327 return (s->app_runstate == RUNSTATE_RUNNING) &&
328 (s->comp_runstate == RUNSTATE_RUNNING) &&
329 (check_disabled | lcore_mapped);
333 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
334 struct core_state *cs, uint32_t service_idx)
336 void *userdata = s->spec.callback_userdata;
338 if (service_stats_enabled(s)) {
339 uint64_t start = rte_rdtsc();
340 s->spec.callback(userdata);
341 uint64_t end = rte_rdtsc();
342 s->cycles_spent += end - start;
343 cs->calls_per_service[service_idx]++;
346 s->spec.callback(userdata);
350 static inline int32_t
351 service_run(uint32_t i, int lcore, struct core_state *cs, uint64_t service_mask)
353 if (!service_valid(i))
355 struct rte_service_spec_impl *s = &rte_services[i];
356 if (s->comp_runstate != RUNSTATE_RUNNING ||
357 s->app_runstate != RUNSTATE_RUNNING ||
358 !(service_mask & (UINT64_C(1) << i))) {
359 s->active_on_lcore[lcore] = 0;
363 s->active_on_lcore[lcore] = 1;
365 /* check do we need cmpset, if MT safe or <= 1 core
366 * mapped, atomic ops are not required.
368 const int use_atomics = (service_mt_safe(s) == 0) &&
369 (rte_atomic32_read(&s->num_mapped_cores) > 1);
371 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
374 rte_service_runner_do_callback(s, cs, i);
375 rte_atomic32_clear(&s->execute_lock);
377 rte_service_runner_do_callback(s, cs, i);
382 int32_t __rte_experimental
383 rte_service_may_be_active(uint32_t id)
385 uint32_t ids[RTE_MAX_LCORE] = {0};
386 struct rte_service_spec_impl *s = &rte_services[id];
387 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
390 if (!service_valid(id))
393 for (i = 0; i < lcore_count; i++) {
394 if (s->active_on_lcore[ids[i]])
401 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
402 uint32_t serialize_mt_unsafe)
404 /* run service on calling core, using all-ones as the service mask */
405 if (!service_valid(id))
408 struct core_state *cs = &lcore_states[rte_lcore_id()];
409 struct rte_service_spec_impl *s = &rte_services[id];
411 /* Atomically add this core to the mapped cores first, then examine if
412 * we can run the service. This avoids a race condition between
413 * checking the value, and atomically adding to the mapped count.
415 if (serialize_mt_unsafe)
416 rte_atomic32_inc(&s->num_mapped_cores);
418 if (service_mt_safe(s) == 0 &&
419 rte_atomic32_read(&s->num_mapped_cores) > 1) {
420 if (serialize_mt_unsafe)
421 rte_atomic32_dec(&s->num_mapped_cores);
425 int ret = service_run(id, rte_lcore_id(), cs, UINT64_MAX);
427 if (serialize_mt_unsafe)
428 rte_atomic32_dec(&s->num_mapped_cores);
434 rte_service_runner_func(void *arg)
438 const int lcore = rte_lcore_id();
439 struct core_state *cs = &lcore_states[lcore];
441 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
442 const uint64_t service_mask = cs->service_mask;
444 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
445 /* return value ignored as no change to code flow */
446 service_run(i, lcore, cs, service_mask);
454 lcore_config[lcore].state = WAIT;
460 rte_service_lcore_count(void)
464 for (i = 0; i < RTE_MAX_LCORE; i++)
465 count += lcore_states[i].is_service_core;
470 rte_service_lcore_list(uint32_t array[], uint32_t n)
472 uint32_t count = rte_service_lcore_count();
481 for (i = 0; i < RTE_MAX_LCORE; i++) {
482 struct core_state *cs = &lcore_states[i];
483 if (cs->is_service_core) {
493 rte_service_lcore_count_services(uint32_t lcore)
495 if (lcore >= RTE_MAX_LCORE)
498 struct core_state *cs = &lcore_states[lcore];
499 if (!cs->is_service_core)
502 return __builtin_popcountll(cs->service_mask);
506 rte_service_start_with_defaults(void)
508 /* create a default mapping from cores to services, then start the
509 * services to make them transparent to unaware applications.
513 uint32_t count = rte_service_get_count();
515 int32_t lcore_iter = 0;
516 uint32_t ids[RTE_MAX_LCORE] = {0};
517 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
519 if (lcore_count == 0)
522 for (i = 0; (int)i < lcore_count; i++)
523 rte_service_lcore_start(ids[i]);
525 for (i = 0; i < count; i++) {
526 /* do 1:1 core mapping here, with each service getting
527 * assigned a single core by default. Adding multiple services
528 * should multiplex to a single core, or 1:1 if there are the
529 * same amount of services as service-cores
531 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
536 if (lcore_iter >= lcore_count)
539 ret = rte_service_runstate_set(i, 1);
548 service_update(struct rte_service_spec *service, uint32_t lcore,
549 uint32_t *set, uint32_t *enabled)
554 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
555 if ((struct rte_service_spec *)&rte_services[i] == service &&
562 if (sid == -1 || lcore >= RTE_MAX_LCORE)
565 if (!lcore_states[lcore].is_service_core)
568 uint64_t sid_mask = UINT64_C(1) << sid;
570 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
573 if (*set && !lcore_mapped) {
574 lcore_states[lcore].service_mask |= sid_mask;
575 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
577 if (!*set && lcore_mapped) {
578 lcore_states[lcore].service_mask &= ~(sid_mask);
579 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
584 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
592 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
594 struct rte_service_spec_impl *s;
595 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
596 uint32_t on = enabled > 0;
597 return service_update(&s->spec, lcore, &on, 0);
601 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
603 struct rte_service_spec_impl *s;
604 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
606 int ret = service_update(&s->spec, lcore, 0, &enabled);
613 set_lcore_state(uint32_t lcore, int32_t state)
615 /* mark core state in hugepage backed config */
616 struct rte_config *cfg = rte_eal_get_configuration();
617 cfg->lcore_role[lcore] = state;
619 /* mark state in process local lcore_config */
620 lcore_config[lcore].core_role = state;
622 /* update per-lcore optimized state tracking */
623 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
627 rte_service_lcore_reset_all(void)
629 /* loop over cores, reset all to mask 0 */
631 for (i = 0; i < RTE_MAX_LCORE; i++) {
632 if (lcore_states[i].is_service_core) {
633 lcore_states[i].service_mask = 0;
634 set_lcore_state(i, ROLE_RTE);
635 lcore_states[i].runstate = RUNSTATE_STOPPED;
638 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
639 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
647 rte_service_lcore_add(uint32_t lcore)
649 if (lcore >= RTE_MAX_LCORE)
651 if (lcore_states[lcore].is_service_core)
654 set_lcore_state(lcore, ROLE_SERVICE);
656 /* ensure that after adding a core the mask and state are defaults */
657 lcore_states[lcore].service_mask = 0;
658 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
662 return rte_eal_wait_lcore(lcore);
666 rte_service_lcore_del(uint32_t lcore)
668 if (lcore >= RTE_MAX_LCORE)
671 struct core_state *cs = &lcore_states[lcore];
672 if (!cs->is_service_core)
675 if (cs->runstate != RUNSTATE_STOPPED)
678 set_lcore_state(lcore, ROLE_RTE);
685 rte_service_lcore_start(uint32_t lcore)
687 if (lcore >= RTE_MAX_LCORE)
690 struct core_state *cs = &lcore_states[lcore];
691 if (!cs->is_service_core)
694 if (cs->runstate == RUNSTATE_RUNNING)
697 /* set core to run state first, and then launch otherwise it will
698 * return immediately as runstate keeps it in the service poll loop
700 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
702 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
703 /* returns -EBUSY if the core is already launched, 0 on success */
708 rte_service_lcore_stop(uint32_t lcore)
710 if (lcore >= RTE_MAX_LCORE)
713 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
717 uint64_t service_mask = lcore_states[lcore].service_mask;
718 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
719 int32_t enabled = service_mask & (UINT64_C(1) << i);
720 int32_t service_running = rte_service_runstate_get(i);
721 int32_t only_core = (1 ==
722 rte_atomic32_read(&rte_services[i].num_mapped_cores));
724 /* if the core is mapped, and the service is running, and this
725 * is the only core that is mapped, the service would cease to
726 * run if this core stopped, so fail instead.
728 if (enabled && service_running && only_core)
732 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
738 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint32_t *attr_value)
740 struct rte_service_spec_impl *s;
741 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
747 case RTE_SERVICE_ATTR_CYCLES:
748 *attr_value = s->cycles_spent;
750 case RTE_SERVICE_ATTR_CALL_COUNT:
751 *attr_value = s->calls;
758 int32_t __rte_experimental
759 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
760 uint64_t *attr_value)
762 struct core_state *cs;
764 if (lcore >= RTE_MAX_LCORE || !attr_value)
767 cs = &lcore_states[lcore];
768 if (!cs->is_service_core)
772 case RTE_SERVICE_LCORE_ATTR_LOOPS:
773 *attr_value = cs->loops;
781 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
782 uint64_t all_cycles, uint32_t reset)
784 /* avoid divide by zero */
801 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
802 PRIu64"\tavg: %"PRIu64"\n",
803 s->spec.name, service_stats_enabled(s), s->calls,
804 s->cycles_spent, s->cycles_spent / calls);
808 rte_service_attr_reset_all(uint32_t id)
810 struct rte_service_spec_impl *s;
811 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
814 rte_service_dump_one(NULL, s, 0, reset);
818 int32_t __rte_experimental
819 rte_service_lcore_attr_reset_all(uint32_t lcore)
821 struct core_state *cs;
823 if (lcore >= RTE_MAX_LCORE)
826 cs = &lcore_states[lcore];
827 if (!cs->is_service_core)
836 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
839 struct core_state *cs = &lcore_states[lcore];
841 fprintf(f, "%02d\t", lcore);
842 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
843 if (!service_valid(i))
845 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
847 cs->calls_per_service[i] = 0;
853 rte_service_dump(FILE *f, uint32_t id)
856 int print_one = (id != UINT32_MAX);
858 uint64_t total_cycles = 0;
860 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
861 if (!service_valid(i))
863 total_cycles += rte_services[i].cycles_spent;
866 /* print only the specified service */
868 struct rte_service_spec_impl *s;
869 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
870 fprintf(f, "Service %s Summary\n", s->spec.name);
872 rte_service_dump_one(f, s, total_cycles, reset);
876 /* print all services, as UINT32_MAX was passed as id */
877 fprintf(f, "Services Summary\n");
878 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
879 if (!service_valid(i))
882 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
885 fprintf(f, "Service Cores Summary\n");
886 for (i = 0; i < RTE_MAX_LCORE; i++) {
887 if (lcore_config[i].core_role != ROLE_SERVICE)
891 service_dump_calls_per_lcore(f, i, reset);