corosync  3.0.2
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 /* These currently 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  for (i = 0; i < INTERFACE_MAX; i++) {
452  num_configured++;
453  }
454  }
455 
456  if (num_configured > 1) {
457  members = totem_config->interfaces[0].member_count;
458 
459  for (i=0; i<totem_config->interfaces[0].member_count; i++) {
460  snprintf(name_key, sizeof(name_key), "nodelist.node.%d.name", i);
461 
462  if (icmap_get_string(name_key, &name_str) != CS_OK) {
463  snprintf (local_error_reason, sizeof(local_error_reason),
464  "for a multi-link configuration, all nodes must have a 'name' attribute");
465  goto parse_error;
466  }
467  }
468 
469  for (i=0; i<num_configured; i++) {
470  if (totem_config->interfaces[i].member_count != members) {
471  snprintf (local_error_reason, sizeof(local_error_reason),
472  "Not all nodes have the same number of links");
473  goto parse_error;
474  }
475  }
476 
477 
478 
479  }
480 
481  return 0;
482 
483 parse_error:
484  snprintf (error_string_response, sizeof(error_string_response),
485  "parse error in config: %s\n", error_reason);
486  *error_string = error_string_response;
487  return (-1);
488 
489 }
490 
491 static int totem_get_crypto(struct totem_config *totem_config, const char **error_string)
492 {
493  char *str;
494  const char *tmp_cipher;
495  const char *tmp_hash;
496  const char *tmp_model;
497 
498  tmp_hash = "none";
499  tmp_cipher = "none";
500  tmp_model = "none";
501 
502  if (icmap_get_string("totem.crypto_model", &str) == CS_OK) {
503  if (strcmp(str, "nss") == 0) {
504  tmp_model = "nss";
505  }
506  if (strcmp(str, "openssl") == 0) {
507  tmp_model = "openssl";
508  }
509  free(str);
510  } else {
511  tmp_model = "nss";
512  }
513 
514  if (icmap_get_string("totem.secauth", &str) == CS_OK) {
515  if (strcmp(str, "on") == 0) {
516  tmp_cipher = "aes256";
517  tmp_hash = "sha256";
518  }
519  free(str);
520  }
521 
522  if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
523  if (strcmp(str, "none") == 0) {
524  tmp_cipher = "none";
525  }
526  if (strcmp(str, "aes256") == 0) {
527  tmp_cipher = "aes256";
528  }
529  if (strcmp(str, "aes192") == 0) {
530  tmp_cipher = "aes192";
531  }
532  if (strcmp(str, "aes128") == 0) {
533  tmp_cipher = "aes128";
534  }
535  free(str);
536  }
537 
538  if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
539  if (strcmp(str, "none") == 0) {
540  tmp_hash = "none";
541  }
542  if (strcmp(str, "md5") == 0) {
543  tmp_hash = "md5";
544  }
545  if (strcmp(str, "sha1") == 0) {
546  tmp_hash = "sha1";
547  }
548  if (strcmp(str, "sha256") == 0) {
549  tmp_hash = "sha256";
550  }
551  if (strcmp(str, "sha384") == 0) {
552  tmp_hash = "sha384";
553  }
554  if (strcmp(str, "sha512") == 0) {
555  tmp_hash = "sha512";
556  }
557  free(str);
558  }
559 
560  if ((strcmp(tmp_cipher, "none") != 0) &&
561  (strcmp(tmp_hash, "none") == 0)) {
562  *error_string = "crypto_cipher requires crypto_hash with value other than none";
563  return -1;
564  }
565 
566  if (strcmp(tmp_model, "none") == 0) {
567  *error_string = "crypto_model should be 'nss' or 'openssl'";
568  return -1;
569  }
570 
573  free(totem_config->crypto_model);
574 
575  totem_config->crypto_cipher_type = strdup(tmp_cipher);
576  totem_config->crypto_hash_type = strdup(tmp_hash);
577  totem_config->crypto_model = strdup(tmp_model);
578 
579  return 0;
580 }
581 
582 static int nodelist_byname(const char *find_name, int strip_domain)
583 {
584  icmap_iter_t iter;
585  const char *iter_key;
586  char name_str[ICMAP_KEYNAME_MAXLEN];
587  int res = 0;
588  unsigned int node_pos;
589  char *name;
590  unsigned int namelen;
591 
592  iter = icmap_iter_init("nodelist.node.");
593  while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
594  res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, name_str);
595  if (res != 2) {
596  continue;
597  }
598  /* ring0_addr is allowed as a fallback */
599  if (strcmp(name_str, "name") && strcmp(name_str, "ring0_addr")) {
600  continue;
601  }
602  if (icmap_get_string(iter_key, &name) != CS_OK) {
603  continue;
604  }
605  namelen = strlen(name);
606 
607  if (strip_domain) {
608  char *dot;
609  dot = strchr(name, '.');
610  if (dot) {
611  namelen = name - dot - 1;
612  }
613  }
614  if (strncmp(find_name, name, namelen) == 0 &&
615  strlen(find_name) == strlen(name)) {
616  icmap_iter_finalize(iter);
617  return node_pos;
618  }
619  }
620  icmap_iter_finalize(iter);
621  return -1;
622 }
623 
624 /* Compare two addresses - only address part (sin_addr/sin6_addr) is checked */
625 static int ipaddr_equal(const struct sockaddr *addr1, const struct sockaddr *addr2)
626 {
627  int addrlen = 0;
628  const void *addr1p, *addr2p;
629 
630  if (addr1->sa_family != addr2->sa_family)
631  return 0;
632 
633  switch (addr1->sa_family) {
634  case AF_INET:
635  addrlen = sizeof(struct in_addr);
636  addr1p = &((struct sockaddr_in *)addr1)->sin_addr;
637  addr2p = &((struct sockaddr_in *)addr2)->sin_addr;
638  break;
639  case AF_INET6:
640  addrlen = sizeof(struct in6_addr);
641  addr1p = &((struct sockaddr_in6 *)addr1)->sin6_addr;
642  addr2p = &((struct sockaddr_in6 *)addr2)->sin6_addr;
643  break;
644  default:
645  assert(0);
646  }
647 
648  return (memcmp(addr1p, addr2p, addrlen) == 0);
649 }
650 
651 
652 /* Finds the local node and returns its position in the nodelist.
653  * Uses nodelist.local_node_pos as a cache to save effort
654  */
655 static int find_local_node(int use_cache)
656 {
657  char nodename2[PATH_MAX];
658  char name_str[ICMAP_KEYNAME_MAXLEN];
659  icmap_iter_t iter;
660  const char *iter_key;
661  unsigned int cached_pos;
662  char *dot = NULL;
663  const char *node;
664  struct ifaddrs *ifa, *ifa_list;
665  struct sockaddr *sa;
666  int found = 0;
667  int node_pos = -1;
668  int res;
669  struct utsname utsname;
670 
671  /* Check for cached value first */
672  if (use_cache) {
673  if (icmap_get_uint32("nodelist.local_node_pos", &cached_pos) == CS_OK) {
674  return cached_pos;
675  }
676  }
677 
678  res = uname(&utsname);
679  if (res) {
680  return -1;
681  }
682  node = utsname.nodename;
683 
684  /* 1. Exact match */
685  node_pos = nodelist_byname(node, 0);
686  if (node_pos > -1) {
687  found = 1;
688  goto ret_found;
689  }
690 
691  /* 2. Try to match with increasingly more
692  * specific versions of it
693  */
694  strcpy(nodename2, node);
695  dot = strrchr(nodename2, '.');
696  while (dot) {
697  *dot = '\0';
698 
699  node_pos = nodelist_byname(nodename2, 0);
700  if (node_pos > -1) {
701  found = 1;
702  goto ret_found;
703  }
704  dot = strrchr(nodename2, '.');
705  }
706 
707  node_pos = nodelist_byname(nodename2, 1);
708  if (node_pos > -1) {
709  found = 1;
710  goto ret_found;
711  }
712 
713  /*
714  * The corosync.conf name may not be related to uname at all,
715  * they may match a hostname on some network interface.
716  */
717  if (getifaddrs(&ifa_list))
718  return -1;
719 
720  for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
721  socklen_t salen = 0;
722 
723  /* Restore this */
724  strcpy(nodename2, node);
725  sa = ifa->ifa_addr;
726  if (!sa) {
727  continue;
728  }
729  if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) {
730  continue;
731  }
732 
733  if (sa->sa_family == AF_INET) {
734  salen = sizeof(struct sockaddr_in);
735  }
736  if (sa->sa_family == AF_INET6) {
737  salen = sizeof(struct sockaddr_in6);
738  }
739 
740  if (getnameinfo(sa, salen,
741  nodename2, sizeof(nodename2),
742  NULL, 0, 0) == 0) {
743 
744  node_pos = nodelist_byname(nodename2, 0);
745  if (node_pos > -1) {
746  found = 1;
747  goto out;
748  }
749 
750  /* Truncate this name and try again */
751  dot = strchr(nodename2, '.');
752  if (dot) {
753  *dot = '\0';
754 
755  node_pos = nodelist_byname(nodename2, 0);
756  if (node_pos > -1) {
757  found = 1;
758  goto out;
759  }
760  }
761  }
762 
763  /* See if it's the IP address that's in corosync.conf */
764  if (getnameinfo(sa, sizeof(*sa),
765  nodename2, sizeof(nodename2),
766  NULL, 0, NI_NUMERICHOST))
767  continue;
768 
769  node_pos = nodelist_byname(nodename2, 0);
770  if (node_pos > -1) {
771  found = 1;
772  goto out;
773  }
774  }
775 
776  out:
777  if (found) {
778  freeifaddrs(ifa_list);
779  goto ret_found;
780  }
781 
782  /*
783  * This section covers the usecase where the nodename specified in cluster.conf
784  * is an alias specified in /etc/hosts. For example:
785  * <ipaddr> hostname alias1 alias2
786  * and <clusternode name="alias2">
787  * the above calls use uname and getnameinfo does not return aliases.
788  * here we take the name specified in cluster.conf, resolve it to an address
789  * and then compare against all known local ip addresses.
790  * if we have a match, we found our nodename. In theory this chunk of code
791  * could replace all the checks above, but let's avoid any possible regressions
792  * and use it as last.
793  */
794 
795  iter = icmap_iter_init("nodelist.node.");
796  while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
797  char *dbnodename = NULL;
798  struct addrinfo hints;
799  struct addrinfo *result = NULL, *rp = NULL;
800 
801  res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, name_str);
802  if (res != 2) {
803  continue;
804  }
805  /* 'ring0_addr' is allowed as a fallback, but 'name' will be found first
806  * because the names are in alpha order.
807  */
808  if (strcmp(name_str, "name") && strcmp(name_str, "ring0_addr")) {
809  continue;
810  }
811  if (icmap_get_string(iter_key, &dbnodename) != CS_OK) {
812  continue;
813  }
814 
815  memset(&hints, 0, sizeof(struct addrinfo));
816  hints.ai_family = AF_UNSPEC;
817  hints.ai_socktype = SOCK_DGRAM;
818  hints.ai_flags = 0;
819  hints.ai_protocol = IPPROTO_UDP;
820 
821  if (getaddrinfo(dbnodename, NULL, &hints, &result)) {
822  continue;
823  }
824 
825  for (rp = result; rp != NULL; rp = rp->ai_next) {
826  for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
827  if (ifa->ifa_addr &&
828  ipaddr_equal(rp->ai_addr, ifa->ifa_addr)) {
829  freeaddrinfo(result);
830  found = 1;
831  goto out2;
832  }
833  }
834  }
835 
836  freeaddrinfo(result);
837  }
838 out2:
839  icmap_iter_finalize(iter);
840  freeifaddrs(ifa_list);
841 
842 ret_found:
843  if (found) {
844  res = icmap_set_uint32("nodelist.local_node_pos", node_pos);
845  }
846 
847  return node_pos;
848 }
849 
850 static enum totem_ip_version_enum totem_config_get_ip_version(struct totem_config *totem_config)
851 {
852  enum totem_ip_version_enum res;
853  char *str;
854 
855  res = TOTEM_IP_VERSION_6_4;
856 
858  res = TOTEM_IP_VERSION_4;
859  }
860 
861  if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
862  if (strcmp(str, "ipv4") == 0) {
863  res = TOTEM_IP_VERSION_4;
864  }
865  if (strcmp(str, "ipv6") == 0) {
866  res = TOTEM_IP_VERSION_6;
867  }
868  if (strcmp(str, "ipv6-4") == 0) {
869  res = TOTEM_IP_VERSION_6_4;
870  }
871  if (strcmp(str, "ipv4-6") == 0) {
872  res = TOTEM_IP_VERSION_4_6;
873  }
874  free(str);
875  }
876 
877  return (res);
878 }
879 
880 static uint16_t generate_cluster_id (const char *cluster_name)
881 {
882  int i;
883  int value = 0;
884 
885  for (i = 0; i < strlen(cluster_name); i++) {
886  value <<= 1;
887  value += cluster_name[i];
888  }
889 
890  return (value & 0xFFFF);
891 }
892 
893 static int get_cluster_mcast_addr (
894  const char *cluster_name,
895  unsigned int linknumber,
896  enum totem_ip_version_enum ip_version,
897  struct totem_ip_address *res)
898 {
899  uint16_t clusterid;
900  char addr[INET6_ADDRSTRLEN + 1];
901  int err;
902 
903  if (cluster_name == NULL) {
904  return (-1);
905  }
906 
907  clusterid = generate_cluster_id(cluster_name) + linknumber;
908  memset (res, 0, sizeof(*res));
909 
910  switch (ip_version) {
911  case TOTEM_IP_VERSION_4:
913  snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
914  break;
915  case TOTEM_IP_VERSION_6:
917  snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
918  break;
919  default:
920  /*
921  * Unknown family
922  */
923  return (-1);
924  }
925 
926  err = totemip_parse (res, addr, ip_version);
927 
928  return (err);
929 }
930 
931 static unsigned int generate_nodeid(
932  struct totem_config *totem_config,
933  char *addr)
934 {
935  unsigned int nodeid;
936  struct totem_ip_address totemip;
937 
938  /* AF_INET hard-coded here because auto-generated nodeids
939  are only for IPv4 */
940  if (totemip_parse(&totemip, addr, TOTEM_IP_VERSION_4) != 0)
941  return -1;
942 
943  memcpy (&nodeid, &totemip.addr, sizeof (unsigned int));
944 
945 #if __BYTE_ORDER == __LITTLE_ENDIAN
946  nodeid = swab32 (nodeid);
947 #endif
948 
950  nodeid &= 0x7FFFFFFF;
951  }
952  return nodeid;
953 }
954 
955 static int check_for_duplicate_nodeids(
956  struct totem_config *totem_config,
957  const char **error_string)
958 {
959  icmap_iter_t iter;
960  icmap_iter_t subiter;
961  const char *iter_key;
962  int res = 0;
963  int retval = 0;
964  char tmp_key[ICMAP_KEYNAME_MAXLEN];
965  char *ring0_addr=NULL;
966  char *ring0_addr1=NULL;
967  unsigned int node_pos;
968  unsigned int node_pos1;
969  unsigned int last_node_pos = -1;
970  unsigned int nodeid;
971  unsigned int nodeid1;
972  int autogenerated;
973 
974  iter = icmap_iter_init("nodelist.node.");
975  while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
976  res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
977  if (res != 2) {
978  continue;
979  }
980 
981  /*
982  * This relies on the fact the icmap keys are always returned in order
983  * so all of the keys for a node will be grouped together. We're basically
984  * just running the code below once for each node.
985  */
986  if (last_node_pos == node_pos) {
987  continue;
988  }
989  last_node_pos = node_pos;
990 
991  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
992  autogenerated = 0;
993 
994  /* Generated nodeids are only allowed for UDP/UDPU so ring0_addr is valid here */
995  if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
996 
997  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
998  if (icmap_get_string(tmp_key, &ring0_addr) != CS_OK) {
999  continue;
1000  }
1001 
1002  /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
1003  nodeid = generate_nodeid(totem_config, ring0_addr);
1004  if (nodeid == -1) {
1005  continue;
1006  }
1007  autogenerated = 1;
1008  }
1009 
1010  node_pos1 = 0;
1011  subiter = icmap_iter_init("nodelist.node.");
1012  while (((iter_key = icmap_iter_next(subiter, NULL, NULL)) != NULL) && (node_pos1 < node_pos)) {
1013  res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos1, tmp_key);
1014  if ((res != 2) || (node_pos1 >= node_pos)) {
1015  continue;
1016  }
1017 
1018  if (strcmp(tmp_key, "nodeid") != 0) {
1019  continue;
1020  }
1021 
1022  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos1);
1023  if (icmap_get_uint32(tmp_key, &nodeid1) != CS_OK) {
1024 
1025  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos1);
1026  if (icmap_get_string(tmp_key, &ring0_addr1) != CS_OK) {
1027  continue;
1028  }
1029  nodeid1 = generate_nodeid(totem_config, ring0_addr1);
1030  if (nodeid1 == -1) {
1031  continue;
1032  }
1033  }
1034 
1035  if (nodeid == nodeid1) {
1036  retval = -1;
1037  snprintf (error_string_response, sizeof(error_string_response),
1038  "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid,
1039  autogenerated?"(autogenerated from ":"",
1040  autogenerated?ring0_addr:"",
1041  autogenerated?")":"");
1042  log_printf (LOGSYS_LEVEL_ERROR, error_string_response);
1043  *error_string = error_string_response;
1044  break;
1045  }
1046  }
1047  icmap_iter_finalize(subiter);
1048  }
1049  icmap_iter_finalize(iter);
1050  return retval;
1051 }
1052 
1053 
1054 /*
1055  * This needs to be done last of all. It would be nice to do it when reading the
1056  * interface params, but the totem params need to have them to be read first. We
1057  * need both, so this is a way round that circular dependancy.
1058  */
1059 static void calc_knet_ping_timers(struct totem_config *totem_config)
1060 {
1061  char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
1062  int interface;
1063 
1064  for (interface = 0; interface < INTERFACE_MAX; interface++) {
1065 
1066  if (totem_config->interfaces[interface].configured) {
1067  if (!totem_config->interfaces[interface].knet_pong_count) {
1069  }
1070  if (!totem_config->interfaces[interface].knet_ping_timeout) {
1073  }
1074  snprintf(runtime_key_name, sizeof(runtime_key_name),
1075  "runtime.config.totem.interface.%d.knet_ping_timeout", interface);
1076  icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_timeout);
1077 
1078  if (!totem_config->interfaces[interface].knet_ping_interval) {
1081  }
1082  snprintf(runtime_key_name, sizeof(runtime_key_name),
1083  "runtime.config.totem.interface.%d.knet_ping_interval", interface);
1084  icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_interval);
1085  }
1086  }
1087 }
1088 
1089 /*
1090  * Compute difference between two set of totem interface arrays. set1 and set2
1091  * are changed so for same ring, ip existing in both set1 and set2 are cleared
1092  * (set to 0), and ips which are only in set1 or set2 remains untouched.
1093  * totempg_node_add/remove is called.
1094  */
1095 static void compute_interfaces_diff(struct totem_interface *set1,
1096  struct totem_interface *set2)
1097 {
1098  int ring_no, set1_pos, set2_pos;
1099  struct totem_ip_address empty_ip_address;
1100 
1101  memset(&empty_ip_address, 0, sizeof(empty_ip_address));
1102 
1103  for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
1104  if (!set1[ring_no].configured && !set2[ring_no].configured) {
1105  continue;
1106  }
1107 
1108  for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
1109  for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
1110  /*
1111  * For current ring_no remove all set1 items existing
1112  * in set2
1113  */
1114  if (memcmp(&set1[ring_no].member_list[set1_pos],
1115  &set2[ring_no].member_list[set2_pos],
1116  sizeof(struct totem_ip_address)) == 0) {
1117  memset(&set1[ring_no].member_list[set1_pos], 0,
1118  sizeof(struct totem_ip_address));
1119  memset(&set2[ring_no].member_list[set2_pos], 0,
1120  sizeof(struct totem_ip_address));
1121  }
1122  }
1123  }
1124  }
1125 
1126  for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
1127  for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
1128  /*
1129  * All items which remained in set1 doesn't exists in set2 any longer so
1130  * node has to be removed.
1131  */
1132  if (memcmp(&set1[ring_no].member_list[set1_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
1134  "removing dynamic member %s for ring %u",
1135  totemip_print(&set1[ring_no].member_list[set1_pos]),
1136  ring_no);
1137 
1138  totempg_member_remove(&set1[ring_no].member_list[set1_pos], ring_no);
1139  }
1140  }
1141  if (!set2[ring_no].configured) {
1142  continue;
1143  }
1144  for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
1145  /*
1146  * All items which remained in set2 doesn't existed in set1 so this is no node
1147  * and has to be added.
1148  */
1149  if (memcmp(&set2[ring_no].member_list[set2_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
1151  "adding dynamic member %s for ring %u",
1152  totemip_print(&set2[ring_no].member_list[set2_pos]),
1153  ring_no);
1154 
1155  totempg_member_add(&set2[ring_no].member_list[set2_pos], ring_no);
1156  }
1157  }
1158  }
1159 }
1160 
1161 /*
1162  * Reconfigure links in totempg. Sets new local IP address and adds params for new links.
1163  */
1164 static void reconfigure_links(struct totem_config *totem_config)
1165 {
1166  int i;
1167  char tmp_key[ICMAP_KEYNAME_MAXLEN];
1168  char *addr_string;
1169  struct totem_ip_address local_ip;
1170  int err;
1171  int local_node_pos = find_local_node(0);
1172 
1173  for (i = 0; i<INTERFACE_MAX; i++) {
1174  if (!totem_config->interfaces[i].configured) {
1175  continue;
1176  }
1177 
1178  log_printf(LOGSYS_LEVEL_INFO, "Configuring link %d\n", i);
1179 
1180  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring%u_addr", local_node_pos, i);
1181  if (icmap_get_string(tmp_key, &addr_string) != CS_OK) {
1182  continue;
1183  }
1184 
1185  err = totemip_parse(&local_ip, addr_string, totem_config->ip_version);
1186  if (err != 0) {
1187  continue;
1188  }
1189  local_ip.nodeid = totem_config->node_id;
1190 
1191  /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
1194 
1195  /* knet_ping_interval & knet_ping_timeout are set later once we know all the other params */
1196 
1202  totem_config->interfaces[i].knet_transport = KNET_TRANSPORT_UDP;
1203  if (!totem_config->interfaces[i].ip_port)
1205 
1206  totempg_iface_set(&local_ip, totem_config->interfaces[i].ip_port, i);
1207  }
1208 }
1209 
1210 /* Check for differences in config that can't be done on-the-fly and print an error */
1211 static void check_things_have_not_changed(struct totem_config *totem_config)
1212 {
1213  int i,j;
1214  const char *ip_str;
1215  char addr_buf[INET6_ADDRSTRLEN];
1216  int changed = 0;
1217 
1218  for (i = 0; i<INTERFACE_MAX; i++) {
1224  "New config has different knet transport for link %d. Internal value was NOT changed.\n", i);
1225  changed = 1;
1226  }
1228  if (memcmp(&totem_config->interfaces[i].member_list[j],
1230  sizeof(struct totem_ip_address))) {
1231 
1233 
1234  /* if ip_str is NULL then the old address was invalid and is allowed to change */
1235  if (ip_str) {
1236  strncpy(addr_buf, ip_str, sizeof(addr_buf));
1237  addr_buf[sizeof(addr_buf) - 1] = '\0';
1239  "new config has different address for link %d (addr changed from %s to %s). Internal value was NOT changed.\n",
1240  i, addr_buf, totemip_print(&totem_config->interfaces[i].member_list[j]));
1241  changed = 1;
1242  }
1243  }
1244  }
1245  }
1246  }
1247 
1248  if (changed) {
1249  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");
1250  }
1251 }
1252 
1253 
1254 static int put_nodelist_members_to_config(struct totem_config *totem_config, int reload, const char **error_string)
1255 {
1256  icmap_iter_t iter, iter2;
1257  const char *iter_key, *iter_key2;
1258  int res = 0;
1259  unsigned int node_pos;
1260  char tmp_key[ICMAP_KEYNAME_MAXLEN];
1261  char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1262  char *node_addr_str;
1263  int member_count;
1264  unsigned int linknumber = 0;
1265  int i, j;
1266  int last_node_pos = -1;
1267  struct totem_interface *new_interfaces = NULL;
1268 
1269  if (reload) {
1270  /*
1271  * We need to compute diff only for reload. Also for initial configuration
1272  * not all totem structures are initialized so corosync will crash during
1273  * member_add/remove
1274  */
1275  new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1276  assert(new_interfaces != NULL);
1277  }
1278 
1279  /* Clear out nodelist so we can put the new one in if needed */
1280  for (i = 0; i < INTERFACE_MAX; i++) {
1281  for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
1282  memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
1283  }
1285  }
1286 
1287  iter = icmap_iter_init("nodelist.node.");
1288  while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1289  res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
1290  if (res != 2) {
1291  continue;
1292  }
1293  /* If it's the same as the last node_pos then skip it */
1294  if (node_pos == last_node_pos) {
1295  continue;
1296  }
1297  last_node_pos = node_pos;
1298 
1299  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1300  iter2 = icmap_iter_init(tmp_key);
1301  while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
1302  unsigned int nodeid;
1303  char *str;
1304 
1305  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
1306  if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
1307  nodeid = 0;
1308  }
1309 
1310  res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1311  if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1312  continue;
1313  }
1314 
1315  if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
1316  continue;
1317  }
1318 
1319  /* Generate nodeids if they are not provided and transport is UDP/U */
1320  if (!nodeid &&
1323  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
1324  if (icmap_get_string(tmp_key, &str) == CS_OK) {
1325  nodeid = generate_nodeid(totem_config, str);
1326  if (nodeid == -1) {
1327  sprintf(error_string_response,
1328  "An IPV6 network requires that a node ID be specified "
1329  "for address '%s'.", node_addr_str);
1330  *error_string = error_string_response;
1331  free(str);
1332 
1333  return (-1);
1334  }
1335 
1337  "Generated nodeid = 0x%x for %s", nodeid, str);
1338  free(str);
1339  }
1340  }
1341 
1344  node_addr_str, totem_config->ip_version);
1345  if (res == 0) {
1347  totem_config->interfaces[linknumber].member_count++;
1348  totem_config->interfaces[linknumber].configured = 1;
1349  } else {
1350  sprintf(error_string_response, "failed to parse node address '%s'\n", node_addr_str);
1351  *error_string = error_string_response;
1352 
1353  memset(&totem_config->interfaces[linknumber].member_list[member_count], 0,
1354  sizeof(struct totem_ip_address));
1355 
1356  free(node_addr_str);
1357  icmap_iter_finalize(iter2);
1358  icmap_iter_finalize(iter);
1359  return -1;
1360  }
1361 
1362  free(node_addr_str);
1363  }
1364 
1365  icmap_iter_finalize(iter2);
1366  }
1367 
1368  icmap_iter_finalize(iter);
1369 
1370  if (reload) {
1371  log_printf(LOGSYS_LEVEL_DEBUG, "About to reconfigure links from nodelist.\n");
1372  reconfigure_links(totem_config);
1373 
1374  memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
1375 
1376  check_things_have_not_changed(totem_config);
1377 
1378  compute_interfaces_diff(totem_config->orig_interfaces, new_interfaces);
1379 
1380  free(new_interfaces);
1381  }
1382  return 0;
1383 }
1384 
1385 static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
1386 {
1387  int res = 0;
1388  int node_pos;
1389  char tmp_key[ICMAP_KEYNAME_MAXLEN];
1390  char tmp_key2[ICMAP_KEYNAME_MAXLEN];
1391  char *node_addr_str;
1392  unsigned int linknumber = 0;
1393  icmap_iter_t iter;
1394  const char *iter_key;
1395 
1396  node_pos = find_local_node(1);
1397  if (node_pos > -1) {
1398  /*
1399  * We found node, so create interface section
1400  */
1401  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
1402  iter = icmap_iter_init(tmp_key);
1403  while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1404  res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
1405  if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
1406  continue ;
1407  }
1408 
1409  if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
1410  continue;
1411  }
1412 
1413  snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1414  icmap_set_string(tmp_key2, node_addr_str);
1415  free(node_addr_str);
1416  }
1417  icmap_iter_finalize(iter);
1418  }
1419 }
1420 
1421 static int get_interface_params(struct totem_config *totem_config,
1422  const char **error_string, uint64_t *warnings,
1423  int reload)
1424 {
1425  int res = 0;
1426  unsigned int linknumber = 0;
1427  int member_count = 0;
1428  int i;
1429  icmap_iter_t iter, member_iter;
1430  const char *iter_key;
1431  const char *member_iter_key;
1432  char linknumber_key[ICMAP_KEYNAME_MAXLEN];
1433  char tmp_key[ICMAP_KEYNAME_MAXLEN];
1434  uint8_t u8;
1435  uint32_t u32;
1436  char *str;
1437  char *cluster_name = NULL;
1438  enum totem_ip_version_enum tmp_ip_version = TOTEM_IP_VERSION_4;
1439 
1440  if (reload) {
1441  for (i=0; i<INTERFACE_MAX; i++) {
1442  /*
1443  * Set back to defaults things that might have been configured and
1444  * now have been taken out of corosync.conf. These won't be caught by the
1445  * code below which only looks at interface{} sections that actually exist.
1446  */
1452  }
1453  }
1454  if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
1455  cluster_name = NULL;
1456  }
1457 
1458  iter = icmap_iter_init("totem.interface.");
1459  while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
1460  res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
1461  if (res != 2) {
1462  continue;
1463  }
1464 
1465  if (strcmp(tmp_key, "bindnetaddr") != 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
1466  continue;
1467  }
1468 
1469  member_count = 0;
1470  linknumber = atoi(linknumber_key);
1471 
1472  if (linknumber >= INTERFACE_MAX) {
1473  free(cluster_name);
1474 
1475  snprintf (error_string_response, sizeof(error_string_response),
1476  "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
1477  linknumber, INTERFACE_MAX - 1);
1478 
1479  *error_string = error_string_response;
1480  return -1;
1481  }
1482 
1483  /* These things are only valid for the initial read */
1484  if (!reload) {
1485  /*
1486  * Get the bind net address
1487  */
1488  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
1489 
1490  if (icmap_get_string(tmp_key, &str) == CS_OK) {
1491  res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
1493 
1494  if (res) {
1495  sprintf(error_string_response, "failed to parse bindnet address '%s'\n", str);
1496  *error_string = error_string_response;
1497  free(str);
1498 
1499  return -1;
1500  }
1501 
1502  free(str);
1503  }
1504 
1505  /*
1506  * Get interface multicast address
1507  */
1508  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
1509  if (icmap_get_string(tmp_key, &str) == CS_OK) {
1510  res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str,
1512 
1513  if (res) {
1514  sprintf(error_string_response, "failed to parse mcast address '%s'\n", str);
1515  *error_string = error_string_response;
1516  free(str);
1517 
1518  return -1;
1519  }
1520 
1521  free(str);
1523  /*
1524  * User not specified address -> autogenerate one from cluster_name key
1525  * (if available). Return code is intentionally ignored, because
1526  * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
1527  * checked later anyway.
1528  */
1529 
1530  if (totem_config->interfaces[0].bindnet.family == AF_INET) {
1531  tmp_ip_version = TOTEM_IP_VERSION_4;
1532  } else if (totem_config->interfaces[0].bindnet.family == AF_INET6) {
1533  tmp_ip_version = TOTEM_IP_VERSION_6;
1534  }
1535 
1536  (void)get_cluster_mcast_addr (cluster_name,
1537  linknumber,
1538  tmp_ip_version,
1539  &totem_config->interfaces[linknumber].mcast_addr);
1540  }
1541 
1542  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
1543  if (icmap_get_string(tmp_key, &str) == CS_OK) {
1544  if (strcmp (str, "yes") == 0) {
1546  }
1547  free(str);
1548  }
1549  }
1550 
1551  /* These things are only valid for the initial read OR a newly-defined link */
1552  if (!reload || (totem_config->interfaces[linknumber].configured == 0)) {
1553 
1554  /*
1555  * Get mcast port
1556  */
1557  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
1558  if (icmap_get_uint16(tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
1559  if (totem_config->broadcast_use) {
1560  totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
1561  } else {
1562  totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + linknumber;
1563  }
1564  }
1565 
1566  /*
1567  * Get the TTL
1568  */
1569  totem_config->interfaces[linknumber].ttl = 1;
1570 
1571  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
1572 
1573  if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1574  totem_config->interfaces[linknumber].ttl = u8;
1575  }
1576 
1578  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
1579  if (icmap_get_string(tmp_key, &str) == CS_OK) {
1580  if (strcmp(str, "sctp") == 0) {
1581  totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
1582  }
1583  else if (strcmp(str, "udp") == 0) {
1584  totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
1585  }
1586  else {
1587  *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
1588  return -1;
1589  }
1590  }
1591  }
1592  totem_config->interfaces[linknumber].configured = 1;
1593 
1594  /*
1595  * Get the knet link params
1596  */
1597  totem_config->interfaces[linknumber].knet_link_priority = 1;
1598  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
1599 
1600  if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
1601  totem_config->interfaces[linknumber].knet_link_priority = u8;
1602  }
1603 
1604  totem_config->interfaces[linknumber].knet_ping_interval = 0; /* real default applied later */
1605  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
1606  if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1607  totem_config->interfaces[linknumber].knet_ping_interval = u32;
1608  }
1609  totem_config->interfaces[linknumber].knet_ping_timeout = 0; /* real default applied later */
1610  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
1611  if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1612  totem_config->interfaces[linknumber].knet_ping_timeout = u32;
1613  }
1615  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
1616  if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1617  totem_config->interfaces[linknumber].knet_ping_precision = u32;
1618  }
1620  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
1621  if (icmap_get_uint32(tmp_key, &u32) == CS_OK) {
1622  totem_config->interfaces[linknumber].knet_pong_count = u32;
1623  }
1624 
1625  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
1626  member_iter = icmap_iter_init(tmp_key);
1627  while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
1628  if (member_count == 0) {
1629  if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
1630  free(str);
1632  break;
1633  } else {
1635  }
1636  }
1637 
1638  if (icmap_get_string(member_iter_key, &str) == CS_OK) {
1640  str, totem_config->ip_version);
1641  if (res) {
1642  sprintf(error_string_response, "failed to parse node address '%s'\n", str);
1643  *error_string = error_string_response;
1644 
1645  icmap_iter_finalize(member_iter);
1646  icmap_iter_finalize(iter);
1647  free(str);
1648  return -1;
1649  }
1650 
1651  free(str);
1652  }
1653  }
1654  icmap_iter_finalize(member_iter);
1655 
1657 
1658  }
1659  icmap_iter_finalize(iter);
1660 
1661  return 0;
1662 }
1663 
1664 extern int totem_config_read (
1665  struct totem_config *totem_config,
1666  const char **error_string,
1667  uint64_t *warnings)
1668 {
1669  int res = 0;
1670  char *str, *ring0_addr_str;
1671  char tmp_key[ICMAP_KEYNAME_MAXLEN];
1672  uint16_t u16;
1673  int i;
1674  int local_node_pos;
1675  int nodeid_set;
1676 
1677  *warnings = 0;
1678 
1679  memset (totem_config, 0, sizeof (struct totem_config));
1680  totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
1681  if (totem_config->interfaces == 0) {
1682  *error_string = "Out of memory trying to allocate ethernet interface storage area";
1683  return -1;
1684  }
1685 
1687  if (icmap_get_string("totem.transport", &str) == CS_OK) {
1688  if (strcmp (str, "udpu") == 0) {
1690  }
1691 
1692  if (strcmp (str, "udp") == 0) {
1694  }
1695 
1696  if (strcmp (str, "knet") == 0) {
1698  }
1699 
1700  free(str);
1701  }
1702 
1703  memset (totem_config->interfaces, 0,
1704  sizeof (struct totem_interface) * INTERFACE_MAX);
1705 
1706  strcpy (totem_config->link_mode, "passive");
1707 
1708  icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
1709 
1710  if (totem_get_crypto(totem_config, error_string) != 0) {
1711  return -1;
1712  }
1713 
1714  if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
1715  if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
1716  *error_string = "totem.link_mode is too long";
1717  free(str);
1718 
1719  return -1;
1720  }
1721  strcpy (totem_config->link_mode, str);
1722  free(str);
1723  }
1724 
1725  icmap_get_uint32("totem.nodeid", &totem_config->node_id);
1726 
1728  if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
1729  if (strcmp (str, "yes") == 0) {
1731  }
1732  free(str);
1733  }
1734 
1735  icmap_get_uint32("totem.threads", &totem_config->threads);
1736 
1737  icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
1738 
1739  totem_config->ip_version = totem_config_get_ip_version(totem_config);
1740 
1741  if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
1742  /*
1743  * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
1744  */
1745  config_convert_nodelist_to_interface(totem_config);
1746  } else {
1747  if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
1748  /*
1749  * Both bindnetaddr and ring0_addr are set.
1750  * Log warning information, and use nodelist instead
1751  */
1753 
1754  config_convert_nodelist_to_interface(totem_config);
1755 
1756  free(ring0_addr_str);
1757  }
1758 
1759  free(str);
1760  }
1761 
1762  /*
1763  * Broadcast option is global but set in interface section,
1764  * so reset before processing interfaces.
1765  */
1767 
1768  res = get_interface_params(totem_config, error_string, warnings, 0);
1769  if (res < 0) {
1770  return res;
1771  }
1772 
1773  /*
1774  * Use broadcast is global, so if set, make sure to fill mcast addr correctly
1775  * broadcast is only supported for UDP so just do interface 0;
1776  */
1777  if (totem_config->broadcast_use) {
1779  "255.255.255.255", TOTEM_IP_VERSION_4);
1780  }
1781 
1782 
1783  /*
1784  * Store automatically generated items back to icmap only for UDP
1785  */
1787  for (i = 0; i < INTERFACE_MAX; i++) {
1788  if (!totem_config->interfaces[i].configured) {
1789  continue;
1790  }
1791  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
1792  if (icmap_get_string(tmp_key, &str) == CS_OK) {
1793  free(str);
1794  } else {
1795  str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
1796  icmap_set_string(tmp_key, str);
1797  }
1798 
1799  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
1800  if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
1802  }
1803  }
1804  }
1805 
1806  /*
1807  * Check existence of nodelist
1808  */
1809  if ((icmap_get_string("nodelist.node.0.name", &str) == CS_OK) ||
1810  (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK)) {
1811  free(str);
1812  /*
1813  * find local node
1814  */
1815  local_node_pos = find_local_node(1);
1816  if (local_node_pos != -1) {
1817 
1818  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
1819 
1820  nodeid_set = (totem_config->node_id != 0);
1821  if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
1823  }
1825  *error_string = "Knet requires an explicit nodeid for the local node";
1826  return -1;
1827  }
1828 
1831 
1832  snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
1833  icmap_get_string(tmp_key, &str);
1834 
1835  totem_config->node_id = generate_nodeid(totem_config, str);
1836  if (totem_config->node_id == -1) {
1837  *error_string = "An IPV6 network requires that a node ID be specified";
1838 
1839  free(str);
1840  return (-1);
1841  }
1842 
1844 
1845  free(str);
1846  }
1847 
1848  /* Users must not change this */
1849  icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
1850  }
1851 
1852  if (put_nodelist_members_to_config(totem_config, 0, error_string)) {
1853  return -1;
1854  }
1855  }
1856 
1857  /*
1858  * Get things that might change in the future (and can depend on totem_config->interfaces);
1859  */
1860  totem_volatile_config_read(totem_config, NULL);
1861 
1862  calc_knet_ping_timers(totem_config);
1863 
1864  icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
1865 
1866  add_totem_config_notification(totem_config);
1867 
1868  return 0;
1869 }
1870 
1871 
1873  struct totem_config *totem_config,
1874  const char **error_string)
1875 {
1876  static char local_error_reason[512];
1877  char parse_error[512];
1878  static char addr_str_buf[INET6_ADDRSTRLEN];
1879  const char *error_reason = local_error_reason;
1880  int i,j;
1881  uint32_t u32;
1882  int num_configured = 0;
1883  unsigned int interface_max = INTERFACE_MAX;
1884 
1885  for (i = 0; i < INTERFACE_MAX; i++) {
1887  num_configured++;
1888  }
1889  }
1890  if (num_configured == 0) {
1891  error_reason = "No interfaces defined";
1892  goto parse_error;
1893  }
1894 
1895  /* Check we found a local node name */
1896  if (icmap_get_uint32("nodelist.local_node_pos", &u32) != CS_OK) {
1897  error_reason = "No valid name found for local host";
1898  goto parse_error;
1899  }
1900 
1901  for (i = 0; i < INTERFACE_MAX; i++) {
1902  /*
1903  * Some error checking of parsed data to make sure its valid
1904  */
1905 
1906  struct totem_ip_address null_addr;
1907 
1908  if (!totem_config->interfaces[i].configured) {
1909  continue;
1910  }
1911 
1912  memset (&null_addr, 0, sizeof (struct totem_ip_address));
1913 
1915  memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
1916  sizeof (struct totem_ip_address)) == 0) {
1917  error_reason = "No multicast address specified";
1918  goto parse_error;
1919  }
1920 
1921  if (totem_config->interfaces[i].ip_port == 0) {
1922  error_reason = "No multicast port specified";
1923  goto parse_error;
1924  }
1925 
1926  if (totem_config->interfaces[i].ttl > 255) {
1927  error_reason = "Invalid TTL (should be 0..255)";
1928  goto parse_error;
1929  }
1931  totem_config->interfaces[i].ttl != 1) {
1932  error_reason = "Can only set ttl on multicast transport types";
1933  goto parse_error;
1934  }
1935  if (totem_config->interfaces[i].knet_link_priority > 255) {
1936  error_reason = "Invalid link priority (should be 0..255)";
1937  goto parse_error;
1938  }
1941  error_reason = "Can only set link priority on knet transport type";
1942  goto parse_error;
1943  }
1944 
1945  if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
1946  totem_config->node_id == 0) {
1947 
1948  error_reason = "An IPV6 network requires that a node ID be specified.";
1949  goto parse_error;
1950  }
1951 
1954  error_reason = "Multicast address family does not match bind address family";
1955  goto parse_error;
1956  }
1957 
1959  error_reason = "mcastaddr is not a correct multicast address.";
1960  goto parse_error;
1961  }
1962  }
1963  /* Verify that all nodes on the same knet link have the same IP family */
1964  for (j=1; j<totem_config->interfaces[i].member_count; j++) {
1968  memcpy(addr_str_buf,
1970  sizeof(addr_str_buf));
1971 
1972  snprintf (local_error_reason, sizeof(local_error_reason),
1973  "Nodes for link %d have different IP families "
1974  "(compared %s with %s)", i,
1975  addr_str_buf,
1977  goto parse_error;
1978  }
1979  }
1980  }
1981  }
1982 
1983  if (totem_config->version != 2) {
1984  error_reason = "This totem parser can only parse version 2 configurations.";
1985  goto parse_error;
1986  }
1987 
1988  if (totem_volatile_config_validate(totem_config, error_string) == -1) {
1989  return (-1);
1990  }
1991 
1992  if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
1993  return (-1);
1994  }
1995 
1996  /*
1997  * KNET Link values validation
1998  */
1999  if (strcmp (totem_config->link_mode, "active") &&
2000  strcmp (totem_config->link_mode, "rr") &&
2001  strcmp (totem_config->link_mode, "passive")) {
2002  snprintf (local_error_reason, sizeof(local_error_reason),
2003  "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
2004  goto parse_error;
2005  }
2006 
2007  /* Only Knet does multiple interfaces */
2009  interface_max = 1;
2010  }
2011 
2012  if (interface_max < num_configured) {
2013  snprintf (parse_error, sizeof(parse_error),
2014  "%d is too many configured interfaces for non-Knet transport.",
2015  num_configured);
2016  error_reason = parse_error;
2017  goto parse_error;
2018  }
2019 
2020  /* Only knet allows crypto */
2022  if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
2023  (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
2024 
2025  snprintf (parse_error, sizeof(parse_error),
2026  "crypto_cipher & crypto_hash are only valid for the Knet transport.");
2027  error_reason = parse_error;
2028  goto parse_error;
2029  }
2030  }
2031 
2032  if (totem_config->net_mtu == 0) {
2034  totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
2035  }
2036  else {
2037  totem_config->net_mtu = 1500;
2038  }
2039  }
2040 
2041  return 0;
2042 
2043 parse_error:
2044  snprintf (error_string_response, sizeof(error_string_response),
2045  "parse error in config: %s\n", error_reason);
2046  *error_string = error_string_response;
2047  return (-1);
2048 
2049 }
2050 
2051 static int read_keyfile (
2052  const char *key_location,
2053  struct totem_config *totem_config,
2054  const char **error_string)
2055 {
2056  int fd;
2057  int res;
2058  int saved_errno;
2059  char error_str[100];
2060  const char *error_ptr;
2061 
2062  fd = open (key_location, O_RDONLY);
2063  if (fd == -1) {
2064  error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
2065  snprintf (error_string_response, sizeof(error_string_response),
2066  "Could not open %s: %s\n",
2067  key_location, error_ptr);
2068  goto parse_error;
2069  }
2070 
2072  saved_errno = errno;
2073  close (fd);
2074 
2075  if (res == -1) {
2076  error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
2077  snprintf (error_string_response, sizeof(error_string_response),
2078  "Could not read %s: %s\n",
2079  key_location, error_ptr);
2080  goto parse_error;
2081  }
2082 
2083  if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
2084  snprintf (error_string_response, sizeof(error_string_response),
2085  "Could only read %d bits of minimum %u bits from %s.\n",
2086  res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
2087  goto parse_error;
2088  }
2089 
2091 
2092  return 0;
2093 
2094 parse_error:
2095  *error_string = error_string_response;
2096  return (-1);
2097 }
2098 
2100  struct totem_config *totem_config,
2101  const char **error_string)
2102 {
2103  int got_key = 0;
2104  char *key_location = NULL;
2105  int res;
2106  size_t key_len;
2107 
2108  memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
2110 
2111  if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
2112  strcmp(totem_config->crypto_hash_type, "none") == 0) {
2113  return (0);
2114  }
2115 
2116  /* cmap may store the location of the key file */
2117  if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
2118  res = read_keyfile(key_location, totem_config, error_string);
2119  free(key_location);
2120  if (res) {
2121  goto key_error;
2122  }
2123  got_key = 1;
2124  } else { /* Or the key itself may be in the cmap */
2125  if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
2126  if (key_len > sizeof(totem_config->private_key)) {
2127  sprintf(error_string_response, "key is too long");
2128  goto key_error;
2129  }
2130  if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
2131  sprintf(error_string_response, "key is too short");
2132  goto key_error;
2133  }
2134  if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
2135  totem_config->private_key_len = key_len;
2136  got_key = 1;
2137  } else {
2138  sprintf(error_string_response, "can't load private key");
2139  goto key_error;
2140  }
2141  }
2142  }
2143 
2144  /* In desperation we read the default filename */
2145  if (!got_key) {
2146  res = read_keyfile(COROSYSCONFDIR "/authkey", totem_config, error_string);
2147  if (res)
2148  goto key_error;
2149  }
2150 
2151  return (0);
2152 
2153 key_error:
2154  *error_string = error_string_response;
2155  return (-1);
2156 
2157 }
2158 
2159 static void debug_dump_totem_config(const struct totem_config *totem_config)
2160 {
2161 
2162  log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
2164  if (totem_config->token_warning) {
2165  uint32_t token_warning_ms = totem_config->token_warning * totem_config->token_timeout / 100;
2166  log_printf(LOGSYS_LEVEL_DEBUG, "Token warning every %d ms (%d%% of Token Timeout)",
2167  token_warning_ms, totem_config->token_warning);
2168  if (token_warning_ms < totem_config->token_retransmit_timeout)
2170  "The token warning interval (%d ms) is less than the token retransmit timeout (%d ms) "
2171  "which can lead to spurious token warnings. Consider increasing the token_warning parameter.",
2172  token_warning_ms, totem_config->token_retransmit_timeout);
2173 
2174  } else
2175  log_printf(LOGSYS_LEVEL_DEBUG, "Token warnings disabled");
2176  log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
2178  log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
2181  log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
2184  "seqno unchanged const (%d rotations) Maximum network MTU %d",
2187  "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
2189  log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
2190  log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
2192  log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
2193 }
2194 
2195 static void totem_change_notify(
2196  int32_t event,
2197  const char *key_name,
2198  struct icmap_notify_value new_val,
2199  struct icmap_notify_value old_val,
2200  void *user_data)
2201 {
2202  struct totem_config *totem_config = (struct totem_config *)user_data;
2203  uint32_t *param;
2204  uint8_t reloading;
2205  const char *deleted_key = NULL;
2206  const char *error_string;
2207 
2208  /*
2209  * If a full reload is in progress then don't do anything until it's done and
2210  * can reconfigure it all atomically
2211  */
2212  if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
2213  return;
2214 
2215  param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
2216  /*
2217  * Process change only if changed key is found in totem_config (-> param is not NULL)
2218  * or for special key token_coefficient. token_coefficient key is not stored in
2219  * totem_config, but it is used for computation of token timeout.
2220  */
2221  if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
2222  return;
2223 
2224  /*
2225  * Values other than UINT32 are not supported, or needed (yet)
2226  */
2227  switch (event) {
2228  case ICMAP_TRACK_DELETE:
2229  deleted_key = key_name;
2230  break;
2231  case ICMAP_TRACK_ADD:
2232  case ICMAP_TRACK_MODIFY:
2233  deleted_key = NULL;
2234  break;
2235  default:
2236  break;
2237  }
2238 
2239  totem_volatile_config_read (totem_config, deleted_key);
2240  log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
2241  debug_dump_totem_config(totem_config);
2242  if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2243  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2244  /*
2245  * TODO: Consider corosync exit and/or load defaults for volatile
2246  * values. For now, log error seems to be enough
2247  */
2248  }
2249 }
2250 
2251 static void totem_reload_notify(
2252  int32_t event,
2253  const char *key_name,
2254  struct icmap_notify_value new_val,
2255  struct icmap_notify_value old_val,
2256  void *user_data)
2257 {
2258  struct totem_config *totem_config = (struct totem_config *)user_data;
2259  const char *error_string;
2260  uint64_t warnings;
2261 
2262  /* Reload has completed */
2263  if (*(uint8_t *)new_val.data == 0) {
2264 
2265  totem_config->orig_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
2266  assert(totem_config->orig_interfaces != NULL);
2268 
2269  get_interface_params(totem_config, &error_string, &warnings, 1);
2270  if (put_nodelist_members_to_config (totem_config, 1, &error_string)) {
2271  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2272  }
2273  totem_volatile_config_read (totem_config, NULL);
2274 
2275  calc_knet_ping_timers(totem_config);
2276 
2277  log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
2278  debug_dump_totem_config(totem_config);
2279  if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
2280  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
2281  /*
2282  * TODO: Consider corosync exit and/or load defaults for volatile
2283  * values. For now, log error seems to be enough
2284  */
2285  }
2286 
2287  /* Reinstate the local_node_pos */
2288  (void)find_local_node(0);
2289 
2290  /* Reconfigure network params as appropriate */
2292 
2294 
2295  icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
2296  } else {
2297  icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
2298  }
2299 }
2300 
2301 static void add_totem_config_notification(struct totem_config *totem_config)
2302 {
2304 
2305  icmap_track_add("totem.",
2307  totem_change_notify,
2308  totem_config,
2309  &icmap_track);
2310 
2311  icmap_track_add("config.reload_in_progress",
2313  totem_reload_notify,
2314  totem_config,
2315  &icmap_track);
2316 }
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:571
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:100
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
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:1664
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
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:1515
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:1217
icmap_iter_finalize
void icmap_iter_finalize(icmap_iter_t iter)
Finalize iterator.
Definition: icmap.c:1108
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
Definition: totemip.h:71
TOTEM_IP_VERSION_6
Definition: totemip.h:72
totem_interface::knet_link_priority
int knet_link_priority
Definition: totem.h:89
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:838
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:723
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
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:77
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
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:625
param
uint8_t param
Definition: exec/votequorum.c:101
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:1087
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:583
icmap_set_uint32
cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
Definition: icmap.c:595
CS_OK
Definition: corotypes.h:95
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:1522
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:1151
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:1529
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:1872
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:850
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
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:1081
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:880
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:844
TOKEN_COEFFICIENT
#define TOKEN_COEFFICIENT
Definition: totemconfig.c:70
nodeid
unsigned int nodeid
Definition: coroapi.h:75
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:2099
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
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:826
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:589
totempg_iface_set
int totempg_iface_set(struct totem_ip_address *interface_addr, unsigned short ip_port, unsigned int iface_no)
Definition: totempg.c:1404
totem_config::knet_compression_model
char * knet_compression_model
Definition: totem.h:224