]> git.proxmox.com Git - mirror_corosync-qdevice.git/blame - qdevices/qdevice-net-algo-test.c
init: Fix init scripts to work with containers
[mirror_corosync-qdevice.git] / qdevices / qdevice-net-algo-test.c
CommitLineData
9a1955a7
JF
1/*
2 * Copyright (c) 2015-2017 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 <sys/types.h>
36
37#include <string.h>
38
39#include "qdevice-net-algo-test.h"
40#include "qdevice-log.h"
41#include "qdevice-net-send.h"
42#include "qdevice-net-cast-vote-timer.h"
43
44/*
45 * Called after qdevice_net_instance is initialized. Connection to server is not yet
46 * established. Used mainly for allocating instance->algorithm_data.
47 *
48 * Callback should return 0 on success or -1 on failure.
49 */
50int
51qdevice_net_algo_test_init(struct qdevice_net_instance *instance)
52{
53
54 instance->algorithm_data = NULL;
55 qdevice_log(LOG_INFO, "algo-test: Initialized");
56
57 return (0);
58}
59
60/*
61 * Called after qdevice connected to qnetd.
62 * send_config_node_list, send_membership_node_list and send_quorum_node_list can be set to
63 * nonzero (default) to make qdevice-net send given lists to qnetd
64 * vote (default TLV_VOTE_WAIT_FOR_REPLY) can be set to update voting timer
65 * heuristics is result of pre connect heuristics and can be updated
66 *
67 * Callback should return 0 on success or -1 on failure (-> disconnect client).
68 */
69int
70qdevice_net_algo_test_connected(struct qdevice_net_instance *instance, enum tlv_heuristics *heuristics,
71 int *send_config_node_list, int *send_membership_node_list, int *send_quorum_node_list, enum tlv_vote *vote)
72{
73
74 qdevice_log(LOG_INFO, "algo-test: Connected");
75
76 return (0);
77}
78
79/*
80 * Called after config node list changed.
81 *
82 * Callback can override send_node_list and vote.
83 * Depending on net_instance->state, they are set acordingly:
84 * If net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS
85 * send_node_list = 0
86 * if cast_vote_timer_vote != TLV_VOTE_ACK
87 * vote = TLV_VOTE_NO_CHANGE
88 * if cast_vote_timer_vote = TLV_VOTE_ACK
89 * vote = TLV_VOTE_NACK.
90 * Otherwise send_node_list = 1 and vote = TLV_VOTE_NO_CHANGE
91 * If send_node_list is set to non zero, node list is sent to qnetd
92 */
93int
94qdevice_net_algo_test_config_node_list_changed(struct qdevice_net_instance *instance,
95 const struct node_list *nlist, int config_version_set, uint64_t config_version,
96 int *send_node_list, enum tlv_vote *vote)
97{
98
99 qdevice_log(LOG_INFO, "algo-test: Config node list changed");
100
101 return (0);
102}
103
104/*
105 * Called after votequorum node list notify is dispatched but heuristics result is not
106 * yet known (heuristics were just executed). Full list together with result of heuristics
107 * are received in qdevice_net_algo_test_votequorum_node_list_heuristics_notify.
108 * This applies also if heuristics are disabled.
109 *
110 * pause_cast_vote_timer is preset to 1. If after callback this value is still != 0,
111 * cast vote timer is paused until heuristics finishes.
112 *
113 * vote is by default set to TLV_VOTE_NO_CHANGE with exception of situation when
114 * net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS and
115 * cast_vote_timer_vote = TLV_VOTE_ACK
116 * then vote is set to TLV_VOTE_NACK. This is to allow qnetd disconnection and still vote
117 * ACK as long as no change happens.
118 *
119 * Callback should return 0 on success or -1 on failure (-> disconnect client).
120 */
121int
122qdevice_net_algo_test_votequorum_node_list_notify(struct qdevice_net_instance *instance,
123 const struct tlv_ring_id *ring_id, uint32_t node_list_entries, uint32_t node_list[],
124 int *pause_cast_vote_timer, enum tlv_vote *vote)
125{
126
127 qdevice_log(LOG_INFO, "algo-test: Votequorum list notify");
128
129 return (0);
130}
131
132/*
133 * Called after votequorum node list notify is dispatched and heuristics result is known.
134 *
135 * Callback should return 0 on success or -1 on failure (-> disconnect client).
136 *
137 * If net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS
138 * send_node_list = 0
139 * if cast_vote_timer_vote != TLV_VOTE_ACK
140 * vote = TLV_VOTE_NO_CHANGE
141 * if cast_vote_timer_vote = TLV_VOTE_ACK
142 * vote = TLV_VOTE_NACK.
143 * Otherwise send_node_list = 1 and vote = TLV_VOTE_WAIT_FOR_REPLY
144 * If send_node_list is set to non zero, node list is sent to qnetd
145 * heuristics results can be used (and overwrite)
146 */
147int
148qdevice_net_algo_test_votequorum_node_list_heuristics_notify(struct qdevice_net_instance *instance,
149 const struct tlv_ring_id *ring_id, uint32_t node_list_entries, uint32_t node_list[],
150 int *send_node_list, enum tlv_vote *vote, enum tlv_heuristics *heuristics)
151{
152
153 qdevice_log(LOG_INFO, "algo-test: Votequorum heuristics list notify");
154
155 return (0);
156}
157
158/*
159 * Called after votequorum quorum notify is dispatched.
160 *
161 * Callback should return 0 on success or -1 on failure (-> disconnect client).
162 *
163 * Callback can override send_node_list and vote.
164 * Depending on net_instance->state, they are set acordingly:
165 * If net_instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS
166 * send_node_list = 0
167 * if cast_vote_timer_vote != TLV_VOTE_ACK
168 * vote = TLV_VOTE_NO_CHANGE
169 * if cast_vote_timer_vote = TLV_VOTE_ACK
170 * vote = TLV_VOTE_NACK.
171 * Otherwise send_node_list = 1 and vote = TLV_VOTE_NO_CHANGE
172 *
173 * If send_node_list is set to non zero, node list is sent to qnetd
174 */
175int
176qdevice_net_algo_test_votequorum_quorum_notify(struct qdevice_net_instance *instance,
177 uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[], int *send_node_list,
178 enum tlv_vote *vote)
179{
180
181 qdevice_log(LOG_INFO, "algo-test: Votequorum quorum notify");
182
183 return (0);
184}
185/*
186 * Called after votequorum expected_votes notify is dispatched.
187 *
188 * Callback should return 0 on success or -1 on failure (-> disconnect client).
189 *
190 * Vote is set to TLV_VOTE_NO_CHANGE
191 */
192int
193qdevice_net_algo_test_votequorum_expected_votes_notify(struct qdevice_net_instance *instance,
194 uint32_t expected_votes, enum tlv_vote *vote)
195{
196
197 qdevice_log(LOG_INFO, "algo-test: Votequorum expected votes notify");
198
199 return (0);
200}
201
202/*
203 * Called when config node list reply is received. Vote is set to value returned by server (and can
204 * be overwriten by algorithm). ring_id is returned by server. ring_id_is_valid is set to 1 only if
205 * received ring id matches last sent ring id. If it is 0, then it usually makes sense to not
206 * update timer.
207 *
208 * Callback should return 0 on success or -1 on failure (-> disconnect client).
209 */
210int
211qdevice_net_algo_test_config_node_list_reply_received(struct qdevice_net_instance *instance,
212 uint32_t seq_number, int initial, const struct tlv_ring_id *ring_id, int ring_id_is_valid,
213 enum tlv_vote *vote)
214{
215
216 qdevice_log(LOG_INFO, "algo-test: Config node list reply");
217
218 if (!ring_id_is_valid) {
219 *vote = TLV_VOTE_NO_CHANGE;
220 }
221
222 return (0);
223}
224
225/*
226 * Called when membership node list reply (reply for votequorum votequorum_nodelist_notify_fn)
227 * is received. Vote is set to value returned by server (and can be overwriten by algorithm).
228 *
229 * Also if server returned TLV_VOTE_ASK_LATER, it's good idea to create timer (call timer_list_add
230 * with instance->main_timer_list parameter) and ask for reply (qdevice_net_send_ask_for_vote).
231 * Another option may be to wait for vote_info message (if server algorithm is configured so).
232 *
233 * ring_id and ring_id_is_valid have same meaning as for
234 * qdevice_net_algo_test_config_node_list_reply_received
235 *
236 * Callback should return 0 on success or -1 on failure (-> disconnect client).
237 */
238int
239qdevice_net_algo_test_membership_node_list_reply_received(struct qdevice_net_instance *instance,
240 uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid,
241 enum tlv_vote *vote)
242{
243
244 qdevice_log(LOG_INFO, "algo-test: Membership node list reply");
245
246 if (!ring_id_is_valid) {
247 *vote = TLV_VOTE_NO_CHANGE;
248 }
249
250 return (0);
251}
252
253/*
254 * Called when quorum node list reply (reply for votequorum votequorum_quorum_notify_fn)
255 * is received. Vote is set to value returned by server (and can be overwriten by algorithm).
256 *
257 * ring_id and ring_id_is_valid have same meaning as for
258 * qdevice_net_algo_test_config_node_list_reply_received
259 *
260 * Callback should return 0 on success or -1 on failure (-> disconnect client).
261 */
262int
263qdevice_net_algo_test_quorum_node_list_reply_received(struct qdevice_net_instance *instance,
264 uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid,
265 enum tlv_vote *vote)
266{
267
268 qdevice_log(LOG_INFO, "algo-test: Quorum node list reply");
269
270 if (!ring_id_is_valid) {
271 *vote = TLV_VOTE_NO_CHANGE;
272 }
273
274 return (0);
275}
276
277/*
278 * Called when reply for ask for vote message was received.
279 * Vote is set to value returned by server (and can be overwriten by algorithm).
280 *
281 * ring_id and ring_id_is_valid have same meaning as for
282 * qdevice_net_algo_test_config_node_list_reply_received
283 *
284 * Callback should return 0 on success or -1 on failure (-> disconnect client).
285 */
286int
287qdevice_net_algo_test_ask_for_vote_reply_received(struct qdevice_net_instance *instance,
288 uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote)
289{
290
291 qdevice_log(LOG_INFO, "algo-test: Ask for vote reply received");
292
293 if (!ring_id_is_valid) {
294 *vote = TLV_VOTE_NO_CHANGE;
295 }
296
297 return (0);
298}
299
300/*
301 * Called when vote info message from server was received.
302 * Vote is set to value sent by server (and can be overwriten by algorithm).
303 *
304 * ring_id and ring_id_is_valid have same meaning as for
305 * qdevice_net_algo_test_config_node_list_reply_received
306 *
307 * Callback should return 0 on success or -1 on failure (-> disconnect client).
308 */
309int
310qdevice_net_algo_test_vote_info_received(struct qdevice_net_instance *instance,
311 uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid, enum tlv_vote *vote)
312{
313
314 qdevice_log(LOG_INFO, "algo-test: Vote info received");
315
316 if (!ring_id_is_valid) {
317 *vote = TLV_VOTE_NO_CHANGE;
318 }
319
320 return (0);
321}
322
323/*
324 * Called when echo reply message was received.
325 * is_expected_seq_number is set to 1 if received seq_number was equal to last sent echo request.
326 *
327 * Callback should return 0 on success or -1 on failure (-> disconnect client).
328 */
329int
330qdevice_net_algo_test_echo_reply_received(struct qdevice_net_instance *instance,
331 uint32_t seq_number, int is_expected_seq_number)
332{
333
334 qdevice_log(LOG_INFO, "algo-test: Echo reply received");
335
336 return (is_expected_seq_number ? 0 : -1);
337}
338
339/*
340 * Called when client is about to send echo request but echo reply to previous echo request
341 * was not yet received.
342 *
343 * Callback should return 0 if processing should continue (echo request is not sent but timer is
344 * scheduled again) otherwise -1 (-> disconnect client).
345 */
346int
347qdevice_net_algo_test_echo_reply_not_received(struct qdevice_net_instance *instance)
348{
349
350 qdevice_log(LOG_INFO, "algo-test: Echo reply not received");
351
352 return (-1);
353}
354
355/*
356 * Called when regular heuristics finished and it's result differs from previous heuristics.
357 *
358 * heuristics contains result of heuristics
359 * send_msg distinquish if message should be sent to qnetd. Default value is 0 if qnetd is not
360 * connected and 1 otherwise
361 * vote can be set to change cast_vote_timer voting value (default is TLV_VOTE_NO_CHANGE)
362 *
363 * It's not possible to set send_msg to 1 and heuristics to TLV_HEURISTICS_UNDEFINED
364 *
365 * Callback should return 0 on success, -1 on failure (-> force exit)
366 */
367int
368qdevice_net_algo_test_heuristics_change(struct qdevice_net_instance *instance,
369 enum tlv_heuristics *heuristics, int *send_msg, enum tlv_vote *vote)
370{
371
372 qdevice_log(LOG_INFO, "algo-test: Heuristics change");
373
374 return (0);
375}
376
377/*
378 * Called when reply for heuristics change was received.
379 * Vote is set to value returned by server (and can be overwriten by algorithm).
380 *
381 * ring_id and ring_id_is_valid have same meaning as for
382 * qdevice_net_algo_test_config_node_list_reply_received
383 *
384 * heuristics is unmodified value sent to server (and set by qdevice_net_algo_test_heuristics_change)
385 *
386 * Callback should return 0 on success or -1 on failure (-> disconnect client).
387 */
388int
389qdevice_net_algo_test_heuristics_change_reply_received(struct qdevice_net_instance *instance,
390 uint32_t seq_number, const struct tlv_ring_id *ring_id, int ring_id_is_valid,
391 enum tlv_heuristics heuristics, enum tlv_vote *vote)
392{
393
394 qdevice_log(LOG_INFO, "algo-test: heuristics change reply received");
395
396 if (!ring_id_is_valid) {
397 *vote = TLV_VOTE_NO_CHANGE;
398 }
399
400 return (0);
401}
402
403/*
404 * Called when client disconnects from server.
405 *
406 * disconnect_reason contains one of QDEVICE_NET_DISCONNECT_REASON_
407 * try_reconnect can be set to non zero value if reconnect to server should be tried
408 * vote (default TLV_VOTE_NO_CHANGE) can be set to update voting timer
409 *
410 * Callback should return 0 on success, -1 on failure (-> force exit)
411 */
412int
413qdevice_net_algo_test_disconnected(struct qdevice_net_instance *instance,
414 enum qdevice_net_disconnect_reason disconnect_reason, int *try_reconnect, enum tlv_vote *vote)
415{
416
417 qdevice_log(LOG_INFO, "algo-test: Disconnected");
418
419 return (0);
420}
421
422/*
423 * Called when qdevice-net is going down.
424 */
425void
426qdevice_net_algo_test_destroy(struct qdevice_net_instance *instance)
427{
428
429 qdevice_log(LOG_INFO, "algo-test: Destroy");
430}
431
432static struct qdevice_net_algorithm qdevice_net_algo_test = {
433 .init = qdevice_net_algo_test_init,
434 .connected = qdevice_net_algo_test_connected,
435 .config_node_list_changed = qdevice_net_algo_test_config_node_list_changed,
436 .votequorum_node_list_notify = qdevice_net_algo_test_votequorum_node_list_notify,
437 .votequorum_node_list_heuristics_notify = qdevice_net_algo_test_votequorum_node_list_heuristics_notify,
438 .votequorum_quorum_notify = qdevice_net_algo_test_votequorum_quorum_notify,
439 .votequorum_expected_votes_notify = qdevice_net_algo_test_votequorum_expected_votes_notify,
440 .config_node_list_reply_received = qdevice_net_algo_test_config_node_list_reply_received,
441 .membership_node_list_reply_received = qdevice_net_algo_test_membership_node_list_reply_received,
442 .quorum_node_list_reply_received = qdevice_net_algo_test_quorum_node_list_reply_received,
443 .ask_for_vote_reply_received = qdevice_net_algo_test_ask_for_vote_reply_received,
444 .vote_info_received = qdevice_net_algo_test_vote_info_received,
445 .echo_reply_received = qdevice_net_algo_test_echo_reply_received,
446 .echo_reply_not_received = qdevice_net_algo_test_echo_reply_not_received,
447 .heuristics_change = qdevice_net_algo_test_heuristics_change,
448 .heuristics_change_reply_received = qdevice_net_algo_test_heuristics_change_reply_received,
449 .disconnected = qdevice_net_algo_test_disconnected,
450 .destroy = qdevice_net_algo_test_destroy,
451};
452
453int
454qdevice_net_algo_test_register(void)
455{
456 return (qdevice_net_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_TEST, &qdevice_net_algo_test));
457}