eal: fix keep alive header for C++
[dpdk.git] / lib / librte_eal / common / rte_keepalive.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 Intel Shannon Ltd. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <inttypes.h>
34
35 #include <rte_common.h>
36 #include <rte_cycles.h>
37 #include <rte_lcore.h>
38 #include <rte_log.h>
39 #include <rte_keepalive.h>
40 #include <rte_malloc.h>
41
42 struct rte_keepalive {
43         /** Core Liveness. */
44         enum rte_keepalive_state {
45                 ALIVE = 1,
46                 MISSING = 0,
47                 DEAD = 2,
48                 GONE = 3
49         } __rte_cache_aligned state_flags[RTE_KEEPALIVE_MAXCORES];
50
51         /** Last-seen-alive timestamps */
52         uint64_t last_alive[RTE_KEEPALIVE_MAXCORES];
53
54         /**
55          * Cores to check.
56          * Indexed by core id, non-zero if the core should be checked.
57          */
58         uint8_t active_cores[RTE_KEEPALIVE_MAXCORES];
59
60         /** Dead core handler. */
61         rte_keepalive_failure_callback_t callback;
62
63         /**
64          * Dead core handler app data.
65          * Pointer is passed to dead core handler.
66          */
67         void *callback_data;
68         uint64_t tsc_initial;
69         uint64_t tsc_mhz;
70 };
71
72
73 static void
74 print_trace(const char *msg, struct rte_keepalive *keepcfg, int idx_core)
75 {
76         RTE_LOG(INFO, EAL, "%sLast seen %" PRId64 "ms ago.\n",
77                 msg,
78                 ((rte_rdtsc() - keepcfg->last_alive[idx_core])*1000)
79                 / rte_get_tsc_hz()
80               );
81 }
82
83
84 void
85 rte_keepalive_dispatch_pings(__rte_unused void *ptr_timer,
86         void *ptr_data)
87 {
88         struct rte_keepalive *keepcfg = ptr_data;
89         int idx_core;
90
91         for (idx_core = 0; idx_core < RTE_KEEPALIVE_MAXCORES; idx_core++) {
92                 if (keepcfg->active_cores[idx_core] == 0)
93                         continue;
94
95                 switch (keepcfg->state_flags[idx_core]) {
96                 case ALIVE: /* Alive */
97                         keepcfg->state_flags[idx_core] = MISSING;
98                         keepcfg->last_alive[idx_core] = rte_rdtsc();
99                         break;
100                 case MISSING: /* MIA */
101                         print_trace("Core MIA. ", keepcfg, idx_core);
102                         keepcfg->state_flags[idx_core] = DEAD;
103                         break;
104                 case DEAD: /* Dead */
105                         keepcfg->state_flags[idx_core] = GONE;
106                         print_trace("Core died. ", keepcfg, idx_core);
107                         if (keepcfg->callback)
108                                 keepcfg->callback(
109                                         keepcfg->callback_data,
110                                         idx_core
111                                         );
112                         break;
113                 case GONE: /* Buried */
114                         break;
115                 }
116         }
117 }
118
119
120 struct rte_keepalive *
121 rte_keepalive_create(rte_keepalive_failure_callback_t callback,
122         void *data)
123 {
124         struct rte_keepalive *keepcfg;
125
126         keepcfg = rte_zmalloc("RTE_EAL_KEEPALIVE",
127                 sizeof(struct rte_keepalive),
128                 RTE_CACHE_LINE_SIZE);
129         if (keepcfg != NULL) {
130                 keepcfg->callback = callback;
131                 keepcfg->callback_data = data;
132                 keepcfg->tsc_initial = rte_rdtsc();
133                 keepcfg->tsc_mhz = rte_get_tsc_hz() / 1000;
134         }
135         return keepcfg;
136 }
137
138
139 void
140 rte_keepalive_register_core(struct rte_keepalive *keepcfg, const int id_core)
141 {
142         if (id_core < RTE_KEEPALIVE_MAXCORES)
143                 keepcfg->active_cores[id_core] = 1;
144 }
145
146
147 void
148 rte_keepalive_mark_alive(struct rte_keepalive *keepcfg)
149 {
150         keepcfg->state_flags[rte_lcore_id()] = ALIVE;
151 }