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;
520 lcore_states[lcore].service_mask |= sid_mask;
521 rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
523 lcore_states[lcore].service_mask &= ~(sid_mask);
524 rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
529 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
537 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
539 struct rte_service_spec_impl *s;
540 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
541 uint32_t on = enabled > 0;
542 return service_update(&s->spec, lcore, &on, 0);
546 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
548 struct rte_service_spec_impl *s;
549 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
551 int ret = service_update(&s->spec, lcore, 0, &enabled);
557 int32_t rte_service_lcore_reset_all(void)
559 /* loop over cores, reset all to mask 0 */
561 for (i = 0; i < RTE_MAX_LCORE; i++) {
562 lcore_states[i].service_mask = 0;
563 lcore_states[i].is_service_core = 0;
564 lcore_states[i].runstate = RUNSTATE_STOPPED;
566 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
567 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
575 set_lcore_state(uint32_t lcore, int32_t state)
577 /* mark core state in hugepage backed config */
578 struct rte_config *cfg = rte_eal_get_configuration();
579 cfg->lcore_role[lcore] = state;
581 /* mark state in process local lcore_config */
582 lcore_config[lcore].core_role = state;
584 /* update per-lcore optimized state tracking */
585 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
589 rte_service_lcore_add(uint32_t lcore)
591 if (lcore >= RTE_MAX_LCORE)
593 if (lcore_states[lcore].is_service_core)
596 set_lcore_state(lcore, ROLE_SERVICE);
598 /* ensure that after adding a core the mask and state are defaults */
599 lcore_states[lcore].service_mask = 0;
600 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
604 return rte_eal_wait_lcore(lcore);
608 rte_service_lcore_del(uint32_t lcore)
610 if (lcore >= RTE_MAX_LCORE)
613 struct core_state *cs = &lcore_states[lcore];
614 if (!cs->is_service_core)
617 if (cs->runstate != RUNSTATE_STOPPED)
620 set_lcore_state(lcore, ROLE_RTE);
627 rte_service_lcore_start(uint32_t lcore)
629 if (lcore >= RTE_MAX_LCORE)
632 struct core_state *cs = &lcore_states[lcore];
633 if (!cs->is_service_core)
636 if (cs->runstate == RUNSTATE_RUNNING)
639 /* set core to run state first, and then launch otherwise it will
640 * return immediately as runstate keeps it in the service poll loop
642 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
644 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
645 /* returns -EBUSY if the core is already launched, 0 on success */
650 rte_service_lcore_stop(uint32_t lcore)
652 if (lcore >= RTE_MAX_LCORE)
655 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
659 uint64_t service_mask = lcore_states[lcore].service_mask;
660 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
661 int32_t enabled = service_mask & (UINT64_C(1) << i);
662 int32_t service_running = rte_service_runstate_get(i);
663 int32_t only_core = (1 ==
664 rte_atomic32_read(&rte_services[i].num_mapped_cores));
666 /* if the core is mapped, and the service is running, and this
667 * is the only core that is mapped, the service would cease to
668 * run if this core stopped, so fail instead.
670 if (enabled && service_running && only_core)
674 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
680 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
681 uint64_t all_cycles, uint32_t reset)
683 /* avoid divide by zero */
691 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
692 PRIu64"\tavg: %"PRIu64"\n",
693 s->spec.name, service_stats_enabled(s), s->calls,
694 s->cycles_spent, s->cycles_spent / calls);
703 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
706 struct core_state *cs = &lcore_states[lcore];
708 fprintf(f, "%02d\t", lcore);
709 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
710 if (!service_valid(i))
712 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
714 cs->calls_per_service[i] = 0;
719 int32_t rte_service_dump(FILE *f, uint32_t id)
722 int print_one = (id != UINT32_MAX);
724 uint64_t total_cycles = 0;
726 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
727 if (!service_valid(i))
729 total_cycles += rte_services[i].cycles_spent;
732 /* print only the specified service */
734 struct rte_service_spec_impl *s;
735 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
736 fprintf(f, "Service %s Summary\n", s->spec.name);
738 rte_service_dump_one(f, s, total_cycles, reset);
742 /* print all services, as UINT32_MAX was passed as id */
743 fprintf(f, "Services Summary\n");
744 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
745 if (!service_valid(i))
748 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
751 fprintf(f, "Service Cores Summary\n");
752 for (i = 0; i < RTE_MAX_LCORE; i++) {
753 if (lcore_config[i].core_role != ROLE_SERVICE)
757 service_dump_calls_per_lcore(f, i, reset);