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 */
64 /* extreme statistics */
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);
428 lcore_config[lcore].state = WAIT;
434 rte_service_lcore_count(void)
438 for (i = 0; i < RTE_MAX_LCORE; i++)
439 count += lcore_states[i].is_service_core;
444 rte_service_lcore_list(uint32_t array[], uint32_t n)
446 uint32_t count = rte_service_lcore_count();
455 for (i = 0; i < RTE_MAX_LCORE; i++) {
456 struct core_state *cs = &lcore_states[i];
457 if (cs->is_service_core) {
467 rte_service_lcore_count_services(uint32_t lcore)
469 if (lcore >= RTE_MAX_LCORE)
472 struct core_state *cs = &lcore_states[lcore];
473 if (!cs->is_service_core)
476 return __builtin_popcountll(cs->service_mask);
480 rte_service_start_with_defaults(void)
482 /* create a default mapping from cores to services, then start the
483 * services to make them transparent to unaware applications.
487 uint32_t count = rte_service_get_count();
489 int32_t lcore_iter = 0;
490 uint32_t ids[RTE_MAX_LCORE] = {0};
491 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
493 if (lcore_count == 0)
496 for (i = 0; (int)i < lcore_count; i++)
497 rte_service_lcore_start(ids[i]);
499 for (i = 0; i < count; i++) {
500 /* do 1:1 core mapping here, with each service getting
501 * assigned a single core by default. Adding multiple services
502 * should multiplex to a single core, or 1:1 if there are the
503 * same amount of services as service-cores
505 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
510 if (lcore_iter >= lcore_count)
513 ret = rte_service_runstate_set(i, 1);
522 service_update(struct rte_service_spec *service, uint32_t lcore,
523 uint32_t *set, uint32_t *enabled)
528 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
529 if ((struct rte_service_spec *)&rte_services[i] == service &&
536 if (sid == -1 || lcore >= RTE_MAX_LCORE)
539 if (!lcore_states[lcore].is_service_core)
542 uint64_t sid_mask = UINT64_C(1) << sid;
544 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
547 if (*set && !lcore_mapped) {
548 lcore_states[lcore].service_mask |= sid_mask;
549 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
551 if (!*set && lcore_mapped) {
552 lcore_states[lcore].service_mask &= ~(sid_mask);
553 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
558 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
566 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
568 struct rte_service_spec_impl *s;
569 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
570 uint32_t on = enabled > 0;
571 return service_update(&s->spec, lcore, &on, 0);
575 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
577 struct rte_service_spec_impl *s;
578 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
580 int ret = service_update(&s->spec, lcore, 0, &enabled);
587 set_lcore_state(uint32_t lcore, int32_t state)
589 /* mark core state in hugepage backed config */
590 struct rte_config *cfg = rte_eal_get_configuration();
591 cfg->lcore_role[lcore] = state;
593 /* mark state in process local lcore_config */
594 lcore_config[lcore].core_role = state;
596 /* update per-lcore optimized state tracking */
597 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
601 rte_service_lcore_reset_all(void)
603 /* loop over cores, reset all to mask 0 */
605 for (i = 0; i < RTE_MAX_LCORE; i++) {
606 if (lcore_states[i].is_service_core) {
607 lcore_states[i].service_mask = 0;
608 set_lcore_state(i, ROLE_RTE);
609 lcore_states[i].runstate = RUNSTATE_STOPPED;
612 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
613 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
621 rte_service_lcore_add(uint32_t lcore)
623 if (lcore >= RTE_MAX_LCORE)
625 if (lcore_states[lcore].is_service_core)
628 set_lcore_state(lcore, ROLE_SERVICE);
630 /* ensure that after adding a core the mask and state are defaults */
631 lcore_states[lcore].service_mask = 0;
632 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
636 return rte_eal_wait_lcore(lcore);
640 rte_service_lcore_del(uint32_t lcore)
642 if (lcore >= RTE_MAX_LCORE)
645 struct core_state *cs = &lcore_states[lcore];
646 if (!cs->is_service_core)
649 if (cs->runstate != RUNSTATE_STOPPED)
652 set_lcore_state(lcore, ROLE_RTE);
659 rte_service_lcore_start(uint32_t lcore)
661 if (lcore >= RTE_MAX_LCORE)
664 struct core_state *cs = &lcore_states[lcore];
665 if (!cs->is_service_core)
668 if (cs->runstate == RUNSTATE_RUNNING)
671 /* set core to run state first, and then launch otherwise it will
672 * return immediately as runstate keeps it in the service poll loop
674 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
676 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
677 /* returns -EBUSY if the core is already launched, 0 on success */
682 rte_service_lcore_stop(uint32_t lcore)
684 if (lcore >= RTE_MAX_LCORE)
687 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
691 uint64_t service_mask = lcore_states[lcore].service_mask;
692 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
693 int32_t enabled = service_mask & (UINT64_C(1) << i);
694 int32_t service_running = rte_service_runstate_get(i);
695 int32_t only_core = (1 ==
696 rte_atomic32_read(&rte_services[i].num_mapped_cores));
698 /* if the core is mapped, and the service is running, and this
699 * is the only core that is mapped, the service would cease to
700 * run if this core stopped, so fail instead.
702 if (enabled && service_running && only_core)
706 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
712 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint32_t *attr_value)
714 struct rte_service_spec_impl *s;
715 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
721 case RTE_SERVICE_ATTR_CYCLES:
722 *attr_value = s->cycles_spent;
724 case RTE_SERVICE_ATTR_CALL_COUNT:
725 *attr_value = s->calls;
733 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
734 uint64_t all_cycles, uint32_t reset)
736 /* avoid divide by zero */
750 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
751 PRIu64"\tavg: %"PRIu64"\n",
752 s->spec.name, service_stats_enabled(s), s->calls,
753 s->cycles_spent, s->cycles_spent / calls);
757 rte_service_attr_reset_all(uint32_t id)
759 struct rte_service_spec_impl *s;
760 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
763 rte_service_dump_one(NULL, s, 0, reset);
768 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
771 struct core_state *cs = &lcore_states[lcore];
773 fprintf(f, "%02d\t", lcore);
774 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
775 if (!service_valid(i))
777 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
779 cs->calls_per_service[i] = 0;
785 rte_service_dump(FILE *f, uint32_t id)
788 int print_one = (id != UINT32_MAX);
790 uint64_t total_cycles = 0;
792 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
793 if (!service_valid(i))
795 total_cycles += rte_services[i].cycles_spent;
798 /* print only the specified service */
800 struct rte_service_spec_impl *s;
801 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
802 fprintf(f, "Service %s Summary\n", s->spec.name);
804 rte_service_dump_one(f, s, total_cycles, reset);
808 /* print all services, as UINT32_MAX was passed as id */
809 fprintf(f, "Services Summary\n");
810 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
811 if (!service_valid(i))
814 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
817 fprintf(f, "Service Cores Summary\n");
818 for (i = 0; i < RTE_MAX_LCORE; i++) {
819 if (lcore_config[i].core_role != ROLE_SERVICE)
823 service_dump_calls_per_lcore(f, i, reset);