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
, "3des") == 0) {
407 if (icmap_get_string("totem.crypto_hash", &str
) == CS_OK
) {
408 if (strcmp(str
, "none") == 0) {
411 if (strcmp(str
, "md5") == 0) {
414 if (strcmp(str
, "sha1") == 0) {
417 if (strcmp(str
, "sha256") == 0) {
420 if (strcmp(str
, "sha384") == 0) {
423 if (strcmp(str
, "sha512") == 0) {
429 if ((strcmp(tmp_cipher
, "none") != 0) &&
430 (strcmp(tmp_hash
, "none") == 0)) {
434 free(totem_config
->crypto_cipher_type
);
435 free(totem_config
->crypto_hash_type
);
437 totem_config
->crypto_cipher_type
= strdup(tmp_cipher
);
438 totem_config
->crypto_hash_type
= strdup(tmp_hash
);
443 static int totem_config_get_ip_version(void)
449 if (icmap_get_string("totem.ip_version", &str
) == CS_OK
) {
450 if (strcmp(str
, "ipv4") == 0) {
453 if (strcmp(str
, "ipv6") == 0) {
462 static uint16_t generate_cluster_id (const char *cluster_name
)
467 for (i
= 0; i
< strlen(cluster_name
); i
++) {
469 value
+= cluster_name
[i
];
472 return (value
& 0xFFFF);
475 static int get_cluster_mcast_addr (
476 const char *cluster_name
,
477 unsigned int linknumber
,
479 struct totem_ip_address
*res
)
482 char addr
[INET6_ADDRSTRLEN
+ 1];
485 if (cluster_name
== NULL
) {
489 clusterid
= generate_cluster_id(cluster_name
) + linknumber
;
490 memset (res
, 0, sizeof(*res
));
492 switch (ip_version
) {
494 snprintf(addr
, sizeof(addr
), "239.192.%d.%d", clusterid
>> 8, clusterid
% 0xFF);
497 snprintf(addr
, sizeof(addr
), "ff15::%x", clusterid
);
506 err
= totemip_parse (res
, addr
, ip_version
);
511 static unsigned int generate_nodeid_for_duplicate_test(
512 struct totem_config
*totem_config
,
516 struct totem_ip_address totemip
;
518 /* AF_INET hard-coded here because auto-generated nodeids
520 if (totemip_parse(&totemip
, addr
, AF_INET
) != 0)
523 memcpy (&nodeid
, &totemip
.addr
, sizeof (unsigned int));
525 #if __BYTE_ORDER == __LITTLE_ENDIAN
526 nodeid
= swab32 (nodeid
);
529 if (totem_config
->clear_node_high_bit
) {
530 nodeid
&= 0x7FFFFFFF;
535 static int check_for_duplicate_nodeids(
536 struct totem_config
*totem_config
,
537 const char **error_string
)
540 icmap_iter_t subiter
;
541 const char *iter_key
;
544 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
545 char *ring0_addr
=NULL
;
546 char *ring0_addr1
=NULL
;
547 unsigned int node_pos
;
548 unsigned int node_pos1
;
550 unsigned int nodeid1
;
553 iter
= icmap_iter_init("nodelist.node.");
554 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
555 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos
, tmp_key
);
560 if (strcmp(tmp_key
, "ring0_addr") != 0) {
564 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", node_pos
);
566 if (icmap_get_uint32(tmp_key
, &nodeid
) != CS_OK
) {
568 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", node_pos
);
569 if (icmap_get_string(tmp_key
, &ring0_addr
) != CS_OK
) {
573 /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
574 nodeid
= generate_nodeid_for_duplicate_test(totem_config
, ring0_addr
);
582 subiter
= icmap_iter_init("nodelist.node.");
583 while (((iter_key
= icmap_iter_next(subiter
, NULL
, NULL
)) != NULL
) && (node_pos1
< node_pos
)) {
584 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos1
, tmp_key
);
585 if ((res
!= 2) || (node_pos1
>= node_pos
)) {
589 if (strcmp(tmp_key
, "ring0_addr") != 0) {
593 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", node_pos1
);
594 if (icmap_get_uint32(tmp_key
, &nodeid1
) != CS_OK
) {
596 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", node_pos1
);
597 if (icmap_get_string(tmp_key
, &ring0_addr1
) != CS_OK
) {
600 nodeid1
= generate_nodeid_for_duplicate_test(totem_config
, ring0_addr1
);
606 if (nodeid
== nodeid1
) {
608 snprintf (error_string_response
, sizeof(error_string_response
),
609 "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid
,
610 autogenerated
?"(autogenerated from ":"",
611 autogenerated
?ring0_addr
:"",
612 autogenerated
?")":"");
613 log_printf (LOGSYS_LEVEL_ERROR
, error_string_response
);
614 *error_string
= error_string_response
;
618 icmap_iter_finalize(subiter
);
620 icmap_iter_finalize(iter
);
625 static int find_local_node_in_nodelist(struct totem_config
*totem_config
)
628 const char *iter_key
;
630 unsigned int node_pos
;
631 int local_node_pos
= -1;
632 struct totem_ip_address bind_addr
;
633 int interface_up
, interface_num
;
634 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
636 struct totem_ip_address node_addr
;
638 res
= totemip_iface_check(&totem_config
->interfaces
[0].bindnet
,
639 &bind_addr
, &interface_up
, &interface_num
,
640 totem_config
->clear_node_high_bit
);
645 iter
= icmap_iter_init("nodelist.node.");
646 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
647 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos
, tmp_key
);
652 if (strcmp(tmp_key
, "ring0_addr") != 0) {
656 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", node_pos
);
657 if (icmap_get_string(tmp_key
, &node_addr_str
) != CS_OK
) {
661 res
= totemip_parse (&node_addr
, node_addr_str
, totem_config
->ip_version
);
667 if (totemip_equal(&bind_addr
, &node_addr
)) {
668 local_node_pos
= node_pos
;
671 icmap_iter_finalize(iter
);
673 return (local_node_pos
);
677 * Compute difference between two set of totem interface arrays. set1 and set2
678 * are changed so for same ring, ip existing in both set1 and set2 are cleared
679 * (set to 0), and ips which are only in set1 or set2 remains untouched.
680 * totempg_node_add/remove is called.
682 static void compute_interfaces_diff(struct totem_interface
*set1
,
683 struct totem_interface
*set2
)
685 int ring_no
, set1_pos
, set2_pos
;
686 struct totem_ip_address empty_ip_address
;
688 memset(&empty_ip_address
, 0, sizeof(empty_ip_address
));
690 for (ring_no
= 0; ring_no
< INTERFACE_MAX
; ring_no
++) {
691 if (!set1
[ring_no
].configured
&& !set2
[ring_no
].configured
) {
695 for (set1_pos
= 0; set1_pos
< set1
[ring_no
].member_count
; set1_pos
++) {
696 for (set2_pos
= 0; set2_pos
< set2
[ring_no
].member_count
; set2_pos
++) {
698 * For current ring_no remove all set1 items existing
701 if (memcmp(&set1
[ring_no
].member_list
[set1_pos
],
702 &set2
[ring_no
].member_list
[set2_pos
],
703 sizeof(struct totem_ip_address
)) == 0) {
704 memset(&set1
[ring_no
].member_list
[set1_pos
], 0,
705 sizeof(struct totem_ip_address
));
706 memset(&set2
[ring_no
].member_list
[set2_pos
], 0,
707 sizeof(struct totem_ip_address
));
713 for (ring_no
= 0; ring_no
< INTERFACE_MAX
; ring_no
++) {
714 for (set1_pos
= 0; set1_pos
< set1
[ring_no
].member_count
; set1_pos
++) {
716 * All items which remained in set1 doesn't exists in set2 any longer so
717 * node has to be removed.
719 if (memcmp(&set1
[ring_no
].member_list
[set1_pos
], &empty_ip_address
, sizeof(empty_ip_address
)) != 0) {
720 log_printf(LOGSYS_LEVEL_DEBUG
,
721 "removing dynamic member %s for ring %u",
722 totemip_print(&set1
[ring_no
].member_list
[set1_pos
]),
725 totempg_member_remove(&set1
[ring_no
].member_list
[set1_pos
], ring_no
);
728 if (!set2
[ring_no
].configured
) {
731 for (set2_pos
= 0; set2_pos
< set2
[ring_no
].member_count
; set2_pos
++) {
733 * All items which remained in set2 doesn't existed in set1 so this is no node
734 * and has to be added.
736 if (memcmp(&set2
[ring_no
].member_list
[set2_pos
], &empty_ip_address
, sizeof(empty_ip_address
)) != 0) {
737 log_printf(LOGSYS_LEVEL_DEBUG
,
738 "adding dynamic member %s for ring %u",
739 totemip_print(&set2
[ring_no
].member_list
[set2_pos
]),
742 totempg_member_add(&set2
[ring_no
].member_list
[set2_pos
], ring_no
);
749 * Reconfigure links in totempg. Sets new local IP address and adds params for new links.
751 static void reconfigure_links(struct totem_config
*totem_config
)
754 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
756 struct totem_ip_address local_ip
;
758 unsigned int local_node_pos
= find_local_node_in_nodelist(totem_config
);
760 for (i
= 0; i
<INTERFACE_MAX
; i
++) {
761 if (!totem_config
->interfaces
[i
].configured
) {
765 log_printf(LOGSYS_LEVEL_INFO
, "Configuring link %d\n", i
);
767 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring%u_addr", local_node_pos
, i
);
768 if (icmap_get_string(tmp_key
, &addr_string
) != CS_OK
) {
772 err
= totemip_parse(&local_ip
, addr_string
, AF_UNSPEC
);
776 local_ip
.nodeid
= totem_config
->node_id
;
778 /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
779 if (!totem_config
->interfaces
[i
].knet_link_priority
)
780 totem_config
->interfaces
[i
].knet_link_priority
= 1;
781 if (!totem_config
->interfaces
[i
].knet_ping_interval
)
782 totem_config
->interfaces
[i
].knet_ping_interval
= KNET_PING_INTERVAL
;
783 if (!totem_config
->interfaces
[i
].knet_ping_timeout
)
784 totem_config
->interfaces
[i
].knet_ping_timeout
= KNET_PING_TIMEOUT
;
785 if (!totem_config
->interfaces
[i
].knet_ping_precision
)
786 totem_config
->interfaces
[i
].knet_ping_precision
= KNET_PING_PRECISION
;
787 if (!totem_config
->interfaces
[i
].knet_pong_count
)
788 totem_config
->interfaces
[i
].knet_pong_count
= KNET_PONG_COUNT
;
789 if (!totem_config
->interfaces
[i
].knet_transport
)
790 totem_config
->interfaces
[i
].knet_transport
= KNET_TRANSPORT_UDP
;
791 if (!totem_config
->interfaces
[i
].ip_port
)
792 totem_config
->interfaces
[i
].ip_port
= DEFAULT_PORT
;
794 totempg_iface_set(&local_ip
, totem_config
->interfaces
[i
].ip_port
, i
);
798 static void put_nodelist_members_to_config(struct totem_config
*totem_config
, int reload
)
800 icmap_iter_t iter
, iter2
;
801 const char *iter_key
, *iter_key2
;
803 unsigned int node_pos
;
804 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
805 char tmp_key2
[ICMAP_KEYNAME_MAXLEN
];
808 unsigned int linknumber
= 0;
810 struct totem_interface
*new_interfaces
= NULL
;
814 * We need to compute diff only for reload. Also for initial configuration
815 * not all totem structures are initialized so corosync will crash during
818 new_interfaces
= malloc (sizeof (struct totem_interface
) * INTERFACE_MAX
);
819 assert(new_interfaces
!= NULL
);
822 /* Clear out nodelist so we can put the new one in if needed */
823 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
824 for (j
= 0; j
< PROCESSOR_COUNT_MAX
; j
++) {
825 memset(&totem_config
->interfaces
[i
].member_list
[j
], 0, sizeof(struct totem_ip_address
));
827 totem_config
->interfaces
[i
].member_count
= 0;
830 iter
= icmap_iter_init("nodelist.node.");
831 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
832 res
= sscanf(iter_key
, "nodelist.node.%u.%s", &node_pos
, tmp_key
);
837 if (strcmp(tmp_key
, "ring0_addr") != 0) {
841 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.", node_pos
);
842 iter2
= icmap_iter_init(tmp_key
);
843 while ((iter_key2
= icmap_iter_next(iter2
, NULL
, NULL
)) != NULL
) {
846 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", node_pos
);
847 if (icmap_get_uint32(tmp_key
, &nodeid
) != CS_OK
) {
850 res
= sscanf(iter_key2
, "nodelist.node.%u.ring%u%s", &node_pos
, &linknumber
, tmp_key2
);
851 if (res
!= 3 || strcmp(tmp_key2
, "_addr") != 0) {
855 if (icmap_get_string(iter_key2
, &node_addr_str
) != CS_OK
) {
859 member_count
= totem_config
->interfaces
[linknumber
].member_count
;
861 res
= totemip_parse(&totem_config
->interfaces
[linknumber
].member_list
[member_count
],
862 node_addr_str
, totem_config
->ip_version
);
864 totem_config
->interfaces
[linknumber
].member_list
[member_count
].nodeid
= nodeid
;
865 totem_config
->interfaces
[linknumber
].member_count
++;
868 totem_config
->interfaces
[linknumber
].configured
= 1;
872 icmap_iter_finalize(iter2
);
875 icmap_iter_finalize(iter
);
878 log_printf(LOGSYS_LEVEL_DEBUG
, "About to reconfigure links from nodelist.\n");
879 reconfigure_links(totem_config
);
881 memcpy(new_interfaces
, totem_config
->interfaces
, sizeof (struct totem_interface
) * INTERFACE_MAX
);
883 compute_interfaces_diff(totem_config
->orig_interfaces
, new_interfaces
);
885 free(new_interfaces
);
889 static void nodelist_dynamic_notify(
891 const char *key_name
,
892 struct icmap_notify_value new_val
,
893 struct icmap_notify_value old_val
,
897 unsigned int ring_no
;
898 unsigned int member_no
;
899 char tmp_str
[ICMAP_KEYNAME_MAXLEN
];
901 struct totem_config
*totem_config
= (struct totem_config
*)user_data
;
904 * If a full reload is in progress then don't do anything until it's done and
905 * can reconfigure it all atomically
907 if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading
) == CS_OK
&& reloading
) {
911 res
= sscanf(key_name
, "nodelist.node.%u.ring%u%s", &member_no
, &ring_no
, tmp_str
);
915 if (strcmp(tmp_str
, "_addr") != 0) {
919 put_nodelist_members_to_config(totem_config
, 1);
924 * Tries to find node (node_pos) in config nodelist which address matches any
925 * local interface. Address can be stored in ring0_addr or if ipaddr_key_prefix is not NULL
926 * key with prefix ipaddr_key is used (there can be multiuple of them)
927 * This function differs * from find_local_node_in_nodelist because it doesn't need bindnetaddr,
928 * but doesn't work when bind addr is network address (so IP must be exact
931 * Returns 1 on success (address was found, node_pos is then correctly set) or 0 on failure.
933 int totem_config_find_local_addr_in_nodelist(const char *ipaddr_key_prefix
, unsigned int *node_pos
)
935 struct qb_list_head addrs
;
936 struct totem_ip_if_address
*if_addr
;
937 icmap_iter_t iter
, iter2
;
938 const char *iter_key
, *iter_key2
;
939 struct qb_list_head
*list
;
940 const char *ipaddr_key
;
942 struct totem_ip_address node_addr
;
946 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
948 if (totemip_getifaddrs(&addrs
) == -1) {
952 ip_version
= totem_config_get_ip_version();
954 iter
= icmap_iter_init("nodelist.node.");
956 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
957 res
= sscanf(iter_key
, "nodelist.node.%u.%s", node_pos
, tmp_key
);
962 if (strcmp(tmp_key
, "ring0_addr") != 0) {
966 if (icmap_get_string(iter_key
, &node_addr_str
) != CS_OK
) {
973 * ring0_addr found -> let's iterate thru ipaddr_key_prefix
975 snprintf(tmp_key
, sizeof(tmp_key
), "nodelist.node.%u.%s", *node_pos
,
976 (ipaddr_key_prefix
!= NULL
? ipaddr_key_prefix
: "ring0_addr"));
978 iter2
= icmap_iter_init(tmp_key
);
979 while ((iter_key2
= icmap_iter_next(iter2
, NULL
, NULL
)) != NULL
) {
981 * ring0_addr must be exact match, not prefix
983 ipaddr_key
= (ipaddr_key_prefix
!= NULL
? iter_key2
: tmp_key
);
984 if (icmap_get_string(ipaddr_key
, &node_addr_str
) != CS_OK
) {
988 if (totemip_parse(&node_addr
, node_addr_str
, ip_version
) == -1) {
995 * Try to match ip with if_addrs
998 qb_list_for_each(list
, &(addrs
)) {
999 if_addr
= qb_list_entry(list
, struct totem_ip_if_address
, list
);
1001 if (totemip_equal(&node_addr
, &if_addr
->ip_addr
)) {
1012 icmap_iter_finalize(iter2
);
1019 icmap_iter_finalize(iter
);
1020 totemip_freeifaddrs(&addrs
);
1022 return (node_found
);
1025 static void config_convert_nodelist_to_interface(struct totem_config
*totem_config
)
1028 unsigned int node_pos
;
1029 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
1030 char tmp_key2
[ICMAP_KEYNAME_MAXLEN
];
1031 char *node_addr_str
;
1032 unsigned int linknumber
= 0;
1034 const char *iter_key
;
1036 if (totem_config_find_local_addr_in_nodelist(NULL
, &node_pos
)) {
1038 * We found node, so create interface section
1040 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.", node_pos
);
1041 iter
= icmap_iter_init(tmp_key
);
1042 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
1043 res
= sscanf(iter_key
, "nodelist.node.%u.ring%u%s", &node_pos
, &linknumber
, tmp_key2
);
1044 if (res
!= 3 || strcmp(tmp_key2
, "_addr") != 0) {
1048 if (icmap_get_string(iter_key
, &node_addr_str
) != CS_OK
) {
1052 snprintf(tmp_key2
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.bindnetaddr", linknumber
);
1053 icmap_set_string(tmp_key2
, node_addr_str
);
1054 free(node_addr_str
);
1056 icmap_iter_finalize(iter
);
1060 static int get_interface_params(struct totem_config
*totem_config
,
1061 const char **error_string
, uint64_t *warnings
,
1065 unsigned int linknumber
= 0;
1066 int member_count
= 0;
1068 icmap_iter_t iter
, member_iter
;
1069 const char *iter_key
;
1070 const char *member_iter_key
;
1071 char linknumber_key
[ICMAP_KEYNAME_MAXLEN
];
1072 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
1076 char *cluster_name
= NULL
;
1079 for (i
=0; i
<INTERFACE_MAX
; i
++) {
1080 totem_config
->interfaces
[i
].configured
= 0;
1083 if (icmap_get_string("totem.cluster_name", &cluster_name
) != CS_OK
) {
1084 cluster_name
= NULL
;
1087 iter
= icmap_iter_init("totem.interface.");
1088 while ((iter_key
= icmap_iter_next(iter
, NULL
, NULL
)) != NULL
) {
1089 res
= sscanf(iter_key
, "totem.interface.%[^.].%s", linknumber_key
, tmp_key
);
1094 if (strcmp(tmp_key
, "bindnetaddr") != 0 && totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) {
1099 linknumber
= atoi(linknumber_key
);
1101 if (linknumber
>= INTERFACE_MAX
) {
1104 snprintf (error_string_response
, sizeof(error_string_response
),
1105 "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1106 linknumber
, INTERFACE_MAX
- 1);
1108 *error_string
= error_string_response
;
1112 /* These things are only valid for the initial read */
1115 * Get the bind net address
1117 if (icmap_get_string(iter_key
, &str
) == CS_OK
) {
1118 res
= totemip_parse (&totem_config
->interfaces
[linknumber
].bindnet
, str
,
1119 totem_config
->ip_version
);
1124 * Get interface multicast address
1126 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastaddr", linknumber
);
1127 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1128 res
= totemip_parse (&totem_config
->interfaces
[linknumber
].mcast_addr
, str
, totem_config
->ip_version
);
1132 * User not specified address -> autogenerate one from cluster_name key
1133 * (if available). Return code is intentionally ignored, because
1134 * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1135 * checked later anyway.
1137 (void)get_cluster_mcast_addr (cluster_name
,
1139 totem_config
->ip_version
,
1140 &totem_config
->interfaces
[linknumber
].mcast_addr
);
1143 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.broadcast", linknumber
);
1144 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1145 if (strcmp (str
, "yes") == 0) {
1146 totem_config
->broadcast_use
= 1;
1152 /* These things are only valid for the initial read OR a newly-defined link */
1153 if (!reload
|| (totem_config
->interfaces
[linknumber
].configured
== 0)) {
1158 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastport", linknumber
);
1159 if (icmap_get_uint16(tmp_key
, &totem_config
->interfaces
[linknumber
].ip_port
) != CS_OK
) {
1160 if (totem_config
->broadcast_use
) {
1161 totem_config
->interfaces
[linknumber
].ip_port
= DEFAULT_PORT
+ (2 * linknumber
);
1163 totem_config
->interfaces
[linknumber
].ip_port
= DEFAULT_PORT
;
1170 totem_config
->interfaces
[linknumber
].ttl
= 1;
1172 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.ttl", linknumber
);
1174 if (icmap_get_uint8(tmp_key
, &u8
) == CS_OK
) {
1175 totem_config
->interfaces
[linknumber
].ttl
= u8
;
1178 totem_config
->interfaces
[linknumber
].knet_transport
= KNET_DEFAULT_TRANSPORT
;
1179 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_transport", linknumber
);
1180 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1181 if (strcmp(str
, "sctp") == 0) {
1182 totem_config
->interfaces
[linknumber
].knet_transport
= KNET_TRANSPORT_SCTP
;
1184 else if (strcmp(str
, "udp") == 0) {
1185 totem_config
->interfaces
[linknumber
].knet_transport
= KNET_TRANSPORT_UDP
;
1188 *error_string
= "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1193 totem_config
->interfaces
[linknumber
].configured
= 1;
1196 * Get the knet link params
1198 totem_config
->interfaces
[linknumber
].knet_link_priority
= 1;
1199 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_link_priority", linknumber
);
1201 if (icmap_get_uint8(tmp_key
, &u8
) == CS_OK
) {
1202 totem_config
->interfaces
[linknumber
].knet_link_priority
= u8
;
1205 totem_config
->interfaces
[linknumber
].knet_ping_interval
= KNET_PING_INTERVAL
;
1206 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_ping_interval", linknumber
);
1207 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1208 totem_config
->interfaces
[linknumber
].knet_ping_interval
= u32
;
1210 totem_config
->interfaces
[linknumber
].knet_ping_timeout
= KNET_PING_TIMEOUT
;
1211 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_ping_timeout", linknumber
);
1212 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1213 totem_config
->interfaces
[linknumber
].knet_ping_timeout
= u32
;
1215 totem_config
->interfaces
[linknumber
].knet_ping_precision
= KNET_PING_PRECISION
;
1216 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_ping_precision", linknumber
);
1217 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1218 totem_config
->interfaces
[linknumber
].knet_ping_precision
= u32
;
1220 totem_config
->interfaces
[linknumber
].knet_pong_count
= KNET_PONG_COUNT
;
1221 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.knet_pong_count", linknumber
);
1222 if (icmap_get_uint32(tmp_key
, &u32
) == CS_OK
) {
1223 totem_config
->interfaces
[linknumber
].knet_pong_count
= u32
;
1226 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.member.", linknumber
);
1227 member_iter
= icmap_iter_init(tmp_key
);
1228 while ((member_iter_key
= icmap_iter_next(member_iter
, NULL
, NULL
)) != NULL
) {
1229 if (member_count
== 0) {
1230 if (icmap_get_string("nodelist.node.0.ring0_addr", &str
) == CS_OK
) {
1232 *warnings
|= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED
;
1235 *warnings
|= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED
;
1239 if (icmap_get_string(member_iter_key
, &str
) == CS_OK
) {
1240 res
= totemip_parse (&totem_config
->interfaces
[linknumber
].member_list
[member_count
++],
1241 str
, totem_config
->ip_version
);
1244 icmap_iter_finalize(member_iter
);
1246 totem_config
->interfaces
[linknumber
].member_count
= member_count
;
1249 icmap_iter_finalize(iter
);
1254 extern int totem_config_read (
1255 struct totem_config
*totem_config
,
1256 const char **error_string
,
1260 char *str
, *ring0_addr_str
;
1261 char tmp_key
[ICMAP_KEYNAME_MAXLEN
];
1269 memset (totem_config
, 0, sizeof (struct totem_config
));
1270 totem_config
->interfaces
= malloc (sizeof (struct totem_interface
) * INTERFACE_MAX
);
1271 if (totem_config
->interfaces
== 0) {
1272 *error_string
= "Out of memory trying to allocate ethernet interface storage area";
1276 memset (totem_config
->interfaces
, 0,
1277 sizeof (struct totem_interface
) * INTERFACE_MAX
);
1279 strcpy (totem_config
->link_mode
, "passive");
1281 icmap_get_uint32("totem.version", (uint32_t *)&totem_config
->version
);
1283 if (totem_get_crypto(totem_config
) != 0) {
1284 *error_string
= "crypto_cipher requires crypto_hash with value other than none";
1288 if (icmap_get_string("totem.link_mode", &str
) == CS_OK
) {
1289 if (strlen(str
) >= TOTEM_LINK_MODE_BYTES
) {
1290 *error_string
= "totem.link_mode is too long";
1295 strcpy (totem_config
->link_mode
, str
);
1299 icmap_get_uint32("totem.nodeid", &totem_config
->node_id
);
1301 totem_config
->clear_node_high_bit
= 0;
1302 if (icmap_get_string("totem.clear_node_high_bit", &str
) == CS_OK
) {
1303 if (strcmp (str
, "yes") == 0) {
1304 totem_config
->clear_node_high_bit
= 1;
1309 icmap_get_uint32("totem.threads", &totem_config
->threads
);
1311 icmap_get_uint32("totem.netmtu", &totem_config
->net_mtu
);
1313 totem_config
->ip_version
= totem_config_get_ip_version();
1315 if (icmap_get_string("totem.interface.0.bindnetaddr", &str
) != CS_OK
) {
1317 * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1319 config_convert_nodelist_to_interface(totem_config
);
1321 if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str
) == CS_OK
) {
1323 * Both bindnetaddr and ring0_addr are set.
1324 * Log warning information, and use nodelist instead
1326 *warnings
|= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET
;
1328 config_convert_nodelist_to_interface(totem_config
);
1330 free(ring0_addr_str
);
1337 * Broadcast option is global but set in interface section,
1338 * so reset before processing interfaces.
1340 totem_config
->broadcast_use
= 0;
1342 res
= get_interface_params(totem_config
, error_string
, warnings
, 0);
1348 * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1349 * broadcast is only supported for UDP so just do interface 0;
1351 if (totem_config
->broadcast_use
) {
1352 totemip_parse (&totem_config
->interfaces
[0].mcast_addr
,
1353 "255.255.255.255", 0);
1356 totem_config
->transport_number
= TOTEM_TRANSPORT_KNET
;
1357 if (icmap_get_string("totem.transport", &str
) == CS_OK
) {
1358 if (strcmp (str
, "udpu") == 0) {
1359 totem_config
->transport_number
= TOTEM_TRANSPORT_UDPU
;
1362 if (strcmp (str
, "udp") == 0) {
1363 totem_config
->transport_number
= TOTEM_TRANSPORT_UDP
;
1366 if (strcmp (str
, "knet") == 0) {
1367 totem_config
->transport_number
= TOTEM_TRANSPORT_KNET
;
1374 * Store automatically generated items back to icmap only for UDP
1376 if (totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) {
1377 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
1378 if (!totem_config
->interfaces
[i
].configured
) {
1381 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastaddr", i
);
1382 if (icmap_get_string(tmp_key
, &str
) == CS_OK
) {
1385 str
= (char *)totemip_print(&totem_config
->interfaces
[i
].mcast_addr
);
1386 icmap_set_string(tmp_key
, str
);
1389 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "totem.interface.%u.mcastport", i
);
1390 if (icmap_get_uint16(tmp_key
, &u16
) != CS_OK
) {
1391 icmap_set_uint16(tmp_key
, totem_config
->interfaces
[i
].ip_port
);
1397 * Check existence of nodelist
1399 if (icmap_get_string("nodelist.node.0.ring0_addr", &str
) == CS_OK
) {
1404 local_node_pos
= find_local_node_in_nodelist(totem_config
);
1405 if (local_node_pos
!= -1) {
1406 icmap_set_uint32("nodelist.local_node_pos", local_node_pos
);
1408 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.nodeid", local_node_pos
);
1410 nodeid_set
= (totem_config
->node_id
!= 0);
1411 if (icmap_get_uint32(tmp_key
, &totem_config
->node_id
) == CS_OK
&& nodeid_set
) {
1412 *warnings
|= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED
;
1414 if ((totem_config
->transport_number
== TOTEM_TRANSPORT_KNET
) && (!totem_config
->node_id
)) {
1415 *error_string
= "With knet, you must specify nodeid for current node";
1420 * Make localnode ring0_addr read only, so we can be sure that local
1421 * node never changes. If rebinding to other IP would be in future
1422 * supported, this must be changed and handled properly!
1424 snprintf(tmp_key
, ICMAP_KEYNAME_MAXLEN
, "nodelist.node.%u.ring0_addr", local_node_pos
);
1425 icmap_set_ro_access(tmp_key
, 0, 1);
1426 icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1429 put_nodelist_members_to_config(totem_config
, 0);
1433 * Get things that might change in the future (and can depend on totem_config->interfaces);
1435 totem_volatile_config_read(totem_config
, NULL
);
1437 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1439 add_totem_config_notification(totem_config
);
1445 int totem_config_validate (
1446 struct totem_config
*totem_config
,
1447 const char **error_string
)
1449 static char local_error_reason
[512];
1450 char parse_error
[512];
1451 const char *error_reason
= local_error_reason
;
1453 int num_configured
= 0;
1454 unsigned int interface_max
= INTERFACE_MAX
;
1458 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
1459 if (totem_config
->interfaces
[i
].configured
) {
1463 if (num_configured
== 0) {
1464 error_reason
= "No interfaces defined";
1468 for (i
= 0; i
< INTERFACE_MAX
; i
++) {
1470 * Some error checking of parsed data to make sure its valid
1473 struct totem_ip_address null_addr
;
1475 if (!totem_config
->interfaces
[i
].configured
) {
1479 memset (&null_addr
, 0, sizeof (struct totem_ip_address
));
1481 if ((totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) &&
1482 memcmp (&totem_config
->interfaces
[i
].mcast_addr
, &null_addr
,
1483 sizeof (struct totem_ip_address
)) == 0) {
1484 error_reason
= "No multicast address specified";
1488 if (totem_config
->interfaces
[i
].ip_port
== 0) {
1489 error_reason
= "No multicast port specified";
1493 if (totem_config
->interfaces
[i
].ttl
> 255) {
1494 error_reason
= "Invalid TTL (should be 0..255)";
1497 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_UDP
&&
1498 totem_config
->interfaces
[i
].ttl
!= 1) {
1499 error_reason
= "Can only set ttl on multicast transport types";
1502 if (totem_config
->interfaces
[i
].knet_link_priority
> 255) {
1503 error_reason
= "Invalid link priority (should be 0..255)";
1506 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_KNET
&&
1507 totem_config
->interfaces
[i
].knet_link_priority
!= 1) {
1508 error_reason
= "Can only set link priority on knet transport type";
1512 if (totem_config
->interfaces
[i
].mcast_addr
.family
== AF_INET6
&&
1513 totem_config
->node_id
== 0) {
1515 error_reason
= "An IPV6 network requires that a node ID be specified.";
1519 if (totem_config
->broadcast_use
== 0 && totem_config
->transport_number
== TOTEM_TRANSPORT_UDP
) {
1520 if (totem_config
->interfaces
[i
].mcast_addr
.family
!= totem_config
->interfaces
[i
].bindnet
.family
) {
1521 error_reason
= "Multicast address family does not match bind address family";
1525 if (totemip_is_mcast (&totem_config
->interfaces
[i
].mcast_addr
) != 0) {
1526 error_reason
= "mcastaddr is not a correct multicast address.";
1531 if (totem_config
->interfaces
[0].bindnet
.family
!= totem_config
->interfaces
[i
].bindnet
.family
) {
1532 error_reason
= "Not all bind address belong to the same IP family";
1537 if (totem_config
->version
!= 2) {
1538 error_reason
= "This totem parser can only parse version 2 configurations.";
1542 if (totem_volatile_config_validate(totem_config
, error_string
) == -1) {
1546 if (check_for_duplicate_nodeids(totem_config
, error_string
) == -1) {
1551 * KNET Link values validation
1553 if (strcmp (totem_config
->link_mode
, "active") &&
1554 strcmp (totem_config
->link_mode
, "rr") &&
1555 strcmp (totem_config
->link_mode
, "passive")) {
1556 snprintf (local_error_reason
, sizeof(local_error_reason
),
1557 "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config
->link_mode
);
1561 /* Only Knet does multiple interfaces */
1562 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_KNET
) {
1566 if (interface_max
< num_configured
) {
1567 snprintf (parse_error
, sizeof(parse_error
),
1568 "%d is too many configured interfaces for non-Knet transport.",
1570 error_reason
= parse_error
;
1574 /* Only knet allows crypto */
1575 if (totem_config
->transport_number
!= TOTEM_TRANSPORT_KNET
) {
1576 if ((strcmp(totem_config
->crypto_cipher_type
, "none") != 0) ||
1577 (strcmp(totem_config
->crypto_hash_type
, "none") != 0)) {
1579 snprintf (parse_error
, sizeof(parse_error
),
1580 "crypto_cipher & crypto_hash are only valid for the Knet transport.");
1581 error_reason
= parse_error
;
1586 if (totem_config
->net_mtu
== 0) {
1587 if (totem_config
->transport_number
== TOTEM_TRANSPORT_KNET
) {
1588 totem_config
->net_mtu
= KNET_MAX_PACKET_SIZE
;
1591 totem_config
->net_mtu
= 1500;
1598 snprintf (error_string_response
, sizeof(error_string_response
),
1599 "parse error in config: %s\n", error_reason
);
1600 *error_string
= error_string_response
;
1605 static int read_keyfile (
1606 const char *key_location
,
1607 struct totem_config
*totem_config
,
1608 const char **error_string
)
1613 char error_str
[100];
1614 const char *error_ptr
;
1616 fd
= open (key_location
, O_RDONLY
);
1618 error_ptr
= qb_strerror_r(errno
, error_str
, sizeof(error_str
));
1619 snprintf (error_string_response
, sizeof(error_string_response
),
1620 "Could not open %s: %s\n",
1621 key_location
, error_ptr
);
1625 res
= read (fd
, totem_config
->private_key
, TOTEM_PRIVATE_KEY_LEN_MAX
);
1626 saved_errno
= errno
;
1630 error_ptr
= qb_strerror_r (saved_errno
, error_str
, sizeof(error_str
));
1631 snprintf (error_string_response
, sizeof(error_string_response
),
1632 "Could not read %s: %s\n",
1633 key_location
, error_ptr
);
1637 if (res
< TOTEM_PRIVATE_KEY_LEN_MIN
) {
1638 snprintf (error_string_response
, sizeof(error_string_response
),
1639 "Could only read %d bits of minimum %u bits from %s.\n",
1640 res
* 8, TOTEM_PRIVATE_KEY_LEN_MIN
* 8, key_location
);
1644 totem_config
->private_key_len
= res
;
1649 *error_string
= error_string_response
;
1653 int totem_config_keyread (
1654 struct totem_config
*totem_config
,
1655 const char **error_string
)
1658 char *key_location
= NULL
;
1662 memset (totem_config
->private_key
, 0, sizeof(totem_config
->private_key
));
1663 totem_config
->private_key_len
= 0;
1665 if (strcmp(totem_config
->crypto_cipher_type
, "none") == 0 &&
1666 strcmp(totem_config
->crypto_hash_type
, "none") == 0) {
1670 /* cmap may store the location of the key file */
1671 if (icmap_get_string("totem.keyfile", &key_location
) == CS_OK
) {
1672 res
= read_keyfile(key_location
, totem_config
, error_string
);
1678 } else { /* Or the key itself may be in the cmap */
1679 if (icmap_get("totem.key", NULL
, &key_len
, NULL
) == CS_OK
) {
1680 if (key_len
> sizeof(totem_config
->private_key
)) {
1681 sprintf(error_string_response
, "key is too long");
1684 if (key_len
< TOTEM_PRIVATE_KEY_LEN_MIN
) {
1685 sprintf(error_string_response
, "key is too short");
1688 if (icmap_get("totem.key", totem_config
->private_key
, &key_len
, NULL
) == CS_OK
) {
1689 totem_config
->private_key_len
= key_len
;
1692 sprintf(error_string_response
, "can't load private key");
1698 /* In desperation we read the default filename */
1700 const char *filename
= getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
1702 filename
= COROSYSCONFDIR
"/authkey";
1703 res
= read_keyfile(filename
, totem_config
, error_string
);
1712 *error_string
= error_string_response
;
1717 static void debug_dump_totem_config(const struct totem_config
*totem_config
)
1720 log_printf(LOGSYS_LEVEL_DEBUG
, "Token Timeout (%d ms) retransmit timeout (%d ms)",
1721 totem_config
->token_timeout
, totem_config
->token_retransmit_timeout
);
1722 log_printf(LOGSYS_LEVEL_DEBUG
, "token hold (%d ms) retransmits before loss (%d retrans)",
1723 totem_config
->token_hold_timeout
, totem_config
->token_retransmits_before_loss_const
);
1724 log_printf(LOGSYS_LEVEL_DEBUG
, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
1725 totem_config
->join_timeout
, totem_config
->send_join_timeout
, totem_config
->consensus_timeout
,
1726 totem_config
->merge_timeout
);
1727 log_printf(LOGSYS_LEVEL_DEBUG
, "downcheck (%d ms) fail to recv const (%d msgs)",
1728 totem_config
->downcheck_timeout
, totem_config
->fail_to_recv_const
);
1729 log_printf(LOGSYS_LEVEL_DEBUG
,
1730 "seqno unchanged const (%d rotations) Maximum network MTU %d",
1731 totem_config
->seqno_unchanged_const
, totem_config
->net_mtu
);
1732 log_printf(LOGSYS_LEVEL_DEBUG
,
1733 "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
1734 totem_config
->window_size
, totem_config
->max_messages
);
1735 log_printf(LOGSYS_LEVEL_DEBUG
, "missed count const (%d messages)", totem_config
->miss_count_const
);
1736 log_printf(LOGSYS_LEVEL_DEBUG
, "heartbeat_failures_allowed (%d)",
1737 totem_config
->heartbeat_failures_allowed
);
1738 log_printf(LOGSYS_LEVEL_DEBUG
, "max_network_delay (%d ms)", totem_config
->max_network_delay
);
1741 static void totem_change_notify(
1743 const char *key_name
,
1744 struct icmap_notify_value new_val
,
1745 struct icmap_notify_value old_val
,
1748 struct totem_config
*totem_config
= (struct totem_config
*)user_data
;
1751 const char *deleted_key
= NULL
;
1752 const char *error_string
;
1755 * If a full reload is in progress then don't do anything until it's done and
1756 * can reconfigure it all atomically
1758 if (icmap_get_uint8("config.reload_in_progress", &reloading
) == CS_OK
&& reloading
)
1761 param
= totem_get_param_by_name((struct totem_config
*)user_data
, key_name
);
1763 * Process change only if changed key is found in totem_config (-> param is not NULL)
1764 * or for special key token_coefficient. token_coefficient key is not stored in
1765 * totem_config, but it is used for computation of token timeout.
1767 if (!param
&& strcmp(key_name
, "totem.token_coefficient") != 0)
1771 * Values other than UINT32 are not supported, or needed (yet)
1774 case ICMAP_TRACK_DELETE
:
1775 deleted_key
= key_name
;
1777 case ICMAP_TRACK_ADD
:
1778 case ICMAP_TRACK_MODIFY
:
1785 totem_volatile_config_read (totem_config
, deleted_key
);
1786 log_printf(LOGSYS_LEVEL_DEBUG
, "Totem related config key changed. Dumping actual totem config.");
1787 debug_dump_totem_config(totem_config
);
1788 if (totem_volatile_config_validate(totem_config
, &error_string
) == -1) {
1789 log_printf (LOGSYS_LEVEL_ERROR
, "%s", error_string
);
1791 * TODO: Consider corosync exit and/or load defaults for volatile
1792 * values. For now, log error seems to be enough
1797 static void totem_reload_notify(
1799 const char *key_name
,
1800 struct icmap_notify_value new_val
,
1801 struct icmap_notify_value old_val
,
1804 struct totem_config
*totem_config
= (struct totem_config
*)user_data
;
1805 uint32_t local_node_pos
;
1806 const char *error_string
;
1809 /* Reload has completed */
1810 if (*(uint8_t *)new_val
.data
== 0) {
1812 totem_config
->orig_interfaces
= malloc (sizeof (struct totem_interface
) * INTERFACE_MAX
);
1813 assert(totem_config
->orig_interfaces
!= NULL
);
1814 memcpy(totem_config
->orig_interfaces
, totem_config
->interfaces
, sizeof (struct totem_interface
) * INTERFACE_MAX
);
1816 get_interface_params(totem_config
, &error_string
, &warnings
, 1);
1817 put_nodelist_members_to_config (totem_config
, 1);
1818 totem_volatile_config_read (totem_config
, NULL
);
1819 log_printf(LOGSYS_LEVEL_DEBUG
, "Configuration reloaded. Dumping actual totem config.");
1820 debug_dump_totem_config(totem_config
);
1821 if (totem_volatile_config_validate(totem_config
, &error_string
) == -1) {
1822 log_printf (LOGSYS_LEVEL_ERROR
, "%s", error_string
);
1824 * TODO: Consider corosync exit and/or load defaults for volatile
1825 * values. For now, log error seems to be enough
1829 /* Reinstate the local_node_pos */
1830 local_node_pos
= find_local_node_in_nodelist(totem_config
);
1831 if (local_node_pos
!= -1) {
1832 icmap_set_uint32("nodelist.local_node_pos", local_node_pos
);
1835 /* Reconfigure network params as appropriate */
1836 totempg_reconfigure();
1838 free(totem_config
->orig_interfaces
);
1840 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1842 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
1846 static void add_totem_config_notification(struct totem_config
*totem_config
)
1848 icmap_track_t icmap_track
;
1850 icmap_track_add("totem.",
1851 ICMAP_TRACK_ADD
| ICMAP_TRACK_DELETE
| ICMAP_TRACK_MODIFY
| ICMAP_TRACK_PREFIX
,
1852 totem_change_notify
,
1856 icmap_track_add("config.reload_in_progress",
1857 ICMAP_TRACK_ADD
| ICMAP_TRACK_MODIFY
,
1858 totem_reload_notify
,
1862 icmap_track_add("nodelist.node.",
1863 ICMAP_TRACK_ADD
| ICMAP_TRACK_DELETE
| ICMAP_TRACK_MODIFY
| ICMAP_TRACK_PREFIX
,
1864 nodelist_dynamic_notify
,
1865 (void *)totem_config
,