totemconfig.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. static int find_local_node_in_nodelist(struct totem_config *totem_config)
  186. {
  187. icmap_iter_t iter;
  188. const char *iter_key;
  189. int res = 0;
  190. int node_pos;
  191. int local_node_pos = -1;
  192. struct totem_ip_address bind_addr;
  193. int interface_up, interface_num;
  194. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  195. char *node_addr_str;
  196. struct totem_ip_address node_addr;
  197. res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
  198. &bind_addr, &interface_up, &interface_num,
  199. totem_config->clear_node_high_bit);
  200. if (res == -1) {
  201. return (-1);
  202. }
  203. iter = icmap_iter_init("nodelist.node.");
  204. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  205. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  206. if (res != 2) {
  207. continue;
  208. }
  209. if (strcmp(tmp_key, "ring0_addr") != 0) {
  210. continue;
  211. }
  212. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  213. if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
  214. continue;
  215. }
  216. res = totemip_parse (&node_addr, node_addr_str, 0);
  217. free(node_addr_str);
  218. if (res == -1) {
  219. continue ;
  220. }
  221. if (totemip_equal(&bind_addr, &node_addr)) {
  222. local_node_pos = node_pos;
  223. }
  224. }
  225. icmap_iter_finalize(iter);
  226. return (local_node_pos);
  227. }
  228. static void put_nodelist_members_to_config(struct totem_config *totem_config)
  229. {
  230. icmap_iter_t iter, iter2;
  231. const char *iter_key, *iter_key2;
  232. int res = 0;
  233. int node_pos;
  234. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  235. char *node_addr_str;
  236. int member_count;
  237. unsigned int ringnumber = 0;
  238. iter = icmap_iter_init("nodelist.node.");
  239. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  240. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  241. if (res != 2) {
  242. continue;
  243. }
  244. if (strcmp(tmp_key, "ring0_addr") != 0) {
  245. continue;
  246. }
  247. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  248. iter2 = icmap_iter_init(tmp_key);
  249. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  250. res = sscanf(iter_key2, "nodelist.node.%u.ring%u_addr", &node_pos, &ringnumber);
  251. if (res != 2) {
  252. continue;
  253. }
  254. if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
  255. continue;
  256. }
  257. member_count = totem_config->interfaces[ringnumber].member_count;
  258. res = totemip_parse(&totem_config->interfaces[ringnumber].member_list[member_count],
  259. node_addr_str, 0);
  260. if (res != -1) {
  261. totem_config->interfaces[ringnumber].member_count++;
  262. }
  263. free(node_addr_str);
  264. }
  265. icmap_iter_finalize(iter2);
  266. }
  267. icmap_iter_finalize(iter);
  268. }
  269. extern int totem_config_read (
  270. struct totem_config *totem_config,
  271. const char **error_string,
  272. uint64_t *warnings)
  273. {
  274. int res = 0;
  275. char *str;
  276. unsigned int ringnumber = 0;
  277. int member_count = 0;
  278. icmap_iter_t iter, member_iter;
  279. const char *iter_key;
  280. const char *member_iter_key;
  281. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  282. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  283. uint8_t u8;
  284. uint16_t u16;
  285. char *cluster_name = NULL;
  286. int i;
  287. int local_node_pos;
  288. *warnings = 0;
  289. memset (totem_config, 0, sizeof (struct totem_config));
  290. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  291. if (totem_config->interfaces == 0) {
  292. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  293. return -1;
  294. }
  295. memset (totem_config->interfaces, 0,
  296. sizeof (struct totem_interface) * INTERFACE_MAX);
  297. totem_config->secauth = 1;
  298. strcpy (totem_config->rrp_mode, "none");
  299. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  300. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  301. if (strcmp (str, "on") == 0) {
  302. totem_config->secauth = 1;
  303. }
  304. if (strcmp (str, "off") == 0) {
  305. totem_config->secauth = 0;
  306. }
  307. free(str);
  308. }
  309. if (totem_config->secauth == 1) {
  310. totem_get_crypto_type(totem_config);
  311. }
  312. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  313. strcpy (totem_config->rrp_mode, str);
  314. free(str);
  315. }
  316. /*
  317. * Get interface node id
  318. */
  319. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  320. totem_config->clear_node_high_bit = 0;
  321. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  322. if (strcmp (str, "yes") == 0) {
  323. totem_config->clear_node_high_bit = 1;
  324. }
  325. free(str);
  326. }
  327. icmap_get_uint32("totem.threads", &totem_config->threads);
  328. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  329. icmap_get_string("totem.cluster_name", &cluster_name);
  330. /*
  331. * Get things that might change in the future
  332. */
  333. totem_volatile_config_read(totem_config);
  334. iter = icmap_iter_init("totem.interface.");
  335. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  336. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  337. if (res != 2) {
  338. continue;
  339. }
  340. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  341. continue;
  342. }
  343. member_count = 0;
  344. ringnumber = atoi(ringnumber_key);
  345. /*
  346. * Get the bind net address
  347. */
  348. if (icmap_get_string(iter_key, &str) == CS_OK) {
  349. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  350. totem_config->interfaces[ringnumber].mcast_addr.family);
  351. free(str);
  352. }
  353. /*
  354. * Get interface multicast address
  355. */
  356. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  357. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  358. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  359. free(str);
  360. } else {
  361. /*
  362. * User not specified address -> autogenerate one from cluster_name key
  363. * (if available)
  364. */
  365. res = get_cluster_mcast_addr (cluster_name,
  366. &totem_config->interfaces[ringnumber].bindnet,
  367. ringnumber,
  368. &totem_config->interfaces[ringnumber].mcast_addr);
  369. }
  370. totem_config->broadcast_use = 0;
  371. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  372. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  373. if (strcmp (str, "yes") == 0) {
  374. totem_config->broadcast_use = 1;
  375. totemip_parse (
  376. &totem_config->interfaces[ringnumber].mcast_addr,
  377. "255.255.255.255", 0);
  378. }
  379. free(str);
  380. }
  381. /*
  382. * Get mcast port
  383. */
  384. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  385. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port) != CS_OK) {
  386. if (totem_config->broadcast_use) {
  387. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT + (2 * ringnumber);
  388. } else {
  389. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT;
  390. }
  391. }
  392. /*
  393. * Get the TTL
  394. */
  395. totem_config->interfaces[ringnumber].ttl = 1;
  396. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  397. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  398. totem_config->interfaces[ringnumber].ttl = u8;
  399. }
  400. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  401. member_iter = icmap_iter_init(tmp_key);
  402. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  403. if (member_count == 0) {
  404. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  405. free(str);
  406. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  407. break;
  408. } else {
  409. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  410. }
  411. }
  412. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  413. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  414. str, 0);
  415. }
  416. }
  417. icmap_iter_finalize(member_iter);
  418. totem_config->interfaces[ringnumber].member_count = member_count;
  419. totem_config->interface_count++;
  420. }
  421. icmap_iter_finalize(iter);
  422. /*
  423. * Store automatically generated items back to icmap
  424. */
  425. for (i = 0; i < totem_config->interface_count; i++) {
  426. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  427. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  428. free(str);
  429. } else {
  430. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  431. icmap_set_string(tmp_key, str);
  432. }
  433. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  434. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  435. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  436. }
  437. }
  438. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  439. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  440. if (strcmp (str, "udpu") == 0) {
  441. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  442. }
  443. if (strcmp (str, "iba") == 0) {
  444. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  445. }
  446. free(str);
  447. }
  448. free(cluster_name);
  449. /*
  450. * Check existence of nodelist
  451. */
  452. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  453. free(str);
  454. /*
  455. * find local node
  456. */
  457. local_node_pos = find_local_node_in_nodelist(totem_config);
  458. if (local_node_pos != -1) {
  459. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  460. }
  461. put_nodelist_members_to_config(totem_config);
  462. }
  463. add_totem_config_notification(totem_config);
  464. return 0;
  465. }
  466. int totem_config_validate (
  467. struct totem_config *totem_config,
  468. const char **error_string)
  469. {
  470. static char local_error_reason[512];
  471. char parse_error[512];
  472. const char *error_reason = local_error_reason;
  473. int i;
  474. unsigned int interface_max = INTERFACE_MAX;
  475. if (totem_config->interface_count == 0) {
  476. error_reason = "No interfaces defined";
  477. goto parse_error;
  478. }
  479. for (i = 0; i < totem_config->interface_count; i++) {
  480. /*
  481. * Some error checking of parsed data to make sure its valid
  482. */
  483. struct totem_ip_address null_addr;
  484. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  485. if ((totem_config->transport_number == 0) &&
  486. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  487. sizeof (struct totem_ip_address)) == 0) {
  488. error_reason = "No multicast address specified";
  489. goto parse_error;
  490. }
  491. if (totem_config->interfaces[i].ip_port == 0) {
  492. error_reason = "No multicast port specified";
  493. goto parse_error;
  494. }
  495. if (totem_config->interfaces[i].ttl > 255) {
  496. error_reason = "Invalid TTL (should be 0..255)";
  497. goto parse_error;
  498. }
  499. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  500. totem_config->interfaces[i].ttl != 1) {
  501. error_reason = "Can only set ttl on multicast transport types";
  502. goto parse_error;
  503. }
  504. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  505. totem_config->node_id == 0) {
  506. error_reason = "An IPV6 network requires that a node ID be specified.";
  507. goto parse_error;
  508. }
  509. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  510. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  511. error_reason = "Multicast address family does not match bind address family";
  512. goto parse_error;
  513. }
  514. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  515. error_reason = "Not all bind address belong to the same IP family";
  516. goto parse_error;
  517. }
  518. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  519. error_reason = "mcastaddr is not a correct multicast address.";
  520. goto parse_error;
  521. }
  522. }
  523. }
  524. if (totem_config->version != 2) {
  525. error_reason = "This totem parser can only parse version 2 configurations.";
  526. goto parse_error;
  527. }
  528. if (totem_config->token_retransmits_before_loss_const == 0) {
  529. totem_config->token_retransmits_before_loss_const =
  530. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  531. }
  532. /*
  533. * Setup timeout values that are not setup by user
  534. */
  535. if (totem_config->token_timeout == 0) {
  536. totem_config->token_timeout = TOKEN_TIMEOUT;
  537. if (totem_config->token_retransmits_before_loss_const == 0) {
  538. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  539. }
  540. if (totem_config->token_retransmit_timeout == 0) {
  541. totem_config->token_retransmit_timeout =
  542. (int)(totem_config->token_timeout /
  543. (totem_config->token_retransmits_before_loss_const + 0.2));
  544. }
  545. if (totem_config->token_hold_timeout == 0) {
  546. totem_config->token_hold_timeout =
  547. (int)(totem_config->token_retransmit_timeout * 0.8 -
  548. (1000/HZ));
  549. }
  550. }
  551. if (totem_config->max_network_delay == 0) {
  552. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  553. }
  554. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  555. snprintf (local_error_reason, sizeof(local_error_reason),
  556. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  557. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  558. goto parse_error;
  559. }
  560. if (totem_config->window_size == 0) {
  561. totem_config->window_size = WINDOW_SIZE;
  562. }
  563. if (totem_config->max_messages == 0) {
  564. totem_config->max_messages = MAX_MESSAGES;
  565. }
  566. if (totem_config->miss_count_const == 0) {
  567. totem_config->miss_count_const = MISS_COUNT_CONST;
  568. }
  569. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  570. snprintf (local_error_reason, sizeof(local_error_reason),
  571. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  572. totem_config->token_timeout, MINIMUM_TIMEOUT);
  573. goto parse_error;
  574. }
  575. if (totem_config->token_retransmit_timeout == 0) {
  576. totem_config->token_retransmit_timeout =
  577. (int)(totem_config->token_timeout /
  578. (totem_config->token_retransmits_before_loss_const + 0.2));
  579. }
  580. if (totem_config->token_hold_timeout == 0) {
  581. totem_config->token_hold_timeout =
  582. (int)(totem_config->token_retransmit_timeout * 0.8 -
  583. (1000/HZ));
  584. }
  585. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  586. snprintf (local_error_reason, sizeof(local_error_reason),
  587. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  588. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  589. goto parse_error;
  590. }
  591. if (totem_config->token_hold_timeout == 0) {
  592. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  593. }
  594. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  595. snprintf (local_error_reason, sizeof(local_error_reason),
  596. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  597. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  598. goto parse_error;
  599. }
  600. if (totem_config->join_timeout == 0) {
  601. totem_config->join_timeout = JOIN_TIMEOUT;
  602. }
  603. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  604. snprintf (local_error_reason, sizeof(local_error_reason),
  605. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  606. totem_config->join_timeout, MINIMUM_TIMEOUT);
  607. goto parse_error;
  608. }
  609. if (totem_config->consensus_timeout == 0) {
  610. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  611. }
  612. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  613. snprintf (local_error_reason, sizeof(local_error_reason),
  614. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  615. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  616. goto parse_error;
  617. }
  618. if (totem_config->merge_timeout == 0) {
  619. totem_config->merge_timeout = MERGE_TIMEOUT;
  620. }
  621. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  622. snprintf (local_error_reason, sizeof(local_error_reason),
  623. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  624. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  625. goto parse_error;
  626. }
  627. if (totem_config->downcheck_timeout == 0) {
  628. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  629. }
  630. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  631. snprintf (local_error_reason, sizeof(local_error_reason),
  632. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  633. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  634. goto parse_error;
  635. }
  636. /*
  637. * RRP values validation
  638. */
  639. if (strcmp (totem_config->rrp_mode, "none") &&
  640. strcmp (totem_config->rrp_mode, "active") &&
  641. strcmp (totem_config->rrp_mode, "passive")) {
  642. snprintf (local_error_reason, sizeof(local_error_reason),
  643. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  644. goto parse_error;
  645. }
  646. if (totem_config->rrp_problem_count_timeout == 0) {
  647. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  648. }
  649. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  650. snprintf (local_error_reason, sizeof(local_error_reason),
  651. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  652. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  653. goto parse_error;
  654. }
  655. if (totem_config->rrp_problem_count_threshold == 0) {
  656. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  657. }
  658. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  659. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  660. }
  661. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  662. snprintf (local_error_reason, sizeof(local_error_reason),
  663. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  664. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  665. goto parse_error;
  666. }
  667. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  668. snprintf (local_error_reason, sizeof(local_error_reason),
  669. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  670. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  671. goto parse_error;
  672. }
  673. if (totem_config->rrp_token_expired_timeout == 0) {
  674. totem_config->rrp_token_expired_timeout =
  675. totem_config->token_retransmit_timeout;
  676. }
  677. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  678. snprintf (local_error_reason, sizeof(local_error_reason),
  679. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  680. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  681. goto parse_error;
  682. }
  683. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  684. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  685. }
  686. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  687. interface_max = 1;
  688. }
  689. if (interface_max < totem_config->interface_count) {
  690. snprintf (parse_error, sizeof(parse_error),
  691. "%d is too many configured interfaces for the rrp_mode setting %s.",
  692. totem_config->interface_count,
  693. totem_config->rrp_mode);
  694. error_reason = parse_error;
  695. goto parse_error;
  696. }
  697. if (totem_config->fail_to_recv_const == 0) {
  698. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  699. }
  700. if (totem_config->seqno_unchanged_const == 0) {
  701. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  702. }
  703. if (totem_config->net_mtu == 0) {
  704. totem_config->net_mtu = 1500;
  705. }
  706. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  707. snprintf (local_error_reason, sizeof(local_error_reason),
  708. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  709. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  710. goto parse_error;
  711. }
  712. if (totem_config->threads > SEND_THREADS_MAX) {
  713. totem_config->threads = SEND_THREADS_MAX;
  714. }
  715. if (totem_config->secauth == 0) {
  716. totem_config->threads = 0;
  717. }
  718. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  719. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  720. goto parse_error;
  721. }
  722. if (totem_config->vsf_type == NULL) {
  723. totem_config->vsf_type = "none";
  724. }
  725. return (0);
  726. parse_error:
  727. snprintf (error_string_response, sizeof(error_string_response),
  728. "parse error in config: %s\n", error_reason);
  729. *error_string = error_string_response;
  730. return (-1);
  731. }
  732. static int read_keyfile (
  733. const char *key_location,
  734. struct totem_config *totem_config,
  735. const char **error_string)
  736. {
  737. int fd;
  738. int res;
  739. ssize_t expected_key_len = sizeof (totem_config->private_key);
  740. int saved_errno;
  741. char error_str[100];
  742. const char *error_ptr;
  743. fd = open (key_location, O_RDONLY);
  744. if (fd == -1) {
  745. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  746. snprintf (error_string_response, sizeof(error_string_response),
  747. "Could not open %s: %s\n",
  748. key_location, error_ptr);
  749. goto parse_error;
  750. }
  751. res = read (fd, totem_config->private_key, expected_key_len);
  752. saved_errno = errno;
  753. close (fd);
  754. if (res == -1) {
  755. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  756. snprintf (error_string_response, sizeof(error_string_response),
  757. "Could not read %s: %s\n",
  758. key_location, error_ptr);
  759. goto parse_error;
  760. }
  761. totem_config->private_key_len = expected_key_len;
  762. if (res != expected_key_len) {
  763. snprintf (error_string_response, sizeof(error_string_response),
  764. "Could only read %d bits of 1024 bits from %s.\n",
  765. res * 8, key_location);
  766. goto parse_error;
  767. }
  768. return 0;
  769. parse_error:
  770. *error_string = error_string_response;
  771. return (-1);
  772. }
  773. int totem_config_keyread (
  774. struct totem_config *totem_config,
  775. const char **error_string)
  776. {
  777. int got_key = 0;
  778. char *key_location = NULL;
  779. int res;
  780. size_t key_len;
  781. memset (totem_config->private_key, 0, 128);
  782. totem_config->private_key_len = 128;
  783. if (totem_config->secauth == 0) {
  784. return (0);
  785. }
  786. /* cmap may store the location of the key file */
  787. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  788. res = read_keyfile(key_location, totem_config, error_string);
  789. free(key_location);
  790. if (res) {
  791. goto key_error;
  792. }
  793. got_key = 1;
  794. } else { /* Or the key itself may be in the cmap */
  795. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  796. if (key_len > sizeof (totem_config->private_key)) {
  797. sprintf(error_string_response, "key is too long");
  798. goto key_error;
  799. }
  800. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  801. totem_config->private_key_len = key_len;
  802. got_key = 1;
  803. } else {
  804. sprintf(error_string_response, "can't store private key");
  805. goto key_error;
  806. }
  807. }
  808. }
  809. /* In desperation we read the default filename */
  810. if (!got_key) {
  811. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  812. if (!filename)
  813. filename = COROSYSCONFDIR "/authkey";
  814. res = read_keyfile(filename, totem_config, error_string);
  815. if (res)
  816. goto key_error;
  817. }
  818. return (0);
  819. key_error:
  820. *error_string = error_string_response;
  821. return (-1);
  822. }
  823. static void totem_change_notify(
  824. int32_t event,
  825. const char *key_name,
  826. struct icmap_notify_value new_val,
  827. struct icmap_notify_value old_val,
  828. void *user_data)
  829. {
  830. totem_volatile_config_read((struct totem_config *)user_data);
  831. }
  832. static void add_totem_config_notification(struct totem_config *totem_config)
  833. {
  834. icmap_track_t icmap_track;
  835. icmap_track_add("totem.",
  836. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  837. totem_change_notify,
  838. totem_config,
  839. &icmap_track);
  840. }