]>
Commit | Line | Data |
---|---|---|
9a1955a7 JF |
1 | /* |
2 | * Copyright (c) 2015-2016 Red Hat, Inc. | |
3 | * | |
4 | * All rights reserved. | |
5 | * | |
6 | * Author: Jan Friesse (jfriesse@redhat.com) | |
7 | * | |
8 | * This software licensed under BSD license, the text of which follows: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions are met: | |
12 | * | |
13 | * - Redistributions of source code must retain the above copyright notice, | |
14 | * this list of conditions and the following disclaimer. | |
15 | * - Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * - Neither the name of the Red Hat, Inc. nor the names of its | |
19 | * contributors may be used to endorse or promote products derived from this | |
20 | * software without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
32 | * THE POSSIBILITY OF SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #include "qdevice-log.h" | |
36 | #include "qdevice-log-debug.h" | |
37 | #include "qdevice-net-send.h" | |
38 | #include "qdevice-net-votequorum.h" | |
39 | #include "msg.h" | |
40 | #include "utils.h" | |
41 | ||
42 | int | |
43 | qdevice_net_send_echo_request(struct qdevice_net_instance *instance) | |
44 | { | |
45 | struct send_buffer_list_entry *send_buffer; | |
46 | ||
47 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
48 | if (send_buffer == NULL) { | |
49 | qdevice_log(LOG_CRIT, "Can't allocate send list buffer for reply msg."); | |
50 | ||
51 | return (-1); | |
52 | } | |
53 | ||
54 | instance->echo_request_expected_msg_seq_num++; | |
55 | ||
56 | if (msg_create_echo_request(&send_buffer->buffer, 1, | |
57 | instance->echo_request_expected_msg_seq_num) == 0) { | |
58 | qdevice_log(LOG_ERR, "Can't allocate send buffer for echo request msg"); | |
59 | ||
60 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
61 | ||
62 | return (-1); | |
63 | } | |
64 | ||
65 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
66 | ||
67 | return (0); | |
68 | } | |
69 | ||
70 | int | |
71 | qdevice_net_send_preinit(struct qdevice_net_instance *instance) | |
72 | { | |
73 | struct send_buffer_list_entry *send_buffer; | |
74 | ||
75 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
76 | if (send_buffer == NULL) { | |
77 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for preinit msg"); | |
78 | ||
79 | return (-1); | |
80 | } | |
81 | ||
82 | if (msg_create_preinit(&send_buffer->buffer, instance->cluster_name, 1, | |
83 | instance->last_msg_seq_num) == 0) { | |
84 | qdevice_log(LOG_ERR, "Can't allocate buffer"); | |
85 | ||
86 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
87 | return (-1); | |
88 | } | |
89 | ||
90 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
91 | ||
92 | instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY; | |
93 | ||
94 | return (0); | |
95 | } | |
96 | ||
97 | int | |
98 | qdevice_net_send_init(struct qdevice_net_instance *instance) | |
99 | { | |
100 | enum msg_type *supported_msgs; | |
101 | size_t no_supported_msgs; | |
102 | enum tlv_opt_type *supported_opts; | |
103 | size_t no_supported_opts; | |
104 | struct send_buffer_list_entry *send_buffer; | |
105 | struct tlv_ring_id tlv_rid; | |
106 | ||
107 | tlv_get_supported_options(&supported_opts, &no_supported_opts); | |
108 | msg_get_supported_messages(&supported_msgs, &no_supported_msgs); | |
109 | instance->last_msg_seq_num++; | |
110 | ||
111 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
112 | if (send_buffer == NULL) { | |
113 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for init msg"); | |
114 | ||
115 | return (-1); | |
116 | } | |
117 | ||
118 | qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid, | |
119 | &instance->qdevice_instance_ptr->vq_node_list_ring_id); | |
120 | ||
121 | if (msg_create_init(&send_buffer->buffer, 1, instance->last_msg_seq_num, | |
122 | instance->decision_algorithm, | |
123 | supported_msgs, no_supported_msgs, supported_opts, no_supported_opts, | |
124 | instance->qdevice_instance_ptr->node_id, instance->heartbeat_interval, | |
125 | &instance->tie_breaker, &tlv_rid) == 0) { | |
126 | qdevice_log(LOG_ERR, "Can't allocate send buffer for init msg"); | |
127 | ||
128 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
129 | return (-1); | |
130 | } | |
131 | ||
132 | memcpy(&instance->last_sent_ring_id, &tlv_rid, sizeof(instance->last_sent_ring_id)); | |
133 | ||
134 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
135 | ||
136 | instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY; | |
137 | ||
138 | return (0); | |
139 | } | |
140 | ||
141 | int | |
142 | qdevice_net_send_ask_for_vote(struct qdevice_net_instance *instance) | |
143 | { | |
144 | struct send_buffer_list_entry *send_buffer; | |
145 | ||
146 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
147 | if (send_buffer == NULL) { | |
148 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for ask for vote msg"); | |
149 | ||
150 | return (-1); | |
151 | } | |
152 | ||
153 | instance->last_msg_seq_num++; | |
154 | ||
155 | qdevice_log(LOG_DEBUG, "Sending ask for vote seq = "UTILS_PRI_MSG_SEQ, | |
156 | instance->last_msg_seq_num); | |
157 | ||
158 | if (msg_create_ask_for_vote(&send_buffer->buffer, instance->last_msg_seq_num) == 0) { | |
159 | qdevice_log(LOG_ERR, "Can't allocate send buffer for ask for vote msg"); | |
160 | ||
161 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
162 | return (-1); | |
163 | } | |
164 | ||
165 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
166 | ||
167 | return (0); | |
168 | } | |
169 | ||
170 | int | |
171 | qdevice_net_send_config_node_list(struct qdevice_net_instance *instance, | |
172 | const struct node_list *nlist, int config_version_set, uint64_t config_version, | |
173 | int initial) | |
174 | { | |
175 | struct send_buffer_list_entry *send_buffer; | |
176 | ||
177 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
178 | if (send_buffer == NULL) { | |
179 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for config " | |
180 | "node list msg"); | |
181 | ||
182 | return (-1); | |
183 | } | |
184 | ||
185 | instance->last_msg_seq_num++; | |
186 | ||
187 | qdevice_log(LOG_DEBUG, "Sending config node list seq = "UTILS_PRI_MSG_SEQ, | |
188 | instance->last_msg_seq_num); | |
189 | qdevice_log_debug_dump_node_list(nlist); | |
190 | ||
191 | if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num, | |
192 | (initial ? TLV_NODE_LIST_TYPE_INITIAL_CONFIG : TLV_NODE_LIST_TYPE_CHANGED_CONFIG), | |
193 | 0, NULL, config_version_set, config_version, 0, TLV_QUORATE_INQUORATE, | |
194 | 0, TLV_HEURISTICS_UNDEFINED, nlist) == 0) { | |
195 | qdevice_log(LOG_ERR, "Can't allocate send buffer for config list msg"); | |
196 | ||
197 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
198 | return (-1); | |
199 | } | |
200 | ||
201 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
202 | ||
203 | return (0); | |
204 | } | |
205 | ||
206 | int | |
207 | qdevice_net_send_heuristics_change(struct qdevice_net_instance *instance, | |
208 | enum tlv_heuristics heuristics) | |
209 | { | |
210 | struct send_buffer_list_entry *send_buffer; | |
211 | ||
212 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
213 | if (send_buffer == NULL) { | |
214 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for heuristics change msg"); | |
215 | ||
216 | return (-1); | |
217 | } | |
218 | ||
219 | instance->last_msg_seq_num++; | |
220 | ||
221 | qdevice_log(LOG_DEBUG, "Sending heuristics change seq = "UTILS_PRI_MSG_SEQ | |
222 | ", heuristics = %s", | |
223 | instance->last_msg_seq_num, tlv_heuristics_to_str(heuristics)); | |
224 | ||
225 | if (msg_create_heuristics_change(&send_buffer->buffer, instance->last_msg_seq_num, | |
226 | heuristics) == 0) { | |
227 | qdevice_log(LOG_ERR, "Can't allocate send buffer for heuristics change msg"); | |
228 | ||
229 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
230 | return (-1); | |
231 | } | |
232 | ||
233 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
234 | ||
235 | return (0); | |
236 | } | |
237 | ||
238 | int | |
239 | qdevice_net_send_membership_node_list(struct qdevice_net_instance *instance, | |
240 | const struct tlv_ring_id *ring_id, | |
241 | uint32_t node_list_entries, uint32_t node_list[], enum tlv_heuristics heuristics) | |
242 | { | |
243 | struct node_list nlist; | |
244 | struct send_buffer_list_entry *send_buffer; | |
245 | uint32_t i; | |
246 | ||
247 | node_list_init(&nlist); | |
248 | ||
249 | for (i = 0; i < node_list_entries; i++) { | |
250 | if (node_list_add(&nlist, node_list[i], 0, TLV_NODE_STATE_NOT_SET) == NULL) { | |
251 | qdevice_log(LOG_ERR, "Can't allocate membership node list."); | |
252 | ||
253 | node_list_free(&nlist); | |
254 | ||
255 | return (-1); | |
256 | } | |
257 | } | |
258 | ||
259 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
260 | if (send_buffer == NULL) { | |
261 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for membership " | |
262 | "node list msg"); | |
263 | ||
264 | node_list_free(&nlist); | |
265 | ||
266 | return (-1); | |
267 | } | |
268 | ||
269 | instance->last_msg_seq_num++; | |
270 | ||
271 | qdevice_log(LOG_DEBUG, "Sending membership node list seq = "UTILS_PRI_MSG_SEQ", " | |
272 | "ringid = ("UTILS_PRI_RING_ID"), heuristics = %s.", instance->last_msg_seq_num, | |
273 | ring_id->node_id, ring_id->seq, tlv_heuristics_to_str(heuristics)); | |
274 | qdevice_log_debug_dump_node_list(&nlist); | |
275 | ||
276 | if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num, | |
277 | TLV_NODE_LIST_TYPE_MEMBERSHIP, | |
278 | 1, ring_id, 0, 0, 0, 0, 1, heuristics, &nlist) == 0) { | |
279 | qdevice_log(LOG_ERR, "Can't allocate send buffer for membership list msg"); | |
280 | ||
281 | node_list_free(&nlist); | |
282 | ||
283 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
284 | return (-1); | |
285 | } | |
286 | ||
287 | memcpy(&instance->last_sent_ring_id, ring_id, sizeof(instance->last_sent_ring_id)); | |
288 | ||
289 | node_list_free(&nlist); | |
290 | ||
291 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
292 | ||
293 | return (0); | |
294 | } | |
295 | ||
296 | int | |
297 | qdevice_net_send_quorum_node_list(struct qdevice_net_instance *instance, | |
298 | enum tlv_quorate quorate, | |
299 | uint32_t node_list_entries, votequorum_node_t node_list[]) | |
300 | { | |
301 | struct node_list nlist; | |
302 | struct send_buffer_list_entry *send_buffer; | |
303 | uint32_t i; | |
304 | ||
305 | node_list_init(&nlist); | |
306 | ||
307 | for (i = 0; i < node_list_entries; i++) { | |
308 | if (node_list[i].nodeid == 0) { | |
309 | continue; | |
310 | } | |
311 | ||
312 | if (node_list_add(&nlist, node_list[i].nodeid, 0, | |
313 | qdevice_net_votequorum_node_state_to_tlv(node_list[i].state)) == NULL) { | |
314 | qdevice_log(LOG_ERR, "Can't allocate quorum node list."); | |
315 | ||
316 | node_list_free(&nlist); | |
317 | ||
318 | return (-1); | |
319 | } | |
320 | } | |
321 | ||
322 | send_buffer = send_buffer_list_get_new(&instance->send_buffer_list); | |
323 | if (send_buffer == NULL) { | |
324 | qdevice_log(LOG_ERR, "Can't allocate send list buffer for quorum " | |
325 | "node list msg"); | |
326 | ||
327 | node_list_free(&nlist); | |
328 | ||
329 | return (-1); | |
330 | } | |
331 | ||
332 | instance->last_msg_seq_num++; | |
333 | ||
334 | qdevice_log(LOG_DEBUG, "Sending quorum node list seq = "UTILS_PRI_MSG_SEQ", quorate = %u", | |
335 | instance->last_msg_seq_num, quorate); | |
336 | qdevice_log_debug_dump_node_list(&nlist); | |
337 | ||
338 | if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num, | |
339 | TLV_NODE_LIST_TYPE_QUORUM, | |
340 | 0, NULL, 0, 0, 1, quorate, 0, TLV_HEURISTICS_UNDEFINED, &nlist) == 0) { | |
341 | qdevice_log(LOG_ERR, "Can't allocate send buffer for quorum list msg"); | |
342 | ||
343 | node_list_free(&nlist); | |
344 | ||
345 | send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer); | |
346 | return (-1); | |
347 | } | |
348 | ||
349 | node_list_free(&nlist); | |
350 | ||
351 | send_buffer_list_put(&instance->send_buffer_list, send_buffer); | |
352 | ||
353 | return (0); | |
354 | } |