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