totemconfig.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 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. #include "util.h"
  57. #include "totemconfig.h"
  58. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  59. #define TOKEN_TIMEOUT 1000
  60. #define JOIN_TIMEOUT 50
  61. #define MERGE_TIMEOUT 200
  62. #define DOWNCHECK_TIMEOUT 1000
  63. #define FAIL_TO_RECV_CONST 2500
  64. #define SEQNO_UNCHANGED_CONST 30
  65. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  66. #define MAX_NETWORK_DELAY 50
  67. #define WINDOW_SIZE 50
  68. #define MAX_MESSAGES 17
  69. #define MISS_COUNT_CONST 5
  70. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  71. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  72. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 2
  73. #define RRP_AUTORECOVERY_CHECK_TIMEOUT 1000
  74. #define DEFAULT_PORT 5405
  75. static char error_string_response[512];
  76. static void add_totem_config_notification(struct totem_config *totem_config);
  77. static void totem_volatile_config_read (struct totem_config *totem_config)
  78. {
  79. char *str;
  80. icmap_get_uint32("totem.token", &totem_config->token_timeout);
  81. icmap_get_uint32("totem.token_retransmit", &totem_config->token_retransmit_timeout);
  82. icmap_get_uint32("totem.hold", &totem_config->token_hold_timeout);
  83. icmap_get_uint32("totem.token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  84. icmap_get_uint32("totem.join", &totem_config->join_timeout);
  85. icmap_get_uint32("totem.send_join", &totem_config->send_join_timeout);
  86. icmap_get_uint32("totem.consensus", &totem_config->consensus_timeout);
  87. icmap_get_uint32("totem.merge", &totem_config->merge_timeout);
  88. icmap_get_uint32("totem.downcheck", &totem_config->downcheck_timeout);
  89. icmap_get_uint32("totem.fail_recv_const", &totem_config->fail_to_recv_const);
  90. icmap_get_uint32("totem.seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  91. icmap_get_uint32("totem.rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  92. icmap_get_uint32("totem.rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  93. icmap_get_uint32("totem.rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  94. icmap_get_uint32("totem.rrp_problem_count_mcast_threshold", &totem_config->rrp_problem_count_mcast_threshold);
  95. icmap_get_uint32("totem.rrp_autorecovery_check_timeout", &totem_config->rrp_autorecovery_check_timeout);
  96. icmap_get_uint32("totem.heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  97. icmap_get_uint32("totem.max_network_delay", &totem_config->max_network_delay);
  98. icmap_get_uint32("totem.window_size", &totem_config->window_size);
  99. icmap_get_uint32("totem.max_messages", &totem_config->max_messages);
  100. icmap_get_uint32("totem.miss_count_const", &totem_config->miss_count_const);
  101. if (icmap_get_string("totem.vsftype", &str) == CS_OK) {
  102. totem_config->vsf_type = str;
  103. }
  104. }
  105. static int totem_get_crypto(struct totem_config *totem_config)
  106. {
  107. char *str;
  108. const char *tmp_cipher;
  109. const char *tmp_hash;
  110. tmp_hash = "sha1";
  111. tmp_cipher = "aes256";
  112. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  113. if (strcmp (str, "off") == 0) {
  114. tmp_hash = "none";
  115. tmp_cipher = "none";
  116. }
  117. free(str);
  118. }
  119. if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
  120. if (strcmp(str, "none") == 0) {
  121. tmp_cipher = "none";
  122. }
  123. if (strcmp(str, "aes256") == 0) {
  124. tmp_cipher = "aes256";
  125. }
  126. if (strcmp(str, "aes192") == 0) {
  127. tmp_cipher = "aes192";
  128. }
  129. if (strcmp(str, "aes128") == 0) {
  130. tmp_cipher = "aes128";
  131. }
  132. if (strcmp(str, "3des") == 0) {
  133. tmp_cipher = "3des";
  134. }
  135. free(str);
  136. }
  137. if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
  138. if (strcmp(str, "none") == 0) {
  139. tmp_hash = "none";
  140. }
  141. if (strcmp(str, "md5") == 0) {
  142. tmp_hash = "md5";
  143. }
  144. if (strcmp(str, "sha1") == 0) {
  145. tmp_hash = "sha1";
  146. }
  147. if (strcmp(str, "sha256") == 0) {
  148. tmp_hash = "sha256";
  149. }
  150. if (strcmp(str, "sha384") == 0) {
  151. tmp_hash = "sha384";
  152. }
  153. if (strcmp(str, "sha512") == 0) {
  154. tmp_hash = "sha512";
  155. }
  156. free(str);
  157. }
  158. if ((strcmp(tmp_cipher, "none") != 0) &&
  159. (strcmp(tmp_hash, "none") == 0)) {
  160. return -1;
  161. }
  162. free(totem_config->crypto_cipher_type);
  163. free(totem_config->crypto_hash_type);
  164. totem_config->crypto_cipher_type = strdup(tmp_cipher);
  165. totem_config->crypto_hash_type = strdup(tmp_hash);
  166. return 0;
  167. }
  168. static uint16_t generate_cluster_id (const char *cluster_name)
  169. {
  170. int i;
  171. int value = 0;
  172. for (i = 0; i < strlen(cluster_name); i++) {
  173. value <<= 1;
  174. value += cluster_name[i];
  175. }
  176. return (value & 0xFFFF);
  177. }
  178. static int get_cluster_mcast_addr (
  179. const char *cluster_name,
  180. const struct totem_ip_address *bindnet,
  181. unsigned int ringnumber,
  182. int ip_version,
  183. struct totem_ip_address *res)
  184. {
  185. uint16_t clusterid;
  186. char addr[INET6_ADDRSTRLEN + 1];
  187. int err;
  188. if (cluster_name == NULL) {
  189. return (-1);
  190. }
  191. clusterid = generate_cluster_id(cluster_name) + ringnumber;
  192. memset (res, 0, sizeof(*res));
  193. switch (bindnet->family) {
  194. case AF_INET:
  195. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  196. break;
  197. case AF_INET6:
  198. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  199. break;
  200. default:
  201. /*
  202. * Unknown family
  203. */
  204. return (-1);
  205. }
  206. err = totemip_parse (res, addr, ip_version);
  207. return (err);
  208. }
  209. static int find_local_node_in_nodelist(struct totem_config *totem_config)
  210. {
  211. icmap_iter_t iter;
  212. const char *iter_key;
  213. int res = 0;
  214. int node_pos;
  215. int local_node_pos = -1;
  216. struct totem_ip_address bind_addr;
  217. int interface_up, interface_num;
  218. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  219. char *node_addr_str;
  220. struct totem_ip_address node_addr;
  221. res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
  222. &bind_addr, &interface_up, &interface_num,
  223. totem_config->clear_node_high_bit);
  224. if (res == -1) {
  225. return (-1);
  226. }
  227. iter = icmap_iter_init("nodelist.node.");
  228. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  229. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  230. if (res != 2) {
  231. continue;
  232. }
  233. if (strcmp(tmp_key, "ring0_addr") != 0) {
  234. continue;
  235. }
  236. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  237. if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
  238. continue;
  239. }
  240. res = totemip_parse (&node_addr, node_addr_str, totem_config->ip_version);
  241. free(node_addr_str);
  242. if (res == -1) {
  243. continue ;
  244. }
  245. if (totemip_equal(&bind_addr, &node_addr)) {
  246. local_node_pos = node_pos;
  247. }
  248. }
  249. icmap_iter_finalize(iter);
  250. return (local_node_pos);
  251. }
  252. static void put_nodelist_members_to_config(struct totem_config *totem_config)
  253. {
  254. icmap_iter_t iter, iter2;
  255. const char *iter_key, *iter_key2;
  256. int res = 0;
  257. int node_pos;
  258. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  259. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  260. char *node_addr_str;
  261. int member_count;
  262. unsigned int ringnumber = 0;
  263. iter = icmap_iter_init("nodelist.node.");
  264. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  265. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  266. if (res != 2) {
  267. continue;
  268. }
  269. if (strcmp(tmp_key, "ring0_addr") != 0) {
  270. continue;
  271. }
  272. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  273. iter2 = icmap_iter_init(tmp_key);
  274. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  275. res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  276. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  277. continue;
  278. }
  279. if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
  280. continue;
  281. }
  282. member_count = totem_config->interfaces[ringnumber].member_count;
  283. res = totemip_parse(&totem_config->interfaces[ringnumber].member_list[member_count],
  284. node_addr_str, totem_config->ip_version);
  285. if (res != -1) {
  286. totem_config->interfaces[ringnumber].member_count++;
  287. }
  288. free(node_addr_str);
  289. }
  290. icmap_iter_finalize(iter2);
  291. }
  292. icmap_iter_finalize(iter);
  293. }
  294. static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
  295. {
  296. icmap_iter_t iter;
  297. const char *iter_key;
  298. int res = 0;
  299. int node_pos;
  300. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  301. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  302. char *node_addr_str;
  303. unsigned int ringnumber = 0;
  304. struct list_head addrs;
  305. struct list_head *list;
  306. struct totem_ip_if_address *if_addr;
  307. struct totem_ip_address node_addr;
  308. int node_found = 0;
  309. if (totemip_getifaddrs(&addrs) == -1) {
  310. return ;
  311. }
  312. iter = icmap_iter_init("nodelist.node.");
  313. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  314. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  315. if (res != 2) {
  316. continue;
  317. }
  318. if (strcmp(tmp_key, "ring0_addr") != 0) {
  319. continue;
  320. }
  321. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  322. continue ;
  323. }
  324. if (totemip_parse(&node_addr, node_addr_str, totem_config->ip_version) == -1) {
  325. free(node_addr_str);
  326. continue ;
  327. }
  328. free(node_addr_str);
  329. /*
  330. * Try to find node in if_addrs
  331. */
  332. node_found = 0;
  333. for (list = addrs.next; list != &addrs; list = list->next) {
  334. if_addr = list_entry(list, struct totem_ip_if_address, list);
  335. if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
  336. node_found = 1;
  337. break;
  338. }
  339. }
  340. if (node_found) {
  341. break ;
  342. }
  343. }
  344. icmap_iter_finalize(iter);
  345. if (node_found) {
  346. /*
  347. * We found node, so create interface section
  348. */
  349. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  350. iter = icmap_iter_init(tmp_key);
  351. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  352. res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  353. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  354. continue ;
  355. }
  356. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  357. continue;
  358. }
  359. snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", ringnumber);
  360. icmap_set_string(tmp_key2, node_addr_str);
  361. free(node_addr_str);
  362. }
  363. icmap_iter_finalize(iter);
  364. }
  365. }
  366. extern int totem_config_read (
  367. struct totem_config *totem_config,
  368. const char **error_string,
  369. uint64_t *warnings)
  370. {
  371. int res = 0;
  372. char *str;
  373. unsigned int ringnumber = 0;
  374. int member_count = 0;
  375. icmap_iter_t iter, member_iter;
  376. const char *iter_key;
  377. const char *member_iter_key;
  378. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  379. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  380. uint8_t u8;
  381. uint16_t u16;
  382. char *cluster_name = NULL;
  383. int i;
  384. int local_node_pos;
  385. int nodeid_set;
  386. *warnings = 0;
  387. memset (totem_config, 0, sizeof (struct totem_config));
  388. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  389. if (totem_config->interfaces == 0) {
  390. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  391. return -1;
  392. }
  393. memset (totem_config->interfaces, 0,
  394. sizeof (struct totem_interface) * INTERFACE_MAX);
  395. strcpy (totem_config->rrp_mode, "none");
  396. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  397. if (totem_get_crypto(totem_config) != 0) {
  398. *error_string = "crypto_cipher requires crypto_hash with value other than none";
  399. return -1;
  400. }
  401. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  402. if (strlen(str) >= TOTEM_RRP_MODE_BYTES) {
  403. *error_string = "totem.rrp_mode is too long";
  404. free(str);
  405. return -1;
  406. }
  407. strcpy (totem_config->rrp_mode, str);
  408. free(str);
  409. }
  410. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  411. totem_config->clear_node_high_bit = 0;
  412. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  413. if (strcmp (str, "yes") == 0) {
  414. totem_config->clear_node_high_bit = 1;
  415. }
  416. free(str);
  417. }
  418. icmap_get_uint32("totem.threads", &totem_config->threads);
  419. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  420. if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
  421. cluster_name = NULL;
  422. }
  423. totem_config->ip_version = AF_INET;
  424. if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
  425. if (strcmp(str, "ipv4") == 0) {
  426. totem_config->ip_version = AF_INET;
  427. }
  428. if (strcmp(str, "ipv6") == 0) {
  429. totem_config->ip_version = AF_INET6;
  430. }
  431. free(str);
  432. }
  433. /*
  434. * Get things that might change in the future
  435. */
  436. totem_volatile_config_read(totem_config);
  437. if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
  438. /*
  439. * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
  440. */
  441. config_convert_nodelist_to_interface(totem_config);
  442. } else {
  443. free(str);
  444. }
  445. iter = icmap_iter_init("totem.interface.");
  446. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  447. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  448. if (res != 2) {
  449. continue;
  450. }
  451. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  452. continue;
  453. }
  454. member_count = 0;
  455. ringnumber = atoi(ringnumber_key);
  456. if (ringnumber >= INTERFACE_MAX) {
  457. free(cluster_name);
  458. snprintf (error_string_response, sizeof(error_string_response),
  459. "parse error in config: interface ring number %u is bigger then allowed maximum %u\n",
  460. ringnumber, INTERFACE_MAX - 1);
  461. *error_string = error_string_response;
  462. return -1;
  463. }
  464. /*
  465. * Get the bind net address
  466. */
  467. if (icmap_get_string(iter_key, &str) == CS_OK) {
  468. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  469. totem_config->interfaces[ringnumber].mcast_addr.family);
  470. free(str);
  471. }
  472. /*
  473. * Get interface multicast address
  474. */
  475. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  476. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  477. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, totem_config->ip_version);
  478. free(str);
  479. } else {
  480. /*
  481. * User not specified address -> autogenerate one from cluster_name key
  482. * (if available)
  483. */
  484. res = get_cluster_mcast_addr (cluster_name,
  485. &totem_config->interfaces[ringnumber].bindnet,
  486. ringnumber,
  487. totem_config->ip_version,
  488. &totem_config->interfaces[ringnumber].mcast_addr);
  489. }
  490. totem_config->broadcast_use = 0;
  491. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  492. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  493. if (strcmp (str, "yes") == 0) {
  494. totem_config->broadcast_use = 1;
  495. totemip_parse (
  496. &totem_config->interfaces[ringnumber].mcast_addr,
  497. "255.255.255.255", totem_config->ip_version);
  498. }
  499. free(str);
  500. }
  501. /*
  502. * Get mcast port
  503. */
  504. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  505. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port) != CS_OK) {
  506. if (totem_config->broadcast_use) {
  507. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT + (2 * ringnumber);
  508. } else {
  509. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT;
  510. }
  511. }
  512. /*
  513. * Get the TTL
  514. */
  515. totem_config->interfaces[ringnumber].ttl = 1;
  516. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  517. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  518. totem_config->interfaces[ringnumber].ttl = u8;
  519. }
  520. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  521. member_iter = icmap_iter_init(tmp_key);
  522. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  523. if (member_count == 0) {
  524. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  525. free(str);
  526. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  527. break;
  528. } else {
  529. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  530. }
  531. }
  532. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  533. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  534. str, totem_config->ip_version);
  535. }
  536. }
  537. icmap_iter_finalize(member_iter);
  538. totem_config->interfaces[ringnumber].member_count = member_count;
  539. totem_config->interface_count++;
  540. }
  541. icmap_iter_finalize(iter);
  542. /*
  543. * Store automatically generated items back to icmap
  544. */
  545. for (i = 0; i < totem_config->interface_count; i++) {
  546. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  547. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  548. free(str);
  549. } else {
  550. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  551. icmap_set_string(tmp_key, str);
  552. }
  553. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  554. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  555. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  556. }
  557. }
  558. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  559. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  560. if (strcmp (str, "udpu") == 0) {
  561. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  562. }
  563. if (strcmp (str, "iba") == 0) {
  564. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  565. }
  566. free(str);
  567. }
  568. free(cluster_name);
  569. /*
  570. * Check existence of nodelist
  571. */
  572. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  573. free(str);
  574. /*
  575. * find local node
  576. */
  577. local_node_pos = find_local_node_in_nodelist(totem_config);
  578. if (local_node_pos != -1) {
  579. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  580. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  581. nodeid_set = (totem_config->node_id != 0);
  582. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  583. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  584. }
  585. /*
  586. * Make localnode ring0_addr read only, so we can be sure that local
  587. * node never changes. If rebinding to other IP would be in future
  588. * supported, this must be changed and handled properly!
  589. */
  590. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  591. icmap_set_ro_access(tmp_key, 0, 1);
  592. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  593. }
  594. put_nodelist_members_to_config(totem_config);
  595. }
  596. add_totem_config_notification(totem_config);
  597. return 0;
  598. }
  599. int totem_config_validate (
  600. struct totem_config *totem_config,
  601. const char **error_string)
  602. {
  603. static char local_error_reason[512];
  604. char parse_error[512];
  605. const char *error_reason = local_error_reason;
  606. int i;
  607. unsigned int interface_max = INTERFACE_MAX;
  608. if (totem_config->interface_count == 0) {
  609. error_reason = "No interfaces defined";
  610. goto parse_error;
  611. }
  612. for (i = 0; i < totem_config->interface_count; i++) {
  613. /*
  614. * Some error checking of parsed data to make sure its valid
  615. */
  616. struct totem_ip_address null_addr;
  617. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  618. if ((totem_config->transport_number == 0) &&
  619. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  620. sizeof (struct totem_ip_address)) == 0) {
  621. error_reason = "No multicast address specified";
  622. goto parse_error;
  623. }
  624. if (totem_config->interfaces[i].ip_port == 0) {
  625. error_reason = "No multicast port specified";
  626. goto parse_error;
  627. }
  628. if (totem_config->interfaces[i].ttl > 255) {
  629. error_reason = "Invalid TTL (should be 0..255)";
  630. goto parse_error;
  631. }
  632. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  633. totem_config->interfaces[i].ttl != 1) {
  634. error_reason = "Can only set ttl on multicast transport types";
  635. goto parse_error;
  636. }
  637. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  638. totem_config->node_id == 0) {
  639. error_reason = "An IPV6 network requires that a node ID be specified.";
  640. goto parse_error;
  641. }
  642. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  643. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  644. error_reason = "Multicast address family does not match bind address family";
  645. goto parse_error;
  646. }
  647. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  648. error_reason = "Not all bind address belong to the same IP family";
  649. goto parse_error;
  650. }
  651. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  652. error_reason = "mcastaddr is not a correct multicast address.";
  653. goto parse_error;
  654. }
  655. }
  656. }
  657. if (totem_config->version != 2) {
  658. error_reason = "This totem parser can only parse version 2 configurations.";
  659. goto parse_error;
  660. }
  661. if (totem_config->token_retransmits_before_loss_const == 0) {
  662. totem_config->token_retransmits_before_loss_const =
  663. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  664. }
  665. /*
  666. * Setup timeout values that are not setup by user
  667. */
  668. if (totem_config->token_timeout == 0) {
  669. totem_config->token_timeout = TOKEN_TIMEOUT;
  670. }
  671. if (totem_config->max_network_delay == 0) {
  672. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  673. }
  674. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  675. snprintf (local_error_reason, sizeof(local_error_reason),
  676. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  677. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  678. goto parse_error;
  679. }
  680. if (totem_config->window_size == 0) {
  681. totem_config->window_size = WINDOW_SIZE;
  682. }
  683. if (totem_config->max_messages == 0) {
  684. totem_config->max_messages = MAX_MESSAGES;
  685. }
  686. if (totem_config->miss_count_const == 0) {
  687. totem_config->miss_count_const = MISS_COUNT_CONST;
  688. }
  689. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  690. snprintf (local_error_reason, sizeof(local_error_reason),
  691. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  692. totem_config->token_timeout, MINIMUM_TIMEOUT);
  693. goto parse_error;
  694. }
  695. if (totem_config->token_retransmit_timeout == 0) {
  696. totem_config->token_retransmit_timeout =
  697. (int)(totem_config->token_timeout /
  698. (totem_config->token_retransmits_before_loss_const + 0.2));
  699. }
  700. if (totem_config->token_hold_timeout == 0) {
  701. totem_config->token_hold_timeout =
  702. (int)(totem_config->token_retransmit_timeout * 0.8 -
  703. (1000/HZ));
  704. }
  705. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  706. snprintf (local_error_reason, sizeof(local_error_reason),
  707. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  708. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  709. goto parse_error;
  710. }
  711. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  712. snprintf (local_error_reason, sizeof(local_error_reason),
  713. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  714. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  715. goto parse_error;
  716. }
  717. if (totem_config->join_timeout == 0) {
  718. totem_config->join_timeout = JOIN_TIMEOUT;
  719. }
  720. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  721. snprintf (local_error_reason, sizeof(local_error_reason),
  722. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  723. totem_config->join_timeout, MINIMUM_TIMEOUT);
  724. goto parse_error;
  725. }
  726. if (totem_config->consensus_timeout == 0) {
  727. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  728. }
  729. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  730. snprintf (local_error_reason, sizeof(local_error_reason),
  731. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  732. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  733. goto parse_error;
  734. }
  735. if (totem_config->merge_timeout == 0) {
  736. totem_config->merge_timeout = MERGE_TIMEOUT;
  737. }
  738. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  739. snprintf (local_error_reason, sizeof(local_error_reason),
  740. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  741. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  742. goto parse_error;
  743. }
  744. if (totem_config->downcheck_timeout == 0) {
  745. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  746. }
  747. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  748. snprintf (local_error_reason, sizeof(local_error_reason),
  749. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  750. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  751. goto parse_error;
  752. }
  753. /*
  754. * RRP values validation
  755. */
  756. if (strcmp (totem_config->rrp_mode, "none") &&
  757. strcmp (totem_config->rrp_mode, "active") &&
  758. strcmp (totem_config->rrp_mode, "passive")) {
  759. snprintf (local_error_reason, sizeof(local_error_reason),
  760. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  761. goto parse_error;
  762. }
  763. if (totem_config->rrp_problem_count_timeout == 0) {
  764. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  765. }
  766. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  767. snprintf (local_error_reason, sizeof(local_error_reason),
  768. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  769. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  770. goto parse_error;
  771. }
  772. if (totem_config->rrp_problem_count_threshold == 0) {
  773. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  774. }
  775. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  776. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  777. }
  778. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  779. snprintf (local_error_reason, sizeof(local_error_reason),
  780. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  781. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  782. goto parse_error;
  783. }
  784. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  785. snprintf (local_error_reason, sizeof(local_error_reason),
  786. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  787. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  788. goto parse_error;
  789. }
  790. if (totem_config->rrp_token_expired_timeout == 0) {
  791. totem_config->rrp_token_expired_timeout =
  792. totem_config->token_retransmit_timeout;
  793. }
  794. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  795. snprintf (local_error_reason, sizeof(local_error_reason),
  796. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  797. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  798. goto parse_error;
  799. }
  800. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  801. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  802. }
  803. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  804. interface_max = 1;
  805. }
  806. if (interface_max < totem_config->interface_count) {
  807. snprintf (parse_error, sizeof(parse_error),
  808. "%d is too many configured interfaces for the rrp_mode setting %s.",
  809. totem_config->interface_count,
  810. totem_config->rrp_mode);
  811. error_reason = parse_error;
  812. goto parse_error;
  813. }
  814. if (totem_config->fail_to_recv_const == 0) {
  815. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  816. }
  817. if (totem_config->seqno_unchanged_const == 0) {
  818. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  819. }
  820. if (totem_config->net_mtu == 0) {
  821. totem_config->net_mtu = 1500;
  822. }
  823. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  824. snprintf (local_error_reason, sizeof(local_error_reason),
  825. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  826. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  827. goto parse_error;
  828. }
  829. if (totem_config->threads > SEND_THREADS_MAX) {
  830. totem_config->threads = SEND_THREADS_MAX;
  831. }
  832. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  833. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  834. goto parse_error;
  835. }
  836. if (totem_config->vsf_type == NULL) {
  837. totem_config->vsf_type = "none";
  838. }
  839. return (0);
  840. parse_error:
  841. snprintf (error_string_response, sizeof(error_string_response),
  842. "parse error in config: %s\n", error_reason);
  843. *error_string = error_string_response;
  844. return (-1);
  845. }
  846. static int read_keyfile (
  847. const char *key_location,
  848. struct totem_config *totem_config,
  849. const char **error_string)
  850. {
  851. int fd;
  852. int res;
  853. ssize_t expected_key_len = sizeof (totem_config->private_key);
  854. int saved_errno;
  855. char error_str[100];
  856. const char *error_ptr;
  857. fd = open (key_location, O_RDONLY);
  858. if (fd == -1) {
  859. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  860. snprintf (error_string_response, sizeof(error_string_response),
  861. "Could not open %s: %s\n",
  862. key_location, error_ptr);
  863. goto parse_error;
  864. }
  865. res = read (fd, totem_config->private_key, expected_key_len);
  866. saved_errno = errno;
  867. close (fd);
  868. if (res == -1) {
  869. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  870. snprintf (error_string_response, sizeof(error_string_response),
  871. "Could not read %s: %s\n",
  872. key_location, error_ptr);
  873. goto parse_error;
  874. }
  875. totem_config->private_key_len = expected_key_len;
  876. if (res != expected_key_len) {
  877. snprintf (error_string_response, sizeof(error_string_response),
  878. "Could only read %d bits of 1024 bits from %s.\n",
  879. res * 8, key_location);
  880. goto parse_error;
  881. }
  882. return 0;
  883. parse_error:
  884. *error_string = error_string_response;
  885. return (-1);
  886. }
  887. int totem_config_keyread (
  888. struct totem_config *totem_config,
  889. const char **error_string)
  890. {
  891. int got_key = 0;
  892. char *key_location = NULL;
  893. int res;
  894. size_t key_len;
  895. memset (totem_config->private_key, 0, 128);
  896. totem_config->private_key_len = 128;
  897. if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
  898. strcmp(totem_config->crypto_hash_type, "none") == 0) {
  899. return (0);
  900. }
  901. /* cmap may store the location of the key file */
  902. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  903. res = read_keyfile(key_location, totem_config, error_string);
  904. free(key_location);
  905. if (res) {
  906. goto key_error;
  907. }
  908. got_key = 1;
  909. } else { /* Or the key itself may be in the cmap */
  910. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  911. if (key_len > sizeof (totem_config->private_key)) {
  912. sprintf(error_string_response, "key is too long");
  913. goto key_error;
  914. }
  915. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  916. totem_config->private_key_len = key_len;
  917. got_key = 1;
  918. } else {
  919. sprintf(error_string_response, "can't store private key");
  920. goto key_error;
  921. }
  922. }
  923. }
  924. /* In desperation we read the default filename */
  925. if (!got_key) {
  926. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  927. if (!filename)
  928. filename = COROSYSCONFDIR "/authkey";
  929. res = read_keyfile(filename, totem_config, error_string);
  930. if (res)
  931. goto key_error;
  932. }
  933. return (0);
  934. key_error:
  935. *error_string = error_string_response;
  936. return (-1);
  937. }
  938. static void totem_change_notify(
  939. int32_t event,
  940. const char *key_name,
  941. struct icmap_notify_value new_val,
  942. struct icmap_notify_value old_val,
  943. void *user_data)
  944. {
  945. totem_volatile_config_read((struct totem_config *)user_data);
  946. }
  947. static void add_totem_config_notification(struct totem_config *totem_config)
  948. {
  949. icmap_track_t icmap_track;
  950. icmap_track_add("totem.",
  951. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  952. totem_change_notify,
  953. totem_config,
  954. &icmap_track);
  955. }