totemconfig.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2011 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. #include <config.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <sys/param.h>
  49. #include <corosync/swab.h>
  50. #include <corosync/list.h>
  51. #include <qb/qbdefs.h>
  52. #include <corosync/totem/totem.h>
  53. #include <corosync/config.h>
  54. #include <corosync/logsys.h>
  55. #include <corosync/icmap.h>
  56. #ifdef HAVE_LIBNSS
  57. #include <nss.h>
  58. #include <pk11pub.h>
  59. #include <pkcs11.h>
  60. #include <prerror.h>
  61. #endif
  62. #include "util.h"
  63. #include "totemconfig.h"
  64. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  65. #define TOKEN_TIMEOUT 1000
  66. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  67. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  68. #define JOIN_TIMEOUT 50
  69. #define MERGE_TIMEOUT 200
  70. #define DOWNCHECK_TIMEOUT 1000
  71. #define FAIL_TO_RECV_CONST 2500
  72. #define SEQNO_UNCHANGED_CONST 30
  73. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  74. #define MAX_NETWORK_DELAY 50
  75. #define WINDOW_SIZE 50
  76. #define MAX_MESSAGES 17
  77. #define MISS_COUNT_CONST 5
  78. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  79. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  80. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 2
  81. #define RRP_AUTORECOVERY_CHECK_TIMEOUT 1000
  82. static char error_string_response[512];
  83. static void add_totem_config_notification(struct totem_config *totem_config);
  84. static void totem_volatile_config_read (struct totem_config *totem_config)
  85. {
  86. char *str;
  87. icmap_get_uint32("totem.token", &totem_config->token_timeout);
  88. icmap_get_uint32("totem.token_retransmit", &totem_config->token_retransmit_timeout);
  89. icmap_get_uint32("totem.hold", &totem_config->token_hold_timeout);
  90. icmap_get_uint32("totem.token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  91. icmap_get_uint32("totem.join", &totem_config->join_timeout);
  92. icmap_get_uint32("totem.send_join", &totem_config->send_join_timeout);
  93. icmap_get_uint32("totem.consensus", &totem_config->consensus_timeout);
  94. icmap_get_uint32("totem.merge", &totem_config->merge_timeout);
  95. icmap_get_uint32("totem.downcheck", &totem_config->downcheck_timeout);
  96. icmap_get_uint32("totem.fail_recv_const", &totem_config->fail_to_recv_const);
  97. icmap_get_uint32("totem.seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  98. icmap_get_uint32("totem.rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  99. icmap_get_uint32("totem.rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  100. icmap_get_uint32("totem.rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  101. icmap_get_uint32("totem.rrp_problem_count_mcast_threshold", &totem_config->rrp_problem_count_mcast_threshold);
  102. icmap_get_uint32("totem.rrp_autorecovery_check_timeout", &totem_config->rrp_autorecovery_check_timeout);
  103. icmap_get_uint32("totem.heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  104. icmap_get_uint32("totem.max_network_delay", &totem_config->max_network_delay);
  105. icmap_get_uint32("totem.window_size", &totem_config->window_size);
  106. icmap_get_uint32("totem.max_messages", &totem_config->max_messages);
  107. icmap_get_uint32("totem.miss_count_const", &totem_config->miss_count_const);
  108. if (icmap_get_string("totem.vsftype", &str) == CS_OK) {
  109. totem_config->vsf_type = str;
  110. }
  111. }
  112. static void totem_get_crypto_type(struct totem_config *totem_config)
  113. {
  114. char *str;
  115. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_OLD;
  116. if (icmap_get_string("totem.crypto_accept", &str) == CS_OK) {
  117. if (strcmp(str, "new") == 0) {
  118. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_NEW;
  119. }
  120. free(str);
  121. }
  122. totem_config->crypto_type = TOTEM_CRYPTO_SOBER;
  123. #ifdef HAVE_LIBNSS
  124. /*
  125. * We must set these even if the key does not exist.
  126. * Encryption type can be set on-the-fly using CFG
  127. */
  128. totem_config->crypto_crypt_type = CKM_AES_CBC_PAD;
  129. totem_config->crypto_sign_type = CKM_SHA256_RSA_PKCS;
  130. #endif
  131. if (icmap_get_string("totem.crypto_type", &str) == CS_OK) {
  132. if (strcmp(str, "sober") == 0) {
  133. free(str);
  134. return;
  135. }
  136. #ifdef HAVE_LIBNSS
  137. if (strcmp(str, "nss") == 0) {
  138. totem_config->crypto_type = TOTEM_CRYPTO_NSS;
  139. }
  140. free(str);
  141. #endif
  142. }
  143. }
  144. static uint16_t generate_cluster_id (const char *cluster_name)
  145. {
  146. int i;
  147. int value = 0;
  148. for (i = 0; i < strlen(cluster_name); i++) {
  149. value <<= 1;
  150. value += cluster_name[i];
  151. }
  152. return (value & 0xFFFF);
  153. }
  154. static int get_cluster_mcast_addr (
  155. const char *cluster_name,
  156. const struct totem_ip_address *bindnet,
  157. unsigned int ringnumber,
  158. struct totem_ip_address *res)
  159. {
  160. uint16_t clusterid;
  161. char addr[INET6_ADDRSTRLEN + 1];
  162. int err;
  163. if (cluster_name == NULL) {
  164. return (-1);
  165. }
  166. clusterid = generate_cluster_id(cluster_name) + ringnumber;
  167. memset (res, 0, sizeof(res));
  168. switch (bindnet->family) {
  169. case AF_INET:
  170. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  171. break;
  172. case AF_INET6:
  173. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  174. break;
  175. default:
  176. /*
  177. * Unknown family
  178. */
  179. return (-1);
  180. }
  181. err = totemip_parse (res, addr, 0);
  182. return (err);
  183. }
  184. extern int totem_config_read (
  185. struct totem_config *totem_config,
  186. const char **error_string)
  187. {
  188. int res = 0;
  189. char *str;
  190. unsigned int ringnumber = 0;
  191. int member_count = 0;
  192. icmap_iter_t iter, member_iter;
  193. const char *iter_key;
  194. const char *member_iter_key;
  195. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  196. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  197. uint8_t u8;
  198. char *cluster_name = NULL;
  199. memset (totem_config, 0, sizeof (struct totem_config));
  200. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  201. if (totem_config->interfaces == 0) {
  202. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  203. return -1;
  204. }
  205. memset (totem_config->interfaces, 0,
  206. sizeof (struct totem_interface) * INTERFACE_MAX);
  207. totem_config->secauth = 1;
  208. strcpy (totem_config->rrp_mode, "none");
  209. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  210. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  211. if (strcmp (str, "on") == 0) {
  212. totem_config->secauth = 1;
  213. }
  214. if (strcmp (str, "off") == 0) {
  215. totem_config->secauth = 0;
  216. }
  217. free(str);
  218. }
  219. if (totem_config->secauth == 1) {
  220. totem_get_crypto_type(totem_config);
  221. }
  222. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  223. strcpy (totem_config->rrp_mode, str);
  224. free(str);
  225. }
  226. /*
  227. * Get interface node id
  228. */
  229. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  230. totem_config->clear_node_high_bit = 0;
  231. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  232. if (strcmp (str, "yes") == 0) {
  233. totem_config->clear_node_high_bit = 1;
  234. }
  235. free(str);
  236. }
  237. icmap_get_uint32("totem.threads", &totem_config->threads);
  238. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  239. icmap_get_string("totem.cluster_name", &cluster_name);
  240. /*
  241. * Get things that might change in the future
  242. */
  243. totem_volatile_config_read(totem_config);
  244. iter = icmap_iter_init("totem.interface.");
  245. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  246. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  247. if (res != 2) {
  248. continue;
  249. }
  250. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  251. continue;
  252. }
  253. member_count = 0;
  254. ringnumber = atoi(ringnumber_key);
  255. /*
  256. * Get the bind net address
  257. */
  258. if (icmap_get_string(iter_key, &str) == CS_OK) {
  259. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  260. totem_config->interfaces[ringnumber].mcast_addr.family);
  261. free(str);
  262. }
  263. /*
  264. * Get interface multicast address
  265. */
  266. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  267. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  268. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  269. free(str);
  270. } else {
  271. /*
  272. * User not specified address -> autogenerate one from cluster_name key
  273. * (if available)
  274. */
  275. res = get_cluster_mcast_addr (cluster_name,
  276. &totem_config->interfaces[ringnumber].bindnet,
  277. ringnumber,
  278. &totem_config->interfaces[ringnumber].mcast_addr);
  279. }
  280. totem_config->broadcast_use = 0;
  281. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  282. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  283. if (strcmp (str, "yes") == 0) {
  284. totem_config->broadcast_use = 1;
  285. totemip_parse (
  286. &totem_config->interfaces[ringnumber].mcast_addr,
  287. "255.255.255.255", 0);
  288. }
  289. free(str);
  290. }
  291. /*
  292. * Get mcast port
  293. */
  294. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  295. icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port);
  296. /*
  297. * Get the TTL
  298. */
  299. totem_config->interfaces[ringnumber].ttl = 1;
  300. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  301. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  302. totem_config->interfaces[ringnumber].ttl = u8;
  303. }
  304. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  305. member_iter = icmap_iter_init(tmp_key);
  306. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  307. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  308. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  309. str, 0);
  310. }
  311. }
  312. icmap_iter_finalize(member_iter);
  313. totem_config->interfaces[ringnumber].member_count = member_count;
  314. totem_config->interface_count++;
  315. }
  316. icmap_iter_finalize(iter);
  317. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  318. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  319. if (strcmp (str, "udpu") == 0) {
  320. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  321. }
  322. if (strcmp (str, "iba") == 0) {
  323. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  324. }
  325. free(str);
  326. }
  327. free(cluster_name);
  328. add_totem_config_notification(totem_config);
  329. return 0;
  330. }
  331. int totem_config_validate (
  332. struct totem_config *totem_config,
  333. const char **error_string)
  334. {
  335. static char local_error_reason[512];
  336. char parse_error[512];
  337. const char *error_reason = local_error_reason;
  338. int i;
  339. unsigned int interface_max = INTERFACE_MAX;
  340. if (totem_config->interface_count == 0) {
  341. error_reason = "No interfaces defined";
  342. goto parse_error;
  343. }
  344. for (i = 0; i < totem_config->interface_count; i++) {
  345. /*
  346. * Some error checking of parsed data to make sure its valid
  347. */
  348. struct totem_ip_address null_addr;
  349. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  350. if ((totem_config->transport_number == 0) &&
  351. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  352. sizeof (struct totem_ip_address)) == 0) {
  353. error_reason = "No multicast address specified";
  354. goto parse_error;
  355. }
  356. if (totem_config->interfaces[i].ip_port == 0) {
  357. error_reason = "No multicast port specified";
  358. goto parse_error;
  359. }
  360. if (totem_config->interfaces[i].ttl > 255) {
  361. error_reason = "Invalid TTL (should be 0..255)";
  362. goto parse_error;
  363. }
  364. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  365. totem_config->interfaces[i].ttl != 1) {
  366. error_reason = "Can only set ttl on multicast transport types";
  367. goto parse_error;
  368. }
  369. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  370. totem_config->node_id == 0) {
  371. error_reason = "An IPV6 network requires that a node ID be specified.";
  372. goto parse_error;
  373. }
  374. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  375. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  376. error_reason = "Multicast address family does not match bind address family";
  377. goto parse_error;
  378. }
  379. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  380. error_reason = "Not all bind address belong to the same IP family";
  381. goto parse_error;
  382. }
  383. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  384. error_reason = "mcastaddr is not a correct multicast address.";
  385. goto parse_error;
  386. }
  387. }
  388. }
  389. if (totem_config->version != 2) {
  390. error_reason = "This totem parser can only parse version 2 configurations.";
  391. goto parse_error;
  392. }
  393. if (totem_config->token_retransmits_before_loss_const == 0) {
  394. totem_config->token_retransmits_before_loss_const =
  395. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  396. }
  397. /*
  398. * Setup timeout values that are not setup by user
  399. */
  400. if (totem_config->token_timeout == 0) {
  401. totem_config->token_timeout = TOKEN_TIMEOUT;
  402. if (totem_config->token_retransmits_before_loss_const == 0) {
  403. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  404. }
  405. if (totem_config->token_retransmit_timeout == 0) {
  406. totem_config->token_retransmit_timeout =
  407. (int)(totem_config->token_timeout /
  408. (totem_config->token_retransmits_before_loss_const + 0.2));
  409. }
  410. if (totem_config->token_hold_timeout == 0) {
  411. totem_config->token_hold_timeout =
  412. (int)(totem_config->token_retransmit_timeout * 0.8 -
  413. (1000/HZ));
  414. }
  415. }
  416. if (totem_config->max_network_delay == 0) {
  417. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  418. }
  419. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  420. snprintf (local_error_reason, sizeof(local_error_reason),
  421. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  422. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  423. goto parse_error;
  424. }
  425. if (totem_config->window_size == 0) {
  426. totem_config->window_size = WINDOW_SIZE;
  427. }
  428. if (totem_config->max_messages == 0) {
  429. totem_config->max_messages = MAX_MESSAGES;
  430. }
  431. if (totem_config->miss_count_const == 0) {
  432. totem_config->miss_count_const = MISS_COUNT_CONST;
  433. }
  434. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  435. snprintf (local_error_reason, sizeof(local_error_reason),
  436. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  437. totem_config->token_timeout, MINIMUM_TIMEOUT);
  438. goto parse_error;
  439. }
  440. if (totem_config->token_retransmit_timeout == 0) {
  441. totem_config->token_retransmit_timeout =
  442. (int)(totem_config->token_timeout /
  443. (totem_config->token_retransmits_before_loss_const + 0.2));
  444. }
  445. if (totem_config->token_hold_timeout == 0) {
  446. totem_config->token_hold_timeout =
  447. (int)(totem_config->token_retransmit_timeout * 0.8 -
  448. (1000/HZ));
  449. }
  450. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  451. snprintf (local_error_reason, sizeof(local_error_reason),
  452. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  453. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  454. goto parse_error;
  455. }
  456. if (totem_config->token_hold_timeout == 0) {
  457. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  458. }
  459. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  460. snprintf (local_error_reason, sizeof(local_error_reason),
  461. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  462. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  463. goto parse_error;
  464. }
  465. if (totem_config->join_timeout == 0) {
  466. totem_config->join_timeout = JOIN_TIMEOUT;
  467. }
  468. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  469. snprintf (local_error_reason, sizeof(local_error_reason),
  470. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  471. totem_config->join_timeout, MINIMUM_TIMEOUT);
  472. goto parse_error;
  473. }
  474. if (totem_config->consensus_timeout == 0) {
  475. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  476. }
  477. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  478. snprintf (local_error_reason, sizeof(local_error_reason),
  479. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  480. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  481. goto parse_error;
  482. }
  483. if (totem_config->merge_timeout == 0) {
  484. totem_config->merge_timeout = MERGE_TIMEOUT;
  485. }
  486. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  487. snprintf (local_error_reason, sizeof(local_error_reason),
  488. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  489. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  490. goto parse_error;
  491. }
  492. if (totem_config->downcheck_timeout == 0) {
  493. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  494. }
  495. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  496. snprintf (local_error_reason, sizeof(local_error_reason),
  497. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  498. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  499. goto parse_error;
  500. }
  501. /*
  502. * RRP values validation
  503. */
  504. if (strcmp (totem_config->rrp_mode, "none") &&
  505. strcmp (totem_config->rrp_mode, "active") &&
  506. strcmp (totem_config->rrp_mode, "passive")) {
  507. snprintf (local_error_reason, sizeof(local_error_reason),
  508. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  509. goto parse_error;
  510. }
  511. if (totem_config->rrp_problem_count_timeout == 0) {
  512. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  513. }
  514. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  515. snprintf (local_error_reason, sizeof(local_error_reason),
  516. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  517. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  518. goto parse_error;
  519. }
  520. if (totem_config->rrp_problem_count_threshold == 0) {
  521. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  522. }
  523. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  524. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  525. }
  526. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  527. snprintf (local_error_reason, sizeof(local_error_reason),
  528. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  529. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  530. goto parse_error;
  531. }
  532. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  533. snprintf (local_error_reason, sizeof(local_error_reason),
  534. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  535. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  536. goto parse_error;
  537. }
  538. if (totem_config->rrp_token_expired_timeout == 0) {
  539. totem_config->rrp_token_expired_timeout =
  540. totem_config->token_retransmit_timeout;
  541. }
  542. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  543. snprintf (local_error_reason, sizeof(local_error_reason),
  544. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  545. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  546. goto parse_error;
  547. }
  548. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  549. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  550. }
  551. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  552. interface_max = 1;
  553. }
  554. if (interface_max < totem_config->interface_count) {
  555. snprintf (parse_error, sizeof(parse_error),
  556. "%d is too many configured interfaces for the rrp_mode setting %s.",
  557. totem_config->interface_count,
  558. totem_config->rrp_mode);
  559. error_reason = parse_error;
  560. goto parse_error;
  561. }
  562. if (totem_config->fail_to_recv_const == 0) {
  563. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  564. }
  565. if (totem_config->seqno_unchanged_const == 0) {
  566. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  567. }
  568. if (totem_config->net_mtu == 0) {
  569. totem_config->net_mtu = 1500;
  570. }
  571. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  572. snprintf (local_error_reason, sizeof(local_error_reason),
  573. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  574. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  575. goto parse_error;
  576. }
  577. if (totem_config->threads > SEND_THREADS_MAX) {
  578. totem_config->threads = SEND_THREADS_MAX;
  579. }
  580. if (totem_config->secauth == 0) {
  581. totem_config->threads = 0;
  582. }
  583. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  584. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  585. goto parse_error;
  586. }
  587. if (totem_config->vsf_type == NULL) {
  588. totem_config->vsf_type = "none";
  589. }
  590. return (0);
  591. parse_error:
  592. snprintf (error_string_response, sizeof(error_string_response),
  593. "parse error in config: %s\n", error_reason);
  594. *error_string = error_string_response;
  595. return (-1);
  596. }
  597. static int read_keyfile (
  598. const char *key_location,
  599. struct totem_config *totem_config,
  600. const char **error_string)
  601. {
  602. int fd;
  603. int res;
  604. ssize_t expected_key_len = sizeof (totem_config->private_key);
  605. int saved_errno;
  606. char error_str[100];
  607. const char *error_ptr;
  608. fd = open (key_location, O_RDONLY);
  609. if (fd == -1) {
  610. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  611. snprintf (error_string_response, sizeof(error_string_response),
  612. "Could not open %s: %s\n",
  613. key_location, error_ptr);
  614. goto parse_error;
  615. }
  616. res = read (fd, totem_config->private_key, expected_key_len);
  617. saved_errno = errno;
  618. close (fd);
  619. if (res == -1) {
  620. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  621. snprintf (error_string_response, sizeof(error_string_response),
  622. "Could not read %s: %s\n",
  623. key_location, error_ptr);
  624. goto parse_error;
  625. }
  626. totem_config->private_key_len = expected_key_len;
  627. if (res != expected_key_len) {
  628. snprintf (error_string_response, sizeof(error_string_response),
  629. "Could only read %d bits of 1024 bits from %s.\n",
  630. res * 8, key_location);
  631. goto parse_error;
  632. }
  633. return 0;
  634. parse_error:
  635. *error_string = error_string_response;
  636. return (-1);
  637. }
  638. int totem_config_keyread (
  639. struct totem_config *totem_config,
  640. const char **error_string)
  641. {
  642. int got_key = 0;
  643. char *key_location = NULL;
  644. int res;
  645. size_t key_len;
  646. memset (totem_config->private_key, 0, 128);
  647. totem_config->private_key_len = 128;
  648. if (totem_config->secauth == 0) {
  649. return (0);
  650. }
  651. /* cmap may store the location of the key file */
  652. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  653. res = read_keyfile(key_location, totem_config, error_string);
  654. free(key_location);
  655. if (res) {
  656. goto key_error;
  657. }
  658. got_key = 1;
  659. } else { /* Or the key itself may be in the cmap */
  660. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  661. if (key_len > sizeof (totem_config->private_key)) {
  662. sprintf(error_string_response, "key is too long");
  663. goto key_error;
  664. }
  665. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  666. totem_config->private_key_len = key_len;
  667. got_key = 1;
  668. } else {
  669. sprintf(error_string_response, "can't store private key");
  670. goto key_error;
  671. }
  672. }
  673. }
  674. /* In desperation we read the default filename */
  675. if (!got_key) {
  676. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  677. if (!filename)
  678. filename = COROSYSCONFDIR "/authkey";
  679. res = read_keyfile(filename, totem_config, error_string);
  680. if (res)
  681. goto key_error;
  682. }
  683. return (0);
  684. key_error:
  685. *error_string = error_string_response;
  686. return (-1);
  687. }
  688. static void totem_change_notify(
  689. int32_t event,
  690. const char *key_name,
  691. struct icmap_notify_value new_val,
  692. struct icmap_notify_value old_val,
  693. void *user_data)
  694. {
  695. totem_volatile_config_read((struct totem_config *)user_data);
  696. }
  697. static void add_totem_config_notification(struct totem_config *totem_config)
  698. {
  699. icmap_track_t icmap_track;
  700. icmap_track_add("totem.",
  701. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  702. totem_change_notify,
  703. totem_config,
  704. &icmap_track);
  705. }