]> git.proxmox.com Git - mirror_corosync.git/blob - exec/totemconfig.c
totemconfig: Make crypto work again
[mirror_corosync.git] / exec / totemconfig.c
1 /*
2 * Copyright (c) 2002-2005 MontaVista Software, Inc.
3 * Copyright (c) 2006-2013 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 <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <sys/param.h>
51
52 #include <corosync/swab.h>
53 #include <qb/qblist.h>
54 #include <qb/qbdefs.h>
55 #include <libknet.h>
56 #include <corosync/totem/totem.h>
57 #include <corosync/config.h>
58 #include <corosync/logsys.h>
59 #include <corosync/icmap.h>
60
61 #include "util.h"
62 #include "totemconfig.h"
63
64 #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
65 #define TOKEN_TIMEOUT 1000
66 #define TOKEN_COEFFICIENT 650
67 #define JOIN_TIMEOUT 50
68 #define MERGE_TIMEOUT 200
69 #define DOWNCHECK_TIMEOUT 1000
70 #define FAIL_TO_RECV_CONST 2500
71 #define SEQNO_UNCHANGED_CONST 30
72 #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
73 #define MAX_NETWORK_DELAY 50
74 #define WINDOW_SIZE 50
75 #define MAX_MESSAGES 17
76 #define MISS_COUNT_CONST 5
77
78 /* These currently match the defaults in libknet.h */
79 #define KNET_PING_INTERVAL 1000
80 #define KNET_PING_TIMEOUT 2000
81 #define KNET_PING_PRECISION 2048
82 #define KNET_PONG_COUNT 2
83 #define KNET_PMTUD_INTERVAL 30
84 #define KNET_DEFAULT_TRANSPORT KNET_TRANSPORT_UDP
85
86 #define DEFAULT_PORT 5405
87
88 static char error_string_response[512];
89
90 static void add_totem_config_notification(struct totem_config *totem_config);
91
92
93 /* All the volatile parameters are uint32s, luckily */
94 static uint32_t *totem_get_param_by_name(struct totem_config *totem_config, const char *param_name)
95 {
96 if (strcmp(param_name, "totem.token") == 0)
97 return &totem_config->token_timeout;
98 if (strcmp(param_name, "totem.token_retransmit") == 0)
99 return &totem_config->token_retransmit_timeout;
100 if (strcmp(param_name, "totem.hold") == 0)
101 return &totem_config->token_hold_timeout;
102 if (strcmp(param_name, "totem.token_retransmits_before_loss_const") == 0)
103 return &totem_config->token_retransmits_before_loss_const;
104 if (strcmp(param_name, "totem.join") == 0)
105 return &totem_config->join_timeout;
106 if (strcmp(param_name, "totem.send_join") == 0)
107 return &totem_config->send_join_timeout;
108 if (strcmp(param_name, "totem.consensus") == 0)
109 return &totem_config->consensus_timeout;
110 if (strcmp(param_name, "totem.merge") == 0)
111 return &totem_config->merge_timeout;
112 if (strcmp(param_name, "totem.downcheck") == 0)
113 return &totem_config->downcheck_timeout;
114 if (strcmp(param_name, "totem.fail_recv_const") == 0)
115 return &totem_config->fail_to_recv_const;
116 if (strcmp(param_name, "totem.seqno_unchanged_const") == 0)
117 return &totem_config->seqno_unchanged_const;
118 if (strcmp(param_name, "totem.heartbeat_failures_allowed") == 0)
119 return &totem_config->heartbeat_failures_allowed;
120 if (strcmp(param_name, "totem.max_network_delay") == 0)
121 return &totem_config->max_network_delay;
122 if (strcmp(param_name, "totem.window_size") == 0)
123 return &totem_config->window_size;
124 if (strcmp(param_name, "totem.max_messages") == 0)
125 return &totem_config->max_messages;
126 if (strcmp(param_name, "totem.miss_count_const") == 0)
127 return &totem_config->miss_count_const;
128 if (strcmp(param_name, "totem.knet_pmtud_interval") == 0)
129 return &totem_config->knet_pmtud_interval;
130
131 return NULL;
132 }
133
134 /*
135 * Read key_name from icmap. If key is not found or key_name == delete_key or if allow_zero is false
136 * and readed value is zero, default value is used and stored into totem_config.
137 */
138 static void totem_volatile_config_set_value (struct totem_config *totem_config,
139 const char *key_name, const char *deleted_key, unsigned int default_value,
140 int allow_zero_value)
141 {
142 char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
143
144 if (icmap_get_uint32(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
145 (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
146 (!allow_zero_value && *totem_get_param_by_name(totem_config, key_name) == 0)) {
147 *totem_get_param_by_name(totem_config, key_name) = default_value;
148 }
149
150 /*
151 * Store totem_config value to cmap runtime section
152 */
153 if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
154 /*
155 * This shouldn't happen
156 */
157 return ;
158 }
159
160 strcpy(runtime_key_name, "runtime.config.");
161 strcat(runtime_key_name, key_name);
162
163 icmap_set_uint32(runtime_key_name, *totem_get_param_by_name(totem_config, key_name));
164 }
165
166
167 /*
168 * Read and validate config values from cmap and store them into totem_config. If key doesn't exists,
169 * default value is stored. deleted_key is name of key beeing processed by delete operation
170 * from cmap. It is considered as non existing even if it can be read. Can be NULL.
171 */
172 static void totem_volatile_config_read (struct totem_config *totem_config, const char *deleted_key)
173 {
174 uint32_t u32;
175
176 totem_volatile_config_set_value(totem_config, "totem.token_retransmits_before_loss_const", deleted_key,
177 TOKEN_RETRANSMITS_BEFORE_LOSS_CONST, 0);
178
179 totem_volatile_config_set_value(totem_config, "totem.token", deleted_key, TOKEN_TIMEOUT, 0);
180
181 if (totem_config->interface_count > 0 && totem_config->interfaces[0].member_count > 2) {
182 u32 = TOKEN_COEFFICIENT;
183 icmap_get_uint32("totem.token_coefficient", &u32);
184 totem_config->token_timeout += (totem_config->interfaces[0].member_count - 2) * u32;
185
186 /*
187 * Store totem_config value to cmap runtime section
188 */
189 icmap_set_uint32("runtime.config.totem.token", totem_config->token_timeout);
190 }
191
192 totem_volatile_config_set_value(totem_config, "totem.max_network_delay", deleted_key, MAX_NETWORK_DELAY, 0);
193
194 totem_volatile_config_set_value(totem_config, "totem.window_size", deleted_key, WINDOW_SIZE, 0);
195
196 totem_volatile_config_set_value(totem_config, "totem.max_messages", deleted_key, MAX_MESSAGES, 0);
197
198 totem_volatile_config_set_value(totem_config, "totem.miss_count_const", deleted_key, MISS_COUNT_CONST, 0);
199 totem_volatile_config_set_value(totem_config, "totem.knet_pmtud_interval", deleted_key, KNET_PMTUD_INTERVAL, 0);
200
201 totem_volatile_config_set_value(totem_config, "totem.token_retransmit", deleted_key,
202 (int)(totem_config->token_timeout / (totem_config->token_retransmits_before_loss_const + 0.2)), 0);
203
204 totem_volatile_config_set_value(totem_config, "totem.hold", deleted_key,
205 (int)(totem_config->token_retransmit_timeout * 0.8 - (1000/HZ)), 0);
206
207 totem_volatile_config_set_value(totem_config, "totem.join", deleted_key, JOIN_TIMEOUT, 0);
208
209 totem_volatile_config_set_value(totem_config, "totem.consensus", deleted_key,
210 (int)(float)(1.2 * totem_config->token_timeout), 0);
211
212 totem_volatile_config_set_value(totem_config, "totem.merge", deleted_key, MERGE_TIMEOUT, 0);
213
214 totem_volatile_config_set_value(totem_config, "totem.downcheck", deleted_key, DOWNCHECK_TIMEOUT, 0);
215
216 totem_volatile_config_set_value(totem_config, "totem.fail_recv_const", deleted_key, FAIL_TO_RECV_CONST, 0);
217
218 totem_volatile_config_set_value(totem_config, "totem.seqno_unchanged_const", deleted_key,
219 SEQNO_UNCHANGED_CONST, 0);
220
221 totem_volatile_config_set_value(totem_config, "totem.send_join", deleted_key, 0, 1);
222
223 totem_volatile_config_set_value(totem_config, "totem.heartbeat_failures_allowed", deleted_key, 0, 1);
224 }
225
226 static int totem_volatile_config_validate (
227 struct totem_config *totem_config,
228 const char **error_string)
229 {
230 static char local_error_reason[512];
231 const char *error_reason = local_error_reason;
232
233 if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
234 snprintf (local_error_reason, sizeof(local_error_reason),
235 "The max_network_delay parameter (%d ms) may not be less than (%d ms).",
236 totem_config->max_network_delay, MINIMUM_TIMEOUT);
237 goto parse_error;
238 }
239
240 if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
241 snprintf (local_error_reason, sizeof(local_error_reason),
242 "The token timeout parameter (%d ms) may not be less than (%d ms).",
243 totem_config->token_timeout, MINIMUM_TIMEOUT);
244 goto parse_error;
245 }
246
247 if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
248 snprintf (local_error_reason, sizeof(local_error_reason),
249 "The token retransmit timeout parameter (%d ms) may not be less than (%d ms).",
250 totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
251 goto parse_error;
252 }
253
254 if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
255 snprintf (local_error_reason, sizeof(local_error_reason),
256 "The token hold timeout parameter (%d ms) may not be less than (%d ms).",
257 totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
258 goto parse_error;
259 }
260
261 if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
262 snprintf (local_error_reason, sizeof(local_error_reason),
263 "The join timeout parameter (%d ms) may not be less than (%d ms).",
264 totem_config->join_timeout, MINIMUM_TIMEOUT);
265 goto parse_error;
266 }
267
268 if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
269 snprintf (local_error_reason, sizeof(local_error_reason),
270 "The consensus timeout parameter (%d ms) may not be less than (%d ms).",
271 totem_config->consensus_timeout, MINIMUM_TIMEOUT);
272 goto parse_error;
273 }
274
275 if (totem_config->consensus_timeout < totem_config->join_timeout) {
276 snprintf (local_error_reason, sizeof(local_error_reason),
277 "The consensus timeout parameter (%d ms) may not be less than join timeout (%d ms).",
278 totem_config->consensus_timeout, totem_config->join_timeout);
279 goto parse_error;
280 }
281
282 if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
283 snprintf (local_error_reason, sizeof(local_error_reason),
284 "The merge timeout parameter (%d ms) may not be less than (%d ms).",
285 totem_config->merge_timeout, MINIMUM_TIMEOUT);
286 goto parse_error;
287 }
288
289 if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
290 snprintf (local_error_reason, sizeof(local_error_reason),
291 "The downcheck timeout parameter (%d ms) may not be less than (%d ms).",
292 totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
293 goto parse_error;
294 }
295
296 return 0;
297
298 parse_error:
299 snprintf (error_string_response, sizeof(error_string_response),
300 "parse error in config: %s\n", error_reason);
301 *error_string = error_string_response;
302 return (-1);
303
304 }
305
306 static int totem_get_crypto(struct totem_config *totem_config)
307 {
308 char *str;
309 const char *tmp_cipher;
310 const char *tmp_hash;
311
312 tmp_hash = "none";
313 tmp_cipher = "none";
314
315 if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
316 if (strcmp(str, "none") == 0) {
317 tmp_cipher = "none";
318 }
319 if (strcmp(str, "aes256") == 0) {
320 tmp_cipher = "aes256";
321 }
322 if (strcmp(str, "aes192") == 0) {
323 tmp_cipher = "aes192";
324 }
325 if (strcmp(str, "aes128") == 0) {
326 tmp_cipher = "aes128";
327 }
328 if (strcmp(str, "aes256") == 0) {
329 tmp_cipher = "aes256";
330 }
331 if (strcmp(str, "3des") == 0) {
332 tmp_cipher = "3des";
333 }
334 free(str);
335 }
336
337 if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
338 if (strcmp(str, "none") == 0) {
339 tmp_hash = "none";
340 }
341 if (strcmp(str, "md5") == 0) {
342 tmp_hash = "md5";
343 }
344 if (strcmp(str, "sha1") == 0) {
345 tmp_hash = "sha1";
346 }
347 if (strcmp(str, "sha256") == 0) {
348 tmp_hash = "sha256";
349 }
350 if (strcmp(str, "sha384") == 0) {
351 tmp_hash = "sha384";
352 }
353 if (strcmp(str, "sha512") == 0) {
354 tmp_hash = "sha512";
355 }
356 free(str);
357 }
358
359 if ((strcmp(tmp_cipher, "none") != 0) &&
360 (strcmp(tmp_hash, "none") == 0)) {
361 return -1;
362 }
363
364 free(totem_config->crypto_cipher_type);
365 free(totem_config->crypto_hash_type);
366
367 totem_config->crypto_cipher_type = strdup(tmp_cipher);
368 totem_config->crypto_hash_type = strdup(tmp_hash);
369
370 return 0;
371 }
372
373 static int totem_config_get_ip_version(void)
374 {
375 int res;
376 char *str;
377
378 res = AF_INET;
379 if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
380 if (strcmp(str, "ipv4") == 0) {
381 res = AF_INET;
382 }
383 if (strcmp(str, "ipv6") == 0) {
384 res = AF_INET6;
385 }
386 free(str);
387 }
388
389 return (res);
390 }
391
392 static uint16_t generate_cluster_id (const char *cluster_name)
393 {
394 int i;
395 int value = 0;
396
397 for (i = 0; i < strlen(cluster_name); i++) {
398 value <<= 1;
399 value += cluster_name[i];
400 }
401
402 return (value & 0xFFFF);
403 }
404
405 static int get_cluster_mcast_addr (
406 const char *cluster_name,
407 unsigned int linknumber,
408 int ip_version,
409 struct totem_ip_address *res)
410 {
411 uint16_t clusterid;
412 char addr[INET6_ADDRSTRLEN + 1];
413 int err;
414
415 if (cluster_name == NULL) {
416 return (-1);
417 }
418
419 clusterid = generate_cluster_id(cluster_name) + linknumber;
420 memset (res, 0, sizeof(*res));
421
422 switch (ip_version) {
423 case AF_INET:
424 snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
425 break;
426 case AF_INET6:
427 snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
428 break;
429 default:
430 /*
431 * Unknown family
432 */
433 return (-1);
434 }
435
436 err = totemip_parse (res, addr, ip_version);
437
438 return (err);
439 }
440
441 static unsigned int generate_nodeid_for_duplicate_test(
442 struct totem_config *totem_config,
443 char *addr)
444 {
445 unsigned int nodeid;
446 struct totem_ip_address totemip;
447
448 /* AF_INET hard-coded here because auto-generated nodeids
449 are only for IPv4 */
450 if (totemip_parse(&totemip, addr, AF_INET) != 0)
451 return -1;
452
453 memcpy (&nodeid, &totemip.addr, sizeof (unsigned int));
454
455 #if __BYTE_ORDER == __LITTLE_ENDIAN
456 nodeid = swab32 (nodeid);
457 #endif
458
459 if (totem_config->clear_node_high_bit) {
460 nodeid &= 0x7FFFFFFF;
461 }
462 return nodeid;
463 }
464
465 static int check_for_duplicate_nodeids(
466 struct totem_config *totem_config,
467 const char **error_string)
468 {
469 icmap_iter_t iter;
470 icmap_iter_t subiter;
471 const char *iter_key;
472 int res = 0;
473 int retval = 0;
474 char tmp_key[ICMAP_KEYNAME_MAXLEN];
475 char *ring0_addr=NULL;
476 char *ring0_addr1=NULL;
477 unsigned int node_pos;
478 unsigned int node_pos1;
479 unsigned int nodeid;
480 unsigned int nodeid1;
481 int autogenerated;
482
483 iter = icmap_iter_init("nodelist.node.");
484 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
485 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
486 if (res != 2) {
487 continue;
488 }
489
490 if (strcmp(tmp_key, "ring0_addr") != 0) {
491 continue;
492 }
493
494 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
495 autogenerated = 0;
496 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
497
498 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
499 if (icmap_get_string(tmp_key, &ring0_addr) != CS_OK) {
500 continue;
501 }
502
503 /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
504 nodeid = generate_nodeid_for_duplicate_test(totem_config, ring0_addr);
505 if (nodeid == -1) {
506 continue;
507 }
508 autogenerated = 1;
509 }
510
511 node_pos1 = 0;
512 subiter = icmap_iter_init("nodelist.node.");
513 while (((iter_key = icmap_iter_next(subiter, NULL, NULL)) != NULL) && (node_pos1 < node_pos)) {
514 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos1, tmp_key);
515 if ((res != 2) || (node_pos1 >= node_pos)) {
516 continue;
517 }
518
519 if (strcmp(tmp_key, "ring0_addr") != 0) {
520 continue;
521 }
522
523 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos1);
524 if (icmap_get_uint32(tmp_key, &nodeid1) != CS_OK) {
525
526 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos1);
527 if (icmap_get_string(tmp_key, &ring0_addr1) != CS_OK) {
528 continue;
529 }
530 nodeid1 = generate_nodeid_for_duplicate_test(totem_config, ring0_addr1);
531 if (nodeid1 == -1) {
532 continue;
533 }
534 }
535
536 if (nodeid == nodeid1) {
537 retval = -1;
538 snprintf (error_string_response, sizeof(error_string_response),
539 "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid,
540 autogenerated?"(autogenerated from ":"",
541 autogenerated?ring0_addr:"",
542 autogenerated?")":"");
543 log_printf (LOGSYS_LEVEL_ERROR, error_string_response);
544 *error_string = error_string_response;
545 break;
546 }
547 }
548 icmap_iter_finalize(subiter);
549 }
550 icmap_iter_finalize(iter);
551 return retval;
552 }
553
554
555 static int find_local_node_in_nodelist(struct totem_config *totem_config)
556 {
557 icmap_iter_t iter;
558 const char *iter_key;
559 int res = 0;
560 unsigned int node_pos;
561 int local_node_pos = -1;
562 struct totem_ip_address bind_addr;
563 int interface_up, interface_num;
564 char tmp_key[ICMAP_KEYNAME_MAXLEN];
565 char *node_addr_str;
566 struct totem_ip_address node_addr;
567
568 res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
569 &bind_addr, &interface_up, &interface_num,
570 totem_config->clear_node_high_bit);
571 if (res == -1) {
572 return (-1);
573 }
574
575 iter = icmap_iter_init("nodelist.node.");
576 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
577 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
578 if (res != 2) {
579 continue;
580 }
581
582 if (strcmp(tmp_key, "ring0_addr") != 0) {
583 continue;
584 }
585
586 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
587 if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
588 continue;
589 }
590
591 res = totemip_parse (&node_addr, node_addr_str, totem_config->ip_version);
592 free(node_addr_str);
593 if (res == -1) {
594 continue ;
595 }
596
597 if (totemip_equal(&bind_addr, &node_addr)) {
598 local_node_pos = node_pos;
599 }
600 }
601 icmap_iter_finalize(iter);
602
603 return (local_node_pos);
604 }
605
606 /*
607 * Compute difference between two set of totem interface arrays. set1 and set2
608 * are changed so for same ring, ip existing in both set1 and set2 are cleared
609 * (set to 0), and ips which are only in set1 or set2 remains untouched.
610 * totempg_node_add/remove is called.
611 */
612 static void compute_interfaces_diff(int interface_count,
613 struct totem_interface *set1,
614 struct totem_interface *set2)
615 {
616 int ring_no, set1_pos, set2_pos;
617 struct totem_ip_address empty_ip_address;
618
619 memset(&empty_ip_address, 0, sizeof(empty_ip_address));
620
621 for (ring_no = 0; ring_no < interface_count; ring_no++) {
622 for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
623 for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
624 /*
625 * For current ring_no remove all set1 items existing
626 * in set2
627 */
628 if (memcmp(&set1[ring_no].member_list[set1_pos],
629 &set2[ring_no].member_list[set2_pos],
630 sizeof(struct totem_ip_address)) == 0) {
631 memset(&set1[ring_no].member_list[set1_pos], 0,
632 sizeof(struct totem_ip_address));
633 memset(&set2[ring_no].member_list[set2_pos], 0,
634 sizeof(struct totem_ip_address));
635 }
636 }
637 }
638 }
639
640 for (ring_no = 0; ring_no < interface_count; ring_no++) {
641 for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
642 /*
643 * All items which remained in set1 doesn't exists in set2 any longer so
644 * node has to be removed.
645 */
646 if (memcmp(&set1[ring_no].member_list[set1_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
647 log_printf(LOGSYS_LEVEL_DEBUG,
648 "removing dynamic member %s for ring %u",
649 totemip_print(&set1[ring_no].member_list[set1_pos]),
650 ring_no);
651
652 totempg_member_remove(&set1[ring_no].member_list[set1_pos], ring_no);
653 }
654 }
655 for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
656 /*
657 * All items which remained in set2 doesn't existed in set1 so this is no node
658 * and has to be added.
659 */
660 if (memcmp(&set2[ring_no].member_list[set2_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
661 log_printf(LOGSYS_LEVEL_DEBUG,
662 "adding dynamic member %s for ring %u",
663 totemip_print(&set2[ring_no].member_list[set2_pos]),
664 ring_no);
665
666 totempg_member_add(&set2[ring_no].member_list[set2_pos], ring_no);
667 }
668 }
669 }
670 }
671
672 static void put_nodelist_members_to_config(struct totem_config *totem_config, int reload)
673 {
674 icmap_iter_t iter, iter2;
675 const char *iter_key, *iter_key2;
676 int res = 0;
677 unsigned int node_pos;
678 char tmp_key[ICMAP_KEYNAME_MAXLEN];
679 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
680 char *node_addr_str;
681 int member_count;
682 unsigned int linknumber = 0;
683 int i, j;
684 struct totem_interface *orig_interfaces = NULL;
685 struct totem_interface *new_interfaces = NULL;
686
687 if (reload) {
688 /*
689 * We need to compute diff only for reload. Also for initial configuration
690 * not all totem structures are initialized so corosync will crash during
691 * member_add/remove
692 */
693 orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
694 assert(orig_interfaces != NULL);
695 new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
696 assert(new_interfaces != NULL);
697
698 memcpy(orig_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
699 }
700
701 /* Clear out nodelist so we can put the new one in if needed */
702 for (i = 0; i < totem_config->interface_count; i++) {
703 for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
704 memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
705 }
706 totem_config->interfaces[i].member_count = 0;
707 }
708
709 iter = icmap_iter_init("nodelist.node.");
710 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
711 res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
712 if (res != 2) {
713 continue;
714 }
715
716 if (strcmp(tmp_key, "ring0_addr") != 0) {
717 continue;
718 }
719
720 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
721 iter2 = icmap_iter_init(tmp_key);
722 while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
723 unsigned int nodeid;
724
725 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
726 if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
727 }
728
729 res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
730 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
731 continue;
732 }
733
734 if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
735 continue;
736 }
737
738 member_count = totem_config->interfaces[linknumber].member_count;
739
740 res = totemip_parse(&totem_config->interfaces[linknumber].member_list[member_count],
741 node_addr_str, totem_config->ip_version);
742 if (res != -1) {
743 totem_config->interfaces[linknumber].member_list[member_count].nodeid = nodeid;
744 totem_config->interfaces[linknumber].member_count++;
745 }
746 free(node_addr_str);
747 }
748
749 icmap_iter_finalize(iter2);
750 }
751
752 icmap_iter_finalize(iter);
753
754 if (reload) {
755 memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
756
757 compute_interfaces_diff(totem_config->interface_count, orig_interfaces, new_interfaces);
758
759 free(new_interfaces);
760 free(orig_interfaces);
761 }
762 }
763
764 static void nodelist_dynamic_notify(
765 int32_t event,
766 const char *key_name,
767 struct icmap_notify_value new_val,
768 struct icmap_notify_value old_val,
769 void *user_data)
770 {
771 int res;
772 unsigned int ring_no;
773 unsigned int member_no;
774 char tmp_str[ICMAP_KEYNAME_MAXLEN];
775 uint8_t reloading;
776 struct totem_config *totem_config = (struct totem_config *)user_data;
777
778 /*
779 * If a full reload is in progress then don't do anything until it's done and
780 * can reconfigure it all atomically
781 */
782 if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
783 return ;
784 }
785
786 res = sscanf(key_name, "nodelist.node.%u.ring%u%s", &member_no, &ring_no, tmp_str);
787 if (res != 3)
788 return ;
789
790 if (strcmp(tmp_str, "_addr") != 0) {
791 return;
792 }
793
794 put_nodelist_members_to_config(totem_config, 1);
795 }
796
797
798 /*
799 * Tries to find node (node_pos) in config nodelist which address matches any
800 * local interface. Address can be stored in ring0_addr or if ipaddr_key_prefix is not NULL
801 * key with prefix ipaddr_key is used (there can be multiuple of them)
802 * This function differs * from find_local_node_in_nodelist because it doesn't need bindnetaddr,
803 * but doesn't work when bind addr is network address (so IP must be exact
804 * match).
805 *
806 * Returns 1 on success (address was found, node_pos is then correctly set) or 0 on failure.
807 */
808 int totem_config_find_local_addr_in_nodelist(const char *ipaddr_key_prefix, unsigned int *node_pos)
809 {
810 struct qb_list_head addrs;
811 struct totem_ip_if_address *if_addr;
812 icmap_iter_t iter, iter2;
813 const char *iter_key, *iter_key2;
814 struct qb_list_head *list;
815 const char *ipaddr_key;
816 int ip_version;
817 struct totem_ip_address node_addr;
818 char *node_addr_str;
819 int node_found = 0;
820 int res = 0;
821 char tmp_key[ICMAP_KEYNAME_MAXLEN];
822
823 if (totemip_getifaddrs(&addrs) == -1) {
824 return 0;
825 }
826
827 ip_version = totem_config_get_ip_version();
828
829 iter = icmap_iter_init("nodelist.node.");
830
831 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
832 res = sscanf(iter_key, "nodelist.node.%u.%s", node_pos, tmp_key);
833 if (res != 2) {
834 continue;
835 }
836
837 if (strcmp(tmp_key, "ring0_addr") != 0) {
838 continue;
839 }
840
841 if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
842 continue ;
843 }
844
845 free(node_addr_str);
846
847 /*
848 * ring0_addr found -> let's iterate thru ipaddr_key_prefix
849 */
850 snprintf(tmp_key, sizeof(tmp_key), "nodelist.node.%u.%s", *node_pos,
851 (ipaddr_key_prefix != NULL ? ipaddr_key_prefix : "ring0_addr"));
852
853 iter2 = icmap_iter_init(tmp_key);
854 while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
855 /*
856 * ring0_addr must be exact match, not prefix
857 */
858 ipaddr_key = (ipaddr_key_prefix != NULL ? iter_key2 : tmp_key);
859 if (icmap_get_string(ipaddr_key, &node_addr_str) != CS_OK) {
860 continue ;
861 }
862
863 if (totemip_parse(&node_addr, node_addr_str, ip_version) == -1) {
864 free(node_addr_str);
865 continue ;
866 }
867 free(node_addr_str);
868
869 /*
870 * Try to match ip with if_addrs
871 */
872 node_found = 0;
873 qb_list_for_each(list, &(addrs)) {
874 if_addr = qb_list_entry(list, struct totem_ip_if_address, list);
875
876 if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
877 node_found = 1;
878 break;
879 }
880 }
881
882 if (node_found) {
883 break ;
884 }
885 }
886
887 icmap_iter_finalize(iter2);
888
889 if (node_found) {
890 break ;
891 }
892 }
893
894 icmap_iter_finalize(iter);
895 totemip_freeifaddrs(&addrs);
896
897 return (node_found);
898 }
899
900 static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
901 {
902 int res = 0;
903 unsigned int node_pos;
904 char tmp_key[ICMAP_KEYNAME_MAXLEN];
905 char tmp_key2[ICMAP_KEYNAME_MAXLEN];
906 char *node_addr_str;
907 unsigned int linknumber = 0;
908 icmap_iter_t iter;
909 const char *iter_key;
910
911 if (totem_config_find_local_addr_in_nodelist(NULL, &node_pos)) {
912 /*
913 * We found node, so create interface section
914 */
915 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
916 iter = icmap_iter_init(tmp_key);
917 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
918 res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
919 if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
920 continue ;
921 }
922
923 if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
924 continue;
925 }
926
927 snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
928 icmap_set_string(tmp_key2, node_addr_str);
929 free(node_addr_str);
930 }
931 icmap_iter_finalize(iter);
932 }
933 }
934
935
936 extern int totem_config_read (
937 struct totem_config *totem_config,
938 const char **error_string,
939 uint64_t *warnings)
940 {
941 int res = 0;
942 char *str, *ring0_addr_str;
943 unsigned int linknumber = 0;
944 int member_count = 0;
945 icmap_iter_t iter, member_iter;
946 const char *iter_key;
947 const char *member_iter_key;
948 char linknumber_key[ICMAP_KEYNAME_MAXLEN];
949 char tmp_key[ICMAP_KEYNAME_MAXLEN];
950 uint8_t u8;
951 uint16_t u16;
952 uint32_t u32;
953 char *cluster_name = NULL;
954 int i;
955 int local_node_pos;
956 int nodeid_set;
957
958 *warnings = 0;
959
960 memset (totem_config, 0, sizeof (struct totem_config));
961 totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
962 if (totem_config->interfaces == 0) {
963 *error_string = "Out of memory trying to allocate ethernet interface storage area";
964 return -1;
965 }
966
967 memset (totem_config->interfaces, 0,
968 sizeof (struct totem_interface) * INTERFACE_MAX);
969
970 strcpy (totem_config->link_mode, "passive");
971
972 icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
973
974 if (totem_get_crypto(totem_config) != 0) {
975 *error_string = "crypto_cipher requires crypto_hash with value other than none";
976 return -1;
977 }
978
979 if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
980 if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
981 *error_string = "totem.link_mode is too long";
982 free(str);
983
984 return -1;
985 }
986 strcpy (totem_config->link_mode, str);
987 free(str);
988 }
989
990 icmap_get_uint32("totem.nodeid", &totem_config->node_id);
991
992 totem_config->clear_node_high_bit = 0;
993 if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
994 if (strcmp (str, "yes") == 0) {
995 totem_config->clear_node_high_bit = 1;
996 }
997 free(str);
998 }
999
1000 icmap_get_uint32("totem.threads", &totem_config->threads);
1001
1002 icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
1003
1004 if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
1005 cluster_name = NULL;
1006 }
1007
1008 totem_config->ip_version = totem_config_get_ip_version();
1009
1010 if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
1011 /*
1012 * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1013 */
1014 config_convert_nodelist_to_interface(totem_config);
1015 } else {
1016 if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
1017 /*
1018 * Both bindnetaddr and ring0_addr are set.
1019 * Log warning information, and use nodelist instead
1020 */
1021 *warnings |= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET;
1022
1023 config_convert_nodelist_to_interface(totem_config);
1024
1025 free(ring0_addr_str);
1026 }
1027
1028 free(str);
1029 }
1030
1031 /*
1032 * Broadcast option is global but set in interface section,
1033 * so reset before processing interfaces.
1034 */
1035 totem_config->broadcast_use = 0;
1036
1037 iter = icmap_iter_init("totem.interface.");
1038 while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1039 res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
1040 if (res != 2) {
1041 continue;
1042 }
1043
1044 if (strcmp(tmp_key, "bindnetaddr") != 0) {
1045 continue;
1046 }
1047
1048 member_count = 0;
1049
1050 linknumber = atoi(linknumber_key);
1051
1052 if (linknumber >= INTERFACE_MAX) {
1053 free(cluster_name);
1054
1055 snprintf (error_string_response, sizeof(error_string_response),
1056 "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1057 linknumber, INTERFACE_MAX - 1);
1058
1059 *error_string = error_string_response;
1060 return -1;
1061 }
1062
1063 /*
1064 * Get the bind net address
1065 */
1066 if (icmap_get_string(iter_key, &str) == CS_OK) {
1067 res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
1068 totem_config->ip_version);
1069 free(str);
1070 }
1071
1072 /*
1073 * Get interface multicast address
1074 */
1075 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
1076 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1077 res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str, totem_config->ip_version);
1078 free(str);
1079 } else {
1080 /*
1081 * User not specified address -> autogenerate one from cluster_name key
1082 * (if available). Return code is intentionally ignored, because
1083 * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1084 * checked later anyway.
1085 */
1086 (void)get_cluster_mcast_addr (cluster_name,
1087 linknumber,
1088 totem_config->ip_version,
1089 &totem_config->interfaces[linknumber].mcast_addr);
1090 }
1091
1092 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
1093 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1094 if (strcmp (str, "yes") == 0) {
1095 totem_config->broadcast_use = 1;
1096 }
1097 free(str);
1098 }
1099
1100 /*
1101 * Get mcast port
1102 */
1103 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
1104 if (icmap_get_uint16(tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
1105 if (totem_config->broadcast_use) {
1106 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
1107 } else {
1108 totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT;
1109 }
1110 }
1111
1112 /*
1113 * Get the TTL
1114 */
1115 totem_config->interfaces[linknumber].ttl = 1;
1116
1117 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
1118
1119 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1120 totem_config->interfaces[linknumber].ttl = u8;
1121 }
1122
1123 /*
1124 * Get the knet link params
1125 */
1126 totem_config->interfaces[linknumber].knet_link_priority = 1;
1127 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
1128
1129 if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1130 totem_config->interfaces[linknumber].knet_link_priority = u8;
1131 }
1132
1133 totem_config->interfaces[linknumber].knet_ping_interval = KNET_PING_INTERVAL;
1134 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
1135 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1136 totem_config->interfaces[linknumber].knet_ping_interval = u32;
1137 }
1138 totem_config->interfaces[linknumber].knet_ping_timeout = KNET_PING_TIMEOUT;
1139 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
1140 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1141 totem_config->interfaces[linknumber].knet_ping_timeout = u32;
1142 }
1143 totem_config->interfaces[linknumber].knet_ping_precision = KNET_PING_PRECISION;
1144 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
1145 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1146 totem_config->interfaces[linknumber].knet_ping_precision = u32;
1147 }
1148 totem_config->interfaces[linknumber].knet_pong_count = KNET_PONG_COUNT;
1149 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
1150 if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1151 totem_config->interfaces[linknumber].knet_pong_count = u32;
1152 }
1153
1154 totem_config->interfaces[linknumber].knet_transport = KNET_DEFAULT_TRANSPORT;
1155 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
1156 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1157 if (strcmp(str, "sctp") == 0) {
1158 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
1159 }
1160 else if (strcmp(str, "udp") == 0) {
1161 totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
1162 }
1163 else {
1164 *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1165 return -1;
1166 }
1167 }
1168
1169 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
1170 member_iter = icmap_iter_init(tmp_key);
1171 while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
1172 if (member_count == 0) {
1173 if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
1174 free(str);
1175 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
1176 break;
1177 } else {
1178 *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
1179 }
1180 }
1181
1182 if (icmap_get_string(member_iter_key, &str) == CS_OK) {
1183 res = totemip_parse (&totem_config->interfaces[linknumber].member_list[member_count++],
1184 str, totem_config->ip_version);
1185 }
1186 }
1187 icmap_iter_finalize(member_iter);
1188
1189 totem_config->interfaces[linknumber].member_count = member_count;
1190 totem_config->interface_count++;
1191 }
1192 icmap_iter_finalize(iter);
1193
1194 /*
1195 * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1196 */
1197 if (totem_config->broadcast_use) {
1198 for (linknumber = 0; linknumber < totem_config->interface_count; linknumber++) {
1199 totemip_parse (&totem_config->interfaces[linknumber].mcast_addr,
1200 "255.255.255.255", 0);
1201 }
1202 }
1203
1204 /*
1205 * Store automatically generated items back to icmap
1206 */
1207 for (i = 0; i < totem_config->interface_count; i++) {
1208 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
1209 if (icmap_get_string(tmp_key, &str) == CS_OK) {
1210 free(str);
1211 } else {
1212 str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
1213 icmap_set_string(tmp_key, str);
1214 }
1215
1216 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
1217 if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
1218 icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
1219 }
1220 }
1221
1222 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1223 if (icmap_get_string("totem.transport", &str) == CS_OK) {
1224 if (strcmp (str, "udpu") == 0) {
1225 totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
1226 }
1227
1228 if (strcmp (str, "udp") == 0) {
1229 totem_config->transport_number = TOTEM_TRANSPORT_UDP;
1230 }
1231
1232 if (strcmp (str, "knet") == 0) {
1233 totem_config->transport_number = TOTEM_TRANSPORT_KNET;
1234 }
1235
1236 free(str);
1237 }
1238
1239 free(cluster_name);
1240
1241 /*
1242 * Check existence of nodelist
1243 */
1244 if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
1245 free(str);
1246 /*
1247 * find local node
1248 */
1249 local_node_pos = find_local_node_in_nodelist(totem_config);
1250 if (local_node_pos != -1) {
1251 icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
1252
1253 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
1254
1255 nodeid_set = (totem_config->node_id != 0);
1256 if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
1257 *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
1258 }
1259
1260 /*
1261 * Make localnode ring0_addr read only, so we can be sure that local
1262 * node never changes. If rebinding to other IP would be in future
1263 * supported, this must be changed and handled properly!
1264 */
1265 snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
1266 icmap_set_ro_access(tmp_key, 0, 1);
1267 icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1268 }
1269
1270 put_nodelist_members_to_config(totem_config, 0);
1271 }
1272
1273 /*
1274 * Get things that might change in the future (and can depend on totem_config->interfaces);
1275 */
1276 totem_volatile_config_read(totem_config, NULL);
1277
1278 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1279
1280 add_totem_config_notification(totem_config);
1281
1282 return 0;
1283 }
1284
1285
1286 int totem_config_validate (
1287 struct totem_config *totem_config,
1288 const char **error_string)
1289 {
1290 static char local_error_reason[512];
1291 char parse_error[512];
1292 const char *error_reason = local_error_reason;
1293 int i, j;
1294 unsigned int interface_max = INTERFACE_MAX;
1295 unsigned int port1, port2;
1296
1297 if (totem_config->interface_count == 0) {
1298 error_reason = "No interfaces defined";
1299 goto parse_error;
1300 }
1301
1302 for (i = 0; i < totem_config->interface_count; i++) {
1303 /*
1304 * Some error checking of parsed data to make sure its valid
1305 */
1306
1307 struct totem_ip_address null_addr;
1308 memset (&null_addr, 0, sizeof (struct totem_ip_address));
1309
1310 if ((totem_config->transport_number == 0) &&
1311 memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
1312 sizeof (struct totem_ip_address)) == 0) {
1313 error_reason = "No multicast address specified";
1314 goto parse_error;
1315 }
1316
1317 if (totem_config->interfaces[i].ip_port == 0) {
1318 error_reason = "No multicast port specified";
1319 goto parse_error;
1320 }
1321
1322 if (totem_config->interfaces[i].ttl > 255) {
1323 error_reason = "Invalid TTL (should be 0..255)";
1324 goto parse_error;
1325 }
1326 if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
1327 totem_config->interfaces[i].ttl != 1) {
1328 error_reason = "Can only set ttl on multicast transport types";
1329 goto parse_error;
1330 }
1331 if (totem_config->interfaces[i].knet_link_priority > 255) {
1332 error_reason = "Invalid link priority (should be 0..255)";
1333 goto parse_error;
1334 }
1335 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET &&
1336 totem_config->interfaces[i].knet_link_priority != 1) {
1337 error_reason = "Can only set link priority on knet transport type";
1338 goto parse_error;
1339 }
1340
1341 if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
1342 totem_config->node_id == 0) {
1343
1344 error_reason = "An IPV6 network requires that a node ID be specified.";
1345 goto parse_error;
1346 }
1347
1348 if (totem_config->broadcast_use == 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1349 if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
1350 error_reason = "Multicast address family does not match bind address family";
1351 goto parse_error;
1352 }
1353
1354 if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
1355 error_reason = "mcastaddr is not a correct multicast address.";
1356 goto parse_error;
1357 }
1358 }
1359
1360 if (totem_config->interfaces[0].bindnet.family != totem_config->interfaces[i].bindnet.family) {
1361 error_reason = "Not all bind address belong to the same IP family";
1362 goto parse_error;
1363 }
1364
1365 /*
1366 * Ensure mcast address/port differs
1367 */
1368 if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1369 for (j = i + 1; j < totem_config->interface_count; j++) {
1370 port1 = totem_config->interfaces[i].ip_port;
1371 port2 = totem_config->interfaces[j].ip_port;
1372 if (totemip_equal(&totem_config->interfaces[i].mcast_addr,
1373 &totem_config->interfaces[j].mcast_addr) &&
1374 (((port1 > port2 ? port1 : port2) - (port1 < port2 ? port1 : port2)) <= 1)) {
1375 error_reason = "Interfaces multicast address/port pair must differ";
1376 goto parse_error;
1377 }
1378 }
1379 }
1380 }
1381
1382 if (totem_config->version != 2) {
1383 error_reason = "This totem parser can only parse version 2 configurations.";
1384 goto parse_error;
1385 }
1386
1387 if (totem_volatile_config_validate(totem_config, error_string) == -1) {
1388 return (-1);
1389 }
1390
1391 if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
1392 return (-1);
1393 }
1394
1395 /*
1396 * KNET Link values validation
1397 */
1398 if (strcmp (totem_config->link_mode, "active") &&
1399 strcmp (totem_config->link_mode, "rr") &&
1400 strcmp (totem_config->link_mode, "passive")) {
1401 snprintf (local_error_reason, sizeof(local_error_reason),
1402 "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
1403 goto parse_error;
1404 }
1405
1406 /* Only Knet does multiple interfaces */
1407 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
1408 interface_max = 1;
1409 }
1410
1411 if (interface_max < totem_config->interface_count) {
1412 snprintf (parse_error, sizeof(parse_error),
1413 "%d is too many configured interfaces for non-Knet transport.",
1414 totem_config->interface_count);
1415 error_reason = parse_error;
1416 goto parse_error;
1417 }
1418
1419 /* Only knet allows crypto */
1420 if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
1421 if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
1422 (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
1423
1424 snprintf (parse_error, sizeof(parse_error),
1425 "crypto_cipher & crypto_hash are only valid for the Knet transport.");
1426 error_reason = parse_error;
1427 goto parse_error;
1428 }
1429 }
1430
1431 if (totem_config->net_mtu == 0) {
1432 if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
1433 totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
1434 }
1435 else {
1436 totem_config->net_mtu = 1500;
1437 }
1438 }
1439
1440 return 0;
1441
1442 parse_error:
1443 snprintf (error_string_response, sizeof(error_string_response),
1444 "parse error in config: %s\n", error_reason);
1445 *error_string = error_string_response;
1446 return (-1);
1447
1448 }
1449
1450 static int read_keyfile (
1451 const char *key_location,
1452 struct totem_config *totem_config,
1453 const char **error_string)
1454 {
1455 int fd;
1456 int res;
1457 int saved_errno;
1458 char error_str[100];
1459 const char *error_ptr;
1460
1461 fd = open (key_location, O_RDONLY);
1462 if (fd == -1) {
1463 error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
1464 snprintf (error_string_response, sizeof(error_string_response),
1465 "Could not open %s: %s\n",
1466 key_location, error_ptr);
1467 goto parse_error;
1468 }
1469
1470 res = read (fd, totem_config->private_key, TOTEM_PRIVATE_KEY_LEN_MAX);
1471 saved_errno = errno;
1472 close (fd);
1473
1474 if (res == -1) {
1475 error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
1476 snprintf (error_string_response, sizeof(error_string_response),
1477 "Could not read %s: %s\n",
1478 key_location, error_ptr);
1479 goto parse_error;
1480 }
1481
1482 if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
1483 snprintf (error_string_response, sizeof(error_string_response),
1484 "Could only read %d bits of minimum %u bits from %s.\n",
1485 res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
1486 goto parse_error;
1487 }
1488
1489 totem_config->private_key_len = res;
1490
1491 return 0;
1492
1493 parse_error:
1494 *error_string = error_string_response;
1495 return (-1);
1496 }
1497
1498 int totem_config_keyread (
1499 struct totem_config *totem_config,
1500 const char **error_string)
1501 {
1502 int got_key = 0;
1503 char *key_location = NULL;
1504 int res;
1505 size_t key_len;
1506
1507 memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
1508 totem_config->private_key_len = 0;
1509
1510 if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
1511 strcmp(totem_config->crypto_hash_type, "none") == 0) {
1512 return (0);
1513 }
1514
1515 /* cmap may store the location of the key file */
1516 if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
1517 res = read_keyfile(key_location, totem_config, error_string);
1518 free(key_location);
1519 if (res) {
1520 goto key_error;
1521 }
1522 got_key = 1;
1523 } else { /* Or the key itself may be in the cmap */
1524 if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
1525 if (key_len > sizeof(totem_config->private_key)) {
1526 sprintf(error_string_response, "key is too long");
1527 goto key_error;
1528 }
1529 if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
1530 sprintf(error_string_response, "key is too short");
1531 goto key_error;
1532 }
1533 if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
1534 totem_config->private_key_len = key_len;
1535 got_key = 1;
1536 } else {
1537 sprintf(error_string_response, "can't load private key");
1538 goto key_error;
1539 }
1540 }
1541 }
1542
1543 /* In desperation we read the default filename */
1544 if (!got_key) {
1545 const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
1546 if (!filename)
1547 filename = COROSYSCONFDIR "/authkey";
1548 res = read_keyfile(filename, totem_config, error_string);
1549 if (res)
1550 goto key_error;
1551
1552 }
1553
1554 return (0);
1555
1556 key_error:
1557 *error_string = error_string_response;
1558 return (-1);
1559
1560 }
1561
1562 static void debug_dump_totem_config(const struct totem_config *totem_config)
1563 {
1564
1565 log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
1566 totem_config->token_timeout, totem_config->token_retransmit_timeout);
1567 log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
1568 totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
1569 log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
1570 totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
1571 totem_config->merge_timeout);
1572 log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
1573 totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
1574 log_printf(LOGSYS_LEVEL_DEBUG,
1575 "seqno unchanged const (%d rotations) Maximum network MTU %d",
1576 totem_config->seqno_unchanged_const, totem_config->net_mtu);
1577 log_printf(LOGSYS_LEVEL_DEBUG,
1578 "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
1579 totem_config->window_size, totem_config->max_messages);
1580 log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
1581 log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
1582 totem_config->heartbeat_failures_allowed);
1583 log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
1584 }
1585
1586 static void totem_change_notify(
1587 int32_t event,
1588 const char *key_name,
1589 struct icmap_notify_value new_val,
1590 struct icmap_notify_value old_val,
1591 void *user_data)
1592 {
1593 struct totem_config *totem_config = (struct totem_config *)user_data;
1594 uint32_t *param;
1595 uint8_t reloading;
1596 const char *deleted_key = NULL;
1597 const char *error_string;
1598
1599 /*
1600 * If a full reload is in progress then don't do anything until it's done and
1601 * can reconfigure it all atomically
1602 */
1603 if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
1604 return;
1605
1606 param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
1607 /*
1608 * Process change only if changed key is found in totem_config (-> param is not NULL)
1609 * or for special key token_coefficient. token_coefficient key is not stored in
1610 * totem_config, but it is used for computation of token timeout.
1611 */
1612 if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
1613 return;
1614
1615 /*
1616 * Values other than UINT32 are not supported, or needed (yet)
1617 */
1618 switch (event) {
1619 case ICMAP_TRACK_DELETE:
1620 deleted_key = key_name;
1621 break;
1622 case ICMAP_TRACK_ADD:
1623 case ICMAP_TRACK_MODIFY:
1624 deleted_key = NULL;
1625 break;
1626 default:
1627 break;
1628 }
1629
1630 totem_volatile_config_read (totem_config, deleted_key);
1631 log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
1632 debug_dump_totem_config(totem_config);
1633 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
1634 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1635 /*
1636 * TODO: Consider corosync exit and/or load defaults for volatile
1637 * values. For now, log error seems to be enough
1638 */
1639 }
1640 }
1641
1642 static void totem_reload_notify(
1643 int32_t event,
1644 const char *key_name,
1645 struct icmap_notify_value new_val,
1646 struct icmap_notify_value old_val,
1647 void *user_data)
1648 {
1649 struct totem_config *totem_config = (struct totem_config *)user_data;
1650 uint32_t local_node_pos;
1651 const char *error_string;
1652
1653 /* Reload has completed */
1654 if (*(uint8_t *)new_val.data == 0) {
1655 put_nodelist_members_to_config (totem_config, 1);
1656 totem_volatile_config_read (totem_config, NULL);
1657 log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
1658 debug_dump_totem_config(totem_config);
1659 if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
1660 log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1661 /*
1662 * TODO: Consider corosync exit and/or load defaults for volatile
1663 * values. For now, log error seems to be enough
1664 */
1665 }
1666
1667 /* Reinstate the local_node_pos */
1668 local_node_pos = find_local_node_in_nodelist(totem_config);
1669 if (local_node_pos != -1) {
1670 icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
1671 }
1672
1673 icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1674 } else {
1675 icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
1676 }
1677 }
1678
1679 static void add_totem_config_notification(struct totem_config *totem_config)
1680 {
1681 icmap_track_t icmap_track;
1682
1683 icmap_track_add("totem.",
1684 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
1685 totem_change_notify,
1686 totem_config,
1687 &icmap_track);
1688
1689 icmap_track_add("config.reload_in_progress",
1690 ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
1691 totem_reload_notify,
1692 totem_config,
1693 &icmap_track);
1694
1695 icmap_track_add("nodelist.node.",
1696 ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
1697 nodelist_dynamic_notify,
1698 (void *)totem_config,
1699 &icmap_track);
1700 }