1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2016 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_cycles.h>
11 #include <rte_keepalive.h>
12 #include <rte_malloc.h>
14 struct rte_keepalive {
16 enum rte_keepalive_state __rte_cache_aligned state_flags[
17 RTE_KEEPALIVE_MAXCORES];
19 /** Last-seen-alive timestamps */
20 uint64_t last_alive[RTE_KEEPALIVE_MAXCORES];
24 * Indexed by core id, non-zero if the core should be checked.
26 uint8_t active_cores[RTE_KEEPALIVE_MAXCORES];
28 /** Dead core handler. */
29 rte_keepalive_failure_callback_t callback;
32 * Dead core handler app data.
33 * Pointer is passed to dead core handler.
39 /** Core state relay handler. */
40 rte_keepalive_relay_callback_t relay_callback;
43 * Core state relay handler app data.
44 * Pointer is passed to live core handler.
46 void *relay_callback_data;
50 print_trace(const char *msg, struct rte_keepalive *keepcfg, int idx_core)
52 RTE_LOG(INFO, EAL, "%sLast seen %" PRId64 "ms ago.\n",
54 ((rte_rdtsc() - keepcfg->last_alive[idx_core])*1000)
60 rte_keepalive_dispatch_pings(__rte_unused void *ptr_timer,
63 struct rte_keepalive *keepcfg = ptr_data;
66 for (idx_core = 0; idx_core < RTE_KEEPALIVE_MAXCORES; idx_core++) {
67 if (keepcfg->active_cores[idx_core] == 0)
70 switch (keepcfg->state_flags[idx_core]) {
71 case RTE_KA_STATE_UNUSED:
73 case RTE_KA_STATE_ALIVE: /* Alive */
74 keepcfg->state_flags[idx_core] = RTE_KA_STATE_MISSING;
75 keepcfg->last_alive[idx_core] = rte_rdtsc();
77 case RTE_KA_STATE_MISSING: /* MIA */
78 print_trace("Core MIA. ", keepcfg, idx_core);
79 keepcfg->state_flags[idx_core] = RTE_KA_STATE_DEAD;
81 case RTE_KA_STATE_DEAD: /* Dead */
82 keepcfg->state_flags[idx_core] = RTE_KA_STATE_GONE;
83 print_trace("Core died. ", keepcfg, idx_core);
84 if (keepcfg->callback)
86 keepcfg->callback_data,
90 case RTE_KA_STATE_GONE: /* Buried */
92 case RTE_KA_STATE_DOZING: /* Core going idle */
93 keepcfg->state_flags[idx_core] = RTE_KA_STATE_SLEEP;
94 keepcfg->last_alive[idx_core] = rte_rdtsc();
96 case RTE_KA_STATE_SLEEP: /* Idled core */
99 if (keepcfg->relay_callback)
100 keepcfg->relay_callback(
101 keepcfg->relay_callback_data,
103 keepcfg->state_flags[idx_core],
104 keepcfg->last_alive[idx_core]
109 struct rte_keepalive *
110 rte_keepalive_create(rte_keepalive_failure_callback_t callback,
113 struct rte_keepalive *keepcfg;
115 keepcfg = rte_zmalloc("RTE_EAL_KEEPALIVE",
116 sizeof(struct rte_keepalive),
117 RTE_CACHE_LINE_SIZE);
118 if (keepcfg != NULL) {
119 keepcfg->callback = callback;
120 keepcfg->callback_data = data;
121 keepcfg->tsc_initial = rte_rdtsc();
122 keepcfg->tsc_mhz = rte_get_tsc_hz() / 1000;
127 void rte_keepalive_register_relay_callback(struct rte_keepalive *keepcfg,
128 rte_keepalive_relay_callback_t callback,
131 keepcfg->relay_callback = callback;
132 keepcfg->relay_callback_data = data;
136 rte_keepalive_register_core(struct rte_keepalive *keepcfg, const int id_core)
138 if (id_core < RTE_KEEPALIVE_MAXCORES) {
139 keepcfg->active_cores[id_core] = RTE_KA_STATE_ALIVE;
140 keepcfg->last_alive[id_core] = rte_rdtsc();
145 rte_keepalive_mark_alive(struct rte_keepalive *keepcfg)
147 keepcfg->state_flags[rte_lcore_id()] = RTE_KA_STATE_ALIVE;
151 rte_keepalive_mark_sleep(struct rte_keepalive *keepcfg)
153 keepcfg->state_flags[rte_lcore_id()] = RTE_KA_STATE_DOZING;