File: qdevice-net-instance.c

package info (click to toggle)
corosync-qdevice 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,516 kB
  • sloc: ansic: 22,520; sh: 11,882; makefile: 391
file content (369 lines) | stat: -rw-r--r-- 12,198 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (c) 2015-2020 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Jan Friesse (jfriesse@redhat.com)
 *
 * This software licensed under BSD license, the text of which follows:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the Red Hat, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <limits.h>

#include "log.h"
#include "qdevice-config.h"
#include "qdevice-net-instance.h"
#include "qnet-config.h"
#include "utils.h"
#include "qdevice-ipc.h"

int
qdevice_net_instance_init(struct qdevice_net_instance *instance,
    enum tlv_tls_supported tls_supported,
    enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval,
    uint32_t sync_heartbeat_interval, uint32_t cast_vote_timer_interval,
    const char *host_addr, uint16_t host_port, const char *cluster_name,
    const struct tlv_tie_breaker *tie_breaker, uint32_t connect_timeout,
    int force_ip_version, enum tlv_keep_active_partition_tie_breaker keep_active_partition_tie_breaker,
    int cmap_fd, int votequorum_fd, int local_socket_fd,
    const struct qdevice_advanced_settings *advanced_settings,
    int heuristics_pipe_cmd_send_fd, int heuristics_pipe_cmd_recv_fd,
    int heuristics_pipe_log_recv_fd)
{

	memset(instance, 0, sizeof(*instance));

	instance->advanced_settings = advanced_settings;
	instance->decision_algorithm = decision_algorithm;
	instance->heartbeat_interval = heartbeat_interval;
	instance->sync_heartbeat_interval = sync_heartbeat_interval;
	instance->cast_vote_timer_interval = cast_vote_timer_interval;
	instance->cast_vote_timer = NULL;
	instance->host_addr = host_addr;
	instance->host_port = host_port;
	instance->cluster_name = cluster_name;
	instance->connect_timeout = connect_timeout;
	instance->last_msg_seq_num = 1;
	instance->echo_request_expected_msg_seq_num = 1;
	instance->echo_reply_received_msg_seq_num = 1;
	instance->force_ip_version = force_ip_version;
	instance->last_echo_reply_received_time = ((time_t) -1);
	instance->connected_since_time = ((time_t) -1);
	instance->keep_active_partition_tie_breaker = keep_active_partition_tie_breaker;

	memcpy(&instance->tie_breaker, tie_breaker, sizeof(*tie_breaker));

	dynar_init(&instance->receive_buffer, advanced_settings->net_initial_msg_receive_size);

	send_buffer_list_init(&instance->send_buffer_list, advanced_settings->net_max_send_buffers,
	    advanced_settings->net_initial_msg_send_size);

	instance->tls_supported = tls_supported;

	return (0);
}

void
qdevice_net_instance_clean(struct qdevice_net_instance *instance)
{

	dynar_clean(&instance->receive_buffer);

	send_buffer_list_free(&instance->send_buffer_list);

	instance->skipping_msg = 0;
	instance->msg_already_received_bytes = 0;
	instance->echo_request_expected_msg_seq_num = instance->echo_reply_received_msg_seq_num;
	instance->using_tls = 0;
	instance->tls_client_cert_sent = 0;
	instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT;

	instance->schedule_disconnect = 0;
	instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNDEFINED;
	instance->last_echo_reply_received_time = ((time_t) -1);
	instance->connected_since_time = ((time_t) -1);
}

int
qdevice_net_instance_destroy(struct qdevice_net_instance *instance)
{

	dynar_destroy(&instance->receive_buffer);

	send_buffer_list_free(&instance->send_buffer_list);

	free((void *)instance->cluster_name);
	free((void *)instance->host_addr);

	return (0);
}

