totemconfig.c 32 KB

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