]> git.proxmox.com Git - mirror_corosync.git/blob - exec/totemconfig.c
udpu: Drop packets from unlisted IPs
[mirror_corosync.git] / exec / totemconfig.c
1 /*
2 * Copyright (c) 2002-2005 MontaVista Software, Inc.
3 * Copyright (c) 2006-2018 Red Hat, Inc.
4 *
5 * All rights reserved.
6 *
7 * Author: Steven Dake (sdake@redhat.com)
8 * Jan Friesse (jfriesse@redhat.com)
9 *
10 * This software licensed under BSD license, the text of which follows:
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * - Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * - Neither the name of the MontaVista Software, Inc. nor the names of its
21 * contributors may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34 * THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <config.h>
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <ifaddrs.h>
49 #include <netdb.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <sys/param.h>
53 #include <sys/utsname.h>
54
55 #include <corosync/swab.h>
56 #include <qb/qblist.h>
57 #include <qb/qbdefs.h>
58 #include <libknet.h>
59 #include <corosync/totem/totem.h>
60 #include <corosync/config.h>
61 #include <corosync/logsys.h>
62 #include <corosync/icmap.h>
63
64 #include "util.h"
65 #include "totemconfig.h"
66
67 #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
68 #define TOKEN_TIMEOUT 1000
69 #define TOKEN_WARNING 75
70 #define TOKEN_COEFFICIENT 650
71 #define JOIN_TIMEOUT 50
72 #define MERGE_TIMEOUT 200
73 #define DOWNCHECK_TIMEOUT 1000
74 #define FAIL_TO_RECV_CONST 2500
75 #define SEQNO_UNCHANGED_CONST 30
76 #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
77 #define MINIMUM_TIMEOUT_HOLD (int)(MINIMUM_TIMEOUT * 0.8 - (1000/HZ))
78 #define MAX_NETWORK_DELAY 50
79 #define WINDOW_SIZE 50
80 #define MAX_MESSAGES 17
81 #define MISS_COUNT_CONST 5
82 #define BLOCK_UNLISTED_IPS 1
83
84 /* These currently match the defaults in libknet.h */
85 #define KNET_PING_INTERVAL 1000
86 #define KNET_PING_TIMEOUT 2000
87 #define KNET_PING_PRECISION 2048
88 #define KNET_PONG_COUNT 2
89 #define KNET_PMTUD_INTERVAL 30
90 #define KNET_DEFAULT_TRANSPORT KNET_TRANSPORT_UDP
91
92 #define DEFAULT_PORT 5405
93
94 static char error_string_response[768];
95
96 static void add_totem_config_notification(struct totem_config *totem_config);
97
98 static void *totem_get_param_by_name(struct totem_config *totem_config, const char *param_name)
99 {
100 if (strcmp(param_name, "totem.token") == 0)
101 return &totem_config->token_timeout;
102 if (strcmp(param_name, "totem.token_warning") == 0)
103 return &totem_config->token_warning;
104 if (strcmp(param_name, "totem.token_retransmit") == 0)
105 return &totem_config->token_retransmit_timeout;
106 if (strcmp(param_name, "totem.hold") == 0)
107 return &totem_config->token_hold_timeout;
108 if (strcmp(param_name, "totem.token_retransmits_before_loss_const") == 0)
109 return &totem_config->token_retransmits_before_loss_const;
110 if (strcmp(param_name, "totem.join") == 0)
111 return &totem_config->join_timeout;
112 if (strcmp(param_name, "totem.send_join") == 0)
113 return &totem_config->send_join_timeout;
114 if (strcmp(param_name, "totem.consensus") == 0)
115 return &totem_config->consensus_timeout;
116 if (strcmp(param_name, "totem.merge") == 0)
117 return &totem_config->merge_timeout;
118 if (strcmp(param_name, "totem.downcheck") == 0)
119 return &totem_config->downcheck_timeout;
120 if (strcmp(param_name, "totem.fail_recv_const") == 0)
121 return &totem_config->fail_to_recv_const;
122 if (strcmp(param_name, "totem.seqno_unchanged_const") == 0)
123 return &totem_config->seqno_unchanged_const;
124 if (strcmp(param_name, "totem.heartbeat_failures_allowed") == 0)
125 return &totem_config->heartbeat_failures_allowed;
126 if (strcmp(param_name, "totem.max_network_delay") == 0)
127 return &totem_config->max_network_delay;
128 if (strcmp(param_name, "totem.window_size") == 0)
129 return &totem_config->window_size;
130 if (strcmp(param_name, "totem.max_messages") == 0)
131 return &totem_config->max_messages;
132 if (strcmp(param_name, "totem.miss_count_const") == 0)
133 return &totem_config->miss_count_const;
134 if (strcmp(param_name, "totem.knet_pmtud_interval") == 0)
135 return &totem_config->knet_pmtud_interval;
136 if (strcmp(param_name, "totem.knet_compression_threshold") == 0)
137 return &totem_config->knet_compression_threshold;
138 if (strcmp(param_name, "totem.knet_compression_level") == 0)
139 return &totem_config->knet_compression_level;
140 if (strcmp(param_name, "totem.knet_compression_model") == 0)
141 return &totem_config->knet_compression_model;
142 if (strcmp(param_name, "totem.block_unlisted_ips") == 0)
143 return &totem_config->block_unlisted_ips;
144
145 return NULL;
146 }
147
148 /*
149 * Read key_name from icmap. If key is not found or key_name == delete_key or if allow_zero is false
150 * and readed value is zero, default value is used and stored into totem_config.
151 */
152 static void totem_volatile_config_set_uint32_value (struct totem_config *totem_config,
153 const char *key_name, const char *deleted_key, unsigned int default_value,
154 int allow_zero_value)
155 {
156 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
157
158 if (icmap_get_uint32(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
159 (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
160 (!allow_zero_value && *(uint32_t *)totem_get_param_by_name(totem_config, key_name) == 0)) {
161 *(uint32_t *)totem_get_param_by_name(totem_config, key_name) = default_value;
162 }
163
164 /*
165 * Store totem_config value to cmap runtime section
166 */
167 if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
168 /*
169 * This shouldn't happen
170 */
171 return ;
172 }
173
174 strcpy(runtime_key_name, "runtime.config.");
175 strcat(runtime_key_name, key_name);
176
177 icmap_set_uint32(runtime_key_name, *(uint32_t *)totem_get_param_by_name(totem_config, key_name));
178 }
179
180 static void totem_volatile_config_set_int32_value (struct totem_config *totem_config,
181 const char *key_name, const char *deleted_key, int default_value,
182 int allow_zero_value)
183 {
184 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
185
186 if (icmap_get_int32(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
187 (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
188 (!allow_zero_value && *(int32_t *)totem_get_param_by_name(totem_config, key_name) == 0)) {
189 *(int32_t *)totem_get_param_by_name(totem_config, key_name) = default_value;
190 }
191
192 /*
193 * Store totem_config value to cmap runtime section
194 */
195 if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
196 /*
197 * This shouldn't happen
198 */
199 return ;
200 }
201
202 strcpy(runtime_key_name, "runtime.config.");
203 strcat(runtime_key_name, key_name);
204
205 icmap_set_int32(runtime_key_name, *(int32_t *)totem_get_param_by_name(totem_config, key_name));
206 }
207
208 static void totem_volatile_config_set_string_value (struct totem_config *totem_config,
209 const char *key_name, const char *deleted_key, const char *default_value)
210 {
211 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
212 void **config_value;
213 void *old_config_ptr;
214
215 config_value = totem_get_param_by_name(totem_config, key_name);
216 old_config_ptr = *config_value;
217 if (icmap_get_string(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
218 (deleted_key != NULL && strcmp(deleted_key, key_name) == 0)) {
219
220 /* Need to strdup() here so that the free() below works for a default and a configured value */
221 *config_value = strdup(default_value);
222 }
223 free(old_config_ptr);
224
225 /*
226 * Store totem_config value to cmap runtime section
227 */
228 if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
229 /*
230 * This shouldn't happen
231 */
232 return ;
233 }
234
235 strcpy(runtime_key_name, "runtime.config.");
236 strcat(runtime_key_name, key_name);
237
238 icmap_set_string(runtime_key_name, (char *)*config_value);
239 }
240
241 /*
242 * Read string value stored in key_name from icmap, use it as a boolean (yes/no) type, convert it
243 * to integer value (1/0) and store into totem_config.
244 *
245 * If key is not found or key_name == delete_key default value is used
246 * and stored into totem_config.
247 */
248 static void totem_volatile_config_set_boolean_value (struct totem_config *totem_config,
249 const char *key_name, const char *deleted_key, unsigned int default_value)
250 {
251 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
252 char *str;
253 int val;
254
255 str = NULL;
256 val = default_value;
257
258 if ((deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
259 (icmap_get_string(key_name, &str) != CS_OK)) {
260 /*
261 * Do nothing. str is NULL (icmap_get_string ether not called or
262 * not changed str).
263 */
264 } else {
265 if (strcmp(str, "yes") == 0) {
266 val = 1;
267 } else if (strcmp(str, "no") == 0) {
268 val = 0;
269 }
270 free(str);
271 }
272
273 /*
274 * Store totem_config value to cmap runtime section
275 */
276 if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
277 /*
278 * This shouldn't happen
279 */
280 return ;
281 }
282
283 strcpy(runtime_key_name, "runtime.config.");
284 strcat(runtime_key_name, key_name);
285
286 *(uint32_t *)totem_get_param_by_name(totem_config, key_name) = val;
287
288 icmap_set_uint32(runtime_key_name, val);
289 }
290
291 /*
292 * Read and validate config values from cmap and store them into totem_config. If key doesn't exists,
293 * default value is stored. deleted_key is name of key beeing processed by delete operation
294 * from cmap. It is considered as non existing even if it can be read. Can be NULL.
295 */
296 static void totem_volatile_config_read (struct totem_config *totem_config, const char *deleted_key)
297 {
298 uint32_t u32;
299
300 totem_volatile_config_set_uint32_value(totem_config, "totem.token_retransmits_before_loss_const", deleted_key,
301 TOKEN_RETRANSMITS_BEFORE_LOSS_CONST, 0);
302
303 totem_volatile_config_set_uint32_value(totem_config, "totem.token", deleted_key, TOKEN_TIMEOUT, 0);
304
305 totem_volatile_config_set_uint32_value(totem_config, "totem.token_warning", deleted_key, TOKEN_WARNING, 1);
306
307 if (totem_config->interfaces[0].member_count > 2) {
308 u32 = TOKEN_COEFFICIENT;
309 icmap_get_uint32("totem.token_coefficient", &u32);
310 totem_config->token_timeout += (totem_config->interfaces[0].member_count - 2) * u32;
311
312 /*
313 * Store totem_config value to cmap runtime section
314 */
315 icmap_set_uint32("runtime.config.totem.token", totem_config->token_timeout);
316 }
317
318 totem_volatile_config_set_uint32_value(totem_config, "totem.max_network_delay", deleted_key, MAX_NETWORK_DELAY, 0);
319
320 totem_volatile_config_set_uint32_value(totem_config, "totem.window_size", deleted_key, WINDOW_SIZE, 0);
321
322 totem_volatile_config_set_uint32_value(totem_config, "totem.max_messages", deleted_key, MAX_MESSAGES, 0);
323
324 totem_volatile_config_set_uint32_value(totem_config, "totem.miss_count_const", deleted_key, MISS_COUNT_CONST, 0);
325 totem_volatile_config_set_uint32_value(totem_config, "totem.knet_pmtud_interval", deleted_key, KNET_PMTUD_INTERVAL, 0);
326
327 totem_volatile_config_set_uint32_value(totem_config, "totem.token_retransmit", deleted_key,
328 (int)(totem_config->token_timeout / (totem_config->token_retransmits_before_loss_const + 0.2)), 0);
329
330 totem_volatile_config_set_uint32_value(totem_config, "totem.hold", deleted_key,
331 (int)(totem_config->token_retransmit_timeout * 0.8 - (1000/HZ)), 0);
332
333 totem_volatile_config_set_uint32_value(totem_config, "totem.join", deleted_key, JOIN_TIMEOUT, 0);
334
335 totem_volatile_config_set_uint32_value(totem_config, "totem.consensus", deleted_key,
336 (int)(float)(1.2 * totem_config->token_timeout), 0);
337
338 totem_volatile_config_set_uint32_value(totem_config, "totem.merge", deleted_key, MERGE_TIMEOUT, 0);
339
340 totem_volatile_config_set_uint32_value(totem_config, "totem.downcheck", deleted_key, DOWNCHECK_TIMEOUT, 0);
341
342 totem_volatile_config_set_uint32_value(totem_config, "totem.fail_recv_const", deleted_key, FAIL_TO_RECV_CONST, 0);
343
344 totem_volatile_config_set_uint32_value(totem_config, "totem.seqno_unchanged_const", deleted_key,
345 SEQNO_UNCHANGED_CONST, 0);
346
347 totem_volatile_config_set_uint32_value(totem_config, "totem.send_join", deleted_key, 0, 1);
348
349 totem_volatile_config_set_uint32_value(totem_config, "totem.heartbeat_failures_allowed", deleted_key, 0, 1);
350
351 totem_volatile_config_set_uint32_value(totem_config, "totem.knet_compression_threshold", deleted_key, 0, 1);
352
353 totem_volatile_config_set_int32_value(totem_config, "totem.knet_compression_level", deleted_key, 0, 1);
354
355 totem_volatile_config_set_string_value(totem_config, "totem.knet_compression_model", deleted_key, "none");
356
357 totem_volatile_config_set_boolean_value(totem_config, "totem.block_unlisted_ips", deleted_key,
358 BLOCK_UNLISTED_IPS);
359 }
360
361 static int totem_volatile_config_validate (
362 struct totem_config *totem_config,
363 const char **error_string)
364 {
365 static char local_error_reason[512];
366 const char *error_reason = local_error_reason;
367 char name_key[ICMAP_KEYNAME_MAXLEN];
368 char *name_str;
369 int i, num_configured, members;
370 uint32_t tmp_config_value;
371
372 if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
373 snprintf (local_error_reason, sizeof(local_error_reason),
374 "The max_network_delay parameter (%d ms) may not be less than (%d ms).",
375 totem_config->max_network_delay, MINIMUM_TIMEOUT);
376 goto parse_error;
377 }
378
379 if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
380 snprintf (local_error_reason, sizeof(local_error_reason),
381 "The token timeout parameter (%d ms) may not be less than (%d ms).",
382 totem_config->token_timeout, MINIMUM_TIMEOUT);
383 goto parse_error;
384 }
385
386 if (totem_config->token_warning > 100 || totem_config->token_warning < 0) {
387 snprintf (local_error_reason, sizeof(local_error_reason),
388 "The token warning parameter (%d%%) must be between 0 (disabled) and 100.",
389 totem_config->token_warning);
390 goto parse_error;
391 }
392
393 if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
394 if (icmap_get_uint32("totem.token_retransmit", &tmp_config_value) == CS_OK) {
395 snprintf (local_error_reason, sizeof(local_error_reason),
396 "The token retransmit timeout parameter (%d ms) may not be less than (%d ms).",
397 totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
398 goto parse_error;
399 } else {
400 snprintf (local_error_reason, sizeof(local_error_reason),
401 "Not appropriate token or token_retransmits_before_loss_const value set");
402 goto parse_error;
403 }
404 }
405
406 if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT_HOLD) {
407 snprintf (local_error_reason, sizeof(local_error_reason),
408 "The token hold timeout parameter (%d ms) may not be less than (%d ms).",
409 totem_config->token_hold_timeout, MINIMUM_TIMEOUT_HOLD);
410 goto parse_error;
411 }
412
413 if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
414 snprintf (local_error_reason, sizeof(local_error_reason),
415 "The join timeout parameter (%d ms) may not be less than (%d ms).",
416 totem_config->join_timeout, MINIMUM_TIMEOUT);
417 goto parse_error;
418 }
419
420 if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
421 snprintf (local_error_reason, sizeof(local_error_reason),
422 "The consensus timeout parameter (%d ms) may not be less than (%d ms).",
423 totem_config->consensus_timeout, MINIMUM_TIMEOUT);
424 goto parse_error;
425 }
426
427 if (totem_config->consensus_timeout < totem_config->join_timeout) {
428 snprintf (local_error_reason, sizeof(local_error_reason),
429 "The consensus timeout parameter (%d ms) may not be less than join timeout (%d ms).",
430 totem_config->consensus_timeout, totem_config->join_timeout);
431 goto parse_error;
432 }
433
434 if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
435 snprintf (local_error_reason, sizeof(local_error_reason),
436 "The merge timeout parameter (%d ms) may not be less than (%d ms).",
437 totem_config->merge_timeout, MINIMUM_TIMEOUT);
438 goto parse_error;
439 }
440
441 if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
442 snprintf (local_error_reason, sizeof(local_error_reason),
443 "The downcheck timeout parameter (%d ms) may not be less than (%d ms).",
444 totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
445 goto parse_error;
446 }
447
448 /* Check that we have nodelist 'name' if there is more than one link */
449 num_configured = 0;
450 for (i = 0; i < INTERFACE_MAX; i++) {
451 if (totem_config->interfaces[i].configured) {
452 num_configured++;
453 }
454 }
455
456 if (num_configured > 1) {
457 members = totem_config->interfaces[0].member_count;
458
459 for (i=0; i<totem_config->interfaces[0].member_count; i++) {
460 snprintf(name_key, sizeof(name_key), "nodelist.node.%d.name", i);
461
462 if (icmap_get_string(name_key, &name_str) != CS_OK) {
463 snprintf (local_error_reason, sizeof(local_error_reason),
464 "for a multi-link configuration, all nodes must have a 'name' attribute");
465 goto parse_error;
466 }
467 }
468
469 for (i=0; i<num_configured; i++) {
470 if (totem_config->interfaces[i].member_count != members) {
471 snprintf (local_error_reason, sizeof(local_error_reason),
472 "Not all nodes have the same number of links");
473 goto parse_error;
474 }
475 }
476
477
478
479 }
480
481 return 0;
482
483 parse_error:
484 snprintf (error_string_response, sizeof(error_string_response),
485 "parse error in config: %s\n", error_reason);
486 *error_string = error_string_response;
487 return (-1);
488
489 }
490
491 static int totem_get_crypto(struct totem_config *totem_config, const char **error_string)
492 {
493 char *str;
494 const char *tmp_cipher;
495 const char *tmp_hash;
496 const char *tmp_model;
497
498 tmp_hash = "none";
499 tmp_cipher = "none";
500 tmp_model = "none";
501
502 if (icmap_get_string("totem.crypto_model", &str) == CS_OK) {
503 if (strcmp(str, "nss") == 0) {
504 tmp_model = "nss";
505 }
506 if (strcmp(str, "openssl") == 0) {
507 tmp_model = "openssl";
508 }
509 free(str);
510 } else {
511 tmp_model = "nss";
512 }
513
514 if (icmap_get_string("totem.secauth", &str) == CS_OK) {
515 if (strcmp(str, "on") == 0) {
516 tmp_cipher = "aes256";
517 tmp_hash = "sha256";
518 }
519 free(str);
520 }
521
522 if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
523 if (strcmp(str, "none") == 0) {
524 tmp_cipher = "none";
525 }
526 if (strcmp(str, "aes256") == 0) {
527 tmp_cipher = "aes256";
528 }
529 if (strcmp(str, "aes192") == 0) {
530 tmp_cipher = "aes192";
531 }
532 if (strcmp(str, "aes128") == 0) {
533 tmp_cipher = "aes128";
534 }
535 free(str);
536 }
537
538 if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
539 if (strcmp(str, "none") == 0) {
540 tmp_hash = "none";
541 }
542 if (strcmp(str, "md5") == 0) {
543 tmp_hash = "md5";
544 }
545 if (strcmp(str, "sha1") == 0) {
546 tmp_hash = "sha1";
547 }
548 if (strcmp(str, "sha256") == 0) {
549 tmp_hash = "sha256";
550 }
551 if (strcmp(str, "sha384") == 0) {
552 tmp_hash = "sha384";
553 }
554 if (strcmp(str, "sha512") == 0) {
555 tmp_hash = "sha512";
556 }
557 free(str);
558 }
559
560 if ((strcmp(tmp_cipher, "none") != 0) &&
561 (strcmp(tmp_hash, "none") == 0)) {
562 *error_string = "crypto_cipher requires crypto_hash with value other than none";
563 return -1;
564 }
565
566 if (strcmp(tmp_model, "none") == 0) {
567 *error_string = "crypto_model should be 'nss' or 'openssl'";
568 return -1;
569 }
570
571 free(totem_config->crypto_cipher_type);
572 free(totem_config->crypto_hash_type);
573 free(totem_config->crypto_model);
574
575 totem_config->crypto_cipher_type = strdup(tmp_cipher);
576 totem_config->crypto_hash_type = strdup(tmp_hash);
577 totem_config->crypto_model = strdup(tmp_model);
578
579 return 0;
580 }
581
582 static int nodelist_byname(const char *find_name, int strip_domain)
583 {
584 icmap_iter_t iter;
585 const char *iter_key;
586 char name_str[ICMAP_KEYNAME_MAXLEN];
587 int res = 0;
588 unsigned int node_pos;
589 char *name;
590 unsigned int namelen;
591
592 iter = icmap_iter_init("nodelist.node.");
593 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
594 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, name_str);
595 if (res != 2) {
596 continue;
597 }
598 /* ring0_addr is allowed as a fallback */
599 if (strcmp(name_str, "name") && strcmp(name_str, "ring0_addr")) {
600 continue;
601 }
602 if (icmap_get_string(iter_key, &name) != CS_OK) {
603 continue;
604 }
605 namelen = strlen(name);
606
607 if (strip_domain) {
608 char *dot;
609 dot = strchr(name, '.');
610 if (dot) {
611 namelen = name - dot - 1;
612 }
613 }
614 if (strncmp(find_name, name, namelen) == 0 &&
615 strlen(find_name) == strlen(name)) {
616 icmap_iter_finalize(iter);
617 return node_pos;
618 }
619 }
620 icmap_iter_finalize(iter);
621 return -1;
622 }
623
624 /* Compare two addresses - only address part (sin_addr/sin6_addr) is checked */
625 static int ipaddr_equal(const struct sockaddr *addr1, const struct sockaddr *addr2)
626 {
627 int addrlen = 0;
628 const void *addr1p, *addr2p;
629
630 if (addr1->sa_family != addr2->sa_family)
631 return 0;
632
633 if (addr1->sa_family == AF_INET) {
634 addrlen = sizeof(struct in_addr);
635 addr1p = &((struct sockaddr_in *)addr1)->sin_addr;
636 addr2p = &((struct sockaddr_in *)addr2)->sin_addr;
637 }
638 if (addr1->sa_family == AF_INET6) {
639 addrlen = sizeof(struct in6_addr);
640 addr1p = &((struct sockaddr_in6 *)addr1)->sin6_addr;
641 addr2p = &((struct sockaddr_in6 *)addr2)->sin6_addr;
642 }
643 assert(addrlen);
644
645 return (memcmp(addr1p, addr2p, addrlen) == 0);
646 }
647
648
649 /* Finds the local node and returns its position in the nodelist.
650 * Uses nodelist.local_node_pos as a cache to save effort
651 */
652 static int find_local_node(int use_cache)
653 {
654 char nodename2[PATH_MAX];
655 char name_str[ICMAP_KEYNAME_MAXLEN];
656 icmap_iter_t iter;
657 const char *iter_key;
658 unsigned int cached_pos;
659 char *dot = NULL;
660 const char *node;
661 struct ifaddrs *ifa, *ifa_list;
662 struct sockaddr *sa;
663 int found = 0;
664 int node_pos = -1;
665 int res;
666 struct utsname utsname;
667
668 /* Check for cached value first */
669 if (use_cache) {
670 if (icmap_get_uint32("nodelist.local_node_pos", &cached_pos) == CS_OK) {
671 return cached_pos;
672 }
673 }
674
675 res = uname(&utsname);
676 if (res) {
677 return -1;
678 }
679 node = utsname.nodename;
680
681 /* 1. Exact match */
682 node_pos = nodelist_byname(node, 0);
683 if (node_pos > -1) {
684 found = 1;
685 goto ret_found;
686 }
687
688 /* 2. Try to match with increasingly more
689 * specific versions of it
690 */
691 strcpy(nodename2, node);
692 dot = strrchr(nodename2, '.');
693 while (dot) {
694 *dot = '\0';
695
696 node_pos = nodelist_byname(nodename2, 0);
697 if (node_pos > -1) {
698 found = 1;
699 goto ret_found;
700 }
701 dot = strrchr(nodename2, '.');
702 }
703
704 node_pos = nodelist_byname(nodename2, 1);
705 if (node_pos > -1) {
706 found = 1;
707 goto ret_found;
708 }
709
710 /*
711 * The corosync.conf name may not be related to uname at all,
712 * they may match a hostname on some network interface.
713 */
714 if (getifaddrs(&ifa_list))
715 return -1;
716
717 for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
718 socklen_t salen = 0;
719
720 /* Restore this */
721 strcpy(nodename2, node);
722 sa = ifa->ifa_addr;
723 if (!sa) {
724 continue;
725 }
726 if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) {
727 continue;
728 }
729
730 if (sa->sa_family == AF_INET) {
731 salen = sizeof(struct sockaddr_in);
732 }
733 if (sa->sa_family == AF_INET6) {
734 salen = sizeof(struct sockaddr_in6);
735 }
736
737 if (getnameinfo(sa, salen,
738 nodename2, sizeof(nodename2),
739 NULL, 0, 0) == 0) {
740
741 node_pos = nodelist_byname(nodename2, 0);
742 if (node_pos > -1) {
743 found = 1;
744 goto out;
745 }
746
747 /* Truncate this name and try again */
748 dot = strchr(nodename2, '.');
749 if (dot) {
750 *dot = '\0';
751
752 node_pos = nodelist_byname(nodename2, 0);
753 if (node_pos > -1) {
754 found = 1;
755 goto out;
756 }
757 }
758 }
759
760 /* See if it's the IP address that's in corosync.conf */
761 if (getnameinfo(sa, sizeof(*sa),
762 nodename2, sizeof(nodename2),
763 NULL, 0, NI_NUMERICHOST))
764 continue;
765
766 node_pos = nodelist_byname(nodename2, 0);
767 if (node_pos > -1) {
768 found = 1;
769 goto out;
770 }
771 }
772
773 out:
774 if (found) {
775 freeifaddrs(ifa_list);
776 goto ret_found;
777 }
778
779 /*
780 * This section covers the usecase where the nodename specified in cluster.conf
781 * is an alias specified in /etc/hosts. For example:
782 * <ipaddr> hostname alias1 alias2
783 * and <clusternode name="alias2">
784 * the above calls use uname and getnameinfo does not return aliases.
785 * here we take the name specified in cluster.conf, resolve it to an address
786 * and then compare against all known local ip addresses.
787 * if we have a match, we found our nodename. In theory this chunk of code
788 * could replace all the checks above, but let's avoid any possible regressions
789 * and use it as last.
790 */
791
792 iter = icmap_iter_init("nodelist.node.");
793 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
794 char *dbnodename = NULL;
795 struct addrinfo hints;
796 struct addrinfo *result = NULL, *rp = NULL;
797
798 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, name_str);
799 if (res != 2) {
800 continue;
801 }
802 /* 'ring0_addr' is allowed as a fallback, but 'name' will be found first
803 * because the names are in alpha order.
804 */
805 if (strcmp(name_str, "name") && strcmp(name_str, "ring0_addr")) {
806 continue;
807 }
808 if (icmap_get_string(iter_key, &dbnodename) != CS_OK) {
809 continue;
810 }
811
812 memset(&hints, 0, sizeof(struct addrinfo));
813 hints.ai_family = AF_UNSPEC;
814 hints.ai_socktype = SOCK_DGRAM;
815 hints.ai_flags = 0;
816 hints.ai_protocol = IPPROTO_UDP;
817
818 if (getaddrinfo(dbnodename, NULL, &hints, &result)) {
819 continue;
820 }
821
822 for (rp = result; rp != NULL; rp = rp->ai_next) {
823 for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
824 if (ifa->ifa_addr &&
825 ipaddr_equal(rp->ai_addr, ifa->ifa_addr)) {
826 freeaddrinfo(result);
827 found = 1;
828 goto out2;
829 }
830 }
831 }
832
833 freeaddrinfo(result);
834 }
835 out2:
836 icmap_iter_finalize(iter);
837 freeifaddrs(ifa_list);
838
839 ret_found:
840 if (found) {
841 res = icmap_set_uint32("nodelist.local_node_pos", node_pos);
842 }
843
844 return node_pos;
845 }
846
847 static enum totem_ip_version_enum totem_config_get_ip_version(struct totem_config *totem_config)
848 {
849 enum totem_ip_version_enum res;
850 char *str;
851
852 res = TOTEM_IP_VERSION_6_4;
853
854 if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
855 res = TOTEM_IP_VERSION_4;
856 }
857
858 if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
859 if (strcmp(str, "ipv4") == 0) {
860 res = TOTEM_IP_VERSION_4;
861 }
862 if (strcmp(str, "ipv6") == 0) {
863 res = TOTEM_IP_VERSION_6;
864 }
865 if (strcmp(str, "ipv6-4") == 0) {
866 res = TOTEM_IP_VERSION_6_4;
867 }
868 if (strcmp(str, "ipv4-6") == 0) {
869 res = TOTEM_IP_VERSION_4_6;
870 }
871 free(str);
872 }
873
874 return (res);
875 }
876
877 static uint16_t generate_cluster_id (const char *cluster_name)
878 {
879 int i;
880 int value = 0;
881
882 for (i = 0; i < strlen(cluster_name); i++) {
883 value <<= 1;
884 value += cluster_name[i];
885 }
886
887 return (value & 0xFFFF);
888 }
889
890 static int get_cluster_mcast_addr (
891 const char *cluster_name,
892 unsigned int linknumber,
893 enum totem_ip_version_enum ip_version,
894 struct totem_ip_address *res)
895 {
896 uint16_t clusterid;
897 char addr[INET6_ADDRSTRLEN + 1];
898 int err;
899
900 if (cluster_name == NULL) {
901 return (-1);
902 }
903
904 clusterid = generate_cluster_id(cluster_name) + linknumber;
905 memset (res, 0, sizeof(*res));
906
907 switch (ip_version) {
908 case TOTEM_IP_VERSION_4:
909 case TOTEM_IP_VERSION_4_6:
910 snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
911 break;
912 case TOTEM_IP_VERSION_6:
913 case TOTEM_IP_VERSION_6_4:
914 snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
915 break;
916 default:
917 /*
918 * Unknown family
919 */
920 return (-1);
921 }
922
923 err = totemip_parse (res, addr, ip_version);
924
925 return (err);
926 }
927
928 static unsigned int generate_nodeid(
929 struct totem_config *totem_config,
930 char *addr)
931 {
932 unsigned int nodeid;
933 struct totem_ip_address totemip;
934
935 /* AF_INET hard-coded here because auto-generated nodeids
936 are only for IPv4 */
937 if (totemip_parse(&totemip, addr, TOTEM_IP_VERSION_4) != 0)
938 return -1;
939
940 memcpy (&nodeid, &totemip.addr, sizeof (unsigned int));
941
942 #if __BYTE_ORDER == __LITTLE_ENDIAN
943 nodeid = swab32 (nodeid);
944 #endif
945
946 if (totem_config->clear_node_high_bit) {
947 nodeid &= 0x7FFFFFFF;
948 }
949 return nodeid;
950 }
951
952 static int check_for_duplicate_nodeids(
953 struct totem_config *totem_config,
954 const char **error_string)
955 {
956 icmap_iter_t iter;
957 icmap_iter_t subiter;
958 const char *iter_key;
959 int res = 0;
960 int retval = 0;
961 char tmp_key[ICMAP_KEYNAME_MAXLEN];
962 char *ring0_addr=NULL;
963 char *ring0_addr1=NULL;
964 unsigned int node_pos;
965 unsigned int node_pos1;
966 unsigned int last_node_pos = -1;
967 unsigned int nodeid;
968 unsigned int nodeid1;
969 int autogenerated;
970
971 iter = icmap_iter_init("nodelist.node.");
972 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
973 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
974 if (res != 2) {
975 continue;
976 }
977
978 /*
979 * This relies on the fact the icmap keys are always returned in order
980 * so all of the keys for a node will be grouped together. We're basically
981 * just running the code below once for each node.
982 */
983 if (last_node_pos == node_pos) {
984 continue;
985 }
986 last_node_pos = node_pos;
987
988 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
989 autogenerated = 0;
990
991 /* Generated nodeids are only allowed for UDP/UDPU so ring0_addr is valid here */
992 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
993
994 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
995 if (icmap_get_string(tmp_key, &ring0_addr) != CS_OK) {
996 continue;
997 }
998
999 /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
1000 nodeid = generate_nodeid(totem_config, ring0_addr);
1001 if (nodeid == -1) {
1002 continue;
1003 }
1004 autogenerated = 1;
1005 }
1006
1007 node_pos1 = 0;
1008 subiter = icmap_iter_init("nodelist.node.");
1009 while (((iter_key = icmap_iter_next(subiter, NULL, NULL)) != NULL) && (node_pos1 < node_pos)) {
1010 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos1, tmp_key);
1011 if ((res != 2) || (node_pos1 >= node_pos)) {
1012 continue;
1013 }
1014
1015 if (strcmp(tmp_key, "nodeid") != 0) {
1016 continue;
1017 }
1018
1019 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos1);
1020 if (icmap_get_uint32(tmp_key, &nodeid1) != CS_OK) {
1021
1022 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos1);
1023 if (icmap_get_string(tmp_key, &ring0_addr1) != CS_OK) {
1024 continue;
1025 }
1026 nodeid1 = generate_nodeid(totem_config, ring0_addr1);
1027 if (nodeid1 == -1) {
1028 continue;
1029 }
1030 }
1031
1032 if (nodeid == nodeid1) {
1033 retval = -1;
1034 snprintf (error_string_response, sizeof(error_string_response),
1035 "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid,
1036 autogenerated?"(autogenerated from ":"",
1037 autogenerated?ring0_addr:"",
1038 autogenerated?")":"");
1039 log_printf (LOGSYS_LEVEL_ERROR, error_string_response);
1040 *error_string = error_string_response;
1041 break;
1042 }
1043 }
1044 icmap_iter_finalize(subiter);
1045 }
1046 icmap_iter_finalize(iter);
1047 return retval;
1048 }
1049
1050
1051 /*
1052 * This needs to be done last of all. It would be nice to do it when reading the
1053 * interface params, but the totem params need to have them to be read first. We
1054 * need both, so this is a way round that circular dependancy.
1055 */
1056 static void calc_knet_ping_timers(struct totem_config *totem_config)
1057 {
1058 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
1059 int interface;
1060
1061 for (interface = 0; interface < INTERFACE_MAX; interface++) {
1062
1063 if (totem_config->interfaces[interface].configured) {
1064 if (!totem_config->interfaces[interface].knet_pong_count) {
1065 totem_config->interfaces[interface].knet_pong_count = KNET_PONG_COUNT;
1066 }
1067 if (!totem_config->interfaces[interface].knet_ping_timeout) {
1068 totem_config->interfaces[interface].knet_ping_timeout =
1069 totem_config->token_timeout / totem_config->interfaces[interface].knet_pong_count;
1070 }
1071 snprintf(runtime_key_name, sizeof(runtime_key_name),
1072 "runtime.config.totem.interface.%d.knet_ping_timeout", interface);
1073 icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_timeout);
1074
1075 if (!totem_config->interfaces[interface].knet_ping_interval) {
1076 totem_config->interfaces[interface].knet_ping_interval =
1077 totem_config->token_timeout / (totem_config->interfaces[interface].knet_pong_count * 2);
1078 }
1079 snprintf(runtime_key_name, sizeof(runtime_key_name),
1080 "runtime.config.totem.interface.%d.knet_ping_interval", interface);
1081 icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_interval);
1082 }
1083 }
1084 }
1085
1086 /*
1087 * Compute difference between two set of totem interface arrays. set1 and set2
1088 * are changed so for same ring, ip existing in both set1 and set2 are cleared
1089 * (set to 0), and ips which are only in set1 or set2 remains untouched.
1090 * totempg_node_add/remove is called.
1091 */
1092 static void compute_interfaces_diff(struct totem_interface *set1,
1093 struct totem_interface *set2)
1094 {
1095 int ring_no, set1_pos, set2_pos;
1096 struct totem_ip_address empty_ip_address;
1097
1098 memset(&empty_ip_address, 0, sizeof(empty_ip_address));
1099
1100 for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
1101 if (!set1[ring_no].configured && !set2[ring_no].configured) {
1102 continue;
1103 }
1104
1105 for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
1106 for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
1107 /*
1108 * For current ring_no remove all set1 items existing
1109 * in set2
1110 */
1111 if (memcmp(&set1[ring_no].member_list[set1_pos],
1112 &set2[ring_no].member_list[set2_pos],
1113 sizeof(struct totem_ip_address)) == 0) {
1114 memset(&set1[ring_no].member_list[set1_pos], 0,
1115 sizeof(struct totem_ip_address));
1116 memset(&set2[ring_no].member_list[set2_pos], 0,
1117 sizeof(struct totem_ip_address));
1118 }
1119 }
1120 }
1121 }
1122
1123 for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
1124 for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
1125 /*
1126 * All items which remained in set1 doesn't exists in set2 any longer so
1127 * node has to be removed.
1128 */
1129 if (memcmp(&set1[ring_no].member_list[set1_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
1130 log_printf(LOGSYS_LEVEL_DEBUG,
1131 "removing dynamic member %s for ring %u",
1132 totemip_print(&set1[ring_no].member_list[set1_pos]),
1133 ring_no);
1134
1135 totempg_member_remove(&set1[ring_no].member_list[set1_pos], ring_no);
1136 }
1137 }
1138 if (!set2[ring_no].configured) {
1139 continue;
1140 }
1141 for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
1142 /*
1143 * All items which remained in set2 doesn't existed in set1 so this is no node
1144 * and has to be added.
1145 */
1146 if (memcmp(&set2[ring_no].member_list[set2_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
1147 log_printf(LOGSYS_LEVEL_DEBUG,
1148 "adding dynamic member %s for ring %u",
1149 totemip_print(&set2[ring_no].member_list[set2_pos]),
1150 ring_no);
1151
1152 totempg_member_add(&set2[ring_no].member_list[set2_pos], ring_no);
1153 }
1154 }
1155 }
1156 }
1157
1158 /*
1159 * Reconfigure links in totempg. Sets new local IP address and adds params for new links.
1160 */
1161 static void reconfigure_links(struct totem_config *totem_config)
1162 {
1163 int i;
1164 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1165 char *addr_string;
1166 struct totem_ip_address local_ip;
1167 int err;
1168 int local_node_pos = find_local_node(0);
1169
1170 for (i = 0; i<INTERFACE_MAX; i++) {
1171 if (!totem_config->interfaces[i].configured) {
1172 continue;
1173 }
1174
1175 log_printf(LOGSYS_LEVEL_INFO, "Configuring link %d\n", i);
1176
1177 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring%u_addr", local_node_pos, i);
1178 if (icmap_get_string(tmp_key, &addr_string) != CS_OK) {
1179 continue;
1180 }
1181
1182 err = totemip_parse(&local_ip, addr_string, totem_config->ip_version);
1183 if (err != 0) {
1184 continue;
1185 }
1186 local_ip.nodeid = totem_config->node_id;
1187
1188 /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
1189 if (!totem_config->interfaces[i].knet_link_priority)
1190 totem_config->interfaces[i].knet_link_priority = 1;
1191
1192 /* knet_ping_interval & knet_ping_timeout are set later once we know all the other params */
1193
1194 if (!totem_config->interfaces[i].knet_ping_precision)
1195 totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
1196 if (!totem_config->interfaces[i].knet_pong_count)
1197 totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
1198 if (!totem_config->interfaces[i].knet_transport)
1199 totem_config->interfaces[i].knet_transport = KNET_TRANSPORT_UDP;
1200 if (!totem_config->interfaces[i].ip_port)
1201 totem_config->interfaces[i].ip_port = DEFAULT_PORT + i;
1202
1203 totempg_iface_set(&local_ip, totem_config->interfaces[i].ip_port, i);
1204 }
1205 }
1206
1207 /* Check for differences in config that can't be done on-the-fly and print an error */
1208 static void check_things_have_not_changed(struct totem_config *totem_config)
1209 {
1210 int i,j;
1211 const char *ip_str;
1212 char addr_buf[INET6_ADDRSTRLEN];
1213 int changed = 0;
1214
1215 for (i = 0; i<INTERFACE_MAX; i++) {
1216 if (totem_config->interfaces[i].configured &&
1217 totem_config->orig_interfaces[i].configured) {
1218 if (totem_config->interfaces[i].knet_transport !=
1219 totem_config->orig_interfaces[i].knet_transport) {
1220 log_printf(LOGSYS_LEVEL_ERROR,
1221 "New config has different knet transport for link %d. Internal value was NOT changed.\n", i);
1222 changed = 1;
1223 }
1224 for (j=0; j < min(totem_config->interfaces[i].member_count, totem_config->orig_interfaces[i].member_count); j++) {
1225 if (memcmp(&totem_config->interfaces[i].member_list[j],
1226 &totem_config->orig_interfaces[i].member_list[j],
1227 sizeof(struct totem_ip_address))) {
1228
1229 ip_str = totemip_print(&totem_config->orig_interfaces[i].member_list[j]);
1230
1231 /* if ip_str is NULL then the old address was invalid and is allowed to change */
1232 if (ip_str) {
1233 strncpy(addr_buf, ip_str, sizeof(addr_buf));
1234 addr_buf[sizeof(addr_buf) - 1] = '\0';
1235 log_printf(LOGSYS_LEVEL_ERROR,
1236 "new config has different address for link %d (addr changed from %s to %s). Internal value was NOT changed.\n",
1237 i, addr_buf, totemip_print(&totem_config->interfaces[i].member_list[j]));
1238 changed = 1;
1239 }
1240 }
1241 }
1242 }
1243 }
1244
1245 if (changed) {
1246 log_printf(LOGSYS_LEVEL_ERROR, "To reconfigure an interface it must be deleted and recreated. A working interface needs to be available to corosync at all times");
1247 }
1248 }
1249
1250
1251 static int put_nodelist_members_to_config(struct totem_config *totem_config, int reload, const char **error_string)
1252 {
1253 icmap_iter_t iter, iter2;
1254 const char *iter_key, *iter_key2;
1255 int res = 0;
1256 unsigned int node_pos;
1257 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1258 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1259 char *node_addr_str;
1260 int member_count;
1261 unsigned int linknumber = 0;
1262 int i, j;
1263 int last_node_pos = -1;
1264 struct totem_interface *new_interfaces = NULL;
1265
1266 if (reload) {
1267 /*
1268 * We need to compute diff only for reload. Also for initial configuration
1269 * not all totem structures are initialized so corosync will crash during
1270 * member_add/remove
1271 */
1272 new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1273 assert(new_interfaces != NULL);
1274 }
1275
1276 /* Clear out nodelist so we can put the new one in if needed */
1277 for (i = 0; i < INTERFACE_MAX; i++) {
1278 for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
1279 memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
1280 }
1281 totem_config->interfaces[i].member_count = 0;
1282 }
1283
1284 iter = icmap_iter_init("nodelist.node.");
1285 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1286 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
1287 if (res != 2) {
1288 continue;
1289 }
1290 /* If it's the same as the last node_pos then skip it */
1291 if (node_pos == last_node_pos) {
1292 continue;
1293 }
1294 last_node_pos = node_pos;
1295
1296 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1297 iter2 = icmap_iter_init(tmp_key);
1298 while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
1299 unsigned int nodeid;
1300 char *str;
1301
1302 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
1303 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
1304 nodeid = 0;
1305 }
1306
1307 res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1308 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1309 continue;
1310 }
1311
1312 if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
1313 continue;
1314 }
1315
1316 /* Generate nodeids if they are not provided and transport is UDP/U */
1317 if (!nodeid &&
1318 (totem_config->transport_number == TOTEM_TRANSPORT_UDP ||
1319 totem_config->transport_number == TOTEM_TRANSPORT_UDPU)) {
1320 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
1321 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1322 nodeid = generate_nodeid(totem_config, str);
1323 if (nodeid == -1) {
1324 sprintf(error_string_response,
1325 "An IPV6 network requires that a node ID be specified "
1326 "for address '%s'.", node_addr_str);
1327 *error_string = error_string_response;
1328 free(str);
1329
1330 return (-1);
1331 }
1332
1333 log_printf(LOGSYS_LEVEL_DEBUG,
1334 "Generated nodeid = 0x%x for %s", nodeid, str);
1335 free(str);
1336 }
1337 }
1338
1339 member_count = totem_config->interfaces[linknumber].member_count;
1340 res = totemip_parse(&totem_config->interfaces[linknumber].member_list[member_count],
1341 node_addr_str, totem_config->ip_version);
1342 if (res == 0) {
1343 totem_config->interfaces[linknumber].member_list[member_count].nodeid = nodeid;
1344 totem_config->interfaces[linknumber].member_count++;
1345 totem_config->interfaces[linknumber].configured = 1;
1346 } else {
1347 sprintf(error_string_response, "failed to parse node address '%s'\n", node_addr_str);
1348 *error_string = error_string_response;
1349
1350 memset(&totem_config->interfaces[linknumber].member_list[member_count], 0,
1351 sizeof(struct totem_ip_address));
1352
1353 free(node_addr_str);
1354 icmap_iter_finalize(iter2);
1355 icmap_iter_finalize(iter);
1356 return -1;
1357 }
1358
1359 free(node_addr_str);
1360 }
1361
1362 icmap_iter_finalize(iter2);
1363 }
1364
1365 icmap_iter_finalize(iter);
1366
1367 if (reload) {
1368 log_printf(LOGSYS_LEVEL_DEBUG, "About to reconfigure links from nodelist.\n");
1369 reconfigure_links(totem_config);
1370
1371 memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
1372
1373 check_things_have_not_changed(totem_config);
1374
1375 compute_interfaces_diff(totem_config->orig_interfaces, new_interfaces);
1376
1377 free(new_interfaces);
1378 }
1379 return 0;
1380 }
1381
1382 static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
1383 {
1384 int res = 0;
1385 int node_pos;
1386 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1387 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1388 char *node_addr_str;
1389 unsigned int linknumber = 0;
1390 icmap_iter_t iter;
1391 const char *iter_key;
1392
1393 node_pos = find_local_node(1);
1394 if (node_pos > -1) {
1395 /*
1396 * We found node, so create interface section
1397 */
1398 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1399 iter = icmap_iter_init(tmp_key);
1400 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1401 res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1402 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1403 continue ;
1404 }
1405
1406 if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
1407 continue;
1408 }
1409
1410 snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1411 icmap_set_string(tmp_key2, node_addr_str);
1412 free(node_addr_str);
1413 }
1414 icmap_iter_finalize(iter);
1415 }
1416 }
1417
1418 static int get_interface_params(struct totem_config *totem_config,
1419 const char **error_string, uint64_t *warnings,
1420 int reload)
1421 {
1422 int res = 0;
1423 unsigned int linknumber = 0;
1424 int member_count = 0;
1425 int i;
1426 icmap_iter_t iter, member_iter;
1427 const char *iter_key;
1428 const char *member_iter_key;
1429 char linknumber_key[ICMAP_KEYNAME_MAXLEN];
1430 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1431 uint8_t u8;
1432 uint32_t u32;
1433 char *str;
1434 char *cluster_name = NULL;
1435 enum totem_ip_version_enum tmp_ip_version = TOTEM_IP_VERSION_4;
1436
1437 if (reload) {
1438 for (i=0; i<INTERFACE_MAX; i++) {
1439 /*
1440 * Set back to defaults things that might have been configured and
1441 * now have been taken out of corosync.conf. These won't be caught by the
1442 * code below which only looks at interface{} sections that actually exist.
1443 */
1444 totem_config->interfaces[i].configured = 0;
1445 totem_config->interfaces[i].knet_ping_timeout = 0;
1446 totem_config->interfaces[i].knet_ping_interval = 0;
1447 totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
1448 totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
1449 }
1450 }
1451 if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
1452 cluster_name = NULL;
1453 }
1454
1455 iter = icmap_iter_init("totem.interface.");
1456 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1457 res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
1458 if (res != 2) {
1459 continue;
1460 }
1461
1462 if (strcmp(tmp_key, "bindnetaddr") != 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1463 continue;
1464 }
1465
1466 member_count = 0;
1467 linknumber = atoi(linknumber_key);
1468
1469 if (linknumber >= INTERFACE_MAX) {
1470 free(cluster_name);
1471
1472 snprintf (error_string_response, sizeof(error_string_response),
1473 "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1474 linknumber, INTERFACE_MAX - 1);
1475
1476 *error_string = error_string_response;
1477 return -1;
1478 }
1479
1480 /* These things are only valid for the initial read */
1481 if (!reload) {
1482 /*
1483 * Get the bind net address
1484 */
1485 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1486
1487 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1488 res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
1489 totem_config->ip_version);
1490
1491 if (res) {
1492 sprintf(error_string_response, "failed to parse bindnet address '%s'\n", str);
1493 *error_string = error_string_response;
1494 free(str);
1495
1496 return -1;
1497 }
1498
1499 free(str);
1500 }
1501
1502 /*
1503 * Get interface multicast address
1504 */
1505 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
1506 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1507 res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str,
1508 totem_config->ip_version);
1509
1510 if (res) {
1511 sprintf(error_string_response, "failed to parse mcast address '%s'\n", str);
1512 *error_string = error_string_response;
1513 free(str);
1514
1515 return -1;
1516 }
1517
1518 free(str);
1519 } else if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1520 /*
1521 * User not specified address -> autogenerate one from cluster_name key
1522 * (if available). Return code is intentionally ignored, because
1523 * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1524 * checked later anyway.
1525 */
1526
1527 if (totem_config->interfaces[0].bindnet.family == AF_INET) {
1528 tmp_ip_version = TOTEM_IP_VERSION_4;
1529 } else if (totem_config->interfaces[0].bindnet.family == AF_INET6) {
1530 tmp_ip_version = TOTEM_IP_VERSION_6;
1531 }
1532
1533 (void)get_cluster_mcast_addr (cluster_name,
1534 linknumber,
1535 tmp_ip_version,
1536 &totem_config->interfaces[linknumber].mcast_addr);
1537 }
1538
1539 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
1540 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1541 if (strcmp (str, "yes") == 0) {
1542 totem_config->broadcast_use = 1;
1543 }
1544 free(str);
1545 }
1546 }
1547
1548 /* These things are only valid for the initial read OR a newly-defined link */
1549 if (!reload || (totem_config->interfaces[linknumber].configured == 0)) {
1550
1551 /*
1552 * Get mcast port
1553 */
1554 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
1555 if (icmap_get_uint16(tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
1556 if (totem_config->broadcast_use) {
1557 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
1558 } else {
1559 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + linknumber;
1560 }
1561 }
1562
1563 /*
1564 * Get the TTL
1565 */
1566 totem_config->interfaces[linknumber].ttl = 1;
1567
1568 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
1569
1570 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1571 totem_config->interfaces[linknumber].ttl = u8;
1572 }
1573
1574 totem_config->interfaces[linknumber].knet_transport = KNET_DEFAULT_TRANSPORT;
1575 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
1576 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1577 if (strcmp(str, "sctp") == 0) {
1578 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
1579 }
1580 else if (strcmp(str, "udp") == 0) {
1581 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
1582 }
1583 else {
1584 *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1585 return -1;
1586 }
1587 }
1588 }
1589 totem_config->interfaces[linknumber].configured = 1;
1590
1591 /*
1592 * Get the knet link params
1593 */
1594 totem_config->interfaces[linknumber].knet_link_priority = 1;
1595 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
1596
1597 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1598 totem_config->interfaces[linknumber].knet_link_priority = u8;
1599 }
1600
1601 totem_config->interfaces[linknumber].knet_ping_interval = 0; /* real default applied later */
1602 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
1603 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1604 totem_config->interfaces[linknumber].knet_ping_interval = u32;
1605 }
1606 totem_config->interfaces[linknumber].knet_ping_timeout = 0; /* real default applied later */
1607 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
1608 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1609 totem_config->interfaces[linknumber].knet_ping_timeout = u32;
1610 }
1611 totem_config->interfaces[linknumber].knet_ping_precision = KNET_PING_PRECISION;
1612 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
1613 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1614 totem_config->interfaces[linknumber].knet_ping_precision = u32;
1615 }
1616 totem_config->interfaces[linknumber].knet_pong_count = KNET_PONG_COUNT;
1617 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
1618 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1619 totem_config->interfaces[linknumber].knet_pong_count = u32;
1620 }
1621
1622 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
1623 member_iter = icmap_iter_init(tmp_key);
1624 while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
1625 if (member_count == 0) {
1626 if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
1627 free(str);
1628 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
1629 break;
1630 } else {
1631 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
1632 }
1633 }
1634
1635 if (icmap_get_string(member_iter_key, &str) == CS_OK) {
1636 res = totemip_parse (&totem_config->interfaces[linknumber].member_list[member_count++],
1637 str, totem_config->ip_version);
1638 if (res) {
1639 sprintf(error_string_response, "failed to parse node address '%s'\n", str);
1640 *error_string = error_string_response;
1641
1642 icmap_iter_finalize(member_iter);
1643 icmap_iter_finalize(iter);
1644 free(str);
1645 return -1;
1646 }
1647
1648 free(str);
1649 }
1650 }
1651 icmap_iter_finalize(member_iter);
1652
1653 totem_config->interfaces[linknumber].member_count = member_count;
1654
1655 }
1656 icmap_iter_finalize(iter);
1657
1658 return 0;
1659 }
1660
1661 extern int totem_config_read (
1662 struct totem_config *totem_config,
1663 const char **error_string,
1664 uint64_t *warnings)
1665 {
1666 int res = 0;
1667 char *str, *ring0_addr_str;
1668 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1669 uint16_t u16;
1670 int i;
1671 int local_node_pos;
1672 int nodeid_set;
1673
1674 *warnings = 0;
1675
1676 memset (totem_config, 0, sizeof (struct totem_config));
1677 totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1678 if (totem_config->interfaces == 0) {
1679 *error_string = "Out of memory trying to allocate ethernet interface storage area";
1680 return -1;
1681 }
1682
1683 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1684 if (icmap_get_string("totem.transport", &str) == CS_OK) {
1685 if (strcmp (str, "udpu") == 0) {
1686 totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
1687 }
1688
1689 if (strcmp (str, "udp") == 0) {
1690 totem_config->transport_number = TOTEM_TRANSPORT_UDP;
1691 }
1692
1693 if (strcmp (str, "knet") == 0) {
1694 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1695 }
1696
1697 free(str);
1698 }
1699
1700 memset (totem_config->interfaces, 0,
1701 sizeof (struct totem_interface) * INTERFACE_MAX);
1702
1703 strcpy (totem_config->link_mode, "passive");
1704
1705 icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
1706
1707 if (totem_get_crypto(totem_config, error_string) != 0) {
1708 return -1;
1709 }
1710
1711 if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
1712 if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
1713 *error_string = "totem.link_mode is too long";
1714 free(str);
1715
1716 return -1;
1717 }
1718 strcpy (totem_config->link_mode, str);
1719 free(str);
1720 }
1721
1722 icmap_get_uint32("totem.nodeid", &totem_config->node_id);
1723
1724 totem_config->clear_node_high_bit = 0;
1725 if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
1726 if (strcmp (str, "yes") == 0) {
1727 totem_config->clear_node_high_bit = 1;
1728 }
1729 free(str);
1730 }
1731
1732 icmap_get_uint32("totem.threads", &totem_config->threads);
1733
1734 icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
1735
1736 totem_config->ip_version = totem_config_get_ip_version(totem_config);
1737
1738 if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
1739 /*
1740 * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1741 */
1742 config_convert_nodelist_to_interface(totem_config);
1743 } else {
1744 if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
1745 /*
1746 * Both bindnetaddr and ring0_addr are set.
1747 * Log warning information, and use nodelist instead
1748 */
1749 *warnings |= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET;
1750
1751 config_convert_nodelist_to_interface(totem_config);
1752
1753 free(ring0_addr_str);
1754 }
1755
1756 free(str);
1757 }
1758
1759 /*
1760 * Broadcast option is global but set in interface section,
1761 * so reset before processing interfaces.
1762 */
1763 totem_config->broadcast_use = 0;
1764
1765 res = get_interface_params(totem_config, error_string, warnings, 0);
1766 if (res < 0) {
1767 return res;
1768 }
1769
1770 /*
1771 * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1772 * broadcast is only supported for UDP so just do interface 0;
1773 */
1774 if (totem_config->broadcast_use) {
1775 totemip_parse (&totem_config->interfaces[0].mcast_addr,
1776 "255.255.255.255", TOTEM_IP_VERSION_4);
1777 }
1778
1779
1780 /*
1781 * Store automatically generated items back to icmap only for UDP
1782 */
1783 if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1784 for (i = 0; i < INTERFACE_MAX; i++) {
1785 if (!totem_config->interfaces[i].configured) {
1786 continue;
1787 }
1788 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
1789 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1790 free(str);
1791 } else {
1792 str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
1793 icmap_set_string(tmp_key, str);
1794 }
1795
1796 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
1797 if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
1798 icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
1799 }
1800 }
1801 }
1802
1803 /*
1804 * Check existence of nodelist
1805 */
1806 if ((icmap_get_string("nodelist.node.0.name", &str) == CS_OK) ||
1807 (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK)) {
1808 free(str);
1809 /*
1810 * find local node
1811 */
1812 local_node_pos = find_local_node(1);
1813 if (local_node_pos != -1) {
1814
1815 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
1816
1817 nodeid_set = (totem_config->node_id != 0);
1818 if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
1819 *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
1820 }
1821 if ((totem_config->transport_number == TOTEM_TRANSPORT_KNET) && (!totem_config->node_id)) {
1822 *error_string = "Knet requires an explicit nodeid for the local node";
1823 return -1;
1824 }
1825
1826 if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP ||
1827 totem_config->transport_number == TOTEM_TRANSPORT_UDPU) && (!totem_config->node_id)) {
1828
1829 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
1830 icmap_get_string(tmp_key, &str);
1831
1832 totem_config->node_id = generate_nodeid(totem_config, str);
1833 if (totem_config->node_id == -1) {
1834 *error_string = "An IPV6 network requires that a node ID be specified";
1835
1836 free(str);
1837 return (-1);
1838 }
1839
1840 totem_config->interfaces[0].member_list[local_node_pos].nodeid = totem_config->node_id;
1841
1842 free(str);
1843 }
1844
1845 /* Users must not change this */
1846 icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1847 }
1848
1849 if (put_nodelist_members_to_config(totem_config, 0, error_string)) {
1850 return -1;
1851 }
1852 }
1853
1854 /*
1855 * Get things that might change in the future (and can depend on totem_config->interfaces);
1856 */
1857 totem_volatile_config_read(totem_config, NULL);
1858
1859 calc_knet_ping_timers(totem_config);
1860
1861 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1862
1863 add_totem_config_notification(totem_config);
1864
1865 return 0;
1866 }
1867
1868
1869 int totem_config_validate (
1870 struct totem_config *totem_config,
1871 const char **error_string)
1872 {
1873 static char local_error_reason[512];
1874 char parse_error[512];
1875 static char addr_str_buf[INET6_ADDRSTRLEN];
1876 const char *error_reason = local_error_reason;
1877 int i,j;
1878 uint32_t u32;
1879 int num_configured = 0;
1880 unsigned int interface_max = INTERFACE_MAX;
1881
1882 for (i = 0; i < INTERFACE_MAX; i++) {
1883 if (totem_config->interfaces[i].configured) {
1884 num_configured++;
1885 }
1886 }
1887 if (num_configured == 0) {
1888 error_reason = "No interfaces defined";
1889 goto parse_error;
1890 }
1891
1892 /* Check we found a local node name */
1893 if (icmap_get_uint32("nodelist.local_node_pos", &u32) != CS_OK) {
1894 error_reason = "No valid name found for local host";
1895 goto parse_error;
1896 }
1897
1898 for (i = 0; i < INTERFACE_MAX; i++) {
1899 /*
1900 * Some error checking of parsed data to make sure its valid
1901 */
1902
1903 struct totem_ip_address null_addr;
1904
1905 if (!totem_config->interfaces[i].configured) {
1906 continue;
1907 }
1908
1909 memset (&null_addr, 0, sizeof (struct totem_ip_address));
1910
1911 if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP) &&
1912 memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
1913 sizeof (struct totem_ip_address)) == 0) {
1914 error_reason = "No multicast address specified";
1915 goto parse_error;
1916 }
1917
1918 if (totem_config->interfaces[i].ip_port == 0) {
1919 error_reason = "No multicast port specified";
1920 goto parse_error;
1921 }
1922
1923 if (totem_config->interfaces[i].ttl > 255) {
1924 error_reason = "Invalid TTL (should be 0..255)";
1925 goto parse_error;
1926 }
1927 if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
1928 totem_config->interfaces[i].ttl != 1) {
1929 error_reason = "Can only set ttl on multicast transport types";
1930 goto parse_error;
1931 }
1932 if (totem_config->interfaces[i].knet_link_priority > 255) {
1933 error_reason = "Invalid link priority (should be 0..255)";
1934 goto parse_error;
1935 }
1936 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET &&
1937 totem_config->interfaces[i].knet_link_priority != 1) {
1938 error_reason = "Can only set link priority on knet transport type";
1939 goto parse_error;
1940 }
1941
1942 if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
1943 totem_config->node_id == 0) {
1944
1945 error_reason = "An IPV6 network requires that a node ID be specified.";
1946 goto parse_error;
1947 }
1948
1949 if (totem_config->broadcast_use == 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1950 if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
1951 error_reason = "Multicast address family does not match bind address family";
1952 goto parse_error;
1953 }
1954
1955 if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
1956 error_reason = "mcastaddr is not a correct multicast address.";
1957 goto parse_error;
1958 }
1959 }
1960 /* Verify that all nodes on the same knet link have the same IP family */
1961 for (j=1; j<totem_config->interfaces[i].member_count; j++) {
1962 if (totem_config->interfaces[i].configured) {
1963 if (totem_config->interfaces[i].member_list[j].family !=
1964 totem_config->interfaces[i].member_list[0].family) {
1965 memcpy(addr_str_buf,
1966 totemip_print(&(totem_config->interfaces[i].member_list[j])),
1967 sizeof(addr_str_buf));
1968
1969 snprintf (local_error_reason, sizeof(local_error_reason),
1970 "Nodes for link %d have different IP families "
1971 "(compared %s with %s)", i,
1972 addr_str_buf,
1973 totemip_print(&(totem_config->interfaces[i].member_list[0])));
1974 goto parse_error;
1975 }
1976 }
1977 }
1978 }
1979
1980 if (totem_config->version != 2) {
1981 error_reason = "This totem parser can only parse version 2 configurations.";
1982 goto parse_error;
1983 }
1984
1985 if (totem_volatile_config_validate(totem_config, error_string) == -1) {
1986 return (-1);
1987 }
1988
1989 if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
1990 return (-1);
1991 }
1992
1993 /*
1994 * KNET Link values validation
1995 */
1996 if (strcmp (totem_config->link_mode, "active") &&
1997 strcmp (totem_config->link_mode, "rr") &&
1998 strcmp (totem_config->link_mode, "passive")) {
1999 snprintf (local_error_reason, sizeof(local_error_reason),
2000 "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
2001 goto parse_error;
2002 }
2003
2004 /* Only Knet does multiple interfaces */
2005 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
2006 interface_max = 1;
2007 }
2008
2009 if (interface_max < num_configured) {
2010 snprintf (parse_error, sizeof(parse_error),
2011 "%d is too many configured interfaces for non-Knet transport.",
2012 num_configured);
2013 error_reason = parse_error;
2014 goto parse_error;
2015 }
2016
2017 /* Only knet allows crypto */
2018 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
2019 if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
2020 (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
2021
2022 snprintf (parse_error, sizeof(parse_error),
2023 "crypto_cipher & crypto_hash are only valid for the Knet transport.");
2024 error_reason = parse_error;
2025 goto parse_error;
2026 }
2027 }
2028
2029 if (totem_config->net_mtu == 0) {
2030 if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
2031 totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
2032 }
2033 else {
2034 totem_config->net_mtu = 1500;
2035 }
2036 }
2037
2038 return 0;
2039
2040 parse_error:
2041 snprintf (error_string_response, sizeof(error_string_response),
2042 "parse error in config: %s\n", error_reason);
2043 *error_string = error_string_response;
2044 return (-1);
2045
2046 }
2047
2048 static int read_keyfile (
2049 const char *key_location,
2050 struct totem_config *totem_config,
2051 const char **error_string)
2052 {
2053 int fd;
2054 int res;
2055 int saved_errno;
2056 char error_str[100];
2057 const char *error_ptr;
2058
2059 fd = open (key_location, O_RDONLY);
2060 if (fd == -1) {
2061 error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
2062 snprintf (error_string_response, sizeof(error_string_response),
2063 "Could not open %s: %s\n",
2064 key_location, error_ptr);
2065 goto parse_error;
2066 }
2067
2068 res = read (fd, totem_config->private_key, TOTEM_PRIVATE_KEY_LEN_MAX);
2069 saved_errno = errno;
2070 close (fd);
2071
2072 if (res == -1) {
2073 error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
2074 snprintf (error_string_response, sizeof(error_string_response),
2075 "Could not read %s: %s\n",
2076 key_location, error_ptr);
2077 goto parse_error;
2078 }
2079
2080 if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
2081 snprintf (error_string_response, sizeof(error_string_response),
2082 "Could only read %d bits of minimum %u bits from %s.\n",
2083 res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
2084 goto parse_error;
2085 }
2086
2087 totem_config->private_key_len = res;
2088
2089 return 0;
2090
2091 parse_error:
2092 *error_string = error_string_response;
2093 return (-1);
2094 }
2095
2096 int totem_config_keyread (
2097 struct totem_config *totem_config,
2098 const char **error_string)
2099 {
2100 int got_key = 0;
2101 char *key_location = NULL;
2102 int res;
2103 size_t key_len;
2104
2105 memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
2106 totem_config->private_key_len = 0;
2107
2108 if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
2109 strcmp(totem_config->crypto_hash_type, "none") == 0) {
2110 return (0);
2111 }
2112
2113 /* cmap may store the location of the key file */
2114 if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
2115 res = read_keyfile(key_location, totem_config, error_string);
2116 free(key_location);
2117 if (res) {
2118 goto key_error;
2119 }
2120 got_key = 1;
2121 } else { /* Or the key itself may be in the cmap */
2122 if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
2123 if (key_len > sizeof(totem_config->private_key)) {
2124 sprintf(error_string_response, "key is too long");
2125 goto key_error;
2126 }
2127 if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
2128 sprintf(error_string_response, "key is too short");
2129 goto key_error;
2130 }
2131 if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
2132 totem_config->private_key_len = key_len;
2133 got_key = 1;
2134 } else {
2135 sprintf(error_string_response, "can't load private key");
2136 goto key_error;
2137 }
2138 }
2139 }
2140
2141 /* In desperation we read the default filename */
2142 if (!got_key) {
2143 res = read_keyfile(COROSYSCONFDIR "/authkey", totem_config, error_string);
2144 if (res)
2145 goto key_error;
2146 }
2147
2148 return (0);
2149
2150 key_error:
2151 *error_string = error_string_response;
2152 return (-1);
2153
2154 }
2155
2156 static void debug_dump_totem_config(const struct totem_config *totem_config)
2157 {
2158
2159 log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
2160 totem_config->token_timeout, totem_config->token_retransmit_timeout);
2161 if (totem_config->token_warning) {
2162 uint32_t token_warning_ms = totem_config->token_warning * totem_config->token_timeout / 100;
2163 log_printf(LOGSYS_LEVEL_DEBUG, "Token warning every %d ms (%d%% of Token Timeout)",
2164 token_warning_ms, totem_config->token_warning);
2165 if (token_warning_ms < totem_config->token_retransmit_timeout)
2166 log_printf (LOGSYS_LEVEL_DEBUG,
2167 "The token warning interval (%d ms) is less than the token retransmit timeout (%d ms) "
2168 "which can lead to spurious token warnings. Consider increasing the token_warning parameter.",
2169 token_warning_ms, totem_config->token_retransmit_timeout);
2170
2171 } else
2172 log_printf(LOGSYS_LEVEL_DEBUG, "Token warnings disabled");
2173 log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
2174 totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
2175 log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
2176 totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
2177 totem_config->merge_timeout);
2178 log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
2179 totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
2180 log_printf(LOGSYS_LEVEL_DEBUG,
2181 "seqno unchanged const (%d rotations) Maximum network MTU %d",
2182 totem_config->seqno_unchanged_const, totem_config->net_mtu);
2183 log_printf(LOGSYS_LEVEL_DEBUG,
2184 "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
2185 totem_config->window_size, totem_config->max_messages);
2186 log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
2187 log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
2188 totem_config->heartbeat_failures_allowed);
2189 log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
2190 }
2191
2192 static void totem_change_notify(
2193 int32_t event,
2194 const char *key_name,
2195 struct icmap_notify_value new_val,
2196 struct icmap_notify_value old_val,
2197 void *user_data)
2198 {
2199 struct totem_config *totem_config = (struct totem_config *)user_data;
2200 uint32_t *param;
2201 uint8_t reloading;
2202 const char *deleted_key = NULL;
2203 const char *error_string;
2204
2205 /*
2206 * If a full reload is in progress then don't do anything until it's done and
2207 * can reconfigure it all atomically
2208 */
2209 if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
2210 return;
2211
2212 param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
2213 /*
2214 * Process change only if changed key is found in totem_config (-> param is not NULL)
2215 * or for special key token_coefficient. token_coefficient key is not stored in
2216 * totem_config, but it is used for computation of token timeout.
2217 */
2218 if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
2219 return;
2220
2221 /*
2222 * Values other than UINT32 are not supported, or needed (yet)
2223 */
2224 switch (event) {
2225 case ICMAP_TRACK_DELETE:
2226 deleted_key = key_name;
2227 break;
2228 case ICMAP_TRACK_ADD:
2229 case ICMAP_TRACK_MODIFY:
2230 deleted_key = NULL;
2231 break;
2232 default:
2233 break;
2234 }
2235
2236 totem_volatile_config_read (totem_config, deleted_key);
2237 log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
2238 debug_dump_totem_config(totem_config);
2239 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2240 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2241 /*
2242 * TODO: Consider corosync exit and/or load defaults for volatile
2243 * values. For now, log error seems to be enough
2244 */
2245 }
2246 }
2247
2248 static void totem_reload_notify(
2249 int32_t event,
2250 const char *key_name,
2251 struct icmap_notify_value new_val,
2252 struct icmap_notify_value old_val,
2253 void *user_data)
2254 {
2255 struct totem_config *totem_config = (struct totem_config *)user_data;
2256 const char *error_string;
2257 uint64_t warnings;
2258
2259 /* Reload has completed */
2260 if (*(uint8_t *)new_val.data == 0) {
2261
2262 totem_config->orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
2263 assert(totem_config->orig_interfaces != NULL);
2264 memcpy(totem_config->orig_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
2265
2266 get_interface_params(totem_config, &error_string, &warnings, 1);
2267 if (put_nodelist_members_to_config (totem_config, 1, &error_string)) {
2268 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2269 }
2270 totem_volatile_config_read (totem_config, NULL);
2271
2272 calc_knet_ping_timers(totem_config);
2273
2274 log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
2275 debug_dump_totem_config(totem_config);
2276 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2277 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2278 /*
2279 * TODO: Consider corosync exit and/or load defaults for volatile
2280 * values. For now, log error seems to be enough
2281 */
2282 }
2283
2284 /* Reinstate the local_node_pos */
2285 (void)find_local_node(0);
2286
2287 /* Reconfigure network params as appropriate */
2288 totempg_reconfigure();
2289
2290 free(totem_config->orig_interfaces);
2291
2292 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
2293 } else {
2294 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
2295 }
2296 }
2297
2298 static void add_totem_config_notification(struct totem_config *totem_config)
2299 {
2300 icmap_track_t icmap_track;
2301
2302 icmap_track_add("totem.",
2303 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
2304 totem_change_notify,
2305 totem_config,
2306 &icmap_track);
2307
2308 icmap_track_add("config.reload_in_progress",
2309 ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
2310 totem_reload_notify,
2311 totem_config,
2312 &icmap_track);
2313 }