int
qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
{
	char *str;
	cmap_handle_t cmap_handle;
	enum tlv_tls_supported tls_supported;
	int i;
	long long int lli;
	enum tlv_decision_algorithm_type decision_algorithm;
	struct tlv_tie_breaker tie_breaker;
	uint32_t heartbeat_interval;
	uint32_t sync_heartbeat_interval;
	uint32_t cast_vote_timer_interval;
	char *host_addr;
	int host_port;
	char *cluster_name;
	uint32_t connect_timeout;
	struct qdevice_net_instance *net_instance;
	int force_ip_version;
	enum tlv_keep_active_partition_tie_breaker keep_active_partition_tb;

	cmap_handle = instance->cmap_handle;

	net_instance = malloc(sizeof(*net_instance));
	if (net_instance == NULL) {
		log(LOG_ERR, "Can't alloc qdevice_net_instance");
		return (-1);
	}

	/*
	 * Check tls
	 */
	tls_supported = QDEVICE_NET_DEFAULT_TLS_SUPPORTED;

	if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
		if ((i = utils_parse_bool_str(str)) == -1) {
			if (strcasecmp(str, "required") != 0) {
				free(str);
				log(LOG_ERR, "quorum.device.net.tls value is not valid.");

				goto error_free_instance;
			} else {
				tls_supported = TLV_TLS_REQUIRED;
			}
		} else {
			if (i == 1) {
				tls_supported = TLV_TLS_SUPPORTED;
			} else {
				tls_supported = TLV_TLS_UNSUPPORTED;
			}
		}

		free(str);
	}

	/*
	 * Host
	 */
	if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
		log(LOG_ERR, "Qdevice net daemon address is not defined (quorum.device.net.host)");
		goto error_free_instance;
	}
	host_addr = str;

	if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
		if (utils_strtonum(str, 1, UINT16_MAX, &lli) == -1) {
			log(LOG_ERR, "quorum.device.net.port must be in range 1-%u", UINT16_MAX);
			free(str);
			goto error_free_host_addr;
		}
		free(str);

		host_port = lli;
	} else {
		host_port = QNETD_DEFAULT_HOST_PORT;
	}

	/*
	 * Cluster name
	 */
	if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
		log(LOG_ERR, "Cluster name (totem.cluster_name) has to be defined.");
		goto error_free_host_addr;
	}
	cluster_name = str;

	/*
	 * Adjust qdevice timeouts to better suit qnetd
	 */
	cast_vote_timer_interval = instance->heartbeat_interval * 0.5;
	heartbeat_interval = instance->heartbeat_interval * 0.8;
	if (heartbeat_interval < instance->advanced_settings->net_heartbeat_interval_min) {
		log(LOG_WARNING, "Heartbeat interval too small %"PRIu32". Adjusting to %"PRIu32".",
		    heartbeat_interval, instance->advanced_settings->net_heartbeat_interval_min);
		heartbeat_interval = instance->advanced_settings->net_heartbeat_interval_min;
	}
	if (heartbeat_interval > instance->advanced_settings->net_heartbeat_interval_max) {
		log(LOG_WARNING, "Heartbeat interval too big %"PRIu32". Adjusting to %"PRIu32".",
		    heartbeat_interval, instance->advanced_settings->net_heartbeat_interval_max);
		heartbeat_interval = instance->advanced_settings->net_heartbeat_interval_max;
	}
	sync_heartbeat_interval = instance->sync_heartbeat_interval * 0.8;

	/*
	 * Choose decision algorithm
	 */
	if (cmap_get_string(cmap_handle, "quorum.device.net.algorithm", &str) != CS_OK) {
		decision_algorithm = QDEVICE_NET_DEFAULT_ALGORITHM;
	} else {
		if (strcmp(str, "test") == 0) {
			decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
		} else if (strcmp(str, "ffsplit") == 0) {
			decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_FFSPLIT;
		} else if (strcmp(str, "2nodelms") == 0) {
			decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_2NODELMS;
		} else if (strcmp(str, "lms") == 0) {
			decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_LMS;
		} else {
			log(LOG_ERR, "Unknown decision algorithm %s", str);
			free(str);
			goto error_free_cluster_name;
		}

		free(str);
	}

	if (decision_algorithm == TLV_DECISION_ALGORITHM_TYPE_TEST &&
	    !instance->advanced_settings->net_test_algorithm_enabled) {
		log(LOG_ERR, "Test algorithm is not enabled. You can force enable it by "
		    "passing -S net_test_algorithm_enabled=on to %s command", QDEVICE_PROGRAM_NAME);

		goto error_free_cluster_name;
	}
	/*
	 * Load tie_breaker mode
	 */
	memset(&tie_breaker, 0, sizeof(tie_breaker));

	if (cmap_get_string(cmap_handle, "quorum.device.net.tie_breaker", &str) != CS_OK) {
		tie_breaker.mode = QDEVICE_NET_DEFAULT_TIE_BREAKER_MODE;
	} else {
		if (strcmp(str, "lowest") == 0) {
			tie_breaker.mode = TLV_TIE_BREAKER_MODE_LOWEST;
		} else if (strcmp(str, "highest") == 0) {
			tie_breaker.mode = TLV_TIE_BREAKER_MODE_HIGHEST;
		} else {
			if (utils_strtonum(str, 1, UINT32_MAX, &lli) == -1) {
				log(LOG_ERR, "tie_breaker must be lowest|highest|valid_node_id");
				free(str);
				goto error_free_cluster_name;
			}

			tie_breaker.mode = TLV_TIE_BREAKER_MODE_NODE_ID;
			tie_breaker.node_id = lli;
		}

		free(str);
	}

	/*
	 * Get connect timeout
	 */
	if (cmap_get_string(cmap_handle, "quorum.device.net.connect_timeout", &str) != CS_OK) {
		connect_timeout = heartbeat_interval;
	} else {
		if (utils_strtonum(str, instance->advanced_settings->net_min_connect_timeout,
		    instance->advanced_settings->net_max_connect_timeout, &lli) == -1) {
			log(LOG_ERR, "connect_timeout must be valid number in "
			    "range <%"PRIu32",%"PRIu32">",
			    instance->advanced_settings->net_min_connect_timeout,
			    instance->advanced_settings->net_max_connect_timeout);
			free(str);
			goto error_free_cluster_name;
		}

		connect_timeout = (uint32_t)lli;

		free(str);
	}

	if (cmap_get_string(cmap_handle, "quorum.device.net.force_ip_version", &str) != CS_OK) {
		force_ip_version = 0;
	} else {
		if ((utils_strtonum(str, 0, 6, &lli) == -1) ||
		    (lli != 0 && lli != 4 && lli != 6)) {
			log(LOG_ERR, "force_ip_version must be one of 0|4|6");
			free(str);
			goto error_free_cluster_name;
		}

		force_ip_version = lli;

		free(str);
	}

	keep_active_partition_tb = QDEVICE_NET_DEFAULT_KEEP_ACTIVE_PARTITION_TB;

	if (cmap_get_string(cmap_handle, "quorum.device.net.keep_active_partition_tie_breaker",
	    &str) == CS_OK) {
		if ((i = utils_parse_bool_str(str)) == -1) {
			log(LOG_ERR, "quorum.device.net.keep_active_partition_tie_breaker "
			    "value is not valid.");
			free(str);
			goto error_free_cluster_name;
		} else {
			if (i == 1) {
				keep_active_partition_tb = TLV_KEEP_ACTIVE_PARTITION_TIE_BREAKER_ENABLED;
			} else {
				keep_active_partition_tb = TLV_KEEP_ACTIVE_PARTITION_TIE_BREAKER_DISABLED;
			}
		}

		free(str);
	}

	/*
	 * Really initialize instance
	 */
	if (qdevice_net_instance_init(net_instance,
	    tls_supported, decision_algorithm,
	    heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
	    host_addr, host_port, cluster_name, &tie_breaker, connect_timeout,
	    force_ip_version, keep_active_partition_tb,
	    instance->cmap_poll_fd, instance->votequorum_poll_fd,
	    instance->local_ipc.socket, instance->advanced_settings,
	    instance->heuristics_instance.pipe_cmd_send,
	    instance->heuristics_instance.pipe_cmd_recv,
	    instance->heuristics_instance.pipe_log_recv) == -1) {
		log(LOG_ERR, "Can't initialize qdevice-net instance");
		goto error_free_instance;
	}

	net_instance->qdevice_instance_ptr = instance;
	instance->model_data = net_instance;

	return (0);

error_free_cluster_name:
	free(cluster_name);
error_free_host_addr:
	free(host_addr);
error_free_instance:
	free(net_instance);
	return (-1);
}