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 /* returns 1 if service is registered and has not been unregistered
112 * Returns 0 if service never registered, or has been unregistered
115 service_valid(uint32_t id)
117 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
120 /* validate ID and retrieve service pointer, or return error value */
121 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
122 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
124 service = &rte_services[id]; \
127 /* returns 1 if statistics should be collected for service
128 * Returns 0 if statistics should not be collected for service
131 service_stats_enabled(struct rte_service_spec_impl *impl)
133 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
137 service_mt_safe(struct rte_service_spec_impl *s)
139 return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
142 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enabled)
144 struct rte_service_spec_impl *s;
145 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
148 s->internal_flags |= SERVICE_F_STATS_ENABLED;
150 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
155 int32_t rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
157 struct rte_service_spec_impl *s;
158 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
161 s->internal_flags |= SERVICE_F_START_CHECK;
163 s->internal_flags &= ~(SERVICE_F_START_CHECK);
169 rte_service_get_count(void)
171 return rte_service_count;
174 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id)
180 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
181 if (service_valid(i) &&
182 strcmp(name, rte_services[i].spec.name) == 0) {
192 rte_service_get_name(uint32_t id)
194 struct rte_service_spec_impl *s;
195 SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
200 rte_service_probe_capability(uint32_t id, uint32_t capability)
202 struct rte_service_spec_impl *s;
203 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
204 return !!(s->spec.capabilities & capability);
208 rte_service_component_register(const struct rte_service_spec *spec,
212 int32_t free_slot = -1;
214 if (spec->callback == NULL || strlen(spec->name) == 0)
217 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
218 if (!service_valid(i)) {
224 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
227 struct rte_service_spec_impl *s = &rte_services[free_slot];
229 s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
241 rte_service_component_unregister(uint32_t id)
244 struct rte_service_spec_impl *s;
245 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
250 s->internal_flags &= ~(SERVICE_F_REGISTERED);
252 /* clear the run-bit in all cores */
253 for (i = 0; i < RTE_MAX_LCORE; i++)
254 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
256 memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
262 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
264 struct rte_service_spec_impl *s;
265 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
268 s->comp_runstate = RUNSTATE_RUNNING;
270 s->comp_runstate = RUNSTATE_STOPPED;
277 rte_service_runstate_set(uint32_t id, uint32_t runstate)
279 struct rte_service_spec_impl *s;
280 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
283 s->app_runstate = RUNSTATE_RUNNING;
285 s->app_runstate = RUNSTATE_STOPPED;
292 rte_service_runstate_get(uint32_t id)
294 struct rte_service_spec_impl *s;
295 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
298 int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
299 int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
301 return (s->app_runstate == RUNSTATE_RUNNING) &&
302 (s->comp_runstate == RUNSTATE_RUNNING) &&
303 (check_disabled | lcore_mapped);
307 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
308 struct core_state *cs, uint32_t service_idx)
310 void *userdata = s->spec.callback_userdata;
312 if (service_stats_enabled(s)) {
313 uint64_t start = rte_rdtsc();
314 s->spec.callback(userdata);
315 uint64_t end = rte_rdtsc();
316 s->cycles_spent += end - start;
317 cs->calls_per_service[service_idx]++;
320 s->spec.callback(userdata);
324 static inline int32_t
325 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask)
327 if (!service_valid(i))
329 struct rte_service_spec_impl *s = &rte_services[i];
330 if (s->comp_runstate != RUNSTATE_RUNNING ||
331 s->app_runstate != RUNSTATE_RUNNING ||
332 !(service_mask & (UINT64_C(1) << i)))
335 /* check do we need cmpset, if MT safe or <= 1 core
336 * mapped, atomic ops are not required.
338 const int use_atomics = (service_mt_safe(s) == 0) &&
339 (rte_atomic32_read(&s->num_mapped_cores) > 1);
341 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
344 rte_service_runner_do_callback(s, cs, i);
345 rte_atomic32_clear(&s->execute_lock);
347 rte_service_runner_do_callback(s, cs, i);
352 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
353 uint32_t serialize_mt_unsafe)
355 /* run service on calling core, using all-ones as the service mask */
356 if (!service_valid(id))
359 struct core_state *cs = &lcore_states[rte_lcore_id()];
360 struct rte_service_spec_impl *s = &rte_services[id];
362 /* Atomically add this core to the mapped cores first, then examine if
363 * we can run the service. This avoids a race condition between
364 * checking the value, and atomically adding to the mapped count.
366 if (serialize_mt_unsafe)
367 rte_atomic32_inc(&s->num_mapped_cores);
369 if (service_mt_safe(s) == 0 &&
370 rte_atomic32_read(&s->num_mapped_cores) > 1) {
371 if (serialize_mt_unsafe)
372 rte_atomic32_dec(&s->num_mapped_cores);
376 int ret = service_run(id, cs, UINT64_MAX);
378 if (serialize_mt_unsafe)
379 rte_atomic32_dec(&s->num_mapped_cores);
385 rte_service_runner_func(void *arg)
389 const int lcore = rte_lcore_id();
390 struct core_state *cs = &lcore_states[lcore];
392 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
393 const uint64_t service_mask = cs->service_mask;
395 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
396 /* return value ignored as no change to code flow */
397 service_run(i, cs, service_mask);
403 lcore_config[lcore].state = WAIT;
409 rte_service_lcore_count(void)
413 for (i = 0; i < RTE_MAX_LCORE; i++)
414 count += lcore_states[i].is_service_core;
419 rte_service_lcore_list(uint32_t array[], uint32_t n)
421 uint32_t count = rte_service_lcore_count();
430 for (i = 0; i < RTE_MAX_LCORE; i++) {
431 struct core_state *cs = &lcore_states[i];
432 if (cs->is_service_core) {
442 rte_service_lcore_count_services(uint32_t lcore)
444 if (lcore >= RTE_MAX_LCORE)
447 struct core_state *cs = &lcore_states[lcore];
448 if (!cs->is_service_core)
451 return __builtin_popcountll(cs->service_mask);
455 rte_service_start_with_defaults(void)
457 /* create a default mapping from cores to services, then start the
458 * services to make them transparent to unaware applications.
462 uint32_t count = rte_service_get_count();
464 int32_t lcore_iter = 0;
465 uint32_t ids[RTE_MAX_LCORE] = {0};
466 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
468 if (lcore_count == 0)
471 for (i = 0; (int)i < lcore_count; i++)
472 rte_service_lcore_start(ids[i]);
474 for (i = 0; i < count; i++) {
475 /* do 1:1 core mapping here, with each service getting
476 * assigned a single core by default. Adding multiple services
477 * should multiplex to a single core, or 1:1 if there are the
478 * same amount of services as service-cores
480 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
485 if (lcore_iter >= lcore_count)
488 ret = rte_service_runstate_set(i, 1);
497 service_update(struct rte_service_spec *service, uint32_t lcore,
498 uint32_t *set, uint32_t *enabled)
503 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
504 if ((struct rte_service_spec *)&rte_services[i] == service &&
511 if (sid == -1 || lcore >= RTE_MAX_LCORE)
514 if (!lcore_states[lcore].is_service_core)
517 uint64_t sid_mask = UINT64_C(1) << sid;
519 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
522 if (*set && !lcore_mapped) {
523 lcore_states[lcore].service_mask |= sid_mask;
524 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
526 if (!*set && lcore_mapped) {
527 lcore_states[lcore].service_mask &= ~(sid_mask);
528 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
533 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
541 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
543 struct rte_service_spec_impl *s;
544 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
545 uint32_t on = enabled > 0;
546 return service_update(&s->spec, lcore, &on, 0);
550 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
552 struct rte_service_spec_impl *s;
553 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
555 int ret = service_update(&s->spec, lcore, 0, &enabled);
562 set_lcore_state(uint32_t lcore, int32_t state)
564 /* mark core state in hugepage backed config */
565 struct rte_config *cfg = rte_eal_get_configuration();
566 cfg->lcore_role[lcore] = state;
568 /* mark state in process local lcore_config */
569 lcore_config[lcore].core_role = state;
571 /* update per-lcore optimized state tracking */
572 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
575 int32_t rte_service_lcore_reset_all(void)
577 /* loop over cores, reset all to mask 0 */
579 for (i = 0; i < RTE_MAX_LCORE; i++) {
580 if (lcore_states[i].is_service_core) {
581 lcore_states[i].service_mask = 0;
582 set_lcore_state(i, ROLE_RTE);
583 lcore_states[i].runstate = RUNSTATE_STOPPED;
586 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
587 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
595 rte_service_lcore_add(uint32_t lcore)
597 if (lcore >= RTE_MAX_LCORE)
599 if (lcore_states[lcore].is_service_core)
602 set_lcore_state(lcore, ROLE_SERVICE);
604 /* ensure that after adding a core the mask and state are defaults */
605 lcore_states[lcore].service_mask = 0;
606 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
610 return rte_eal_wait_lcore(lcore);
614 rte_service_lcore_del(uint32_t lcore)
616 if (lcore >= RTE_MAX_LCORE)
619 struct core_state *cs = &lcore_states[lcore];
620 if (!cs->is_service_core)
623 if (cs->runstate != RUNSTATE_STOPPED)
626 set_lcore_state(lcore, ROLE_RTE);
633 rte_service_lcore_start(uint32_t lcore)
635 if (lcore >= RTE_MAX_LCORE)
638 struct core_state *cs = &lcore_states[lcore];
639 if (!cs->is_service_core)
642 if (cs->runstate == RUNSTATE_RUNNING)
645 /* set core to run state first, and then launch otherwise it will
646 * return immediately as runstate keeps it in the service poll loop
648 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
650 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
651 /* returns -EBUSY if the core is already launched, 0 on success */
656 rte_service_lcore_stop(uint32_t lcore)
658 if (lcore >= RTE_MAX_LCORE)
661 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
665 uint64_t service_mask = lcore_states[lcore].service_mask;
666 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
667 int32_t enabled = service_mask & (UINT64_C(1) << i);
668 int32_t service_running = rte_service_runstate_get(i);
669 int32_t only_core = (1 ==
670 rte_atomic32_read(&rte_services[i].num_mapped_cores));
672 /* if the core is mapped, and the service is running, and this
673 * is the only core that is mapped, the service would cease to
674 * run if this core stopped, so fail instead.
676 if (enabled && service_running && only_core)
680 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
686 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
687 uint64_t all_cycles, uint32_t reset)
689 /* avoid divide by zero */
697 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
698 PRIu64"\tavg: %"PRIu64"\n",
699 s->spec.name, service_stats_enabled(s), s->calls,
700 s->cycles_spent, s->cycles_spent / calls);
709 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
712 struct core_state *cs = &lcore_states[lcore];
714 fprintf(f, "%02d\t", lcore);
715 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
716 if (!service_valid(i))
718 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
720 cs->calls_per_service[i] = 0;
725 int32_t rte_service_dump(FILE *f, uint32_t id)
728 int print_one = (id != UINT32_MAX);
730 uint64_t total_cycles = 0;
732 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
733 if (!service_valid(i))
735 total_cycles += rte_services[i].cycles_spent;
738 /* print only the specified service */
740 struct rte_service_spec_impl *s;
741 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
742 fprintf(f, "Service %s Summary\n", s->spec.name);
744 rte_service_dump_one(f, s, total_cycles, reset);
748 /* print all services, as UINT32_MAX was passed as id */
749 fprintf(f, "Services Summary\n");
750 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
751 if (!service_valid(i))
754 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
757 fprintf(f, "Service Cores Summary\n");
758 for (i = 0; i < RTE_MAX_LCORE; i++) {
759 if (lcore_config[i].core_role != ROLE_SERVICE)
763 service_dump_calls_per_lcore(f, i, reset);