4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <rte_service.h>
42 #include "include/rte_service_component.h"
45 #include <rte_lcore.h>
46 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_cycles.h>
49 #include <rte_atomic.h>
50 #include <rte_memory.h>
51 #include <rte_malloc.h>
53 #define RTE_SERVICE_NUM_MAX 64
55 #define SERVICE_F_REGISTERED (1 << 0)
56 #define SERVICE_F_STATS_ENABLED (1 << 1)
58 /* runstates for services and lcores, denoting if they are active or not */
59 #define RUNSTATE_STOPPED 0
60 #define RUNSTATE_RUNNING 1
62 /* internal representation of a service */
63 struct rte_service_spec_impl {
64 /* public part of the struct */
65 struct rte_service_spec spec;
67 /* atomic lock that when set indicates a service core is currently
68 * running this service callback. When not set, a core may take the
69 * lock and then run the service callback.
71 rte_atomic32_t execute_lock;
73 /* API set/get-able variables */
75 uint8_t internal_flags;
77 /* per service statistics */
78 uint32_t num_mapped_cores;
80 uint64_t cycles_spent;
81 } __rte_cache_aligned;
83 /* the internal values of a service core */
85 /* map of services IDs are run on this core */
86 uint64_t service_mask;
87 uint8_t runstate; /* running or stopped */
88 uint8_t is_service_core; /* set if core is currently a service core */
90 /* extreme statistics */
91 uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
92 } __rte_cache_aligned;
94 static uint32_t rte_service_count;
95 static struct rte_service_spec_impl *rte_services;
96 static struct core_state *lcore_states;
97 static uint32_t rte_service_library_initialized;
99 int32_t rte_service_init(void)
101 if (rte_service_library_initialized) {
102 printf("service library init() called, init flag %d\n",
103 rte_service_library_initialized);
107 rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
108 sizeof(struct rte_service_spec_impl),
109 RTE_CACHE_LINE_SIZE);
111 printf("error allocating rte services array\n");
115 lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
116 sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
118 printf("error allocating core states array\n");
124 struct rte_config *cfg = rte_eal_get_configuration();
125 for (i = 0; i < RTE_MAX_LCORE; i++) {
126 if (lcore_config[i].core_role == ROLE_SERVICE) {
127 if ((unsigned int)i == cfg->master_lcore)
129 rte_service_lcore_add(i);
134 rte_service_library_initialized = 1;
138 /* returns 1 if service is registered and has not been unregistered
139 * Returns 0 if service never registered, or has been unregistered
142 service_valid(uint32_t id)
144 return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
147 /* validate ID and retrieve service pointer, or return error value */
148 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do { \
149 if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id)) \
151 service = &rte_services[id]; \
154 /* returns 1 if statistics should be colleced for service
155 * Returns 0 if statistics should not be collected for service
158 service_stats_enabled(struct rte_service_spec_impl *impl)
160 return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
164 service_mt_safe(struct rte_service_spec_impl *s)
166 return s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE;
169 int32_t rte_service_set_stats_enable(struct rte_service_spec *service,
172 struct rte_service_spec_impl *impl =
173 (struct rte_service_spec_impl *)service;
178 impl->internal_flags |= SERVICE_F_STATS_ENABLED;
180 impl->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
186 rte_service_get_count(void)
188 return rte_service_count;
191 struct rte_service_spec *
192 rte_service_get_by_id(uint32_t id)
194 struct rte_service_spec *service = NULL;
195 if (id < rte_service_count)
196 service = (struct rte_service_spec *)&rte_services[id];
201 struct rte_service_spec *rte_service_get_by_name(const char *name)
203 struct rte_service_spec *service = NULL;
205 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
206 if (service_valid(i) &&
207 strcmp(name, rte_services[i].spec.name) == 0) {
208 service = (struct rte_service_spec *)&rte_services[i];
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_is_running(const struct rte_service_spec *spec)
235 const struct rte_service_spec_impl *impl =
236 (const struct rte_service_spec_impl *)spec;
240 return (impl->runstate == RUNSTATE_RUNNING) &&
241 (impl->num_mapped_cores > 0);
245 rte_service_register(const struct rte_service_spec *spec)
248 int32_t free_slot = -1;
250 if (spec->callback == NULL || strlen(spec->name) == 0)
253 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
254 if (!service_valid(i)) {
260 if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
263 struct rte_service_spec_impl *s = &rte_services[free_slot];
265 s->internal_flags |= SERVICE_F_REGISTERED;
274 rte_service_unregister(struct rte_service_spec *spec)
276 struct rte_service_spec_impl *s = NULL;
277 struct rte_service_spec_impl *spec_impl =
278 (struct rte_service_spec_impl *)spec;
282 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
283 if (&rte_services[i] == spec_impl) {
296 s->internal_flags &= ~(SERVICE_F_REGISTERED);
298 for (i = 0; i < RTE_MAX_LCORE; i++)
299 lcore_states[i].service_mask &= ~(UINT64_C(1) << service_id);
301 memset(&rte_services[service_id], 0,
302 sizeof(struct rte_service_spec_impl));
308 rte_service_start(struct rte_service_spec *service)
310 struct rte_service_spec_impl *s =
311 (struct rte_service_spec_impl *)service;
312 s->runstate = RUNSTATE_RUNNING;
318 rte_service_stop(struct rte_service_spec *service)
320 struct rte_service_spec_impl *s =
321 (struct rte_service_spec_impl *)service;
322 s->runstate = RUNSTATE_STOPPED;
328 rte_service_runner_func(void *arg)
332 const int lcore = rte_lcore_id();
333 struct core_state *cs = &lcore_states[lcore];
335 while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
336 const uint64_t service_mask = cs->service_mask;
337 for (i = 0; i < rte_service_count; i++) {
338 struct rte_service_spec_impl *s = &rte_services[i];
339 if (s->runstate != RUNSTATE_RUNNING ||
340 !(service_mask & (UINT64_C(1) << i)))
343 /* check do we need cmpset, if MT safe or <= 1 core
344 * mapped, atomic ops are not required.
346 const int need_cmpset = !((service_mt_safe(s) == 0) &&
347 (s->num_mapped_cores > 1));
348 uint32_t *lock = (uint32_t *)&s->execute_lock;
350 if (need_cmpset || rte_atomic32_cmpset(lock, 0, 1)) {
351 void *userdata = s->spec.callback_userdata;
353 if (service_stats_enabled(s)) {
354 uint64_t start = rte_rdtsc();
355 s->spec.callback(userdata);
356 uint64_t end = rte_rdtsc();
357 s->cycles_spent += end - start;
358 cs->calls_per_service[i]++;
361 s->spec.callback(userdata);
364 rte_atomic32_clear(&s->execute_lock);
371 lcore_config[lcore].state = WAIT;
377 rte_service_lcore_count(void)
381 for (i = 0; i < RTE_MAX_LCORE; i++)
382 count += lcore_states[i].is_service_core;
387 rte_service_lcore_list(uint32_t array[], uint32_t n)
389 uint32_t count = rte_service_lcore_count();
398 for (i = 0; i < RTE_MAX_LCORE; i++) {
399 struct core_state *cs = &lcore_states[i];
400 if (cs->is_service_core) {
410 rte_service_lcore_count_services(uint32_t lcore)
412 if (lcore >= RTE_MAX_LCORE)
415 struct core_state *cs = &lcore_states[lcore];
416 if (!cs->is_service_core)
419 return __builtin_popcountll(cs->service_mask);
423 rte_service_start_with_defaults(void)
425 /* create a default mapping from cores to services, then start the
426 * services to make them transparent to unaware applications.
430 uint32_t count = rte_service_get_count();
432 int32_t lcore_iter = 0;
433 uint32_t ids[RTE_MAX_LCORE];
434 int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
436 if (lcore_count == 0)
439 for (i = 0; (int)i < lcore_count; i++)
440 rte_service_lcore_start(ids[i]);
442 for (i = 0; i < count; i++) {
443 struct rte_service_spec *s = rte_service_get_by_id(i);
447 /* do 1:1 core mapping here, with each service getting
448 * assigned a single core by default. Adding multiple services
449 * should multiplex to a single core, or 1:1 if there are the
450 * same amount of services as service-cores
452 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
457 if (lcore_iter >= lcore_count)
460 ret = rte_service_start(s);
469 service_update(struct rte_service_spec *service, uint32_t lcore,
470 uint32_t *set, uint32_t *enabled)
475 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
476 if ((struct rte_service_spec *)&rte_services[i] == service &&
483 if (sid == -1 || lcore >= RTE_MAX_LCORE)
486 if (!lcore_states[lcore].is_service_core)
489 uint64_t sid_mask = UINT64_C(1) << sid;
492 lcore_states[lcore].service_mask |= sid_mask;
493 rte_services[sid].num_mapped_cores++;
495 lcore_states[lcore].service_mask &= ~(sid_mask);
496 rte_services[sid].num_mapped_cores--;
501 *enabled = (lcore_states[lcore].service_mask & (sid_mask));
509 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
511 struct rte_service_spec_impl *s;
512 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
513 uint32_t on = enabled > 0;
514 return service_update(&s->spec, lcore, &on, 0);
518 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
520 struct rte_service_spec_impl *s;
521 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
523 int ret = service_update(&s->spec, lcore, 0, &enabled);
529 int32_t rte_service_lcore_reset_all(void)
531 /* loop over cores, reset all to mask 0 */
533 for (i = 0; i < RTE_MAX_LCORE; i++) {
534 lcore_states[i].service_mask = 0;
535 lcore_states[i].is_service_core = 0;
536 lcore_states[i].runstate = RUNSTATE_STOPPED;
538 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
539 rte_services[i].num_mapped_cores = 0;
547 set_lcore_state(uint32_t lcore, int32_t state)
549 /* mark core state in hugepage backed config */
550 struct rte_config *cfg = rte_eal_get_configuration();
551 cfg->lcore_role[lcore] = state;
553 /* mark state in process local lcore_config */
554 lcore_config[lcore].core_role = state;
556 /* update per-lcore optimized state tracking */
557 lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
561 rte_service_lcore_add(uint32_t lcore)
563 if (lcore >= RTE_MAX_LCORE)
565 if (lcore_states[lcore].is_service_core)
568 set_lcore_state(lcore, ROLE_SERVICE);
570 /* ensure that after adding a core the mask and state are defaults */
571 lcore_states[lcore].service_mask = 0;
572 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
579 rte_service_lcore_del(uint32_t lcore)
581 if (lcore >= RTE_MAX_LCORE)
584 struct core_state *cs = &lcore_states[lcore];
585 if (!cs->is_service_core)
588 if (cs->runstate != RUNSTATE_STOPPED)
591 set_lcore_state(lcore, ROLE_RTE);
598 rte_service_lcore_start(uint32_t lcore)
600 if (lcore >= RTE_MAX_LCORE)
603 struct core_state *cs = &lcore_states[lcore];
604 if (!cs->is_service_core)
607 if (cs->runstate == RUNSTATE_RUNNING)
610 /* set core to run state first, and then launch otherwise it will
611 * return immediately as runstate keeps it in the service poll loop
613 lcore_states[lcore].runstate = RUNSTATE_RUNNING;
615 int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
616 /* returns -EBUSY if the core is already launched, 0 on success */
621 rte_service_lcore_stop(uint32_t lcore)
623 if (lcore >= RTE_MAX_LCORE)
626 if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
630 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
632 lcore_states[i].service_mask & (UINT64_C(1) << i);
633 int32_t service_running = rte_services[i].runstate !=
635 int32_t only_core = rte_services[i].num_mapped_cores == 1;
637 /* if the core is mapped, and the service is running, and this
638 * is the only core that is mapped, the service would cease to
639 * run if this core stopped, so fail instead.
641 if (enabled && service_running && only_core)
645 lcore_states[lcore].runstate = RUNSTATE_STOPPED;
651 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
652 uint64_t all_cycles, uint32_t reset)
654 /* avoid divide by zero */
662 fprintf(f, " %s: stats %d\tcalls %"PRIu64"\tcycles %"
663 PRIu64"\tavg: %"PRIu64"\n",
664 s->spec.name, service_stats_enabled(s), s->calls,
665 s->cycles_spent, s->cycles_spent / calls);
674 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
677 struct core_state *cs = &lcore_states[lcore];
679 fprintf(f, "%02d\t", lcore);
680 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
681 if (!service_valid(i))
683 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
685 cs->calls_per_service[i] = 0;
690 int32_t rte_service_dump(FILE *f, struct rte_service_spec *service)
694 uint64_t total_cycles = 0;
695 for (i = 0; i < rte_service_count; i++) {
696 if (!service_valid(i))
698 total_cycles += rte_services[i].cycles_spent;
702 struct rte_service_spec_impl *s =
703 (struct rte_service_spec_impl *)service;
704 fprintf(f, "Service %s Summary\n", s->spec.name);
706 rte_service_dump_one(f, s, total_cycles, reset);
710 fprintf(f, "Services Summary\n");
711 for (i = 0; i < rte_service_count; i++) {
713 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
716 fprintf(f, "Service Cores Summary\n");
717 for (i = 0; i < RTE_MAX_LCORE; i++) {
718 if (lcore_config[i].core_role != ROLE_SERVICE)
722 service_dump_calls_per_lcore(f, i, reset);