]> git.proxmox.com Git - mirror_corosync.git/blob - exec/totemconfig.c
config: Fix crash in reload if new interfaces are added
[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 int totem_config_get_ip_version(struct totem_config *totem_config)
782 {
783 int res;
784 char *str;
785
786 res = AF_INET;
787 if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
788 res = AF_UNSPEC;
789 } else {
790 if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
791 if (strcmp(str, "ipv4") == 0) {
792 res = AF_INET;
793 }
794 if (strcmp(str, "ipv6") == 0) {
795 res = AF_INET6;
796 }
797 free(str);
798 }
799 }
800
801 return (res);
802 }
803
804 static uint16_t generate_cluster_id (const char *cluster_name)
805 {
806 int i;
807 int value = 0;
808
809 for (i = 0; i < strlen(cluster_name); i++) {
810 value <<= 1;
811 value += cluster_name[i];
812 }
813
814 return (value & 0xFFFF);
815 }
816
817 static int get_cluster_mcast_addr (
818 const char *cluster_name,
819 unsigned int linknumber,
820 int ip_version,
821 struct totem_ip_address *res)
822 {
823 uint16_t clusterid;
824 char addr[INET6_ADDRSTRLEN + 1];
825 int err;
826
827 if (cluster_name == NULL) {
828 return (-1);
829 }
830
831 clusterid = generate_cluster_id(cluster_name) + linknumber;
832 memset (res, 0, sizeof(*res));
833
834 switch (ip_version) {
835 case AF_INET:
836 snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
837 break;
838 case AF_INET6:
839 snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
840 break;
841 default:
842 /*
843 * Unknown family
844 */
845 return (-1);
846 }
847
848 err = totemip_parse (res, addr, ip_version);
849
850 return (err);
851 }
852
853 static unsigned int generate_nodeid_for_duplicate_test(
854 struct totem_config *totem_config,
855 char *addr)
856 {
857 unsigned int nodeid;
858 struct totem_ip_address totemip;
859
860 /* AF_INET hard-coded here because auto-generated nodeids
861 are only for IPv4 */
862 if (totemip_parse(&totemip, addr, AF_INET) != 0)
863 return -1;
864
865 memcpy (&nodeid, &totemip.addr, sizeof (unsigned int));
866
867 #if __BYTE_ORDER == __LITTLE_ENDIAN
868 nodeid = swab32 (nodeid);
869 #endif
870
871 if (totem_config->clear_node_high_bit) {
872 nodeid &= 0x7FFFFFFF;
873 }
874 return nodeid;
875 }
876
877 static int check_for_duplicate_nodeids(
878 struct totem_config *totem_config,
879 const char **error_string)
880 {
881 icmap_iter_t iter;
882 icmap_iter_t subiter;
883 const char *iter_key;
884 int res = 0;
885 int retval = 0;
886 char tmp_key[ICMAP_KEYNAME_MAXLEN];
887 char *ring0_addr=NULL;
888 char *ring0_addr1=NULL;
889 unsigned int node_pos;
890 unsigned int node_pos1;
891 unsigned int nodeid;
892 unsigned int nodeid1;
893 int autogenerated;
894
895 iter = icmap_iter_init("nodelist.node.");
896 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
897 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
898 if (res != 2) {
899 continue;
900 }
901
902 if (strcmp(tmp_key, "nodeid") != 0) {
903 continue;
904 }
905
906 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
907 autogenerated = 0;
908
909 /* Generated nodeids are only allowed for UDP/UDPU so ring0_addr is valid here */
910 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
911
912 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
913 if (icmap_get_string(tmp_key, &ring0_addr) != CS_OK) {
914 continue;
915 }
916
917 /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
918 nodeid = generate_nodeid_for_duplicate_test(totem_config, ring0_addr);
919 if (nodeid == -1) {
920 continue;
921 }
922 autogenerated = 1;
923 }
924
925 node_pos1 = 0;
926 subiter = icmap_iter_init("nodelist.node.");
927 while (((iter_key = icmap_iter_next(subiter, NULL, NULL)) != NULL) && (node_pos1 < node_pos)) {
928 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos1, tmp_key);
929 if ((res != 2) || (node_pos1 >= node_pos)) {
930 continue;
931 }
932
933 if (strcmp(tmp_key, "nodeid") != 0) {
934 continue;
935 }
936
937 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos1);
938 if (icmap_get_uint32(tmp_key, &nodeid1) != CS_OK) {
939
940 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos1);
941 if (icmap_get_string(tmp_key, &ring0_addr1) != CS_OK) {
942 continue;
943 }
944 nodeid1 = generate_nodeid_for_duplicate_test(totem_config, ring0_addr1);
945 if (nodeid1 == -1) {
946 continue;
947 }
948 }
949
950 if (nodeid == nodeid1) {
951 retval = -1;
952 snprintf (error_string_response, sizeof(error_string_response),
953 "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid,
954 autogenerated?"(autogenerated from ":"",
955 autogenerated?ring0_addr:"",
956 autogenerated?")":"");
957 log_printf (LOGSYS_LEVEL_ERROR, error_string_response);
958 *error_string = error_string_response;
959 break;
960 }
961 }
962 icmap_iter_finalize(subiter);
963 }
964 icmap_iter_finalize(iter);
965 return retval;
966 }
967
968
969 /*
970 * This needs to be done last of all. It would be nice to do it when reading the
971 * interface params, but the totem params need to have them to be read first. We
972 * need both, so this is a way round that circular dependancy.
973 */
974 static void calc_knet_ping_timers(struct totem_config *totem_config)
975 {
976 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
977 int interface;
978
979 for (interface = 0; interface < INTERFACE_MAX; interface++) {
980
981 if (totem_config->interfaces[interface].configured) {
982 if (!totem_config->interfaces[interface].knet_pong_count) {
983 totem_config->interfaces[interface].knet_pong_count = KNET_PONG_COUNT;
984 }
985 if (!totem_config->interfaces[interface].knet_ping_timeout) {
986 totem_config->interfaces[interface].knet_ping_timeout =
987 totem_config->token_timeout / totem_config->interfaces[interface].knet_pong_count;
988 }
989 snprintf(runtime_key_name, sizeof(runtime_key_name),
990 "runtime.config.totem.interface.%d.knet_ping_timeout", interface);
991 icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_timeout);
992
993 if (!totem_config->interfaces[interface].knet_ping_interval) {
994 totem_config->interfaces[interface].knet_ping_interval =
995 totem_config->token_timeout / (totem_config->interfaces[interface].knet_pong_count * 2);
996 }
997 snprintf(runtime_key_name, sizeof(runtime_key_name),
998 "runtime.config.totem.interface.%d.knet_ping_interval", interface);
999 icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_interval);
1000 }
1001 }
1002 }
1003
1004 /*
1005 * Compute difference between two set of totem interface arrays. set1 and set2
1006 * are changed so for same ring, ip existing in both set1 and set2 are cleared
1007 * (set to 0), and ips which are only in set1 or set2 remains untouched.
1008 * totempg_node_add/remove is called.
1009 */
1010 static void compute_interfaces_diff(struct totem_interface *set1,
1011 struct totem_interface *set2)
1012 {
1013 int ring_no, set1_pos, set2_pos;
1014 struct totem_ip_address empty_ip_address;
1015
1016 memset(&empty_ip_address, 0, sizeof(empty_ip_address));
1017
1018 for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
1019 if (!set1[ring_no].configured && !set2[ring_no].configured) {
1020 continue;
1021 }
1022
1023 for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
1024 for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
1025 /*
1026 * For current ring_no remove all set1 items existing
1027 * in set2
1028 */
1029 if (memcmp(&set1[ring_no].member_list[set1_pos],
1030 &set2[ring_no].member_list[set2_pos],
1031 sizeof(struct totem_ip_address)) == 0) {
1032 memset(&set1[ring_no].member_list[set1_pos], 0,
1033 sizeof(struct totem_ip_address));
1034 memset(&set2[ring_no].member_list[set2_pos], 0,
1035 sizeof(struct totem_ip_address));
1036 }
1037 }
1038 }
1039 }
1040
1041 for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
1042 for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
1043 /*
1044 * All items which remained in set1 doesn't exists in set2 any longer so
1045 * node has to be removed.
1046 */
1047 if (memcmp(&set1[ring_no].member_list[set1_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
1048 log_printf(LOGSYS_LEVEL_DEBUG,
1049 "removing dynamic member %s for ring %u",
1050 totemip_print(&set1[ring_no].member_list[set1_pos]),
1051 ring_no);
1052
1053 totempg_member_remove(&set1[ring_no].member_list[set1_pos], ring_no);
1054 }
1055 }
1056 if (!set2[ring_no].configured) {
1057 continue;
1058 }
1059 for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
1060 /*
1061 * All items which remained in set2 doesn't existed in set1 so this is no node
1062 * and has to be added.
1063 */
1064 if (memcmp(&set2[ring_no].member_list[set2_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
1065 log_printf(LOGSYS_LEVEL_DEBUG,
1066 "adding dynamic member %s for ring %u",
1067 totemip_print(&set2[ring_no].member_list[set2_pos]),
1068 ring_no);
1069
1070 totempg_member_add(&set2[ring_no].member_list[set2_pos], ring_no);
1071 }
1072 }
1073 }
1074 }
1075
1076 /*
1077 * Reconfigure links in totempg. Sets new local IP address and adds params for new links.
1078 */
1079 static void reconfigure_links(struct totem_config *totem_config)
1080 {
1081 int i;
1082 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1083 char *addr_string;
1084 struct totem_ip_address local_ip;
1085 int err;
1086 int local_node_pos = find_local_node(0);
1087
1088 for (i = 0; i<INTERFACE_MAX; i++) {
1089 if (!totem_config->interfaces[i].configured) {
1090 continue;
1091 }
1092
1093 log_printf(LOGSYS_LEVEL_INFO, "Configuring link %d\n", i);
1094
1095 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring%u_addr", local_node_pos, i);
1096 if (icmap_get_string(tmp_key, &addr_string) != CS_OK) {
1097 continue;
1098 }
1099
1100 err = totemip_parse(&local_ip, addr_string, AF_UNSPEC);
1101 if (err != 0) {
1102 continue;
1103 }
1104 local_ip.nodeid = totem_config->node_id;
1105
1106 /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
1107 if (!totem_config->interfaces[i].knet_link_priority)
1108 totem_config->interfaces[i].knet_link_priority = 1;
1109
1110 /* knet_ping_interval & knet_ping_timeout are set later once we know all the other params */
1111
1112 if (!totem_config->interfaces[i].knet_ping_precision)
1113 totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
1114 if (!totem_config->interfaces[i].knet_pong_count)
1115 totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
1116 if (!totem_config->interfaces[i].knet_transport)
1117 totem_config->interfaces[i].knet_transport = KNET_TRANSPORT_UDP;
1118 if (!totem_config->interfaces[i].ip_port)
1119 totem_config->interfaces[i].ip_port = DEFAULT_PORT + i;
1120
1121 totempg_iface_set(&local_ip, totem_config->interfaces[i].ip_port, i);
1122 }
1123 }
1124
1125 /* Check for differences in config that can't be done on-the-fly and print an error */
1126 static void check_things_have_not_changed(struct totem_config *totem_config)
1127 {
1128 int i,j;
1129 const char *ip_str;
1130 char addr_buf[INET6_ADDRSTRLEN];
1131 int changed = 0;
1132
1133 for (i = 0; i<INTERFACE_MAX; i++) {
1134 if (totem_config->interfaces[i].configured) {
1135 if (totem_config->interfaces[i].knet_transport !=
1136 totem_config->orig_interfaces[i].knet_transport) {
1137 log_printf(LOGSYS_LEVEL_ERROR, "New config has different knet transport for link %d. Internal value was NOT changed.\n", i);
1138 changed = 1;
1139 }
1140 for (j=0; j < min(totem_config->interfaces[i].member_count, totem_config->orig_interfaces[i].member_count); j++) {
1141 if (memcmp(&totem_config->interfaces[i].member_list[j],
1142 &totem_config->orig_interfaces[i].member_list[j],
1143 sizeof(struct totem_ip_address))) {
1144
1145 ip_str = totemip_print(&totem_config->orig_interfaces[i].member_list[j]);
1146 strcpy(addr_buf, ip_str);
1147 log_printf(LOGSYS_LEVEL_ERROR, "new config has different address for link %d (addr changed from %s to %s). Internal value was NOT changed.\n", i, addr_buf, totemip_print(&totem_config->interfaces[i].member_list[j]));
1148 changed = 1;
1149 }
1150 }
1151 }
1152 }
1153
1154 if (changed) {
1155 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");
1156 }
1157 }
1158
1159
1160 static void put_nodelist_members_to_config(struct totem_config *totem_config, int reload)
1161 {
1162 icmap_iter_t iter, iter2;
1163 const char *iter_key, *iter_key2;
1164 int res = 0;
1165 unsigned int node_pos;
1166 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1167 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1168 char *node_addr_str;
1169 int member_count;
1170 unsigned int linknumber = 0;
1171 int i, j;
1172 struct totem_interface *new_interfaces = NULL;
1173
1174 if (reload) {
1175 /*
1176 * We need to compute diff only for reload. Also for initial configuration
1177 * not all totem structures are initialized so corosync will crash during
1178 * member_add/remove
1179 */
1180 new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1181 assert(new_interfaces != NULL);
1182 }
1183
1184 /* Clear out nodelist so we can put the new one in if needed */
1185 for (i = 0; i < INTERFACE_MAX; i++) {
1186 for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
1187 memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
1188 }
1189 totem_config->interfaces[i].member_count = 0;
1190 }
1191
1192 iter = icmap_iter_init("nodelist.node.");
1193 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1194 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
1195 if (res != 2) {
1196 continue;
1197 }
1198 if (strcmp(tmp_key, "nodeid") != 0) {
1199 continue;
1200 }
1201
1202 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1203 iter2 = icmap_iter_init(tmp_key);
1204 while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
1205 unsigned int nodeid;
1206
1207 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
1208 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
1209 }
1210
1211 res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1212 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1213 continue;
1214 }
1215
1216 if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
1217 continue;
1218 }
1219
1220 member_count = totem_config->interfaces[linknumber].member_count;
1221 res = totemip_parse(&totem_config->interfaces[linknumber].member_list[member_count],
1222 node_addr_str, totem_config->ip_version);
1223 if (res != -1) {
1224 totem_config->interfaces[linknumber].member_list[member_count].nodeid = nodeid;
1225 totem_config->interfaces[linknumber].member_count++;
1226 }
1227 totem_config->interfaces[linknumber].configured = 1;
1228 free(node_addr_str);
1229 }
1230
1231 icmap_iter_finalize(iter2);
1232 }
1233
1234 icmap_iter_finalize(iter);
1235
1236 if (reload) {
1237 log_printf(LOGSYS_LEVEL_DEBUG, "About to reconfigure links from nodelist.\n");
1238 reconfigure_links(totem_config);
1239
1240 memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
1241
1242 check_things_have_not_changed(totem_config);
1243
1244 compute_interfaces_diff(totem_config->orig_interfaces, new_interfaces);
1245
1246 free(new_interfaces);
1247 }
1248 }
1249
1250 static void nodelist_dynamic_notify(
1251 int32_t event,
1252 const char *key_name,
1253 struct icmap_notify_value new_val,
1254 struct icmap_notify_value old_val,
1255 void *user_data)
1256 {
1257 int res;
1258 unsigned int ring_no;
1259 unsigned int member_no;
1260 char tmp_str[ICMAP_KEYNAME_MAXLEN];
1261 uint8_t reloading;
1262 struct totem_config *totem_config = (struct totem_config *)user_data;
1263
1264 /*
1265 * If a full reload is in progress then don't do anything until it's done and
1266 * can reconfigure it all atomically
1267 */
1268 if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
1269 return ;
1270 }
1271
1272 res = sscanf(key_name, "nodelist.node.%u.ring%u%s", &member_no, &ring_no, tmp_str);
1273 if (res != 3)
1274 return ;
1275
1276 if (strcmp(tmp_str, "_addr") != 0) {
1277 return;
1278 }
1279
1280 put_nodelist_members_to_config(totem_config, 1);
1281 }
1282
1283
1284
1285 static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
1286 {
1287 int res = 0;
1288 int node_pos;
1289 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1290 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1291 char *node_addr_str;
1292 unsigned int linknumber = 0;
1293 icmap_iter_t iter;
1294 const char *iter_key;
1295
1296 node_pos = find_local_node(1);
1297 if (node_pos > -1) {
1298 /*
1299 * We found node, so create interface section
1300 */
1301 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1302 iter = icmap_iter_init(tmp_key);
1303 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1304 res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1305 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1306 continue ;
1307 }
1308
1309 if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
1310 continue;
1311 }
1312
1313 snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1314 icmap_set_string(tmp_key2, node_addr_str);
1315 free(node_addr_str);
1316 }
1317 icmap_iter_finalize(iter);
1318 }
1319 }
1320
1321 static int get_interface_params(struct totem_config *totem_config,
1322 const char **error_string, uint64_t *warnings,
1323 int reload)
1324 {
1325 int res = 0;
1326 unsigned int linknumber = 0;
1327 int member_count = 0;
1328 int i;
1329 icmap_iter_t iter, member_iter;
1330 const char *iter_key;
1331 const char *member_iter_key;
1332 char linknumber_key[ICMAP_KEYNAME_MAXLEN];
1333 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1334 uint8_t u8;
1335 uint32_t u32;
1336 char *str;
1337 char *cluster_name = NULL;
1338
1339 if (reload) {
1340 for (i=0; i<INTERFACE_MAX; i++) {
1341 /*
1342 * Set back to defaults things that might have been configured and
1343 * now have been taken out of corosync.conf. These won't be caught by the
1344 * code below which only looks at interface{} sections that actually exist.
1345 */
1346 totem_config->interfaces[i].configured = 0;
1347 totem_config->interfaces[i].knet_ping_timeout = 0;
1348 totem_config->interfaces[i].knet_ping_interval = 0;
1349 totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
1350 totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
1351 }
1352 }
1353 if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
1354 cluster_name = NULL;
1355 }
1356
1357 iter = icmap_iter_init("totem.interface.");
1358 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1359 res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
1360 if (res != 2) {
1361 continue;
1362 }
1363
1364 if (strcmp(tmp_key, "bindnetaddr") != 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1365 continue;
1366 }
1367
1368 member_count = 0;
1369 linknumber = atoi(linknumber_key);
1370
1371 if (linknumber >= INTERFACE_MAX) {
1372 free(cluster_name);
1373
1374 snprintf (error_string_response, sizeof(error_string_response),
1375 "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1376 linknumber, INTERFACE_MAX - 1);
1377
1378 *error_string = error_string_response;
1379 return -1;
1380 }
1381
1382 /* These things are only valid for the initial read */
1383 if (!reload) {
1384 /*
1385 * Get the bind net address
1386 */
1387 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1388
1389 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1390 res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
1391 totem_config->ip_version);
1392 free(str);
1393 }
1394
1395 /*
1396 * Get interface multicast address
1397 */
1398 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
1399 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1400 res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str, totem_config->ip_version);
1401 free(str);
1402 } else {
1403 /*
1404 * User not specified address -> autogenerate one from cluster_name key
1405 * (if available). Return code is intentionally ignored, because
1406 * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1407 * checked later anyway.
1408 */
1409 (void)get_cluster_mcast_addr (cluster_name,
1410 linknumber,
1411 totem_config->ip_version,
1412 &totem_config->interfaces[linknumber].mcast_addr);
1413 }
1414
1415 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
1416 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1417 if (strcmp (str, "yes") == 0) {
1418 totem_config->broadcast_use = 1;
1419 }
1420 free(str);
1421 }
1422 }
1423
1424 /* These things are only valid for the initial read OR a newly-defined link */
1425 if (!reload || (totem_config->interfaces[linknumber].configured == 0)) {
1426
1427 /*
1428 * Get mcast port
1429 */
1430 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
1431 if (icmap_get_uint16(tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
1432 if (totem_config->broadcast_use) {
1433 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
1434 } else {
1435 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + linknumber;
1436 }
1437 }
1438
1439 /*
1440 * Get the TTL
1441 */
1442 totem_config->interfaces[linknumber].ttl = 1;
1443
1444 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
1445
1446 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1447 totem_config->interfaces[linknumber].ttl = u8;
1448 }
1449
1450 totem_config->interfaces[linknumber].knet_transport = KNET_DEFAULT_TRANSPORT;
1451 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
1452 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1453 if (strcmp(str, "sctp") == 0) {
1454 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
1455 }
1456 else if (strcmp(str, "udp") == 0) {
1457 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
1458 }
1459 else {
1460 *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1461 return -1;
1462 }
1463 }
1464 }
1465 totem_config->interfaces[linknumber].configured = 1;
1466
1467 /*
1468 * Get the knet link params
1469 */
1470 totem_config->interfaces[linknumber].knet_link_priority = 1;
1471 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
1472
1473 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1474 totem_config->interfaces[linknumber].knet_link_priority = u8;
1475 }
1476
1477 totem_config->interfaces[linknumber].knet_ping_interval = 0; /* real default applied later */
1478 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
1479 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1480 totem_config->interfaces[linknumber].knet_ping_interval = u32;
1481 }
1482 totem_config->interfaces[linknumber].knet_ping_timeout = 0; /* real default applied later */
1483 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
1484 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1485 totem_config->interfaces[linknumber].knet_ping_timeout = u32;
1486 }
1487 totem_config->interfaces[linknumber].knet_ping_precision = KNET_PING_PRECISION;
1488 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
1489 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1490 totem_config->interfaces[linknumber].knet_ping_precision = u32;
1491 }
1492 totem_config->interfaces[linknumber].knet_pong_count = KNET_PONG_COUNT;
1493 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
1494 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1495 totem_config->interfaces[linknumber].knet_pong_count = u32;
1496 }
1497
1498 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
1499 member_iter = icmap_iter_init(tmp_key);
1500 while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
1501 if (member_count == 0) {
1502 if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
1503 free(str);
1504 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
1505 break;
1506 } else {
1507 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
1508 }
1509 }
1510
1511 if (icmap_get_string(member_iter_key, &str) == CS_OK) {
1512 res = totemip_parse (&totem_config->interfaces[linknumber].member_list[member_count++],
1513 str, totem_config->ip_version);
1514 }
1515 }
1516 icmap_iter_finalize(member_iter);
1517
1518 totem_config->interfaces[linknumber].member_count = member_count;
1519
1520 }
1521 icmap_iter_finalize(iter);
1522
1523 return 0;
1524 }
1525
1526 extern int totem_config_read (
1527 struct totem_config *totem_config,
1528 const char **error_string,
1529 uint64_t *warnings)
1530 {
1531 int res = 0;
1532 char *str, *ring0_addr_str;
1533 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1534 uint16_t u16;
1535 int i;
1536 int local_node_pos;
1537 int nodeid_set;
1538
1539 *warnings = 0;
1540
1541 memset (totem_config, 0, sizeof (struct totem_config));
1542 totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1543 if (totem_config->interfaces == 0) {
1544 *error_string = "Out of memory trying to allocate ethernet interface storage area";
1545 return -1;
1546 }
1547
1548 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1549 if (icmap_get_string("totem.transport", &str) == CS_OK) {
1550 if (strcmp (str, "udpu") == 0) {
1551 totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
1552 }
1553
1554 if (strcmp (str, "udp") == 0) {
1555 totem_config->transport_number = TOTEM_TRANSPORT_UDP;
1556 }
1557
1558 if (strcmp (str, "knet") == 0) {
1559 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1560 }
1561
1562 free(str);
1563 }
1564
1565 memset (totem_config->interfaces, 0,
1566 sizeof (struct totem_interface) * INTERFACE_MAX);
1567
1568 strcpy (totem_config->link_mode, "passive");
1569
1570 icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
1571
1572 if (totem_get_crypto(totem_config, error_string) != 0) {
1573 return -1;
1574 }
1575
1576 if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
1577 if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
1578 *error_string = "totem.link_mode is too long";
1579 free(str);
1580
1581 return -1;
1582 }
1583 strcpy (totem_config->link_mode, str);
1584 free(str);
1585 }
1586
1587 icmap_get_uint32("totem.nodeid", &totem_config->node_id);
1588
1589 totem_config->clear_node_high_bit = 0;
1590 if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
1591 if (strcmp (str, "yes") == 0) {
1592 totem_config->clear_node_high_bit = 1;
1593 }
1594 free(str);
1595 }
1596
1597 icmap_get_uint32("totem.threads", &totem_config->threads);
1598
1599 icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
1600
1601 totem_config->ip_version = totem_config_get_ip_version(totem_config);
1602
1603 if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
1604 /*
1605 * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1606 */
1607 config_convert_nodelist_to_interface(totem_config);
1608 } else {
1609 if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
1610 /*
1611 * Both bindnetaddr and ring0_addr are set.
1612 * Log warning information, and use nodelist instead
1613 */
1614 *warnings |= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET;
1615
1616 config_convert_nodelist_to_interface(totem_config);
1617
1618 free(ring0_addr_str);
1619 }
1620
1621 free(str);
1622 }
1623
1624 /*
1625 * Broadcast option is global but set in interface section,
1626 * so reset before processing interfaces.
1627 */
1628 totem_config->broadcast_use = 0;
1629
1630 res = get_interface_params(totem_config, error_string, warnings, 0);
1631 if (res < 0) {
1632 return res;
1633 }
1634
1635 /*
1636 * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1637 * broadcast is only supported for UDP so just do interface 0;
1638 */
1639 if (totem_config->broadcast_use) {
1640 totemip_parse (&totem_config->interfaces[0].mcast_addr,
1641 "255.255.255.255", 0);
1642 }
1643
1644
1645 /*
1646 * Store automatically generated items back to icmap only for UDP
1647 */
1648 if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1649 for (i = 0; i < INTERFACE_MAX; i++) {
1650 if (!totem_config->interfaces[i].configured) {
1651 continue;
1652 }
1653 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
1654 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1655 free(str);
1656 } else {
1657 str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
1658 icmap_set_string(tmp_key, str);
1659 }
1660
1661 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
1662 if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
1663 icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
1664 }
1665 }
1666 }
1667
1668 /*
1669 * Check existence of nodelist
1670 */
1671 if ((icmap_get_string("nodelist.node.0.name", &str) == CS_OK) ||
1672 (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK)) {
1673 free(str);
1674 /*
1675 * find local node
1676 */
1677 local_node_pos = find_local_node(1);
1678 if (local_node_pos != -1) {
1679
1680 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
1681
1682 nodeid_set = (totem_config->node_id != 0);
1683 if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
1684 *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
1685 }
1686 if ((totem_config->transport_number == TOTEM_TRANSPORT_KNET) && (!totem_config->node_id)) {
1687 *error_string = "With knet, you must specify nodeid for current node";
1688 return -1;
1689 }
1690
1691 /* Users must not change this */
1692 icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1693 }
1694
1695 put_nodelist_members_to_config(totem_config, 0);
1696 }
1697
1698 /*
1699 * Get things that might change in the future (and can depend on totem_config->interfaces);
1700 */
1701 totem_volatile_config_read(totem_config, NULL);
1702
1703 calc_knet_ping_timers(totem_config);
1704
1705 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1706
1707 add_totem_config_notification(totem_config);
1708
1709 return 0;
1710 }
1711
1712
1713 int totem_config_validate (
1714 struct totem_config *totem_config,
1715 const char **error_string)
1716 {
1717 static char local_error_reason[512];
1718 char parse_error[512];
1719 const char *error_reason = local_error_reason;
1720 int i,j;
1721 uint32_t u32;
1722 int num_configured = 0;
1723 unsigned int interface_max = INTERFACE_MAX;
1724
1725 for (i = 0; i < INTERFACE_MAX; i++) {
1726 if (totem_config->interfaces[i].configured) {
1727 num_configured++;
1728 }
1729 }
1730 if (num_configured == 0) {
1731 error_reason = "No interfaces defined";
1732 goto parse_error;
1733 }
1734
1735 /* Check we found a local node name */
1736 if (icmap_get_uint32("nodelist.local_node_pos", &u32) != CS_OK) {
1737 error_reason = "No valid name found for local host";
1738 goto parse_error;
1739 }
1740
1741 for (i = 0; i < INTERFACE_MAX; i++) {
1742 /*
1743 * Some error checking of parsed data to make sure its valid
1744 */
1745
1746 struct totem_ip_address null_addr;
1747
1748 if (!totem_config->interfaces[i].configured) {
1749 continue;
1750 }
1751
1752 memset (&null_addr, 0, sizeof (struct totem_ip_address));
1753
1754 if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP) &&
1755 memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
1756 sizeof (struct totem_ip_address)) == 0) {
1757 error_reason = "No multicast address specified";
1758 goto parse_error;
1759 }
1760
1761 if (totem_config->interfaces[i].ip_port == 0) {
1762 error_reason = "No multicast port specified";
1763 goto parse_error;
1764 }
1765
1766 if (totem_config->interfaces[i].ttl > 255) {
1767 error_reason = "Invalid TTL (should be 0..255)";
1768 goto parse_error;
1769 }
1770 if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
1771 totem_config->interfaces[i].ttl != 1) {
1772 error_reason = "Can only set ttl on multicast transport types";
1773 goto parse_error;
1774 }
1775 if (totem_config->interfaces[i].knet_link_priority > 255) {
1776 error_reason = "Invalid link priority (should be 0..255)";
1777 goto parse_error;
1778 }
1779 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET &&
1780 totem_config->interfaces[i].knet_link_priority != 1) {
1781 error_reason = "Can only set link priority on knet transport type";
1782 goto parse_error;
1783 }
1784
1785 if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
1786 totem_config->node_id == 0) {
1787
1788 error_reason = "An IPV6 network requires that a node ID be specified.";
1789 goto parse_error;
1790 }
1791
1792 if (totem_config->broadcast_use == 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1793 if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
1794 error_reason = "Multicast address family does not match bind address family";
1795 goto parse_error;
1796 }
1797
1798 if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
1799 error_reason = "mcastaddr is not a correct multicast address.";
1800 goto parse_error;
1801 }
1802 }
1803 /* Verify that all nodes on the same knet link have the same IP family */
1804 for (j=1; j<totem_config->interfaces[i].member_count; j++) {
1805 if (totem_config->interfaces[i].configured) {
1806 if (totem_config->interfaces[i].member_list[j].family !=
1807 totem_config->interfaces[i].member_list[0].family) {
1808 snprintf (local_error_reason, sizeof(local_error_reason),
1809 "Nodes for link %d have different IP families", i);
1810 goto parse_error;
1811 }
1812 }
1813 }
1814 }
1815
1816 if (totem_config->version != 2) {
1817 error_reason = "This totem parser can only parse version 2 configurations.";
1818 goto parse_error;
1819 }
1820
1821 if (totem_volatile_config_validate(totem_config, error_string) == -1) {
1822 return (-1);
1823 }
1824
1825 if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
1826 return (-1);
1827 }
1828
1829 /*
1830 * KNET Link values validation
1831 */
1832 if (strcmp (totem_config->link_mode, "active") &&
1833 strcmp (totem_config->link_mode, "rr") &&
1834 strcmp (totem_config->link_mode, "passive")) {
1835 snprintf (local_error_reason, sizeof(local_error_reason),
1836 "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
1837 goto parse_error;
1838 }
1839
1840 /* Only Knet does multiple interfaces */
1841 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
1842 interface_max = 1;
1843 }
1844
1845 if (interface_max < num_configured) {
1846 snprintf (parse_error, sizeof(parse_error),
1847 "%d is too many configured interfaces for non-Knet transport.",
1848 num_configured);
1849 error_reason = parse_error;
1850 goto parse_error;
1851 }
1852
1853 /* Only knet allows crypto */
1854 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
1855 if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
1856 (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
1857
1858 snprintf (parse_error, sizeof(parse_error),
1859 "crypto_cipher & crypto_hash are only valid for the Knet transport.");
1860 error_reason = parse_error;
1861 goto parse_error;
1862 }
1863 }
1864
1865 if (totem_config->net_mtu == 0) {
1866 if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
1867 totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
1868 }
1869 else {
1870 totem_config->net_mtu = 1500;
1871 }
1872 }
1873
1874 return 0;
1875
1876 parse_error:
1877 snprintf (error_string_response, sizeof(error_string_response),
1878 "parse error in config: %s\n", error_reason);
1879 *error_string = error_string_response;
1880 return (-1);
1881
1882 }
1883
1884 static int read_keyfile (
1885 const char *key_location,
1886 struct totem_config *totem_config,
1887 const char **error_string)
1888 {
1889 int fd;
1890 int res;
1891 int saved_errno;
1892 char error_str[100];
1893 const char *error_ptr;
1894
1895 fd = open (key_location, O_RDONLY);
1896 if (fd == -1) {
1897 error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
1898 snprintf (error_string_response, sizeof(error_string_response),
1899 "Could not open %s: %s\n",
1900 key_location, error_ptr);
1901 goto parse_error;
1902 }
1903
1904 res = read (fd, totem_config->private_key, TOTEM_PRIVATE_KEY_LEN_MAX);
1905 saved_errno = errno;
1906 close (fd);
1907
1908 if (res == -1) {
1909 error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
1910 snprintf (error_string_response, sizeof(error_string_response),
1911 "Could not read %s: %s\n",
1912 key_location, error_ptr);
1913 goto parse_error;
1914 }
1915
1916 if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
1917 snprintf (error_string_response, sizeof(error_string_response),
1918 "Could only read %d bits of minimum %u bits from %s.\n",
1919 res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
1920 goto parse_error;
1921 }
1922
1923 totem_config->private_key_len = res;
1924
1925 return 0;
1926
1927 parse_error:
1928 *error_string = error_string_response;
1929 return (-1);
1930 }
1931
1932 int totem_config_keyread (
1933 struct totem_config *totem_config,
1934 const char **error_string)
1935 {
1936 int got_key = 0;
1937 char *key_location = NULL;
1938 int res;
1939 size_t key_len;
1940
1941 memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
1942 totem_config->private_key_len = 0;
1943
1944 if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
1945 strcmp(totem_config->crypto_hash_type, "none") == 0) {
1946 return (0);
1947 }
1948
1949 /* cmap may store the location of the key file */
1950 if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
1951 res = read_keyfile(key_location, totem_config, error_string);
1952 free(key_location);
1953 if (res) {
1954 goto key_error;
1955 }
1956 got_key = 1;
1957 } else { /* Or the key itself may be in the cmap */
1958 if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
1959 if (key_len > sizeof(totem_config->private_key)) {
1960 sprintf(error_string_response, "key is too long");
1961 goto key_error;
1962 }
1963 if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
1964 sprintf(error_string_response, "key is too short");
1965 goto key_error;
1966 }
1967 if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
1968 totem_config->private_key_len = key_len;
1969 got_key = 1;
1970 } else {
1971 sprintf(error_string_response, "can't load private key");
1972 goto key_error;
1973 }
1974 }
1975 }
1976
1977 /* In desperation we read the default filename */
1978 if (!got_key) {
1979 const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
1980 if (!filename)
1981 filename = COROSYSCONFDIR "/authkey";
1982 res = read_keyfile(filename, totem_config, error_string);
1983 if (res)
1984 goto key_error;
1985
1986 }
1987
1988 return (0);
1989
1990 key_error:
1991 *error_string = error_string_response;
1992 return (-1);
1993
1994 }
1995
1996 static void debug_dump_totem_config(const struct totem_config *totem_config)
1997 {
1998
1999 log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
2000 totem_config->token_timeout, totem_config->token_retransmit_timeout);
2001 if (totem_config->token_warning) {
2002 uint32_t token_warning_ms = totem_config->token_warning * totem_config->token_timeout / 100;
2003 log_printf(LOGSYS_LEVEL_DEBUG, "Token warning every %d ms (%d%% of Token Timeout)",
2004 token_warning_ms, totem_config->token_warning);
2005 if (token_warning_ms < totem_config->token_retransmit_timeout)
2006 log_printf (LOGSYS_LEVEL_DEBUG,
2007 "The token warning interval (%d ms) is less than the token retransmit timeout (%d ms) "
2008 "which can lead to spurious token warnings. Consider increasing the token_warning parameter.",
2009 token_warning_ms, totem_config->token_retransmit_timeout);
2010
2011 } else
2012 log_printf(LOGSYS_LEVEL_DEBUG, "Token warnings disabled");
2013 log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
2014 totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
2015 log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
2016 totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
2017 totem_config->merge_timeout);
2018 log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
2019 totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
2020 log_printf(LOGSYS_LEVEL_DEBUG,
2021 "seqno unchanged const (%d rotations) Maximum network MTU %d",
2022 totem_config->seqno_unchanged_const, totem_config->net_mtu);
2023 log_printf(LOGSYS_LEVEL_DEBUG,
2024 "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
2025 totem_config->window_size, totem_config->max_messages);
2026 log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
2027 log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
2028 totem_config->heartbeat_failures_allowed);
2029 log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
2030 }
2031
2032 static void totem_change_notify(
2033 int32_t event,
2034 const char *key_name,
2035 struct icmap_notify_value new_val,
2036 struct icmap_notify_value old_val,
2037 void *user_data)
2038 {
2039 struct totem_config *totem_config = (struct totem_config *)user_data;
2040 uint32_t *param;
2041 uint8_t reloading;
2042 const char *deleted_key = NULL;
2043 const char *error_string;
2044
2045 /*
2046 * If a full reload is in progress then don't do anything until it's done and
2047 * can reconfigure it all atomically
2048 */
2049 if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
2050 return;
2051
2052 param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
2053 /*
2054 * Process change only if changed key is found in totem_config (-> param is not NULL)
2055 * or for special key token_coefficient. token_coefficient key is not stored in
2056 * totem_config, but it is used for computation of token timeout.
2057 */
2058 if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
2059 return;
2060
2061 /*
2062 * Values other than UINT32 are not supported, or needed (yet)
2063 */
2064 switch (event) {
2065 case ICMAP_TRACK_DELETE:
2066 deleted_key = key_name;
2067 break;
2068 case ICMAP_TRACK_ADD:
2069 case ICMAP_TRACK_MODIFY:
2070 deleted_key = NULL;
2071 break;
2072 default:
2073 break;
2074 }
2075
2076 totem_volatile_config_read (totem_config, deleted_key);
2077 log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
2078 debug_dump_totem_config(totem_config);
2079 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2080 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2081 /*
2082 * TODO: Consider corosync exit and/or load defaults for volatile
2083 * values. For now, log error seems to be enough
2084 */
2085 }
2086 }
2087
2088 static void totem_reload_notify(
2089 int32_t event,
2090 const char *key_name,
2091 struct icmap_notify_value new_val,
2092 struct icmap_notify_value old_val,
2093 void *user_data)
2094 {
2095 struct totem_config *totem_config = (struct totem_config *)user_data;
2096 const char *error_string;
2097 uint64_t warnings;
2098
2099 /* Reload has completed */
2100 if (*(uint8_t *)new_val.data == 0) {
2101
2102 totem_config->orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
2103 assert(totem_config->orig_interfaces != NULL);
2104 memcpy(totem_config->orig_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
2105
2106 get_interface_params(totem_config, &error_string, &warnings, 1);
2107 put_nodelist_members_to_config (totem_config, 1);
2108 totem_volatile_config_read (totem_config, NULL);
2109
2110 calc_knet_ping_timers(totem_config);
2111
2112 log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
2113 debug_dump_totem_config(totem_config);
2114 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2115 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2116 /*
2117 * TODO: Consider corosync exit and/or load defaults for volatile
2118 * values. For now, log error seems to be enough
2119 */
2120 }
2121
2122 /* Reinstate the local_node_pos */
2123 (void)find_local_node(0);
2124
2125 /* Reconfigure network params as appropriate */
2126 totempg_reconfigure();
2127
2128 free(totem_config->orig_interfaces);
2129
2130 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
2131 } else {
2132 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
2133 }
2134 }
2135
2136 static void add_totem_config_notification(struct totem_config *totem_config)
2137 {
2138 icmap_track_t icmap_track;
2139
2140 icmap_track_add("totem.",
2141 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
2142 totem_change_notify,
2143 totem_config,
2144 &icmap_track);
2145
2146 icmap_track_add("config.reload_in_progress",
2147 ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
2148 totem_reload_notify,
2149 totem_config,
2150 &icmap_track);
2151
2152 icmap_track_add("nodelist.node.",
2153 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
2154 nodelist_dynamic_notify,
2155 (void *)totem_config,
2156 &icmap_track);
2157 }