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