2 * Copyright (c) 2002-2005 MontaVista Software, Inc.
3 * Copyright (c) 2006-2013 Red Hat, Inc.
7 * Author: Steven Dake (sdake@redhat.com)
8 * Jan Friesse (jfriesse@redhat.com)
10 * This software licensed under BSD license, the text of which follows:
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
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.
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.
44 #include <sys/socket.h>
45 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <sys/param.h>
52 #include <corosync/swab.h>
53 #include <qb/qblist.h>
54 #include <qb/qbdefs.h>
56 #include <corosync/totem/totem.h>
57 #include <corosync/config.h>
58 #include <corosync/logsys.h>
59 #include <corosync/icmap.h>
62 #include "totemconfig.h"
64 #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
65 #define TOKEN_TIMEOUT 1000
66 #define TOKEN_COEFFICIENT 650
67 #define JOIN_TIMEOUT 50
68 #define MERGE_TIMEOUT 200
69 #define DOWNCHECK_TIMEOUT 1000
70 #define FAIL_TO_RECV_CONST 2500
71 #define SEQNO_UNCHANGED_CONST 30
72 #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
73 #define MAX_NETWORK_DELAY 50
74 #define WINDOW_SIZE 50
75 #define MAX_MESSAGES 17
76 #define MISS_COUNT_CONST 5
78 /* These currently match the defaults in libknet.h */
79 #define KNET_PING_INTERVAL 1000
80 #define KNET_PING_TIMEOUT 2000
81 #define KNET_PING_PRECISION 2048
82 #define KNET_PONG_COUNT 2
83 #define KNET_PMTUD_INTERVAL 30
84 #define KNET_DEFAULT_TRANSPORT KNET_TRANSPORT_UDP
86 #define DEFAULT_PORT 5405
88 static char error_string_response
[512];
90 static void add_totem_config_notification(struct totem_config
*totem_config
);
92 static void *totem_get_param_by_name(struct totem_config
*totem_config
, const char *param_name
)
94 if (strcmp(param_name
, "totem.token") == 0)
95 return &totem_config
->token_timeout
;
96 if (strcmp(param_name
, "totem.token_retransmit") == 0)
97 return &totem_config
->token_retransmit_timeout
;
98 if (strcmp(param_name
, "totem.hold") == 0)
99 return &totem_config
->token_hold_timeout
;
100 if (strcmp(param_name
, "totem.token_retransmits_before_loss_const") == 0)
101 return &totem_config
->token_retransmits_before_loss_const
;
102 if (strcmp(param_name
, "totem.join") == 0)
103 return &totem_config
->join_timeout
;
104 if (strcmp(param_name
, "totem.send_join") == 0)
105 return &totem_config
->send_join_timeout
;
106 if (strcmp(param_name
, "totem.consensus") == 0)
107 return &totem_config
->consensus_timeout
;
108 if (strcmp(param_name
, "totem.merge") == 0)
109 return &totem_config
->merge_timeout
;
110 if (strcmp(param_name
, "totem.downcheck") == 0)
111 return &totem_config
->downcheck_timeout
;
112 if (strcmp(param_name
, "totem.fail_recv_const") == 0)
113 return &totem_config
->fail_to_recv_const
;
114 if (strcmp(param_name
, "totem.seqno_unchanged_const") == 0)
115 return &totem_config
->seqno_unchanged_const
;
116 if (strcmp(param_name
, "totem.heartbeat_failures_allowed") == 0)
117 return &totem_config
->heartbeat_failures_allowed
;
118 if (strcmp(param_name
, "totem.max_network_delay") == 0)
119 return &totem_config
->max_network_delay
;
120 if (strcmp(param_name
, "totem.window_size") == 0)
121 return &totem_config
->window_size
;
122 if (strcmp(param_name
, "totem.max_messages") == 0)
123 return &totem_config
->max_messages
;
124 if (strcmp(param_name
, "totem.miss_count_const") == 0)
125 return &totem_config
->miss_count_const
;
126 if (strcmp(param_name
, "totem.knet_pmtud_interval") == 0)
127 return &totem_config
->knet_pmtud_interval
;
128 if (strcmp(param_name
, "totem.knet_compression_threshold") == 0)
129 return &totem_config
->knet_compression_threshold
;
130 if (strcmp(param_name
, "totem.knet_compression_level") == 0)
131 return &totem_config
->knet_compression_level
;
132 if (strcmp(param_name
, "totem.knet_compression_model") == 0)
133 return &totem_config
->knet_compression_model
;
139 * Read key_name from icmap. If key is not found or key_name == delete_key or if allow_zero is false
140 * and readed value is zero, default value is used and stored into totem_config.
142 static void totem_volatile_config_set_uint32_value (struct totem_config
*totem_config
,
143 const char *key_name
, const char *deleted_key
, unsigned int default_value
,
144 int allow_zero_value
)
146 char runtime_key_name
[ICMAP_KEYNAME_MAXLEN
];
148 if (icmap_get_uint32(key_name
, totem_get_param_by_name(totem_config
, key_name
)) != CS_OK
||
149 (deleted_key
!= NULL
&& strcmp(deleted_key
, key_name
) == 0) ||
150 (!allow_zero_value
&& *(uint32_t *)totem_get_param_by_name(totem_config
, key_name
) == 0)) {
151 *(uint32_t *)totem_get_param_by_name(totem_config
, key_name
) = default_value
;
155 * Store totem_config value to cmap runtime section
157 if (strlen("runtime.config.") + strlen(key_name
) >= ICMAP_KEYNAME_MAXLEN
) {
159 * This shouldn't happen
164 strcpy(runtime_key_name
, "runtime.config.");
165 strcat(runtime_key_name
, key_name
);
167 icmap_set_uint32(runtime_key_name
, *(uint32_t *)totem_get_param_by_name(totem_config
, key_name
));
170 static void totem_volatile_config_set_int32_value (struct totem_config
*totem_config
,
171 const char *key_name
, const char *deleted_key
, int default_value
,
172 int allow_zero_value
)
174 char runtime_key_name
[ICMAP_KEYNAME_MAXLEN
];
176 if (icmap_get_int32(key_name
, totem_get_param_by_name(totem_config
, key_name
)) != CS_OK
||
177 (deleted_key
!= NULL
&& strcmp(deleted_key
, key_name
) == 0) ||
178 (!allow_zero_value
&& *(int32_t *)totem_get_param_by_name(totem_config
, key_name
) == 0)) {
179 *(int32_t *)totem_get_param_by_name(totem_config
, key_name
) = default_value
;
183 * Store totem_config value to cmap runtime section
185 if (strlen("runtime.config.") + strlen(key_name
) >= ICMAP_KEYNAME_MAXLEN
) {
187 * This shouldn't happen
192 strcpy(runtime_key_name
, "runtime.config.");
193 strcat(runtime_key_name
, key_name
);
195 icmap_set_int32(runtime_key_name
, *(int32_t *)totem_get_param_by_name(totem_config
, key_name
));
198 static void totem_volatile_config_set_string_value (struct totem_config
*totem_config
,
199 const char *key_name
, const char *deleted_key
, const char *default_value
)
201 char runtime_key_name
[ICMAP_KEYNAME_MAXLEN
];
203 void *old_config_ptr
;
205 config_value
= totem_get_param_by_name(totem_config
, key_name
);
206 old_config_ptr
= *config_value
;
207 if (icmap_get_string(key_name
, totem_get_param_by_name(totem_config
, key_name
)) != CS_OK
||
208 (deleted_key
!= NULL
&& strcmp(deleted_key
, key_name
) == 0)) {
210 /* Need to strdup() here so that the free() below works for a default and a configured value */
211 *config_value
= strdup(default_value
);
213 free(old_config_ptr
);
216 * Store totem_config value to cmap runtime section
218 if (strlen("runtime.config.") + strlen(key_name
) >= ICMAP_KEYNAME_MAXLEN
) {
220 * This shouldn't happen
225 strcpy(runtime_key_name
, "runtime.config.");
226 strcat(runtime_key_name
, key_name
);
228 icmap_set_string(runtime_key_name
, (char *)*config_value
);
233 * Read and validate config values from cmap and store them into totem_config. If key doesn't exists,
234 * default value is stored. deleted_key is name of key beeing processed by delete operation
235 * from cmap. It is considered as non existing even if it can be read. Can be NULL.
237 static void totem_volatile_config_read (struct totem_config
*totem_config
, const char *deleted_key
)
241 totem_volatile_config_set_uint32_value(totem_config
, "totem.token_retransmits_before_loss_const", deleted_key
,
242 TOKEN_RETRANSMITS_BEFORE_LOSS_CONST
, 0);
244 totem_volatile_config_set_uint32_value(totem_config
, "totem.token", deleted_key
, TOKEN_TIMEOUT
, 0);
246 if (totem_config
->interfaces
[0].member_count
> 2) {
247 u32
= TOKEN_COEFFICIENT
;
248 icmap_get_uint32("totem.token_coefficient", &u32
);
249 totem_config
->token_timeout
+= (totem_config
->interfaces
[0].member_count
- 2) * u32
;
252 * Store totem_config value to cmap runtime section
254 icmap_set_uint32("runtime.config.totem.token", totem_config
->token_timeout
);
257 totem_volatile_config_set_uint32_value(totem_config
, "totem.max_network_delay", deleted_key
, MAX_NETWORK_DELAY
, 0);
259 totem_volatile_config_set_uint32_value(totem_config
, "totem.window_size", deleted_key
, WINDOW_SIZE
, 0);
261 totem_volatile_config_set_uint32_value(totem_config
, "totem.max_messages", deleted_key
, MAX_MESSAGES
, 0);
263 totem_volatile_config_set_uint32_value(totem_config
, "totem.miss_count_const", deleted_key
, MISS_COUNT_CONST
, 0);
264 totem_volatile_config_set_uint32_value(totem_config
, "totem.knet_pmtud_interval", deleted_key
, KNET_PMTUD_INTERVAL
, 0);
266 totem_volatile_config_set_uint32_value(totem_config
, "totem.token_retransmit", deleted_key
,
267 (int)(totem_config
->token_timeout
/ (totem_config
->token_retransmits_before_loss_const
+ 0.2)), 0);
269 totem_volatile_config_set_uint32_value(totem_config
, "totem.hold", deleted_key
,
270 (int)(totem_config
->token_retransmit_timeout
* 0.8 - (1000/HZ
)), 0);
272 totem_volatile_config_set_uint32_value(totem_config
, "totem.join", deleted_key
, JOIN_TIMEOUT
, 0);
274 totem_volatile_config_set_uint32_value(totem_config
, "totem.consensus", deleted_key
,
275 (int)(float)(1.2 * totem_config
->token_timeout
), 0);
277 totem_volatile_config_set_uint32_value(totem_config
, "totem.merge", deleted_key
, MERGE_TIMEOUT
, 0);
279 totem_volatile_config_set_uint32_value(totem_config
, "totem.downcheck", deleted_key
, DOWNCHECK_TIMEOUT
, 0);
281 totem_volatile_config_set_uint32_value(totem_config
, "totem.fail_recv_const", deleted_key
, FAIL_TO_RECV_CONST
, 0);
283 totem_volatile_config_set_uint32_value(totem_config
, "totem.seqno_unchanged_const", deleted_key
,
284 SEQNO_UNCHANGED_CONST
, 0);
286 totem_volatile_config_set_uint32_value(totem_config
, "totem.send_join", deleted_key
, 0, 1);
288 totem_volatile_config_set_uint32_value(totem_config
, "totem.heartbeat_failures_allowed", deleted_key
, 0, 1);
290 totem_volatile_config_set_uint32_value(totem_config
, "totem.knet_compression_threshold", deleted_key
, 0, 1);
292 totem_volatile_config_set_int32_value(totem_config
, "totem.knet_compression_level", deleted_key
, 0, 1);
294 totem_volatile_config_set_string_value(totem_config
, "totem.knet_compression_model", deleted_key
, "none");
299 static int totem_volatile_config_validate (
300 struct totem_config
*totem_config
,
301 const char **error_string
)
303 static char local_error_reason
[512];
304 const char *error_reason
= local_error_reason
;
306 if (totem_config
->max_network_delay
< MINIMUM_TIMEOUT
) {
307 snprintf (local_error_reason
, sizeof(local_error_reason
),
308 "The max_network_delay parameter (%d ms) may not be less than (%d ms).",
309 totem_config
->max_network_delay
, MINIMUM_TIMEOUT
);
313 if (totem_config
->token_timeout
< MINIMUM_TIMEOUT
) {
314 snprintf (local_error_reason
, sizeof(local_error_reason
),
315 "The token timeout parameter (%d ms) may not be less than (%d ms).",
316 totem_config
->token_timeout
, MINIMUM_TIMEOUT
);
320 if (totem_config
->token_retransmit_timeout
< MINIMUM_TIMEOUT
) {
321 snprintf (local_error_reason
, sizeof(local_error_reason
),
322 "The token retransmit timeout parameter (%d ms) may not be less than (%d ms).",
323 totem_config
->token_retransmit_timeout
, MINIMUM_TIMEOUT
);
327 if (totem_config
->token_hold_timeout
< MINIMUM_TIMEOUT
) {
328 snprintf (local_error_reason
, sizeof(local_error_reason
),
329 "The token hold timeout parameter (%d ms) may not be less than (%d ms).",
330 totem_config
->token_hold_timeout
, MINIMUM_TIMEOUT
);
334 if (totem_config
->join_timeout
< MINIMUM_TIMEOUT
) {
335 snprintf (local_error_reason
, sizeof(local_error_reason
),
336 "The join timeout parameter (%d ms) may not be less than (%d ms).",
337 totem_config
->join_timeout
, MINIMUM_TIMEOUT
);
341 if (totem_config
->consensus_timeout
< MINIMUM_TIMEOUT
) {
342 snprintf (local_error_reason
, sizeof(local_error_reason
),
343 "The consensus timeout parameter (%d ms) may not be less than (%d ms).",
344 totem_config
->consensus_timeout
, MINIMUM_TIMEOUT
);
348 if (totem_config
->consensus_timeout
< totem_config
->join_timeout
) {
349 snprintf (local_error_reason
, sizeof(local_error_reason
),
350 "The consensus timeout parameter (%d ms) may not be less than join timeout (%d ms).",
351 totem_config
->consensus_timeout
, totem_config
->join_timeout
);
355 if (totem_config
->merge_timeout
< MINIMUM_TIMEOUT
) {
356 snprintf (local_error_reason
, sizeof(local_error_reason
),
357 "The merge timeout parameter (%d ms) may not be less than (%d ms).",
358 totem_config
->merge_timeout
, MINIMUM_TIMEOUT
);
362 if (totem_config
->downcheck_timeout
< MINIMUM_TIMEOUT
) {
363 snprintf (local_error_reason
, sizeof(local_error_reason
),
364 "The downcheck timeout parameter (%d ms) may not be less than (%d ms).",
365 totem_config
->downcheck_timeout
, MINIMUM_TIMEOUT
);
372 snprintf (error_string_response
, sizeof(error_string_response
),
373 "parse error in config: %s\n", error_reason
);
374 *error_string
= error_string_response
;
379 static int totem_get_crypto(struct totem_config
*totem_config
)
382 const char *tmp_cipher
;
383 const char *tmp_hash
;
388 if (icmap_get_string("totem.crypto_cipher", &str
) == CS_OK
) {
389 if (strcmp(str
, "none") == 0) {
392 if (strcmp(str
, "aes256") == 0) {
393 tmp_cipher
= "aes256";
395 if (strcmp(str
, "aes192") == 0) {
396 tmp_cipher
= "aes192";
398 if (strcmp(str
, "aes128") == 0) {
399 tmp_cipher
= "aes128";
401 if (strcmp(str
, "aes256") == 0) {
402 tmp_cipher
= "aes256";
404 if (strcmp(str
, "3des") == 0) {
410 if (icmap_get_string("totem.crypto_hash", &str
) == CS_OK
) {
411 if (strcmp(str
, "none") == 0) {
414 if (strcmp(str
, "md5") == 0) {
417 if (strcmp(str
, "sha1") == 0) {
420 if (strcmp(str
, "sha256") == 0) {
423 if (strcmp(str
, "sha384") == 0) {
426 if (strcmp(str
, "sha512") == 0) {
432 if ((strcmp(tmp_cipher
, "none") != 0) &&
433 (strcmp(tmp_hash
, "none") == 0)) {
437 free(totem_config
->crypto_cipher_type
);
438 free(totem_config
->crypto_hash_type
);
440 totem_config
->crypto_cipher_type
= strdup(tmp_cipher
);
441 totem_config
->crypto_hash_type
= strdup(tmp_hash
);
446 static int totem_config_get_ip_version(void)
452 if (icmap_get_string("totem.ip_version", &str
) == CS_OK
) {
453 if (strcmp(str
, "ipv4") == 0) {
456 if (strcmp(str
, "ipv6") == 0) {
465 static uint16_t generate_cluster_id (const char *cluster_name
)
470 for (i
= 0; i
< strlen(cluster_name
); i
++) {
472 value
+= cluster_name
[i
];
475 return (value
& 0xFFFF);
478 static int get_cluster_mcast_addr (
479 const char *cluster_name
,
480 unsigned int linknumber
,
482 struct totem_ip_address
*res
)
485 char addr
[INET6_ADDRSTRLEN
+ 1];
488 if (cluster_name
== NULL
) {
492 clusterid
= generate_cluster_id(cluster_name
) + linknumber
;
493 memset (res
, 0, sizeof(*res
));
495 switch (ip_version
) {
497 snprintf(addr
, sizeof(addr
), "239.192.%d.%d", clusterid
>> 8, clusterid
% 0xFF);
500 snprintf(addr
, sizeof(addr
), "ff15::%x", clusterid
);
509 err
= totemip_parse (res
, addr
, ip_version
);
514 static unsigned int generate_nodeid_for_duplicate_test(
515 struct totem_config
*totem_config
,
519 struct totem_ip_address totemip
;
521 /* AF_INET hard-coded here because auto-generated nodeids
523 if (totemip_parse(&totemip
, addr
, AF_INET
) != 0)
526 memcpy (&nodeid
, &totemip
.addr
, sizeof (unsigned int));
528 #if __BYTE_ORDER == __LITTLE_ENDIAN
529 nodeid
= swab32 (nodeid
);
532 if (totem_config
->clear_node_high_bit
) {
533 nodeid
&= 0x7FFFFFFF;
538 static int check_for_duplicate_nodeids(
539 struct totem_config
*totem_config
,
540 const char **error_string
)
543 icmap_iter_t subiter
;
544 const char *iter_key
;
547 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
548 char *ring0_addr
=NULL
;
549 char *ring0_addr1
=NULL
;
550 unsigned int node_pos
;
551 unsigned int node_pos1
;
553 unsigned int nodeid1
;
556 iter
= icmap_iter_init("nodelist.node.");
557 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
558 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos
, tmp_key
);
563 if (strcmp(tmp_key
, "ring0_addr") != 0) {
567 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", node_pos
);
569 if (icmap_get_uint32(tmp_key
, &nodeid
) != CS_OK
) {
571 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", node_pos
);
572 if (icmap_get_string(tmp_key
, &ring0_addr
) != CS_OK
) {
576 /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
577 nodeid
= generate_nodeid_for_duplicate_test(totem_config
, ring0_addr
);
585 subiter
= icmap_iter_init("nodelist.node.");
586 while (((iter_key
= icmap_iter_next(subiter
, NULL
, NULL
)) != NULL
) && (node_pos1
< node_pos
)) {
587 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos1
, tmp_key
);
588 if ((res
!= 2) || (node_pos1
>= node_pos
)) {
592 if (strcmp(tmp_key
, "ring0_addr") != 0) {
596 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", node_pos1
);
597 if (icmap_get_uint32(tmp_key
, &nodeid1
) != CS_OK
) {
599 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", node_pos1
);
600 if (icmap_get_string(tmp_key
, &ring0_addr1
) != CS_OK
) {
603 nodeid1
= generate_nodeid_for_duplicate_test(totem_config
, ring0_addr1
);
609 if (nodeid
== nodeid1
) {
611 snprintf (error_string_response
, sizeof(error_string_response
),
612 "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid
,
613 autogenerated
?"(autogenerated from ":"",
614 autogenerated
?ring0_addr
:"",
615 autogenerated
?")":"");
616 log_printf (LOGSYS_LEVEL_ERROR
, error_string_response
);
617 *error_string
= error_string_response
;
621 icmap_iter_finalize(subiter
);
623 icmap_iter_finalize(iter
);
628 static int find_local_node_in_nodelist(struct totem_config
*totem_config
)
631 const char *iter_key
;
633 unsigned int node_pos
;
634 int local_node_pos
= -1;
635 struct totem_ip_address bind_addr
;
636 int interface_up
, interface_num
;
637 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
639 struct totem_ip_address node_addr
;
641 res
= totemip_iface_check(&totem_config
->interfaces
[0].bindnet
,
642 &bind_addr
, &interface_up
, &interface_num
,
643 totem_config
->clear_node_high_bit
);
648 iter
= icmap_iter_init("nodelist.node.");
649 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
650 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos
, tmp_key
);
655 if (strcmp(tmp_key
, "ring0_addr") != 0) {
659 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", node_pos
);
660 if (icmap_get_string(tmp_key
, &node_addr_str
) != CS_OK
) {
664 res
= totemip_parse (&node_addr
, node_addr_str
, totem_config
->ip_version
);
670 if (totemip_equal(&bind_addr
, &node_addr
)) {
671 local_node_pos
= node_pos
;
674 icmap_iter_finalize(iter
);
676 return (local_node_pos
);
680 * Compute difference between two set of totem interface arrays. set1 and set2
681 * are changed so for same ring, ip existing in both set1 and set2 are cleared
682 * (set to 0), and ips which are only in set1 or set2 remains untouched.
683 * totempg_node_add/remove is called.
685 static void compute_interfaces_diff(struct totem_interface
*set1
,
686 struct totem_interface
*set2
)
688 int ring_no
, set1_pos
, set2_pos
;
689 struct totem_ip_address empty_ip_address
;
691 memset(&empty_ip_address
, 0, sizeof(empty_ip_address
));
693 for (ring_no
= 0; ring_no
< INTERFACE_MAX
; ring_no
++) {
694 if (!set1
[ring_no
].configured
&& !set2
[ring_no
].configured
) {
698 for (set1_pos
= 0; set1_pos
< set1
[ring_no
].member_count
; set1_pos
++) {
699 for (set2_pos
= 0; set2_pos
< set2
[ring_no
].member_count
; set2_pos
++) {
701 * For current ring_no remove all set1 items existing
704 if (memcmp(&set1
[ring_no
].member_list
[set1_pos
],
705 &set2
[ring_no
].member_list
[set2_pos
],
706 sizeof(struct totem_ip_address
)) == 0) {
707 memset(&set1
[ring_no
].member_list
[set1_pos
], 0,
708 sizeof(struct totem_ip_address
));
709 memset(&set2
[ring_no
].member_list
[set2_pos
], 0,
710 sizeof(struct totem_ip_address
));
716 for (ring_no
= 0; ring_no
< INTERFACE_MAX
; ring_no
++) {
717 for (set1_pos
= 0; set1_pos
< set1
[ring_no
].member_count
; set1_pos
++) {
719 * All items which remained in set1 doesn't exists in set2 any longer so
720 * node has to be removed.
722 if (memcmp(&set1
[ring_no
].member_list
[set1_pos
], &empty_ip_address
, sizeof(empty_ip_address
)) != 0) {
723 log_printf(LOGSYS_LEVEL_DEBUG
,
724 "removing dynamic member %s for ring %u",
725 totemip_print(&set1
[ring_no
].member_list
[set1_pos
]),
728 totempg_member_remove(&set1
[ring_no
].member_list
[set1_pos
], ring_no
);
731 if (!set2
[ring_no
].configured
) {
734 for (set2_pos
= 0; set2_pos
< set2
[ring_no
].member_count
; set2_pos
++) {
736 * All items which remained in set2 doesn't existed in set1 so this is no node
737 * and has to be added.
739 if (memcmp(&set2
[ring_no
].member_list
[set2_pos
], &empty_ip_address
, sizeof(empty_ip_address
)) != 0) {
740 log_printf(LOGSYS_LEVEL_DEBUG
,
741 "adding dynamic member %s for ring %u",
742 totemip_print(&set2
[ring_no
].member_list
[set2_pos
]),
745 totempg_member_add(&set2
[ring_no
].member_list
[set2_pos
], ring_no
);
752 * Reconfigure links in totempg. Sets new local IP address and adds params for new links.
754 static void reconfigure_links(struct totem_config
*totem_config
)
757 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
759 struct totem_ip_address local_ip
;
761 unsigned int local_node_pos
= find_local_node_in_nodelist(totem_config
);
763 for (i
= 0; i
<INTERFACE_MAX
; i
++) {
764 if (!totem_config
->interfaces
[i
].configured
) {
768 log_printf(LOGSYS_LEVEL_INFO
, "Configuring link %d\n", i
);
770 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring%u_addr", local_node_pos
, i
);
771 if (icmap_get_string(tmp_key
, &addr_string
) != CS_OK
) {
775 err
= totemip_parse(&local_ip
, addr_string
, AF_UNSPEC
);
779 local_ip
.nodeid
= totem_config
->node_id
;
781 /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
782 if (!totem_config
->interfaces
[i
].knet_link_priority
)
783 totem_config
->interfaces
[i
].knet_link_priority
= 1;
784 if (!totem_config
->interfaces
[i
].knet_ping_interval
)
785 totem_config
->interfaces
[i
].knet_ping_interval
= KNET_PING_INTERVAL
;
786 if (!totem_config
->interfaces
[i
].knet_ping_timeout
)
787 totem_config
->interfaces
[i
].knet_ping_timeout
= KNET_PING_TIMEOUT
;
788 if (!totem_config
->interfaces
[i
].knet_ping_precision
)
789 totem_config
->interfaces
[i
].knet_ping_precision
= KNET_PING_PRECISION
;
790 if (!totem_config
->interfaces
[i
].knet_pong_count
)
791 totem_config
->interfaces
[i
].knet_pong_count
= KNET_PONG_COUNT
;
792 if (!totem_config
->interfaces
[i
].knet_transport
)
793 totem_config
->interfaces
[i
].knet_transport
= KNET_TRANSPORT_UDP
;
794 if (!totem_config
->interfaces
[i
].ip_port
)
795 totem_config
->interfaces
[i
].ip_port
= DEFAULT_PORT
;
797 totempg_iface_set(&local_ip
, totem_config
->interfaces
[i
].ip_port
, i
);
801 static void put_nodelist_members_to_config(struct totem_config
*totem_config
, int reload
)
803 icmap_iter_t iter
, iter2
;
804 const char *iter_key
, *iter_key2
;
806 unsigned int node_pos
;
807 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
808 char tmp_key2
[ICMAP_KEYNAME_MAXLEN
];
811 unsigned int linknumber
= 0;
813 struct totem_interface
*new_interfaces
= NULL
;
817 * We need to compute diff only for reload. Also for initial configuration
818 * not all totem structures are initialized so corosync will crash during
821 new_interfaces
= malloc (sizeof (struct totem_interface
) * INTERFACE_MAX
);
822 assert(new_interfaces
!= NULL
);
825 /* Clear out nodelist so we can put the new one in if needed */
826 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
827 for (j
= 0; j
< PROCESSOR_COUNT_MAX
; j
++) {
828 memset(&totem_config
->interfaces
[i
].member_list
[j
], 0, sizeof(struct totem_ip_address
));
830 totem_config
->interfaces
[i
].member_count
= 0;
833 iter
= icmap_iter_init("nodelist.node.");
834 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
835 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos
, tmp_key
);
840 if (strcmp(tmp_key
, "ring0_addr") != 0) {
844 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.", node_pos
);
845 iter2
= icmap_iter_init(tmp_key
);
846 while ((iter_key2
= icmap_iter_next(iter2
, NULL
, NULL
)) != NULL
) {
849 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", node_pos
);
850 if (icmap_get_uint32(tmp_key
, &nodeid
) != CS_OK
) {
853 res
= sscanf(iter_key2
, "nodelist.node.%u.ring%u%s", &node_pos
, &linknumber
, tmp_key2
);
854 if (res
!= 3 || strcmp(tmp_key2
, "_addr") != 0) {
858 if (icmap_get_string(iter_key2
, &node_addr_str
) != CS_OK
) {
862 member_count
= totem_config
->interfaces
[linknumber
].member_count
;
864 res
= totemip_parse(&totem_config
->interfaces
[linknumber
].member_list
[member_count
],
865 node_addr_str
, totem_config
->ip_version
);
867 totem_config
->interfaces
[linknumber
].member_list
[member_count
].nodeid
= nodeid
;
868 totem_config
->interfaces
[linknumber
].member_count
++;
871 totem_config
->interfaces
[linknumber
].configured
= 1;
875 icmap_iter_finalize(iter2
);
878 icmap_iter_finalize(iter
);
881 log_printf(LOGSYS_LEVEL_DEBUG
, "About to reconfigure links from nodelist.\n");
882 reconfigure_links(totem_config
);
884 memcpy(new_interfaces
, totem_config
->interfaces
, sizeof (struct totem_interface
) * INTERFACE_MAX
);
886 compute_interfaces_diff(totem_config
->orig_interfaces
, new_interfaces
);
888 free(new_interfaces
);
892 static void nodelist_dynamic_notify(
894 const char *key_name
,
895 struct icmap_notify_value new_val
,
896 struct icmap_notify_value old_val
,
900 unsigned int ring_no
;
901 unsigned int member_no
;
902 char tmp_str
[ICMAP_KEYNAME_MAXLEN
];
904 struct totem_config
*totem_config
= (struct totem_config
*)user_data
;
907 * If a full reload is in progress then don't do anything until it's done and
908 * can reconfigure it all atomically
910 if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading
) == CS_OK
&& reloading
) {
914 res
= sscanf(key_name
, "nodelist.node.%u.ring%u%s", &member_no
, &ring_no
, tmp_str
);
918 if (strcmp(tmp_str
, "_addr") != 0) {
922 put_nodelist_members_to_config(totem_config
, 1);
927 * Tries to find node (node_pos) in config nodelist which address matches any
928 * local interface. Address can be stored in ring0_addr or if ipaddr_key_prefix is not NULL
929 * key with prefix ipaddr_key is used (there can be multiuple of them)
930 * This function differs * from find_local_node_in_nodelist because it doesn't need bindnetaddr,
931 * but doesn't work when bind addr is network address (so IP must be exact
934 * Returns 1 on success (address was found, node_pos is then correctly set) or 0 on failure.
936 int totem_config_find_local_addr_in_nodelist(const char *ipaddr_key_prefix
, unsigned int *node_pos
)
938 struct qb_list_head addrs
;
939 struct totem_ip_if_address
*if_addr
;
940 icmap_iter_t iter
, iter2
;
941 const char *iter_key
, *iter_key2
;
942 struct qb_list_head
*list
;
943 const char *ipaddr_key
;
945 struct totem_ip_address node_addr
;
949 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
951 if (totemip_getifaddrs(&addrs
) == -1) {
955 ip_version
= totem_config_get_ip_version();
957 iter
= icmap_iter_init("nodelist.node.");
959 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
960 res
= sscanf(iter_key
, "nodelist.node.%u.%s", node_pos
, tmp_key
);
965 if (strcmp(tmp_key
, "ring0_addr") != 0) {
969 if (icmap_get_string(iter_key
, &node_addr_str
) != CS_OK
) {
976 * ring0_addr found -> let's iterate thru ipaddr_key_prefix
978 snprintf(tmp_key
, sizeof(tmp_key
), "nodelist.node.%u.%s", *node_pos
,
979 (ipaddr_key_prefix
!= NULL
? ipaddr_key_prefix
: "ring0_addr"));
981 iter2
= icmap_iter_init(tmp_key
);
982 while ((iter_key2
= icmap_iter_next(iter2
, NULL
, NULL
)) != NULL
) {
984 * ring0_addr must be exact match, not prefix
986 ipaddr_key
= (ipaddr_key_prefix
!= NULL
? iter_key2
: tmp_key
);
987 if (icmap_get_string(ipaddr_key
, &node_addr_str
) != CS_OK
) {
991 if (totemip_parse(&node_addr
, node_addr_str
, ip_version
) == -1) {
998 * Try to match ip with if_addrs
1001 qb_list_for_each(list
, &(addrs
)) {
1002 if_addr
= qb_list_entry(list
, struct totem_ip_if_address
, list
);
1004 if (totemip_equal(&node_addr
, &if_addr
->ip_addr
)) {
1015 icmap_iter_finalize(iter2
);
1022 icmap_iter_finalize(iter
);
1023 totemip_freeifaddrs(&addrs
);
1025 return (node_found
);
1028 static void config_convert_nodelist_to_interface(struct totem_config
*totem_config
)
1031 unsigned int node_pos
;
1032 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
1033 char tmp_key2
[ICMAP_KEYNAME_MAXLEN
];
1034 char *node_addr_str
;
1035 unsigned int linknumber
= 0;
1037 const char *iter_key
;
1039 if (totem_config_find_local_addr_in_nodelist(NULL
, &node_pos
)) {
1041 * We found node, so create interface section
1043 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.", node_pos
);
1044 iter
= icmap_iter_init(tmp_key
);
1045 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
1046 res
= sscanf(iter_key
, "nodelist.node.%u.ring%u%s", &node_pos
, &linknumber
, tmp_key2
);
1047 if (res
!= 3 || strcmp(tmp_key2
, "_addr") != 0) {
1051 if (icmap_get_string(iter_key
, &node_addr_str
) != CS_OK
) {
1055 snprintf(tmp_key2
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.bindnetaddr", linknumber
);
1056 icmap_set_string(tmp_key2
, node_addr_str
);
1057 free(node_addr_str
);
1059 icmap_iter_finalize(iter
);
1063 static int get_interface_params(struct totem_config
*totem_config
,
1064 const char **error_string
, uint64_t *warnings
,
1068 unsigned int linknumber
= 0;
1069 int member_count
= 0;
1071 icmap_iter_t iter
, member_iter
;
1072 const char *iter_key
;
1073 const char *member_iter_key
;
1074 char linknumber_key
[ICMAP_KEYNAME_MAXLEN
];
1075 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
1079 char *cluster_name
= NULL
;
1082 for (i
=0; i
<INTERFACE_MAX
; i
++) {
1083 totem_config
->interfaces
[i
].configured
= 0;
1086 if (icmap_get_string("totem.cluster_name", &cluster_name
) != CS_OK
) {
1087 cluster_name
= NULL
;
1090 iter
= icmap_iter_init("totem.interface.");
1091 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
1092 res
= sscanf(iter_key
, "totem.interface.%[^.].%s", linknumber_key
, tmp_key
);
1097 if (strcmp(tmp_key
, "bindnetaddr") != 0 && totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) {
1102 linknumber
= atoi(linknumber_key
);
1104 if (linknumber
>= INTERFACE_MAX
) {
1107 snprintf (error_string_response
, sizeof(error_string_response
),
1108 "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1109 linknumber
, INTERFACE_MAX
- 1);
1111 *error_string
= error_string_response
;
1115 /* These things are only valid for the initial read */
1118 * Get the bind net address
1120 if (icmap_get_string(iter_key
, &str
) == CS_OK
) {
1121 res
= totemip_parse (&totem_config
->interfaces
[linknumber
].bindnet
, str
,
1122 totem_config
->ip_version
);
1127 * Get interface multicast address
1129 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastaddr", linknumber
);
1130 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1131 res
= totemip_parse (&totem_config
->interfaces
[linknumber
].mcast_addr
, str
, totem_config
->ip_version
);
1135 * User not specified address -> autogenerate one from cluster_name key
1136 * (if available). Return code is intentionally ignored, because
1137 * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1138 * checked later anyway.
1140 (void)get_cluster_mcast_addr (cluster_name
,
1142 totem_config
->ip_version
,
1143 &totem_config
->interfaces
[linknumber
].mcast_addr
);
1146 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.broadcast", linknumber
);
1147 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1148 if (strcmp (str
, "yes") == 0) {
1149 totem_config
->broadcast_use
= 1;
1155 /* These things are only valid for the initial read OR a newly-defined link */
1156 if (!reload
|| (totem_config
->interfaces
[linknumber
].configured
== 0)) {
1161 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastport", linknumber
);
1162 if (icmap_get_uint16(tmp_key
, &totem_config
->interfaces
[linknumber
].ip_port
) != CS_OK
) {
1163 if (totem_config
->broadcast_use
) {
1164 totem_config
->interfaces
[linknumber
].ip_port
= DEFAULT_PORT
+ (2 * linknumber
);
1166 totem_config
->interfaces
[linknumber
].ip_port
= DEFAULT_PORT
;
1173 totem_config
->interfaces
[linknumber
].ttl
= 1;
1175 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.ttl", linknumber
);
1177 if (icmap_get_uint8(tmp_key
, &u8
) == CS_OK
) {
1178 totem_config
->interfaces
[linknumber
].ttl
= u8
;
1181 totem_config
->interfaces
[linknumber
].knet_transport
= KNET_DEFAULT_TRANSPORT
;
1182 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_transport", linknumber
);
1183 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1184 if (strcmp(str
, "sctp") == 0) {
1185 totem_config
->interfaces
[linknumber
].knet_transport
= KNET_TRANSPORT_SCTP
;
1187 else if (strcmp(str
, "udp") == 0) {
1188 totem_config
->interfaces
[linknumber
].knet_transport
= KNET_TRANSPORT_UDP
;
1191 *error_string
= "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1196 totem_config
->interfaces
[linknumber
].configured
= 1;
1199 * Get the knet link params
1201 totem_config
->interfaces
[linknumber
].knet_link_priority
= 1;
1202 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_link_priority", linknumber
);
1204 if (icmap_get_uint8(tmp_key
, &u8
) == CS_OK
) {
1205 totem_config
->interfaces
[linknumber
].knet_link_priority
= u8
;
1208 totem_config
->interfaces
[linknumber
].knet_ping_interval
= KNET_PING_INTERVAL
;
1209 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_ping_interval", linknumber
);
1210 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1211 totem_config
->interfaces
[linknumber
].knet_ping_interval
= u32
;
1213 totem_config
->interfaces
[linknumber
].knet_ping_timeout
= KNET_PING_TIMEOUT
;
1214 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_ping_timeout", linknumber
);
1215 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1216 totem_config
->interfaces
[linknumber
].knet_ping_timeout
= u32
;
1218 totem_config
->interfaces
[linknumber
].knet_ping_precision
= KNET_PING_PRECISION
;
1219 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_ping_precision", linknumber
);
1220 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1221 totem_config
->interfaces
[linknumber
].knet_ping_precision
= u32
;
1223 totem_config
->interfaces
[linknumber
].knet_pong_count
= KNET_PONG_COUNT
;
1224 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_pong_count", linknumber
);
1225 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1226 totem_config
->interfaces
[linknumber
].knet_pong_count
= u32
;
1229 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.member.", linknumber
);
1230 member_iter
= icmap_iter_init(tmp_key
);
1231 while ((member_iter_key
= icmap_iter_next(member_iter
, NULL
, NULL
)) != NULL
) {
1232 if (member_count
== 0) {
1233 if (icmap_get_string("nodelist.node.0.ring0_addr", &str
) == CS_OK
) {
1235 *warnings
|= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED
;
1238 *warnings
|= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED
;
1242 if (icmap_get_string(member_iter_key
, &str
) == CS_OK
) {
1243 res
= totemip_parse (&totem_config
->interfaces
[linknumber
].member_list
[member_count
++],
1244 str
, totem_config
->ip_version
);
1247 icmap_iter_finalize(member_iter
);
1249 totem_config
->interfaces
[linknumber
].member_count
= member_count
;
1252 icmap_iter_finalize(iter
);
1257 extern int totem_config_read (
1258 struct totem_config
*totem_config
,
1259 const char **error_string
,
1263 char *str
, *ring0_addr_str
;
1264 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
1272 memset (totem_config
, 0, sizeof (struct totem_config
));
1273 totem_config
->interfaces
= malloc (sizeof (struct totem_interface
) * INTERFACE_MAX
);
1274 if (totem_config
->interfaces
== 0) {
1275 *error_string
= "Out of memory trying to allocate ethernet interface storage area";
1279 memset (totem_config
->interfaces
, 0,
1280 sizeof (struct totem_interface
) * INTERFACE_MAX
);
1282 strcpy (totem_config
->link_mode
, "passive");
1284 icmap_get_uint32("totem.version", (uint32_t *)&totem_config
->version
);
1286 if (totem_get_crypto(totem_config
) != 0) {
1287 *error_string
= "crypto_cipher requires crypto_hash with value other than none";
1291 if (icmap_get_string("totem.link_mode", &str
) == CS_OK
) {
1292 if (strlen(str
) >= TOTEM_LINK_MODE_BYTES
) {
1293 *error_string
= "totem.link_mode is too long";
1298 strcpy (totem_config
->link_mode
, str
);
1302 icmap_get_uint32("totem.nodeid", &totem_config
->node_id
);
1304 totem_config
->clear_node_high_bit
= 0;
1305 if (icmap_get_string("totem.clear_node_high_bit", &str
) == CS_OK
) {
1306 if (strcmp (str
, "yes") == 0) {
1307 totem_config
->clear_node_high_bit
= 1;
1312 icmap_get_uint32("totem.threads", &totem_config
->threads
);
1314 icmap_get_uint32("totem.netmtu", &totem_config
->net_mtu
);
1316 totem_config
->ip_version
= totem_config_get_ip_version();
1318 if (icmap_get_string("totem.interface.0.bindnetaddr", &str
) != CS_OK
) {
1320 * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1322 config_convert_nodelist_to_interface(totem_config
);
1324 if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str
) == CS_OK
) {
1326 * Both bindnetaddr and ring0_addr are set.
1327 * Log warning information, and use nodelist instead
1329 *warnings
|= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET
;
1331 config_convert_nodelist_to_interface(totem_config
);
1333 free(ring0_addr_str
);
1340 * Broadcast option is global but set in interface section,
1341 * so reset before processing interfaces.
1343 totem_config
->broadcast_use
= 0;
1345 res
= get_interface_params(totem_config
, error_string
, warnings
, 0);
1351 * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1352 * broadcast is only supported for UDP so just do interface 0;
1354 if (totem_config
->broadcast_use
) {
1355 totemip_parse (&totem_config
->interfaces
[0].mcast_addr
,
1356 "255.255.255.255", 0);
1360 * Store automatically generated items back to icmap
1362 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
1363 if (!totem_config
->interfaces
[i
].configured
) {
1366 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastaddr", i
);
1367 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1370 str
= (char *)totemip_print(&totem_config
->interfaces
[i
].mcast_addr
);
1371 icmap_set_string(tmp_key
, str
);
1374 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastport", i
);
1375 if (icmap_get_uint16(tmp_key
, &u16
) != CS_OK
) {
1376 icmap_set_uint16(tmp_key
, totem_config
->interfaces
[i
].ip_port
);
1380 totem_config
->transport_number
= TOTEM_TRANSPORT_KNET
;
1381 if (icmap_get_string("totem.transport", &str
) == CS_OK
) {
1382 if (strcmp (str
, "udpu") == 0) {
1383 totem_config
->transport_number
= TOTEM_TRANSPORT_UDPU
;
1386 if (strcmp (str
, "udp") == 0) {
1387 totem_config
->transport_number
= TOTEM_TRANSPORT_UDP
;
1390 if (strcmp (str
, "knet") == 0) {
1391 totem_config
->transport_number
= TOTEM_TRANSPORT_KNET
;
1398 * Check existence of nodelist
1400 if (icmap_get_string("nodelist.node.0.ring0_addr", &str
) == CS_OK
) {
1405 local_node_pos
= find_local_node_in_nodelist(totem_config
);
1406 if (local_node_pos
!= -1) {
1407 icmap_set_uint32("nodelist.local_node_pos", local_node_pos
);
1409 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", local_node_pos
);
1411 nodeid_set
= (totem_config
->node_id
!= 0);
1412 if (icmap_get_uint32(tmp_key
, &totem_config
->node_id
) == CS_OK
&& nodeid_set
) {
1413 *warnings
|= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED
;
1415 if ((totem_config
->transport_number
== TOTEM_TRANSPORT_KNET
) && (!totem_config
->node_id
)) {
1416 *error_string
= "With knet, you must specify nodeid for current node";
1421 * Make localnode ring0_addr read only, so we can be sure that local
1422 * node never changes. If rebinding to other IP would be in future
1423 * supported, this must be changed and handled properly!
1425 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", local_node_pos
);
1426 icmap_set_ro_access(tmp_key
, 0, 1);
1427 icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1430 put_nodelist_members_to_config(totem_config
, 0);
1434 * Get things that might change in the future (and can depend on totem_config->interfaces);
1436 totem_volatile_config_read(totem_config
, NULL
);
1438 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1440 add_totem_config_notification(totem_config
);
1446 int totem_config_validate (
1447 struct totem_config
*totem_config
,
1448 const char **error_string
)
1450 static char local_error_reason
[512];
1451 char parse_error
[512];
1452 const char *error_reason
= local_error_reason
;
1454 int num_configured
= 0;
1455 unsigned int interface_max
= INTERFACE_MAX
;
1459 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
1460 if (totem_config
->interfaces
[i
].configured
) {
1464 if (num_configured
== 0) {
1465 error_reason
= "No interfaces defined";
1469 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
1471 * Some error checking of parsed data to make sure its valid
1474 struct totem_ip_address null_addr
;
1476 if (!totem_config
->interfaces
[i
].configured
) {
1480 memset (&null_addr
, 0, sizeof (struct totem_ip_address
));
1482 if ((totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) &&
1483 memcmp (&totem_config
->interfaces
[i
].mcast_addr
, &null_addr
,
1484 sizeof (struct totem_ip_address
)) == 0) {
1485 error_reason
= "No multicast address specified";
1489 if (totem_config
->interfaces
[i
].ip_port
== 0) {
1490 error_reason
= "No multicast port specified";
1494 if (totem_config
->interfaces
[i
].ttl
> 255) {
1495 error_reason
= "Invalid TTL (should be 0..255)";
1498 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_UDP
&&
1499 totem_config
->interfaces
[i
].ttl
!= 1) {
1500 error_reason
= "Can only set ttl on multicast transport types";
1503 if (totem_config
->interfaces
[i
].knet_link_priority
> 255) {
1504 error_reason
= "Invalid link priority (should be 0..255)";
1507 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_KNET
&&
1508 totem_config
->interfaces
[i
].knet_link_priority
!= 1) {
1509 error_reason
= "Can only set link priority on knet transport type";
1513 if (totem_config
->interfaces
[i
].mcast_addr
.family
== AF_INET6
&&
1514 totem_config
->node_id
== 0) {
1516 error_reason
= "An IPV6 network requires that a node ID be specified.";
1520 if (totem_config
->broadcast_use
== 0 && totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) {
1521 if (totem_config
->interfaces
[i
].mcast_addr
.family
!= totem_config
->interfaces
[i
].bindnet
.family
) {
1522 error_reason
= "Multicast address family does not match bind address family";
1526 if (totemip_is_mcast (&totem_config
->interfaces
[i
].mcast_addr
) != 0) {
1527 error_reason
= "mcastaddr is not a correct multicast address.";
1532 if (totem_config
->interfaces
[0].bindnet
.family
!= totem_config
->interfaces
[i
].bindnet
.family
) {
1533 error_reason
= "Not all bind address belong to the same IP family";
1538 if (totem_config
->version
!= 2) {
1539 error_reason
= "This totem parser can only parse version 2 configurations.";
1543 if (totem_volatile_config_validate(totem_config
, error_string
) == -1) {
1547 if (check_for_duplicate_nodeids(totem_config
, error_string
) == -1) {
1552 * KNET Link values validation
1554 if (strcmp (totem_config
->link_mode
, "active") &&
1555 strcmp (totem_config
->link_mode
, "rr") &&
1556 strcmp (totem_config
->link_mode
, "passive")) {
1557 snprintf (local_error_reason
, sizeof(local_error_reason
),
1558 "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config
->link_mode
);
1562 /* Only Knet does multiple interfaces */
1563 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_KNET
) {
1567 if (interface_max
< num_configured
) {
1568 snprintf (parse_error
, sizeof(parse_error
),
1569 "%d is too many configured interfaces for non-Knet transport.",
1571 error_reason
= parse_error
;
1575 /* Only knet allows crypto */
1576 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_KNET
) {
1577 if ((strcmp(totem_config
->crypto_cipher_type
, "none") != 0) ||
1578 (strcmp(totem_config
->crypto_hash_type
, "none") != 0)) {
1580 snprintf (parse_error
, sizeof(parse_error
),
1581 "crypto_cipher & crypto_hash are only valid for the Knet transport.");
1582 error_reason
= parse_error
;
1587 if (totem_config
->net_mtu
== 0) {
1588 if (totem_config
->transport_number
== TOTEM_TRANSPORT_KNET
) {
1589 totem_config
->net_mtu
= KNET_MAX_PACKET_SIZE
;
1592 totem_config
->net_mtu
= 1500;
1599 snprintf (error_string_response
, sizeof(error_string_response
),
1600 "parse error in config: %s\n", error_reason
);
1601 *error_string
= error_string_response
;
1606 static int read_keyfile (
1607 const char *key_location
,
1608 struct totem_config
*totem_config
,
1609 const char **error_string
)
1614 char error_str
[100];
1615 const char *error_ptr
;
1617 fd
= open (key_location
, O_RDONLY
);
1619 error_ptr
= qb_strerror_r(errno
, error_str
, sizeof(error_str
));
1620 snprintf (error_string_response
, sizeof(error_string_response
),
1621 "Could not open %s: %s\n",
1622 key_location
, error_ptr
);
1626 res
= read (fd
, totem_config
->private_key
, TOTEM_PRIVATE_KEY_LEN_MAX
);
1627 saved_errno
= errno
;
1631 error_ptr
= qb_strerror_r (saved_errno
, error_str
, sizeof(error_str
));
1632 snprintf (error_string_response
, sizeof(error_string_response
),
1633 "Could not read %s: %s\n",
1634 key_location
, error_ptr
);
1638 if (res
< TOTEM_PRIVATE_KEY_LEN_MIN
) {
1639 snprintf (error_string_response
, sizeof(error_string_response
),
1640 "Could only read %d bits of minimum %u bits from %s.\n",
1641 res
* 8, TOTEM_PRIVATE_KEY_LEN_MIN
* 8, key_location
);
1645 totem_config
->private_key_len
= res
;
1650 *error_string
= error_string_response
;
1654 int totem_config_keyread (
1655 struct totem_config
*totem_config
,
1656 const char **error_string
)
1659 char *key_location
= NULL
;
1663 memset (totem_config
->private_key
, 0, sizeof(totem_config
->private_key
));
1664 totem_config
->private_key_len
= 0;
1666 if (strcmp(totem_config
->crypto_cipher_type
, "none") == 0 &&
1667 strcmp(totem_config
->crypto_hash_type
, "none") == 0) {
1671 /* cmap may store the location of the key file */
1672 if (icmap_get_string("totem.keyfile", &key_location
) == CS_OK
) {
1673 res
= read_keyfile(key_location
, totem_config
, error_string
);
1679 } else { /* Or the key itself may be in the cmap */
1680 if (icmap_get("totem.key", NULL
, &key_len
, NULL
) == CS_OK
) {
1681 if (key_len
> sizeof(totem_config
->private_key
)) {
1682 sprintf(error_string_response
, "key is too long");
1685 if (key_len
< TOTEM_PRIVATE_KEY_LEN_MIN
) {
1686 sprintf(error_string_response
, "key is too short");
1689 if (icmap_get("totem.key", totem_config
->private_key
, &key_len
, NULL
) == CS_OK
) {
1690 totem_config
->private_key_len
= key_len
;
1693 sprintf(error_string_response
, "can't load private key");
1699 /* In desperation we read the default filename */
1701 const char *filename
= getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
1703 filename
= COROSYSCONFDIR
"/authkey";
1704 res
= read_keyfile(filename
, totem_config
, error_string
);
1713 *error_string
= error_string_response
;
1718 static void debug_dump_totem_config(const struct totem_config
*totem_config
)
1721 log_printf(LOGSYS_LEVEL_DEBUG
, "Token Timeout (%d ms) retransmit timeout (%d ms)",
1722 totem_config
->token_timeout
, totem_config
->token_retransmit_timeout
);
1723 log_printf(LOGSYS_LEVEL_DEBUG
, "token hold (%d ms) retransmits before loss (%d retrans)",
1724 totem_config
->token_hold_timeout
, totem_config
->token_retransmits_before_loss_const
);
1725 log_printf(LOGSYS_LEVEL_DEBUG
, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
1726 totem_config
->join_timeout
, totem_config
->send_join_timeout
, totem_config
->consensus_timeout
,
1727 totem_config
->merge_timeout
);
1728 log_printf(LOGSYS_LEVEL_DEBUG
, "downcheck (%d ms) fail to recv const (%d msgs)",
1729 totem_config
->downcheck_timeout
, totem_config
->fail_to_recv_const
);
1730 log_printf(LOGSYS_LEVEL_DEBUG
,
1731 "seqno unchanged const (%d rotations) Maximum network MTU %d",
1732 totem_config
->seqno_unchanged_const
, totem_config
->net_mtu
);
1733 log_printf(LOGSYS_LEVEL_DEBUG
,
1734 "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
1735 totem_config
->window_size
, totem_config
->max_messages
);
1736 log_printf(LOGSYS_LEVEL_DEBUG
, "missed count const (%d messages)", totem_config
->miss_count_const
);
1737 log_printf(LOGSYS_LEVEL_DEBUG
, "heartbeat_failures_allowed (%d)",
1738 totem_config
->heartbeat_failures_allowed
);
1739 log_printf(LOGSYS_LEVEL_DEBUG
, "max_network_delay (%d ms)", totem_config
->max_network_delay
);
1742 static void totem_change_notify(
1744 const char *key_name
,
1745 struct icmap_notify_value new_val
,
1746 struct icmap_notify_value old_val
,
1749 struct totem_config
*totem_config
= (struct totem_config
*)user_data
;
1752 const char *deleted_key
= NULL
;
1753 const char *error_string
;
1756 * If a full reload is in progress then don't do anything until it's done and
1757 * can reconfigure it all atomically
1759 if (icmap_get_uint8("config.reload_in_progress", &reloading
) == CS_OK
&& reloading
)
1762 param
= totem_get_param_by_name((struct totem_config
*)user_data
, key_name
);
1764 * Process change only if changed key is found in totem_config (-> param is not NULL)
1765 * or for special key token_coefficient. token_coefficient key is not stored in
1766 * totem_config, but it is used for computation of token timeout.
1768 if (!param
&& strcmp(key_name
, "totem.token_coefficient") != 0)
1772 * Values other than UINT32 are not supported, or needed (yet)
1775 case ICMAP_TRACK_DELETE
:
1776 deleted_key
= key_name
;
1778 case ICMAP_TRACK_ADD
:
1779 case ICMAP_TRACK_MODIFY
:
1786 totem_volatile_config_read (totem_config
, deleted_key
);
1787 log_printf(LOGSYS_LEVEL_DEBUG
, "Totem related config key changed. Dumping actual totem config.");
1788 debug_dump_totem_config(totem_config
);
1789 if (totem_volatile_config_validate(totem_config
, &error_string
) == -1) {
1790 log_printf (LOGSYS_LEVEL_ERROR
, "%s", error_string
);
1792 * TODO: Consider corosync exit and/or load defaults for volatile
1793 * values. For now, log error seems to be enough
1798 static void totem_reload_notify(
1800 const char *key_name
,
1801 struct icmap_notify_value new_val
,
1802 struct icmap_notify_value old_val
,
1805 struct totem_config
*totem_config
= (struct totem_config
*)user_data
;
1806 uint32_t local_node_pos
;
1807 const char *error_string
;
1810 /* Reload has completed */
1811 if (*(uint8_t *)new_val
.data
== 0) {
1813 totem_config
->orig_interfaces
= malloc (sizeof (struct totem_interface
) * INTERFACE_MAX
);
1814 assert(totem_config
->orig_interfaces
!= NULL
);
1815 memcpy(totem_config
->orig_interfaces
, totem_config
->interfaces
, sizeof (struct totem_interface
) * INTERFACE_MAX
);
1817 get_interface_params(totem_config
, &error_string
, &warnings
, 1);
1818 put_nodelist_members_to_config (totem_config
, 1);
1819 totem_volatile_config_read (totem_config
, NULL
);
1820 log_printf(LOGSYS_LEVEL_DEBUG
, "Configuration reloaded. Dumping actual totem config.");
1821 debug_dump_totem_config(totem_config
);
1822 if (totem_volatile_config_validate(totem_config
, &error_string
) == -1) {
1823 log_printf (LOGSYS_LEVEL_ERROR
, "%s", error_string
);
1825 * TODO: Consider corosync exit and/or load defaults for volatile
1826 * values. For now, log error seems to be enough
1830 /* Reinstate the local_node_pos */
1831 local_node_pos
= find_local_node_in_nodelist(totem_config
);
1832 if (local_node_pos
!= -1) {
1833 icmap_set_uint32("nodelist.local_node_pos", local_node_pos
);
1836 /* Reconfigure network params as appropriate */
1837 totempg_reconfigure();
1839 free(totem_config
->orig_interfaces
);
1841 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1843 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
1847 static void add_totem_config_notification(struct totem_config
*totem_config
)
1849 icmap_track_t icmap_track
;
1851 icmap_track_add("totem.",
1852 ICMAP_TRACK_ADD
| ICMAP_TRACK_DELETE
| ICMAP_TRACK_MODIFY
| ICMAP_TRACK_PREFIX
,
1853 totem_change_notify
,
1857 icmap_track_add("config.reload_in_progress",
1858 ICMAP_TRACK_ADD
| ICMAP_TRACK_MODIFY
,
1859 totem_reload_notify
,
1863 icmap_track_add("nodelist.node.",
1864 ICMAP_TRACK_ADD
| ICMAP_TRACK_DELETE
| ICMAP_TRACK_MODIFY
| ICMAP_TRACK_PREFIX
,
1865 nodelist_dynamic_notify
,
1866 (void *)totem_config
,