totemconfig.c 26 KB

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