1 /*
2 * Copyright 2009-2020 the Pacemaker project contributors
3 *
4 * This source code is licensed under the GNU General Public License version 2
5 * or later (GPLv2+) WITHOUT ANY WARRANTY.
6 */
7
8 #include <stdint.h> // uint32_t, uint64_t
9 #include <crm/common/mainloop.h>
10
11 /*!
12 * \internal
13 * \brief Check to see if target was fenced in the last few seconds.
14 * \param tolerance, The number of seconds to look back in time
15 * \param target, The node to search for
16 * \param action, The action we want to match.
17 *
18 * \retval FALSE, not match
19 * \retval TRUE, fencing operation took place in the last 'tolerance' number of seconds.
20 */
21 gboolean stonith_check_fence_tolerance(int tolerance, const char *target, const char *action);
22
23 typedef struct stonith_device_s {
24 char *id;
25 char *agent;
26 char *namespace;
27
28 /*! list of actions that must execute on the target node. Used for unfencing */
29 char *on_target_actions;
30 GListPtr targets;
31 time_t targets_age;
32 gboolean has_attr_map;
33 /* should nodeid parameter for victim be included in agent arguments */
34 gboolean include_nodeid;
35 /* whether the cluster should automatically unfence nodes with the device */
36 gboolean automatic_unfencing;
37 guint priority;
38
39 uint32_t flags; // Group of enum st_device_flags
40
41 GHashTable *params;
42 GHashTable *aliases;
43 GList *pending_ops;
44 crm_trigger_t *work;
45 xmlNode *agent_metadata;
46
47 /*! A verified device is one that has contacted the
48 * agent successfully to perform a monitor operation */
49 gboolean verified;
50
51 gboolean cib_registered;
52 gboolean api_registered;
53 } stonith_device_t;
54
55 /* These values are used to index certain arrays by "phase". Usually an
56 * operation has only one "phase", so phase is always zero. However, some
57 * reboots are remapped to "off" then "on", in which case "reboot" will be
58 * phase 0, "off" will be phase 1 and "on" will be phase 2.
59 */
60 enum st_remap_phase {
61 st_phase_requested = 0,
62 st_phase_off = 1,
63 st_phase_on = 2,
64 st_phase_max = 3
65 };
66
67 /* These values provide additional information for STONITH's asynchronous reply response.
68 * The st_reply_opt_merged value indicates an operation that has been merged and completed without being executed.
69 */
70 enum st_replay_option {
71 st_reply_opt_none = 0x00000000,
72 st_reply_opt_merged = 0x00000001,
73 };
74
75 typedef struct remote_fencing_op_s {
76 /* The unique id associated with this operation */
77 char *id;
78 /*! The node this operation will fence */
79 char *target;
80 /*! The fencing action to perform on the target. (reboot, on, off) */
81 char *action;
82
83 /*! When was the fencing action recorded (seconds since epoch) */
84 time_t created;
85
86 /*! Marks if the final notifications have been sent to local stonith clients. */
87 gboolean notify_sent;
88 /*! The number of query replies received */
89 guint replies;
90 /*! The number of query replies expected */
91 guint replies_expected;
92 /*! Does this node own control of this operation */
93 gboolean owner;
94 /*! After query is complete, This the high level timer that expires the entire operation */
95 guint op_timer_total;
96 /*! This timer expires the current fencing request. Many fencing
97 * requests may exist in a single operation */
98 guint op_timer_one;
99 /*! This timer expires the query request sent out to determine
100 * what nodes are contain what devices, and who those devices can fence */
101 guint query_timer;
102 /*! This is the default timeout to use for each fencing device if no
103 * custom timeout is received in the query. */
104 gint base_timeout;
105 /*! This is the calculated total timeout an operation can take before
106 * expiring. This is calculated by adding together all the timeout
107 * values associated with the devices this fencing operation may call */
108 gint total_timeout;
109
110 /*! Requested fencing delay.
111 * Value -1 means disable any static/random fencing delays. */
112 int delay;
113
114 /*! Delegate is the node being asked to perform a fencing action
115 * on behalf of the node that owns the remote operation. Some operations
116 * will involve multiple delegates. This value represents the final delegate
117 * that is used. */
118 char *delegate;
119 /*! The point at which the remote operation completed */
120 time_t completed;
121 //! Group of enum stonith_call_options associated with this operation
122 uint32_t call_options;
123
124 /*! The current state of the remote operation. This indicates
125 * what stage the op is in, query, exec, done, duplicate, failed. */
126 enum op_state state;
127 /*! The node that owns the remote operation */
128 char *originator;
129 /*! The local client id that initiated the fencing request */
130 char *client_id;
131 /*! The client's call_id that initiated the fencing request */
132 int client_callid;
133 /*! The name of client that initiated the fencing request */
134 char *client_name;
135 /*! List of the received query results for all the nodes in the cpg group */
136 GListPtr query_results;
137 /*! The original request that initiated the remote stonith operation */
138 xmlNode *request;
139
140 /*! The current topology level being executed */
141 guint level;
142 /*! The current operation phase being executed */
143 enum st_remap_phase phase;
144
145 /*! Devices with automatic unfencing (always run if "on" requested, never if remapped) */
146 GListPtr automatic_list;
147 /*! List of all devices at the currently executing topology level */
148 GListPtr devices_list;
149 /*! Current entry in the topology device list */
150 GListPtr devices;
151
152 /*! List of duplicate operations attached to this operation. Once this operation
153 * completes, the duplicate operations will be closed out as well. */
154 GListPtr duplicates;
155
156 } remote_fencing_op_t;
157
158 /*!
159 * \internal
160 * \brief Broadcast the result of an operation to the peers.
161 * \param op, Operation whose result should be broadcast
162 * \param rc, Result of the operation
163 */
164 void stonith_bcast_result_to_peers(remote_fencing_op_t * op, int rc, gboolean op_merged);
165
166 // Fencer-specific client flags
167 enum st_client_flags {
168 st_callback_unknown = UINT64_C(0),
169 st_callback_notify_fence = (UINT64_C(1) << 0),
170 st_callback_device_add = (UINT64_C(1) << 2),
171 st_callback_device_del = (UINT64_C(1) << 4),
172 st_callback_notify_history = (UINT64_C(1) << 5),
173 st_callback_notify_history_synced = (UINT64_C(1) << 6)
174 };
175
176 /*
177 * Complex fencing requirements are specified via fencing topologies.
178 * A topology consists of levels; each level is a list of fencing devices.
179 * Topologies are stored in a hash table by node name. When a node needs to be
180 * fenced, if it has an entry in the topology table, the levels are tried
181 * sequentially, and the devices in each level are tried sequentially.
182 * Fencing is considered successful as soon as any level succeeds;
183 * a level is considered successful if all its devices succeed.
184 * Essentially, all devices at a given level are "and-ed" and the
185 * levels are "or-ed".
186 *
187 * This structure is used for the topology table entries.
188 * Topology levels start from 1, so levels[0] is unused and always NULL.
189 */
190 typedef struct stonith_topology_s {
191 int kind;
192
193 /*! Node name regex or attribute name=value for which topology applies */
194 char *target;
195 char *target_value;
196 char *target_pattern;
197 char *target_attribute;
198
199 /*! Names of fencing devices at each topology level */
200 GListPtr levels[ST_LEVEL_MAX];
201
202 } stonith_topology_t;
203
204 void init_device_list(void);
205 void free_device_list(void);
206 void init_topology_list(void);
207 void free_topology_list(void);
208 void free_stonith_remote_op_list(void);
209 void init_stonith_remote_op_hash_table(GHashTable **table);
210 void free_metadata_cache(void);
211
212 uint64_t get_stonith_flag(const char *name);
213
214 void stonith_command(pcmk__client_t *client, uint32_t id, uint32_t flags,
215 xmlNode *op_request, const char *remote_peer);
216
217 int stonith_device_register(xmlNode * msg, const char **desc, gboolean from_cib);
218
219 int stonith_device_remove(const char *id, gboolean from_cib);
220
221 char *stonith_level_key(xmlNode * msg, int mode);
222 int stonith_level_kind(xmlNode * msg);
223 int stonith_level_register(xmlNode * msg, char **desc);
224
225 int stonith_level_remove(xmlNode * msg, char **desc);
226
227 stonith_topology_t *find_topology_for_host(const char *host);
228
229 void do_local_reply(xmlNode * notify_src, const char *client_id, gboolean sync_reply,
230 gboolean from_peer);
231
232 xmlNode *stonith_construct_reply(xmlNode * request, const char *output, xmlNode * data,
233 int rc);
234
235 void
236 do_stonith_async_timeout_update(const char *client, const char *call_id, int timeout);
237
238 void do_stonith_notify(int options, const char *type, int result, xmlNode * data);
239 void do_stonith_notify_device(int options, const char *op, int rc, const char *desc);
240 void do_stonith_notify_level(int options, const char *op, int rc, const char *desc);
241
242 remote_fencing_op_t *initiate_remote_stonith_op(pcmk__client_t *client,
243 xmlNode *request,
244 gboolean manual_ack);
245
246 int process_remote_stonith_exec(xmlNode * msg);
247
248 int process_remote_stonith_query(xmlNode * msg);
249
250 void *create_remote_stonith_op(const char *client, xmlNode * request, gboolean peer);
251
252 int stonith_fence_history(xmlNode *msg, xmlNode **output,
253 const char *remote_peer, int options);
254
255 void stonith_fence_history_trim(void);
256
257 bool fencing_peer_active(crm_node_t *peer);
258
259 int stonith_manual_ack(xmlNode * msg, remote_fencing_op_t * op);
260
261 gboolean string_in_list(GListPtr list, const char *item);
262
263 gboolean node_has_attr(const char *node, const char *name, const char *value);
264
265 extern char *stonith_our_uname;
266 extern gboolean stand_alone;
267 extern GHashTable *device_list;
268 extern GHashTable *topology;
269 extern long stonith_watchdog_timeout_ms;
270
271 extern GHashTable *stonith_remote_op_list;