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