lib: use SPDX tag for Intel copyright files
[dpdk.git] / lib / librte_eal / common / rte_keepalive.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2016 Intel Corporation
3  */
4
5 #include <inttypes.h>
6
7 #include <rte_common.h>
8 #include <rte_cycles.h>
9 #include <rte_lcore.h>
10 #include <rte_log.h>
11 #include <rte_keepalive.h>
12 #include <rte_malloc.h>
13
14 struct rte_keepalive {
15         /** Core Liveness. */
16         enum rte_keepalive_state __rte_cache_aligned state_flags[
17                 RTE_KEEPALIVE_MAXCORES];
18
19         /** Last-seen-alive timestamps */
20         uint64_t last_alive[RTE_KEEPALIVE_MAXCORES];
21
22         /**
23          * Cores to check.
24          * Indexed by core id, non-zero if the core should be checked.
25          */
26         uint8_t active_cores[RTE_KEEPALIVE_MAXCORES];
27
28         /** Dead core handler. */
29         rte_keepalive_failure_callback_t callback;
30
31         /**
32          * Dead core handler app data.
33          * Pointer is passed to dead core handler.
34          */
35         void *callback_data;
36         uint64_t tsc_initial;
37         uint64_t tsc_mhz;
38
39         /** Core state relay handler. */
40         rte_keepalive_relay_callback_t relay_callback;
41
42         /**
43          * Core state relay handler app data.
44          * Pointer is passed to live core handler.
45          */
46         void *relay_callback_data;
47 };
48
49 static void
50 print_trace(const char *msg, struct rte_keepalive *keepcfg, int idx_core)
51 {
52         RTE_LOG(INFO, EAL, "%sLast seen %" PRId64 "ms ago.\n",
53                 msg,
54                 ((rte_rdtsc() - keepcfg->last_alive[idx_core])*1000)
55                 / rte_get_tsc_hz()
56               );
57 }
58
59 void
60 rte_keepalive_dispatch_pings(__rte_unused void *ptr_timer,
61         void *ptr_data)
62 {
63         struct rte_keepalive *keepcfg = ptr_data;
64         int idx_core;
65
66         for (idx_core = 0; idx_core < RTE_KEEPALIVE_MAXCORES; idx_core++) {
67                 if (keepcfg->active_cores[idx_core] == 0)
68                         continue;
69
70                 switch (keepcfg->state_flags[idx_core]) {
71                 case RTE_KA_STATE_UNUSED:
72                         break;
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();
76                         break;
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;
80                         break;
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)
85                                 keepcfg->callback(
86                                         keepcfg->callback_data,
87                                         idx_core
88                                         );
89                         break;
90                 case RTE_KA_STATE_GONE: /* Buried */
91                         break;
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();
95                         break;
96                 case RTE_KA_STATE_SLEEP: /* Idled core */
97                         break;
98                 }
99                 if (keepcfg->relay_callback)
100                         keepcfg->relay_callback(
101                                 keepcfg->relay_callback_data,
102                                 idx_core,
103                                 keepcfg->state_flags[idx_core],
104                                 keepcfg->last_alive[idx_core]
105                                 );
106         }
107 }
108
109 struct rte_keepalive *
110 rte_keepalive_create(rte_keepalive_failure_callback_t callback,
111         void *data)
112 {
113         struct rte_keepalive *keepcfg;
114
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;
123         }
124         return keepcfg;
125 }
126
127 void rte_keepalive_register_relay_callback(struct rte_keepalive *keepcfg,
128         rte_keepalive_relay_callback_t callback,
129         void *data)
130 {
131         keepcfg->relay_callback = callback;
132         keepcfg->relay_callback_data = data;
133 }
134
135 void
136 rte_keepalive_register_core(struct rte_keepalive *keepcfg, const int id_core)
137 {
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();
141         }
142 }
143
144 void
145 rte_keepalive_mark_alive(struct rte_keepalive *keepcfg)
146 {
147         keepcfg->state_flags[rte_lcore_id()] = RTE_KA_STATE_ALIVE;
148 }
149
150 void
151 rte_keepalive_mark_sleep(struct rte_keepalive *keepcfg)
152 {
153         keepcfg->state_flags[rte_lcore_id()] = RTE_KA_STATE_DOZING;
154 }