]> git.proxmox.com Git - mirror_corosync.git/blob - exec/totemconfig.c
totemconfig: Replace strcpy by strncpy
[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 strncpy(addr_buf, ip_str, sizeof(addr_buf));
1147 addr_buf[sizeof(addr_buf) - 1] = '\0';
1148 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]));
1149 changed = 1;
1150 }
1151 }
1152 }
1153 }
1154
1155 if (changed) {
1156 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");
1157 }
1158 }
1159
1160
1161 static void put_nodelist_members_to_config(struct totem_config *totem_config, int reload)
1162 {
1163 icmap_iter_t iter, iter2;
1164 const char *iter_key, *iter_key2;
1165 int res = 0;
1166 unsigned int node_pos;
1167 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1168 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1169 char *node_addr_str;
1170 int member_count;
1171 unsigned int linknumber = 0;
1172 int i, j;
1173 struct totem_interface *new_interfaces = NULL;
1174
1175 if (reload) {
1176 /*
1177 * We need to compute diff only for reload. Also for initial configuration
1178 * not all totem structures are initialized so corosync will crash during
1179 * member_add/remove
1180 */
1181 new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1182 assert(new_interfaces != NULL);
1183 }
1184
1185 /* Clear out nodelist so we can put the new one in if needed */
1186 for (i = 0; i < INTERFACE_MAX; i++) {
1187 for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
1188 memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
1189 }
1190 totem_config->interfaces[i].member_count = 0;
1191 }
1192
1193 iter = icmap_iter_init("nodelist.node.");
1194 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1195 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
1196 if (res != 2) {
1197 continue;
1198 }
1199 if (strcmp(tmp_key, "nodeid") != 0) {
1200 continue;
1201 }
1202
1203 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1204 iter2 = icmap_iter_init(tmp_key);
1205 while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
1206 unsigned int nodeid;
1207
1208 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
1209 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
1210 }
1211
1212 res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1213 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1214 continue;
1215 }
1216
1217 if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
1218 continue;
1219 }
1220
1221 member_count = totem_config->interfaces[linknumber].member_count;
1222 res = totemip_parse(&totem_config->interfaces[linknumber].member_list[member_count],
1223 node_addr_str, totem_config->ip_version);
1224 if (res != -1) {
1225 totem_config->interfaces[linknumber].member_list[member_count].nodeid = nodeid;
1226 totem_config->interfaces[linknumber].member_count++;
1227 }
1228 totem_config->interfaces[linknumber].configured = 1;
1229 free(node_addr_str);
1230 }
1231
1232 icmap_iter_finalize(iter2);
1233 }
1234
1235 icmap_iter_finalize(iter);
1236
1237 if (reload) {
1238 log_printf(LOGSYS_LEVEL_DEBUG, "About to reconfigure links from nodelist.\n");
1239 reconfigure_links(totem_config);
1240
1241 memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
1242
1243 check_things_have_not_changed(totem_config);
1244
1245 compute_interfaces_diff(totem_config->orig_interfaces, new_interfaces);
1246
1247 free(new_interfaces);
1248 }
1249 }
1250
1251 static void nodelist_dynamic_notify(
1252 int32_t event,
1253 const char *key_name,
1254 struct icmap_notify_value new_val,
1255 struct icmap_notify_value old_val,
1256 void *user_data)
1257 {
1258 int res;
1259 unsigned int ring_no;
1260 unsigned int member_no;
1261 char tmp_str[ICMAP_KEYNAME_MAXLEN];
1262 uint8_t reloading;
1263 struct totem_config *totem_config = (struct totem_config *)user_data;
1264
1265 /*
1266 * If a full reload is in progress then don't do anything until it's done and
1267 * can reconfigure it all atomically
1268 */
1269 if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
1270 return ;
1271 }
1272
1273 res = sscanf(key_name, "nodelist.node.%u.ring%u%s", &member_no, &ring_no, tmp_str);
1274 if (res != 3)
1275 return ;
1276
1277 if (strcmp(tmp_str, "_addr") != 0) {
1278 return;
1279 }
1280
1281 put_nodelist_members_to_config(totem_config, 1);
1282 }
1283
1284
1285
1286 static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
1287 {
1288 int res = 0;
1289 int node_pos;
1290 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1291 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1292 char *node_addr_str;
1293 unsigned int linknumber = 0;
1294 icmap_iter_t iter;
1295 const char *iter_key;
1296
1297 node_pos = find_local_node(1);
1298 if (node_pos > -1) {
1299 /*
1300 * We found node, so create interface section
1301 */
1302 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1303 iter = icmap_iter_init(tmp_key);
1304 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1305 res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1306 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1307 continue ;
1308 }
1309
1310 if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
1311 continue;
1312 }
1313
1314 snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1315 icmap_set_string(tmp_key2, node_addr_str);
1316 free(node_addr_str);
1317 }
1318 icmap_iter_finalize(iter);
1319 }
1320 }
1321
1322 static int get_interface_params(struct totem_config *totem_config,
1323 const char **error_string, uint64_t *warnings,
1324 int reload)
1325 {
1326 int res = 0;
1327 unsigned int linknumber = 0;
1328 int member_count = 0;
1329 int i;
1330 icmap_iter_t iter, member_iter;
1331 const char *iter_key;
1332 const char *member_iter_key;
1333 char linknumber_key[ICMAP_KEYNAME_MAXLEN];
1334 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1335 uint8_t u8;
1336 uint32_t u32;
1337 char *str;
1338 char *cluster_name = NULL;
1339
1340 if (reload) {
1341 for (i=0; i<INTERFACE_MAX; i++) {
1342 /*
1343 * Set back to defaults things that might have been configured and
1344 * now have been taken out of corosync.conf. These won't be caught by the
1345 * code below which only looks at interface{} sections that actually exist.
1346 */
1347 totem_config->interfaces[i].configured = 0;
1348 totem_config->interfaces[i].knet_ping_timeout = 0;
1349 totem_config->interfaces[i].knet_ping_interval = 0;
1350 totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
1351 totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
1352 }
1353 }
1354 if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
1355 cluster_name = NULL;
1356 }
1357
1358 iter = icmap_iter_init("totem.interface.");
1359 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1360 res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
1361 if (res != 2) {
1362 continue;
1363 }
1364
1365 if (strcmp(tmp_key, "bindnetaddr") != 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1366 continue;
1367 }
1368
1369 member_count = 0;
1370 linknumber = atoi(linknumber_key);
1371
1372 if (linknumber >= INTERFACE_MAX) {
1373 free(cluster_name);
1374
1375 snprintf (error_string_response, sizeof(error_string_response),
1376 "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1377 linknumber, INTERFACE_MAX - 1);
1378
1379 *error_string = error_string_response;
1380 return -1;
1381 }
1382
1383 /* These things are only valid for the initial read */
1384 if (!reload) {
1385 /*
1386 * Get the bind net address
1387 */
1388 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1389
1390 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1391 res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
1392 totem_config->ip_version);
1393 free(str);
1394 }
1395
1396 /*
1397 * Get interface multicast address
1398 */
1399 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
1400 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1401 res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str, totem_config->ip_version);
1402 free(str);
1403 } else {
1404 /*
1405 * User not specified address -> autogenerate one from cluster_name key
1406 * (if available). Return code is intentionally ignored, because
1407 * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1408 * checked later anyway.
1409 */
1410 (void)get_cluster_mcast_addr (cluster_name,
1411 linknumber,
1412 totem_config->ip_version,
1413 &totem_config->interfaces[linknumber].mcast_addr);
1414 }
1415
1416 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
1417 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1418 if (strcmp (str, "yes") == 0) {
1419 totem_config->broadcast_use = 1;
1420 }
1421 free(str);
1422 }
1423 }
1424
1425 /* These things are only valid for the initial read OR a newly-defined link */
1426 if (!reload || (totem_config->interfaces[linknumber].configured == 0)) {
1427
1428 /*
1429 * Get mcast port
1430 */
1431 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
1432 if (icmap_get_uint16(tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
1433 if (totem_config->broadcast_use) {
1434 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
1435 } else {
1436 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + linknumber;
1437 }
1438 }
1439
1440 /*
1441 * Get the TTL
1442 */
1443 totem_config->interfaces[linknumber].ttl = 1;
1444
1445 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
1446
1447 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1448 totem_config->interfaces[linknumber].ttl = u8;
1449 }
1450
1451 totem_config->interfaces[linknumber].knet_transport = KNET_DEFAULT_TRANSPORT;
1452 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
1453 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1454 if (strcmp(str, "sctp") == 0) {
1455 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
1456 }
1457 else if (strcmp(str, "udp") == 0) {
1458 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
1459 }
1460 else {
1461 *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1462 return -1;
1463 }
1464 }
1465 }
1466 totem_config->interfaces[linknumber].configured = 1;
1467
1468 /*
1469 * Get the knet link params
1470 */
1471 totem_config->interfaces[linknumber].knet_link_priority = 1;
1472 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
1473
1474 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1475 totem_config->interfaces[linknumber].knet_link_priority = u8;
1476 }
1477
1478 totem_config->interfaces[linknumber].knet_ping_interval = 0; /* real default applied later */
1479 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
1480 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1481 totem_config->interfaces[linknumber].knet_ping_interval = u32;
1482 }
1483 totem_config->interfaces[linknumber].knet_ping_timeout = 0; /* real default applied later */
1484 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
1485 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1486 totem_config->interfaces[linknumber].knet_ping_timeout = u32;
1487 }
1488 totem_config->interfaces[linknumber].knet_ping_precision = KNET_PING_PRECISION;
1489 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
1490 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1491 totem_config->interfaces[linknumber].knet_ping_precision = u32;
1492 }
1493 totem_config->interfaces[linknumber].knet_pong_count = KNET_PONG_COUNT;
1494 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
1495 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1496 totem_config->interfaces[linknumber].knet_pong_count = u32;
1497 }
1498
1499 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
1500 member_iter = icmap_iter_init(tmp_key);
1501 while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
1502 if (member_count == 0) {
1503 if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
1504 free(str);
1505 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
1506 break;
1507 } else {
1508 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
1509 }
1510 }
1511
1512 if (icmap_get_string(member_iter_key, &str) == CS_OK) {
1513 res = totemip_parse (&totem_config->interfaces[linknumber].member_list[member_count++],
1514 str, totem_config->ip_version);
1515 }
1516 }
1517 icmap_iter_finalize(member_iter);
1518
1519 totem_config->interfaces[linknumber].member_count = member_count;
1520
1521 }
1522 icmap_iter_finalize(iter);
1523
1524 return 0;
1525 }
1526
1527 extern int totem_config_read (
1528 struct totem_config *totem_config,
1529 const char **error_string,
1530 uint64_t *warnings)
1531 {
1532 int res = 0;
1533 char *str, *ring0_addr_str;
1534 char tmp_key[ICMAP_KEYNAME_MAXLEN];
1535 uint16_t u16;
1536 int i;
1537 int local_node_pos;
1538 int nodeid_set;
1539
1540 *warnings = 0;
1541
1542 memset (totem_config, 0, sizeof (struct totem_config));
1543 totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1544 if (totem_config->interfaces == 0) {
1545 *error_string = "Out of memory trying to allocate ethernet interface storage area";
1546 return -1;
1547 }
1548
1549 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1550 if (icmap_get_string("totem.transport", &str) == CS_OK) {
1551 if (strcmp (str, "udpu") == 0) {
1552 totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
1553 }
1554
1555 if (strcmp (str, "udp") == 0) {
1556 totem_config->transport_number = TOTEM_TRANSPORT_UDP;
1557 }
1558
1559 if (strcmp (str, "knet") == 0) {
1560 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1561 }
1562
1563 free(str);
1564 }
1565
1566 memset (totem_config->interfaces, 0,
1567 sizeof (struct totem_interface) * INTERFACE_MAX);
1568
1569 strcpy (totem_config->link_mode, "passive");
1570
1571 icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
1572
1573 if (totem_get_crypto(totem_config, error_string) != 0) {
1574 return -1;
1575 }
1576
1577 if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
1578 if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
1579 *error_string = "totem.link_mode is too long";
1580 free(str);
1581
1582 return -1;
1583 }
1584 strcpy (totem_config->link_mode, str);
1585 free(str);
1586 }
1587
1588 icmap_get_uint32("totem.nodeid", &totem_config->node_id);
1589
1590 totem_config->clear_node_high_bit = 0;
1591 if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
1592 if (strcmp (str, "yes") == 0) {
1593 totem_config->clear_node_high_bit = 1;
1594 }
1595 free(str);
1596 }
1597
1598 icmap_get_uint32("totem.threads", &totem_config->threads);
1599
1600 icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
1601
1602 totem_config->ip_version = totem_config_get_ip_version(totem_config);
1603
1604 if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
1605 /*
1606 * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1607 */
1608 config_convert_nodelist_to_interface(totem_config);
1609 } else {
1610 if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
1611 /*
1612 * Both bindnetaddr and ring0_addr are set.
1613 * Log warning information, and use nodelist instead
1614 */
1615 *warnings |= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET;
1616
1617 config_convert_nodelist_to_interface(totem_config);
1618
1619 free(ring0_addr_str);
1620 }
1621
1622 free(str);
1623 }
1624
1625 /*
1626 * Broadcast option is global but set in interface section,
1627 * so reset before processing interfaces.
1628 */
1629 totem_config->broadcast_use = 0;
1630
1631 res = get_interface_params(totem_config, error_string, warnings, 0);
1632 if (res < 0) {
1633 return res;
1634 }
1635
1636 /*
1637 * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1638 * broadcast is only supported for UDP so just do interface 0;
1639 */
1640 if (totem_config->broadcast_use) {
1641 totemip_parse (&totem_config->interfaces[0].mcast_addr,
1642 "255.255.255.255", 0);
1643 }
1644
1645
1646 /*
1647 * Store automatically generated items back to icmap only for UDP
1648 */
1649 if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1650 for (i = 0; i < INTERFACE_MAX; i++) {
1651 if (!totem_config->interfaces[i].configured) {
1652 continue;
1653 }
1654 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
1655 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1656 free(str);
1657 } else {
1658 str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
1659 icmap_set_string(tmp_key, str);
1660 }
1661
1662 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
1663 if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
1664 icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
1665 }
1666 }
1667 }
1668
1669 /*
1670 * Check existence of nodelist
1671 */
1672 if ((icmap_get_string("nodelist.node.0.name", &str) == CS_OK) ||
1673 (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK)) {
1674 free(str);
1675 /*
1676 * find local node
1677 */
1678 local_node_pos = find_local_node(1);
1679 if (local_node_pos != -1) {
1680
1681 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
1682
1683 nodeid_set = (totem_config->node_id != 0);
1684 if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
1685 *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
1686 }
1687 if ((totem_config->transport_number == TOTEM_TRANSPORT_KNET) && (!totem_config->node_id)) {
1688 *error_string = "With knet, you must specify nodeid for current node";
1689 return -1;
1690 }
1691
1692 /* Users must not change this */
1693 icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1694 }
1695
1696 put_nodelist_members_to_config(totem_config, 0);
1697 }
1698
1699 /*
1700 * Get things that might change in the future (and can depend on totem_config->interfaces);
1701 */
1702 totem_volatile_config_read(totem_config, NULL);
1703
1704 calc_knet_ping_timers(totem_config);
1705
1706 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1707
1708 add_totem_config_notification(totem_config);
1709
1710 return 0;
1711 }
1712
1713
1714 int totem_config_validate (
1715 struct totem_config *totem_config,
1716 const char **error_string)
1717 {
1718 static char local_error_reason[512];
1719 char parse_error[512];
1720 const char *error_reason = local_error_reason;
1721 int i,j;
1722 uint32_t u32;
1723 int num_configured = 0;
1724 unsigned int interface_max = INTERFACE_MAX;
1725
1726 for (i = 0; i < INTERFACE_MAX; i++) {
1727 if (totem_config->interfaces[i].configured) {
1728 num_configured++;
1729 }
1730 }
1731 if (num_configured == 0) {
1732 error_reason = "No interfaces defined";
1733 goto parse_error;
1734 }
1735
1736 /* Check we found a local node name */
1737 if (icmap_get_uint32("nodelist.local_node_pos", &u32) != CS_OK) {
1738 error_reason = "No valid name found for local host";
1739 goto parse_error;
1740 }
1741
1742 for (i = 0; i < INTERFACE_MAX; i++) {
1743 /*
1744 * Some error checking of parsed data to make sure its valid
1745 */
1746
1747 struct totem_ip_address null_addr;
1748
1749 if (!totem_config->interfaces[i].configured) {
1750 continue;
1751 }
1752
1753 memset (&null_addr, 0, sizeof (struct totem_ip_address));
1754
1755 if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP) &&
1756 memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
1757 sizeof (struct totem_ip_address)) == 0) {
1758 error_reason = "No multicast address specified";
1759 goto parse_error;
1760 }
1761
1762 if (totem_config->interfaces[i].ip_port == 0) {
1763 error_reason = "No multicast port specified";
1764 goto parse_error;
1765 }
1766
1767 if (totem_config->interfaces[i].ttl > 255) {
1768 error_reason = "Invalid TTL (should be 0..255)";
1769 goto parse_error;
1770 }
1771 if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
1772 totem_config->interfaces[i].ttl != 1) {
1773 error_reason = "Can only set ttl on multicast transport types";
1774 goto parse_error;
1775 }
1776 if (totem_config->interfaces[i].knet_link_priority > 255) {
1777 error_reason = "Invalid link priority (should be 0..255)";
1778 goto parse_error;
1779 }
1780 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET &&
1781 totem_config->interfaces[i].knet_link_priority != 1) {
1782 error_reason = "Can only set link priority on knet transport type";
1783 goto parse_error;
1784 }
1785
1786 if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
1787 totem_config->node_id == 0) {
1788
1789 error_reason = "An IPV6 network requires that a node ID be specified.";
1790 goto parse_error;
1791 }
1792
1793 if (totem_config->broadcast_use == 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1794 if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
1795 error_reason = "Multicast address family does not match bind address family";
1796 goto parse_error;
1797 }
1798
1799 if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
1800 error_reason = "mcastaddr is not a correct multicast address.";
1801 goto parse_error;
1802 }
1803 }
1804 /* Verify that all nodes on the same knet link have the same IP family */
1805 for (j=1; j<totem_config->interfaces[i].member_count; j++) {
1806 if (totem_config->interfaces[i].configured) {
1807 if (totem_config->interfaces[i].member_list[j].family !=
1808 totem_config->interfaces[i].member_list[0].family) {
1809 snprintf (local_error_reason, sizeof(local_error_reason),
1810 "Nodes for link %d have different IP families", i);
1811 goto parse_error;
1812 }
1813 }
1814 }
1815 }
1816
1817 if (totem_config->version != 2) {
1818 error_reason = "This totem parser can only parse version 2 configurations.";
1819 goto parse_error;
1820 }
1821
1822 if (totem_volatile_config_validate(totem_config, error_string) == -1) {
1823 return (-1);
1824 }
1825
1826 if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
1827 return (-1);
1828 }
1829
1830 /*
1831 * KNET Link values validation
1832 */
1833 if (strcmp (totem_config->link_mode, "active") &&
1834 strcmp (totem_config->link_mode, "rr") &&
1835 strcmp (totem_config->link_mode, "passive")) {
1836 snprintf (local_error_reason, sizeof(local_error_reason),
1837 "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
1838 goto parse_error;
1839 }
1840
1841 /* Only Knet does multiple interfaces */
1842 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
1843 interface_max = 1;
1844 }
1845
1846 if (interface_max < num_configured) {
1847 snprintf (parse_error, sizeof(parse_error),
1848 "%d is too many configured interfaces for non-Knet transport.",
1849 num_configured);
1850 error_reason = parse_error;
1851 goto parse_error;
1852 }
1853
1854 /* Only knet allows crypto */
1855 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
1856 if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
1857 (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
1858
1859 snprintf (parse_error, sizeof(parse_error),
1860 "crypto_cipher & crypto_hash are only valid for the Knet transport.");
1861 error_reason = parse_error;
1862 goto parse_error;
1863 }
1864 }
1865
1866 if (totem_config->net_mtu == 0) {
1867 if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
1868 totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
1869 }
1870 else {
1871 totem_config->net_mtu = 1500;
1872 }
1873 }
1874
1875 return 0;
1876
1877 parse_error:
1878 snprintf (error_string_response, sizeof(error_string_response),
1879 "parse error in config: %s\n", error_reason);
1880 *error_string = error_string_response;
1881 return (-1);
1882
1883 }
1884
1885 static int read_keyfile (
1886 const char *key_location,
1887 struct totem_config *totem_config,
1888 const char **error_string)
1889 {
1890 int fd;
1891 int res;
1892 int saved_errno;
1893 char error_str[100];
1894 const char *error_ptr;
1895
1896 fd = open (key_location, O_RDONLY);
1897 if (fd == -1) {
1898 error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
1899 snprintf (error_string_response, sizeof(error_string_response),
1900 "Could not open %s: %s\n",
1901 key_location, error_ptr);
1902 goto parse_error;
1903 }
1904
1905 res = read (fd, totem_config->private_key, TOTEM_PRIVATE_KEY_LEN_MAX);
1906 saved_errno = errno;
1907 close (fd);
1908
1909 if (res == -1) {
1910 error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
1911 snprintf (error_string_response, sizeof(error_string_response),
1912 "Could not read %s: %s\n",
1913 key_location, error_ptr);
1914 goto parse_error;
1915 }
1916
1917 if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
1918 snprintf (error_string_response, sizeof(error_string_response),
1919 "Could only read %d bits of minimum %u bits from %s.\n",
1920 res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
1921 goto parse_error;
1922 }
1923
1924 totem_config->private_key_len = res;
1925
1926 return 0;
1927
1928 parse_error:
1929 *error_string = error_string_response;
1930 return (-1);
1931 }
1932
1933 int totem_config_keyread (
1934 struct totem_config *totem_config,
1935 const char **error_string)
1936 {
1937 int got_key = 0;
1938 char *key_location = NULL;
1939 int res;
1940 size_t key_len;
1941
1942 memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
1943 totem_config->private_key_len = 0;
1944
1945 if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
1946 strcmp(totem_config->crypto_hash_type, "none") == 0) {
1947 return (0);
1948 }
1949
1950 /* cmap may store the location of the key file */
1951 if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
1952 res = read_keyfile(key_location, totem_config, error_string);
1953 free(key_location);
1954 if (res) {
1955 goto key_error;
1956 }
1957 got_key = 1;
1958 } else { /* Or the key itself may be in the cmap */
1959 if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
1960 if (key_len > sizeof(totem_config->private_key)) {
1961 sprintf(error_string_response, "key is too long");
1962 goto key_error;
1963 }
1964 if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
1965 sprintf(error_string_response, "key is too short");
1966 goto key_error;
1967 }
1968 if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
1969 totem_config->private_key_len = key_len;
1970 got_key = 1;
1971 } else {
1972 sprintf(error_string_response, "can't load private key");
1973 goto key_error;
1974 }
1975 }
1976 }
1977
1978 /* In desperation we read the default filename */
1979 if (!got_key) {
1980 const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
1981 if (!filename)
1982 filename = COROSYSCONFDIR "/authkey";
1983 res = read_keyfile(filename, totem_config, error_string);
1984 if (res)
1985 goto key_error;
1986
1987 }
1988
1989 return (0);
1990
1991 key_error:
1992 *error_string = error_string_response;
1993 return (-1);
1994
1995 }
1996
1997 static void debug_dump_totem_config(const struct totem_config *totem_config)
1998 {
1999
2000 log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
2001 totem_config->token_timeout, totem_config->token_retransmit_timeout);
2002 if (totem_config->token_warning) {
2003 uint32_t token_warning_ms = totem_config->token_warning * totem_config->token_timeout / 100;
2004 log_printf(LOGSYS_LEVEL_DEBUG, "Token warning every %d ms (%d%% of Token Timeout)",
2005 token_warning_ms, totem_config->token_warning);
2006 if (token_warning_ms < totem_config->token_retransmit_timeout)
2007 log_printf (LOGSYS_LEVEL_DEBUG,
2008 "The token warning interval (%d ms) is less than the token retransmit timeout (%d ms) "
2009 "which can lead to spurious token warnings. Consider increasing the token_warning parameter.",
2010 token_warning_ms, totem_config->token_retransmit_timeout);
2011
2012 } else
2013 log_printf(LOGSYS_LEVEL_DEBUG, "Token warnings disabled");
2014 log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
2015 totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
2016 log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
2017 totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
2018 totem_config->merge_timeout);
2019 log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
2020 totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
2021 log_printf(LOGSYS_LEVEL_DEBUG,
2022 "seqno unchanged const (%d rotations) Maximum network MTU %d",
2023 totem_config->seqno_unchanged_const, totem_config->net_mtu);
2024 log_printf(LOGSYS_LEVEL_DEBUG,
2025 "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
2026 totem_config->window_size, totem_config->max_messages);
2027 log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
2028 log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
2029 totem_config->heartbeat_failures_allowed);
2030 log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
2031 }
2032
2033 static void totem_change_notify(
2034 int32_t event,
2035 const char *key_name,
2036 struct icmap_notify_value new_val,
2037 struct icmap_notify_value old_val,
2038 void *user_data)
2039 {
2040 struct totem_config *totem_config = (struct totem_config *)user_data;
2041 uint32_t *param;
2042 uint8_t reloading;
2043 const char *deleted_key = NULL;
2044 const char *error_string;
2045
2046 /*
2047 * If a full reload is in progress then don't do anything until it's done and
2048 * can reconfigure it all atomically
2049 */
2050 if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
2051 return;
2052
2053 param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
2054 /*
2055 * Process change only if changed key is found in totem_config (-> param is not NULL)
2056 * or for special key token_coefficient. token_coefficient key is not stored in
2057 * totem_config, but it is used for computation of token timeout.
2058 */
2059 if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
2060 return;
2061
2062 /*
2063 * Values other than UINT32 are not supported, or needed (yet)
2064 */
2065 switch (event) {
2066 case ICMAP_TRACK_DELETE:
2067 deleted_key = key_name;
2068 break;
2069 case ICMAP_TRACK_ADD:
2070 case ICMAP_TRACK_MODIFY:
2071 deleted_key = NULL;
2072 break;
2073 default:
2074 break;
2075 }
2076
2077 totem_volatile_config_read (totem_config, deleted_key);
2078 log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
2079 debug_dump_totem_config(totem_config);
2080 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2081 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2082 /*
2083 * TODO: Consider corosync exit and/or load defaults for volatile
2084 * values. For now, log error seems to be enough
2085 */
2086 }
2087 }
2088
2089 static void totem_reload_notify(
2090 int32_t event,
2091 const char *key_name,
2092 struct icmap_notify_value new_val,
2093 struct icmap_notify_value old_val,
2094 void *user_data)
2095 {
2096 struct totem_config *totem_config = (struct totem_config *)user_data;
2097 const char *error_string;
2098 uint64_t warnings;
2099
2100 /* Reload has completed */
2101 if (*(uint8_t *)new_val.data == 0) {
2102
2103 totem_config->orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
2104 assert(totem_config->orig_interfaces != NULL);
2105 memcpy(totem_config->orig_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
2106
2107 get_interface_params(totem_config, &error_string, &warnings, 1);
2108 put_nodelist_members_to_config (totem_config, 1);
2109 totem_volatile_config_read (totem_config, NULL);
2110
2111 calc_knet_ping_timers(totem_config);
2112
2113 log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
2114 debug_dump_totem_config(totem_config);
2115 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2116 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2117 /*
2118 * TODO: Consider corosync exit and/or load defaults for volatile
2119 * values. For now, log error seems to be enough
2120 */
2121 }
2122
2123 /* Reinstate the local_node_pos */
2124 (void)find_local_node(0);
2125
2126 /* Reconfigure network params as appropriate */
2127 totempg_reconfigure();
2128
2129 free(totem_config->orig_interfaces);
2130
2131 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
2132 } else {
2133 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
2134 }
2135 }
2136
2137 static void add_totem_config_notification(struct totem_config *totem_config)
2138 {
2139 icmap_track_t icmap_track;
2140
2141 icmap_track_add("totem.",
2142 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
2143 totem_change_notify,
2144 totem_config,
2145 &icmap_track);
2146
2147 icmap_track_add("config.reload_in_progress",
2148 ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
2149 totem_reload_notify,
2150 totem_config,
2151 &icmap_track);
2152
2153 icmap_track_add("nodelist.node.",
2154 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
2155 nodelist_dynamic_notify,
2156 (void *)totem_config,
2157 &icmap_track);
2158 }