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 } __rte_cache_aligned;
57 /* the internal values of a service core */
59 /* map of services IDs are run on this core */
60 uint64_t service_mask;
61 uint8_t runstate; /* running or stopped */
62 uint8_t is_service_core; /* set if core is currently a service core */
65 uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
66 } __rte_cache_aligned;
68 static uint32_t rte_service_count;
69 static struct rte_service_spec_impl *rte_services;
70 static struct core_state *lcore_states;
71 static uint32_t rte_service_library_initialized;
73 int32_t rte_service_init(void)
75 if (rte_service_library_initialized) {
76 printf("service library init() called, init flag %d\n",
77 rte_service_library_initialized);
81 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
82 sizeof(struct rte_service_spec_impl),
85 printf("error allocating rte services array\n");
89 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
90 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
92 printf("error allocating core states array\n");
98 struct rte_config *cfg = rte_eal_get_configuration();
99 for (i = 0; i < RTE_MAX_LCORE; i++) {
100 if (lcore_config[i].core_role == ROLE_SERVICE) {
101 if ((unsigned int)i == cfg->master_lcore)
103 rte_service_lcore_add(i);
108 rte_service_library_initialized = 1;
112 rte_free(rte_services);
114 rte_free(lcore_states);
119 rte_service_finalize(void)
121 if (!rte_service_library_initialized)
125 rte_free(rte_services);
128 rte_free(lcore_states);
130 rte_service_library_initialized = 0;
133 /* returns 1 if service is registered and has not been unregistered
134 * Returns 0 if service never registered, or has been unregistered
137 service_valid(uint32_t id)
139 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
142 /* validate ID and retrieve service pointer, or return error value */
143 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
144 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
146 service = &rte_services[id]; \
149 /* returns 1 if statistics should be collected for service
150 * Returns 0 if statistics should not be collected for service
153 service_stats_enabled(struct rte_service_spec_impl *impl)
155 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
159 service_mt_safe(struct rte_service_spec_impl *s)
161 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
165 rte_service_set_stats_enable(uint32_t id, int32_t enabled)
167 struct rte_service_spec_impl *s;
168 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
171 s->internal_flags |= SERVICE_F_STATS_ENABLED;
173 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
179 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
181 struct rte_service_spec_impl *s;
182 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
185 s->internal_flags |= SERVICE_F_START_CHECK;
187 s->internal_flags &= ~(SERVICE_F_START_CHECK);
193 rte_service_get_count(void)
195 return rte_service_count;
199 rte_service_get_by_name(const char *name, uint32_t *service_id)
205 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
206 if (service_valid(i) &&
207 strcmp(name, rte_services[i].spec.name) == 0) {
217 rte_service_get_name(uint32_t id)
219 struct rte_service_spec_impl *s;
220 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
225 rte_service_probe_capability(uint32_t id, uint32_t capability)
227 struct rte_service_spec_impl *s;
228 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
229 return !!(s->spec.capabilities & capability);
233 rte_service_component_register(const struct rte_service_spec *spec,
237 int32_t free_slot = -1;
239 if (spec->callback == NULL || strlen(spec->name) == 0)
242 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
243 if (!service_valid(i)) {
249 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
252 struct rte_service_spec_impl *s = &rte_services[free_slot];
254 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
266 rte_service_component_unregister(uint32_t id)
269 struct rte_service_spec_impl *s;
270 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
275 s->internal_flags &= ~(SERVICE_F_REGISTERED);
277 /* clear the run-bit in all cores */
278 for (i = 0; i < RTE_MAX_LCORE; i++)
279 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
281 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
287 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
289 struct rte_service_spec_impl *s;
290 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
293 s->comp_runstate = RUNSTATE_RUNNING;
295 s->comp_runstate = RUNSTATE_STOPPED;
302 rte_service_runstate_set(uint32_t id, uint32_t runstate)
304 struct rte_service_spec_impl *s;
305 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
308 s->app_runstate = RUNSTATE_RUNNING;
310 s->app_runstate = RUNSTATE_STOPPED;
317 rte_service_runstate_get(uint32_t id)
319 struct rte_service_spec_impl *s;
320 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
323 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
324 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
326 return (s->app_runstate == RUNSTATE_RUNNING) &&
327 (s->comp_runstate == RUNSTATE_RUNNING) &&
328 (check_disabled | lcore_mapped);
332 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
333 struct core_state *cs, uint32_t service_idx)
335 void *userdata = s->spec.callback_userdata;
337 if (service_stats_enabled(s)) {
338 uint64_t start = rte_rdtsc();
339 s->spec.callback(userdata);
340 uint64_t end = rte_rdtsc();
341 s->cycles_spent += end - start;
342 cs->calls_per_service[service_idx]++;
345 s->spec.callback(userdata);
349 static inline int32_t
350 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask)
352 if (!service_valid(i))
354 struct rte_service_spec_impl *s = &rte_services[i];
355 if (s->comp_runstate != RUNSTATE_RUNNING ||
356 s->app_runstate != RUNSTATE_RUNNING ||
357 !(service_mask & (UINT64_C(1) << i)))
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);
377 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
378 uint32_t serialize_mt_unsafe)
380 /* run service on calling core, using all-ones as the service mask */
381 if (!service_valid(id))
384 struct core_state *cs = &lcore_states[rte_lcore_id()];
385 struct rte_service_spec_impl *s = &rte_services[id];
387 /* Atomically add this core to the mapped cores first, then examine if
388 * we can run the service. This avoids a race condition between
389 * checking the value, and atomically adding to the mapped count.
391 if (serialize_mt_unsafe)
392 rte_atomic32_inc(&s->num_mapped_cores);
394 if (service_mt_safe(s) == 0 &&
395 rte_atomic32_read(&s->num_mapped_cores) > 1) {
396 if (serialize_mt_unsafe)
397 rte_atomic32_dec(&s->num_mapped_cores);
401 int ret = service_run(id, cs, UINT64_MAX);
403 if (serialize_mt_unsafe)
404 rte_atomic32_dec(&s->num_mapped_cores);
410 rte_service_runner_func(void *arg)
414 const int lcore = rte_lcore_id();
415 struct core_state *cs = &lcore_states[lcore];
417 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
418 const uint64_t service_mask = cs->service_mask;
420 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
421 /* return value ignored as no change to code flow */
422 service_run(i, cs, service_mask);
430 lcore_config[lcore].state = WAIT;
436 rte_service_lcore_count(void)
440 for (i = 0; i < RTE_MAX_LCORE; i++)
441 count += lcore_states[i].is_service_core;
446 rte_service_lcore_list(uint32_t array[], uint32_t n)
448 uint32_t count = rte_service_lcore_count();
457 for (i = 0; i < RTE_MAX_LCORE; i++) {
458 struct core_state *cs = &lcore_states[i];
459 if (cs->is_service_core) {
469 rte_service_lcore_count_services(uint32_t lcore)
471 if (lcore >= RTE_MAX_LCORE)
474 struct core_state *cs = &lcore_states[lcore];
475 if (!cs->is_service_core)
478 return __builtin_popcountll(cs->service_mask);
482 rte_service_start_with_defaults(void)
484 /* create a default mapping from cores to services, then start the
485 * services to make them transparent to unaware applications.
489 uint32_t count = rte_service_get_count();
491 int32_t lcore_iter = 0;
492 uint32_t ids[RTE_MAX_LCORE] = {0};
493 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
495 if (lcore_count == 0)
498 for (i = 0; (int)i < lcore_count; i++)
499 rte_service_lcore_start(ids[i]);
501 for (i = 0; i < count; i++) {
502 /* do 1:1 core mapping here, with each service getting
503 * assigned a single core by default. Adding multiple services
504 * should multiplex to a single core, or 1:1 if there are the
505 * same amount of services as service-cores
507 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
512 if (lcore_iter >= lcore_count)
515 ret = rte_service_runstate_set(i, 1);
524 service_update(struct rte_service_spec *service, uint32_t lcore,
525 uint32_t *set, uint32_t *enabled)
530 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
531 if ((struct rte_service_spec *)&rte_services[i] == service &&
538 if (sid == -1 || lcore >= RTE_MAX_LCORE)
541 if (!lcore_states[lcore].is_service_core)
544 uint64_t sid_mask = UINT64_C(1) << sid;
546 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
549 if (*set && !lcore_mapped) {
550 lcore_states[lcore].service_mask |= sid_mask;
551 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
553 if (!*set && lcore_mapped) {
554 lcore_states[lcore].service_mask &= ~(sid_mask);
555 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
560 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
568 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
570 struct rte_service_spec_impl *s;
571 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
572 uint32_t on = enabled > 0;
573 return service_update(&s->spec, lcore, &on, 0);
577 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
579 struct rte_service_spec_impl *s;
580 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
582 int ret = service_update(&s->spec, lcore, 0, &enabled);
589 set_lcore_state(uint32_t lcore, int32_t state)
591 /* mark core state in hugepage backed config */
592 struct rte_config *cfg = rte_eal_get_configuration();
593 cfg->lcore_role[lcore] = state;
595 /* mark state in process local lcore_config */
596 lcore_config[lcore].core_role = state;
598 /* update per-lcore optimized state tracking */
599 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
603 rte_service_lcore_reset_all(void)
605 /* loop over cores, reset all to mask 0 */
607 for (i = 0; i < RTE_MAX_LCORE; i++) {
608 if (lcore_states[i].is_service_core) {
609 lcore_states[i].service_mask = 0;
610 set_lcore_state(i, ROLE_RTE);
611 lcore_states[i].runstate = RUNSTATE_STOPPED;
614 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
615 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
623 rte_service_lcore_add(uint32_t lcore)
625 if (lcore >= RTE_MAX_LCORE)
627 if (lcore_states[lcore].is_service_core)
630 set_lcore_state(lcore, ROLE_SERVICE);
632 /* ensure that after adding a core the mask and state are defaults */
633 lcore_states[lcore].service_mask = 0;
634 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
638 return rte_eal_wait_lcore(lcore);
642 rte_service_lcore_del(uint32_t lcore)
644 if (lcore >= RTE_MAX_LCORE)
647 struct core_state *cs = &lcore_states[lcore];
648 if (!cs->is_service_core)
651 if (cs->runstate != RUNSTATE_STOPPED)
654 set_lcore_state(lcore, ROLE_RTE);
661 rte_service_lcore_start(uint32_t lcore)
663 if (lcore >= RTE_MAX_LCORE)
666 struct core_state *cs = &lcore_states[lcore];
667 if (!cs->is_service_core)
670 if (cs->runstate == RUNSTATE_RUNNING)
673 /* set core to run state first, and then launch otherwise it will
674 * return immediately as runstate keeps it in the service poll loop
676 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
678 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
679 /* returns -EBUSY if the core is already launched, 0 on success */
684 rte_service_lcore_stop(uint32_t lcore)
686 if (lcore >= RTE_MAX_LCORE)
689 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
693 uint64_t service_mask = lcore_states[lcore].service_mask;
694 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
695 int32_t enabled = service_mask & (UINT64_C(1) << i);
696 int32_t service_running = rte_service_runstate_get(i);
697 int32_t only_core = (1 ==
698 rte_atomic32_read(&rte_services[i].num_mapped_cores));
700 /* if the core is mapped, and the service is running, and this
701 * is the only core that is mapped, the service would cease to
702 * run if this core stopped, so fail instead.
704 if (enabled && service_running && only_core)
708 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
714 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint32_t *attr_value)
716 struct rte_service_spec_impl *s;
717 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
723 case RTE_SERVICE_ATTR_CYCLES:
724 *attr_value = s->cycles_spent;
726 case RTE_SERVICE_ATTR_CALL_COUNT:
727 *attr_value = s->calls;
734 int32_t __rte_experimental
735 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
736 uint64_t *attr_value)
738 struct core_state *cs;
740 if (lcore >= RTE_MAX_LCORE || !attr_value)
743 cs = &lcore_states[lcore];
744 if (!cs->is_service_core)
748 case RTE_SERVICE_LCORE_ATTR_LOOPS:
749 *attr_value = cs->loops;
757 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
758 uint64_t all_cycles, uint32_t reset)
760 /* avoid divide by zero */
774 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
775 PRIu64"\tavg: %"PRIu64"\n",
776 s->spec.name, service_stats_enabled(s), s->calls,
777 s->cycles_spent, s->cycles_spent / calls);
781 rte_service_attr_reset_all(uint32_t id)
783 struct rte_service_spec_impl *s;
784 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
787 rte_service_dump_one(NULL, s, 0, reset);
791 int32_t __rte_experimental
792 rte_service_lcore_attr_reset_all(uint32_t lcore)
794 struct core_state *cs;
796 if (lcore >= RTE_MAX_LCORE)
799 cs = &lcore_states[lcore];
800 if (!cs->is_service_core)
809 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
812 struct core_state *cs = &lcore_states[lcore];
814 fprintf(f, "%02d\t", lcore);
815 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
816 if (!service_valid(i))
818 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
820 cs->calls_per_service[i] = 0;
826 rte_service_dump(FILE *f, uint32_t id)
829 int print_one = (id != UINT32_MAX);
831 uint64_t total_cycles = 0;
833 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
834 if (!service_valid(i))
836 total_cycles += rte_services[i].cycles_spent;
839 /* print only the specified service */
841 struct rte_service_spec_impl *s;
842 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
843 fprintf(f, "Service %s Summary\n", s->spec.name);
845 rte_service_dump_one(f, s, total_cycles, reset);
849 /* print all services, as UINT32_MAX was passed as id */
850 fprintf(f, "Services Summary\n");
851 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
852 if (!service_valid(i))
855 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
858 fprintf(f, "Service Cores Summary\n");
859 for (i = 0; i < RTE_MAX_LCORE; i++) {
860 if (lcore_config[i].core_role != ROLE_SERVICE)
864 service_dump_calls_per_lcore(f, i, reset);