4
0

totemconfig.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2013 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 TOKEN_COEFFICIENT 650
  61. #define JOIN_TIMEOUT 50
  62. #define MERGE_TIMEOUT 200
  63. #define DOWNCHECK_TIMEOUT 1000
  64. #define FAIL_TO_RECV_CONST 2500
  65. #define SEQNO_UNCHANGED_CONST 30
  66. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  67. #define MAX_NETWORK_DELAY 50
  68. #define WINDOW_SIZE 50
  69. #define MAX_MESSAGES 17
  70. #define MISS_COUNT_CONST 5
  71. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  72. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  73. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 2
  74. #define RRP_AUTORECOVERY_CHECK_TIMEOUT 1000
  75. #define DEFAULT_PORT 5405
  76. static char error_string_response[512];
  77. static void add_totem_config_notification(struct totem_config *totem_config);
  78. /* All the volatile parameters are uint32s, luckily */
  79. static uint32_t *totem_get_param_by_name(struct totem_config *totem_config, const char *param_name)
  80. {
  81. if (strcmp(param_name, "totem.token") == 0)
  82. return &totem_config->token_timeout;
  83. if (strcmp(param_name, "totem.token_retransmit") == 0)
  84. return &totem_config->token_retransmit_timeout;
  85. if (strcmp(param_name, "totem.hold") == 0)
  86. return &totem_config->token_hold_timeout;
  87. if (strcmp(param_name, "totem.token_retransmits_before_loss_const") == 0)
  88. return &totem_config->token_retransmits_before_loss_const;
  89. if (strcmp(param_name, "totem.join") == 0)
  90. return &totem_config->join_timeout;
  91. if (strcmp(param_name, "totem.send_join") == 0)
  92. return &totem_config->send_join_timeout;
  93. if (strcmp(param_name, "totem.consensus") == 0)
  94. return &totem_config->consensus_timeout;
  95. if (strcmp(param_name, "totem.merge") == 0)
  96. return &totem_config->merge_timeout;
  97. if (strcmp(param_name, "totem.downcheck") == 0)
  98. return &totem_config->downcheck_timeout;
  99. if (strcmp(param_name, "totem.fail_recv_const") == 0)
  100. return &totem_config->fail_to_recv_const;
  101. if (strcmp(param_name, "totem.seqno_unchanged_const") == 0)
  102. return &totem_config->seqno_unchanged_const;
  103. if (strcmp(param_name, "totem.rrp_token_expired_timeout") == 0)
  104. return &totem_config->rrp_token_expired_timeout;
  105. if (strcmp(param_name, "totem.rrp_problem_count_timeout") == 0)
  106. return &totem_config->rrp_problem_count_timeout;
  107. if (strcmp(param_name, "totem.rrp_problem_count_threshold") == 0)
  108. return &totem_config->rrp_problem_count_threshold;
  109. if (strcmp(param_name, "totem.rrp_problem_count_mcast_threshold") == 0)
  110. return &totem_config->rrp_problem_count_mcast_threshold;
  111. if (strcmp(param_name, "totem.rrp_autorecovery_check_timeout") == 0)
  112. return &totem_config->rrp_autorecovery_check_timeout;
  113. if (strcmp(param_name, "totem.heartbeat_failures_allowed") == 0)
  114. return &totem_config->heartbeat_failures_allowed;
  115. if (strcmp(param_name, "totem.max_network_delay") == 0)
  116. return &totem_config->max_network_delay;
  117. if (strcmp(param_name, "totem.window_size") == 0)
  118. return &totem_config->window_size;
  119. if (strcmp(param_name, "totem.max_messages") == 0)
  120. return &totem_config->max_messages;
  121. if (strcmp(param_name, "totem.miss_count_const") == 0)
  122. return &totem_config->miss_count_const;
  123. return NULL;
  124. }
  125. /*
  126. * Read key_name from icmap. If key is not found or key_name == delete_key or if allow_zero is false
  127. * and readed value is zero, default value is used and stored into totem_config.
  128. */
  129. static void totem_volatile_config_set_value (struct totem_config *totem_config,
  130. const char *key_name, const char *deleted_key, unsigned int default_value,
  131. int allow_zero_value)
  132. {
  133. if (icmap_get_uint32(key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
  134. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
  135. (!allow_zero_value && *totem_get_param_by_name(totem_config, key_name) == 0)) {
  136. *totem_get_param_by_name(totem_config, key_name) = default_value;
  137. }
  138. }
  139. /*
  140. * Read and validate config values from cmap and store them into totem_config. If key doesn't exists,
  141. * default value is stored. deleted_key is name of key beeing processed by delete operation
  142. * from cmap. It is considered as non existing even if it can be read. Can be NULL.
  143. */
  144. static void totem_volatile_config_read (struct totem_config *totem_config, const char *deleted_key)
  145. {
  146. uint32_t u32;
  147. totem_volatile_config_set_value(totem_config, "totem.token_retransmits_before_loss_const", deleted_key,
  148. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST, 0);
  149. totem_volatile_config_set_value(totem_config, "totem.token", deleted_key, TOKEN_TIMEOUT, 0);
  150. if (totem_config->interface_count > 0 && totem_config->interfaces[0].member_count > 2) {
  151. u32 = TOKEN_COEFFICIENT;
  152. icmap_get_uint32("totem.token_coefficient", &u32);
  153. totem_config->token_timeout += (totem_config->interfaces[0].member_count - 2) * u32;
  154. }
  155. totem_volatile_config_set_value(totem_config, "totem.max_network_delay", deleted_key, MAX_NETWORK_DELAY, 0);
  156. totem_volatile_config_set_value(totem_config, "totem.window_size", deleted_key, WINDOW_SIZE, 0);
  157. totem_volatile_config_set_value(totem_config, "totem.max_messages", deleted_key, MAX_MESSAGES, 0);
  158. totem_volatile_config_set_value(totem_config, "totem.miss_count_const", deleted_key, MISS_COUNT_CONST, 0);
  159. totem_volatile_config_set_value(totem_config, "totem.token_retransmit", deleted_key,
  160. (int)(totem_config->token_timeout / (totem_config->token_retransmits_before_loss_const + 0.2)), 0);
  161. totem_volatile_config_set_value(totem_config, "totem.hold", deleted_key,
  162. (int)(totem_config->token_retransmit_timeout * 0.8 - (1000/HZ)), 0);
  163. totem_volatile_config_set_value(totem_config, "totem.join", deleted_key, JOIN_TIMEOUT, 0);
  164. totem_volatile_config_set_value(totem_config, "totem.consensus", deleted_key,
  165. (int)(float)(1.2 * totem_config->token_timeout), 0);
  166. totem_volatile_config_set_value(totem_config, "totem.merge", deleted_key, MERGE_TIMEOUT, 0);
  167. totem_volatile_config_set_value(totem_config, "totem.downcheck", deleted_key, DOWNCHECK_TIMEOUT, 0);
  168. totem_volatile_config_set_value(totem_config, "totem.fail_recv_const", deleted_key, FAIL_TO_RECV_CONST, 0);
  169. totem_volatile_config_set_value(totem_config, "totem.seqno_unchanged_const", deleted_key,
  170. SEQNO_UNCHANGED_CONST, 0);
  171. totem_volatile_config_set_value(totem_config, "totem.send_join", deleted_key, 0, 1);
  172. totem_volatile_config_set_value(totem_config, "totem.rrp_problem_count_timeout", deleted_key,
  173. RRP_PROBLEM_COUNT_TIMEOUT, 0);
  174. totem_volatile_config_set_value(totem_config, "totem.rrp_problem_count_threshold", deleted_key,
  175. RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT, 0);
  176. totem_volatile_config_set_value(totem_config, "totem.rrp_problem_count_mcast_threshold", deleted_key,
  177. totem_config->rrp_problem_count_threshold * 10, 0);
  178. totem_volatile_config_set_value(totem_config, "totem.rrp_token_expired_timeout", deleted_key,
  179. totem_config->token_retransmit_timeout, 0);
  180. totem_volatile_config_set_value(totem_config, "totem.rrp_autorecovery_check_timeout", deleted_key,
  181. RRP_AUTORECOVERY_CHECK_TIMEOUT, 0);
  182. totem_volatile_config_set_value(totem_config, "totem.heartbeat_failures_allowed", deleted_key, 0, 1);
  183. }
  184. static int totem_volatile_config_validate (
  185. struct totem_config *totem_config,
  186. const char **error_string)
  187. {
  188. static char local_error_reason[512];
  189. const char *error_reason = local_error_reason;
  190. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  191. snprintf (local_error_reason, sizeof(local_error_reason),
  192. "The max_network_delay parameter (%d ms) may not be less than (%d ms).",
  193. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  194. goto parse_error;
  195. }
  196. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  197. snprintf (local_error_reason, sizeof(local_error_reason),
  198. "The token timeout parameter (%d ms) may not be less than (%d ms).",
  199. totem_config->token_timeout, MINIMUM_TIMEOUT);
  200. goto parse_error;
  201. }
  202. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  203. snprintf (local_error_reason, sizeof(local_error_reason),
  204. "The token retransmit timeout parameter (%d ms) may not be less than (%d ms).",
  205. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  206. goto parse_error;
  207. }
  208. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  209. snprintf (local_error_reason, sizeof(local_error_reason),
  210. "The token hold timeout parameter (%d ms) may not be less than (%d ms).",
  211. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  212. goto parse_error;
  213. }
  214. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  215. snprintf (local_error_reason, sizeof(local_error_reason),
  216. "The join timeout parameter (%d ms) may not be less than (%d ms).",
  217. totem_config->join_timeout, MINIMUM_TIMEOUT);
  218. goto parse_error;
  219. }
  220. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  221. snprintf (local_error_reason, sizeof(local_error_reason),
  222. "The consensus timeout parameter (%d ms) may not be less than (%d ms).",
  223. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  224. goto parse_error;
  225. }
  226. if (totem_config->consensus_timeout < totem_config->join_timeout) {
  227. snprintf (local_error_reason, sizeof(local_error_reason),
  228. "The consensus timeout parameter (%d ms) may not be less than join timeout (%d ms).",
  229. totem_config->consensus_timeout, totem_config->join_timeout);
  230. goto parse_error;
  231. }
  232. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  233. snprintf (local_error_reason, sizeof(local_error_reason),
  234. "The merge timeout parameter (%d ms) may not be less than (%d ms).",
  235. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  236. goto parse_error;
  237. }
  238. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  239. snprintf (local_error_reason, sizeof(local_error_reason),
  240. "The downcheck timeout parameter (%d ms) may not be less than (%d ms).",
  241. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  242. goto parse_error;
  243. }
  244. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  245. snprintf (local_error_reason, sizeof(local_error_reason),
  246. "The RRP problem count timeout parameter (%d ms) may not be less than (%d ms).",
  247. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  248. goto parse_error;
  249. }
  250. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  251. snprintf (local_error_reason, sizeof(local_error_reason),
  252. "The RRP problem count threshold (%d problem count) may not be less than (%d problem count).",
  253. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  254. goto parse_error;
  255. }
  256. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  257. snprintf (local_error_reason, sizeof(local_error_reason),
  258. "The RRP multicast problem count threshold (%d problem count) may not be less than (%d problem count).",
  259. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  260. goto parse_error;
  261. }
  262. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  263. snprintf (local_error_reason, sizeof(local_error_reason),
  264. "The RRP token expired timeout parameter (%d ms) may not be less than (%d ms).",
  265. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  266. goto parse_error;
  267. }
  268. return 0;
  269. parse_error:
  270. snprintf (error_string_response, sizeof(error_string_response),
  271. "parse error in config: %s\n", error_reason);
  272. *error_string = error_string_response;
  273. return (-1);
  274. }
  275. static int totem_get_crypto(struct totem_config *totem_config)
  276. {
  277. char *str;
  278. const char *tmp_cipher;
  279. const char *tmp_hash;
  280. tmp_hash = "sha1";
  281. tmp_cipher = "aes256";
  282. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  283. if (strcmp (str, "off") == 0) {
  284. tmp_hash = "none";
  285. tmp_cipher = "none";
  286. }
  287. free(str);
  288. }
  289. if (icmap_get_string("totem.crypto_cipher", &str) == CS_OK) {
  290. if (strcmp(str, "none") == 0) {
  291. tmp_cipher = "none";
  292. }
  293. if (strcmp(str, "aes256") == 0) {
  294. tmp_cipher = "aes256";
  295. }
  296. if (strcmp(str, "aes192") == 0) {
  297. tmp_cipher = "aes192";
  298. }
  299. if (strcmp(str, "aes128") == 0) {
  300. tmp_cipher = "aes128";
  301. }
  302. if (strcmp(str, "3des") == 0) {
  303. tmp_cipher = "3des";
  304. }
  305. free(str);
  306. }
  307. if (icmap_get_string("totem.crypto_hash", &str) == CS_OK) {
  308. if (strcmp(str, "none") == 0) {
  309. tmp_hash = "none";
  310. }
  311. if (strcmp(str, "md5") == 0) {
  312. tmp_hash = "md5";
  313. }
  314. if (strcmp(str, "sha1") == 0) {
  315. tmp_hash = "sha1";
  316. }
  317. if (strcmp(str, "sha256") == 0) {
  318. tmp_hash = "sha256";
  319. }
  320. if (strcmp(str, "sha384") == 0) {
  321. tmp_hash = "sha384";
  322. }
  323. if (strcmp(str, "sha512") == 0) {
  324. tmp_hash = "sha512";
  325. }
  326. free(str);
  327. }
  328. if ((strcmp(tmp_cipher, "none") != 0) &&
  329. (strcmp(tmp_hash, "none") == 0)) {
  330. return -1;
  331. }
  332. free(totem_config->crypto_cipher_type);
  333. free(totem_config->crypto_hash_type);
  334. totem_config->crypto_cipher_type = strdup(tmp_cipher);
  335. totem_config->crypto_hash_type = strdup(tmp_hash);
  336. return 0;
  337. }
  338. static int totem_config_get_ip_version(void)
  339. {
  340. int res;
  341. char *str;
  342. res = AF_INET;
  343. if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
  344. if (strcmp(str, "ipv4") == 0) {
  345. res = AF_INET;
  346. }
  347. if (strcmp(str, "ipv6") == 0) {
  348. res = AF_INET6;
  349. }
  350. free(str);
  351. }
  352. return (res);
  353. }
  354. static uint16_t generate_cluster_id (const char *cluster_name)
  355. {
  356. int i;
  357. int value = 0;
  358. for (i = 0; i < strlen(cluster_name); i++) {
  359. value <<= 1;
  360. value += cluster_name[i];
  361. }
  362. return (value & 0xFFFF);
  363. }
  364. static int get_cluster_mcast_addr (
  365. const char *cluster_name,
  366. const struct totem_ip_address *bindnet,
  367. unsigned int ringnumber,
  368. int ip_version,
  369. struct totem_ip_address *res)
  370. {
  371. uint16_t clusterid;
  372. char addr[INET6_ADDRSTRLEN + 1];
  373. int err;
  374. if (cluster_name == NULL) {
  375. return (-1);
  376. }
  377. clusterid = generate_cluster_id(cluster_name) + ringnumber;
  378. memset (res, 0, sizeof(*res));
  379. switch (bindnet->family) {
  380. case AF_INET:
  381. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  382. break;
  383. case AF_INET6:
  384. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  385. break;
  386. default:
  387. /*
  388. * Unknown family
  389. */
  390. return (-1);
  391. }
  392. err = totemip_parse (res, addr, ip_version);
  393. return (err);
  394. }
  395. static int find_local_node_in_nodelist(struct totem_config *totem_config)
  396. {
  397. icmap_iter_t iter;
  398. const char *iter_key;
  399. int res = 0;
  400. unsigned int node_pos;
  401. int local_node_pos = -1;
  402. struct totem_ip_address bind_addr;
  403. int interface_up, interface_num;
  404. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  405. char *node_addr_str;
  406. struct totem_ip_address node_addr;
  407. res = totemip_iface_check(&totem_config->interfaces[0].bindnet,
  408. &bind_addr, &interface_up, &interface_num,
  409. totem_config->clear_node_high_bit);
  410. if (res == -1) {
  411. return (-1);
  412. }
  413. iter = icmap_iter_init("nodelist.node.");
  414. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  415. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  416. if (res != 2) {
  417. continue;
  418. }
  419. if (strcmp(tmp_key, "ring0_addr") != 0) {
  420. continue;
  421. }
  422. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  423. if (icmap_get_string(tmp_key, &node_addr_str) != CS_OK) {
  424. continue;
  425. }
  426. res = totemip_parse (&node_addr, node_addr_str, totem_config->ip_version);
  427. free(node_addr_str);
  428. if (res == -1) {
  429. continue ;
  430. }
  431. if (totemip_equal(&bind_addr, &node_addr)) {
  432. local_node_pos = node_pos;
  433. }
  434. }
  435. icmap_iter_finalize(iter);
  436. return (local_node_pos);
  437. }
  438. static void put_nodelist_members_to_config(struct totem_config *totem_config)
  439. {
  440. icmap_iter_t iter, iter2;
  441. const char *iter_key, *iter_key2;
  442. int res = 0;
  443. unsigned int node_pos;
  444. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  445. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  446. char *node_addr_str;
  447. int member_count;
  448. unsigned int ringnumber = 0;
  449. int i, j;
  450. /* Clear out nodelist so we can put the new one in if needed */
  451. for (i = 0; i < totem_config->interface_count; i++) {
  452. for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
  453. memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
  454. }
  455. totem_config->interfaces[i].member_count = 0;
  456. }
  457. iter = icmap_iter_init("nodelist.node.");
  458. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  459. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  460. if (res != 2) {
  461. continue;
  462. }
  463. if (strcmp(tmp_key, "ring0_addr") != 0) {
  464. continue;
  465. }
  466. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  467. iter2 = icmap_iter_init(tmp_key);
  468. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  469. res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  470. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  471. continue;
  472. }
  473. if (icmap_get_string(iter_key2, &node_addr_str) != CS_OK) {
  474. continue;
  475. }
  476. member_count = totem_config->interfaces[ringnumber].member_count;
  477. res = totemip_parse(&totem_config->interfaces[ringnumber].member_list[member_count],
  478. node_addr_str, totem_config->ip_version);
  479. if (res != -1) {
  480. totem_config->interfaces[ringnumber].member_count++;
  481. }
  482. free(node_addr_str);
  483. }
  484. icmap_iter_finalize(iter2);
  485. }
  486. icmap_iter_finalize(iter);
  487. }
  488. /*
  489. * Tries to find node (node_pos) in config nodelist which address matches any
  490. * local interface. Address can be stored in ring0_addr or if ipaddr_key_prefix is not NULL
  491. * key with prefix ipaddr_key is used (there can be multiuple of them)
  492. * This function differs * from find_local_node_in_nodelist because it doesn't need bindnetaddr,
  493. * but doesn't work when bind addr is network address (so IP must be exact
  494. * match).
  495. *
  496. * Returns 1 on success (address was found, node_pos is then correctly set) or 0 on failure.
  497. */
  498. int totem_config_find_local_addr_in_nodelist(const char *ipaddr_key_prefix, unsigned int *node_pos)
  499. {
  500. struct list_head addrs;
  501. struct totem_ip_if_address *if_addr;
  502. icmap_iter_t iter, iter2;
  503. const char *iter_key, *iter_key2;
  504. struct list_head *list;
  505. const char *ipaddr_key;
  506. int ip_version;
  507. struct totem_ip_address node_addr;
  508. char *node_addr_str;
  509. int node_found = 0;
  510. int res = 0;
  511. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  512. if (totemip_getifaddrs(&addrs) == -1) {
  513. return 0;
  514. }
  515. ip_version = totem_config_get_ip_version();
  516. iter = icmap_iter_init("nodelist.node.");
  517. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  518. res = sscanf(iter_key, "nodelist.node.%u.%s", node_pos, tmp_key);
  519. if (res != 2) {
  520. continue;
  521. }
  522. if (strcmp(tmp_key, "ring0_addr") != 0) {
  523. continue;
  524. }
  525. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  526. continue ;
  527. }
  528. free(node_addr_str);
  529. /*
  530. * ring0_addr found -> let's iterate thru ipaddr_key_prefix
  531. */
  532. snprintf(tmp_key, sizeof(tmp_key), "nodelist.node.%u.%s", *node_pos,
  533. (ipaddr_key_prefix != NULL ? ipaddr_key_prefix : "ring0_addr"));
  534. iter2 = icmap_iter_init(tmp_key);
  535. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  536. /*
  537. * ring0_addr must be exact match, not prefix
  538. */
  539. ipaddr_key = (ipaddr_key_prefix != NULL ? iter_key2 : tmp_key);
  540. if (icmap_get_string(ipaddr_key, &node_addr_str) != CS_OK) {
  541. continue ;
  542. }
  543. if (totemip_parse(&node_addr, node_addr_str, ip_version) == -1) {
  544. free(node_addr_str);
  545. continue ;
  546. }
  547. free(node_addr_str);
  548. /*
  549. * Try to match ip with if_addrs
  550. */
  551. node_found = 0;
  552. for (list = addrs.next; list != &addrs; list = list->next) {
  553. if_addr = list_entry(list, struct totem_ip_if_address, list);
  554. if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
  555. node_found = 1;
  556. break;
  557. }
  558. }
  559. if (node_found) {
  560. break ;
  561. }
  562. }
  563. icmap_iter_finalize(iter2);
  564. if (node_found) {
  565. break ;
  566. }
  567. }
  568. icmap_iter_finalize(iter);
  569. totemip_freeifaddrs(&addrs);
  570. return (node_found);
  571. }
  572. static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
  573. {
  574. int res = 0;
  575. unsigned int node_pos;
  576. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  577. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  578. char *node_addr_str;
  579. unsigned int ringnumber = 0;
  580. icmap_iter_t iter;
  581. const char *iter_key;
  582. if (totem_config_find_local_addr_in_nodelist(NULL, &node_pos)) {
  583. /*
  584. * We found node, so create interface section
  585. */
  586. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  587. iter = icmap_iter_init(tmp_key);
  588. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  589. res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &ringnumber, tmp_key2);
  590. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  591. continue ;
  592. }
  593. if (icmap_get_string(iter_key, &node_addr_str) != CS_OK) {
  594. continue;
  595. }
  596. snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", ringnumber);
  597. icmap_set_string(tmp_key2, node_addr_str);
  598. free(node_addr_str);
  599. }
  600. icmap_iter_finalize(iter);
  601. }
  602. }
  603. extern int totem_config_read (
  604. struct totem_config *totem_config,
  605. const char **error_string,
  606. uint64_t *warnings)
  607. {
  608. int res = 0;
  609. char *str;
  610. unsigned int ringnumber = 0;
  611. int member_count = 0;
  612. icmap_iter_t iter, member_iter;
  613. const char *iter_key;
  614. const char *member_iter_key;
  615. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  616. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  617. uint8_t u8;
  618. uint16_t u16;
  619. char *cluster_name = NULL;
  620. int i;
  621. int local_node_pos;
  622. int nodeid_set;
  623. *warnings = 0;
  624. memset (totem_config, 0, sizeof (struct totem_config));
  625. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  626. if (totem_config->interfaces == 0) {
  627. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  628. return -1;
  629. }
  630. memset (totem_config->interfaces, 0,
  631. sizeof (struct totem_interface) * INTERFACE_MAX);
  632. strcpy (totem_config->rrp_mode, "none");
  633. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  634. if (totem_get_crypto(totem_config) != 0) {
  635. *error_string = "crypto_cipher requires crypto_hash with value other than none";
  636. return -1;
  637. }
  638. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  639. if (strlen(str) >= TOTEM_RRP_MODE_BYTES) {
  640. *error_string = "totem.rrp_mode is too long";
  641. free(str);
  642. return -1;
  643. }
  644. strcpy (totem_config->rrp_mode, str);
  645. free(str);
  646. }
  647. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  648. totem_config->clear_node_high_bit = 0;
  649. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  650. if (strcmp (str, "yes") == 0) {
  651. totem_config->clear_node_high_bit = 1;
  652. }
  653. free(str);
  654. }
  655. icmap_get_uint32("totem.threads", &totem_config->threads);
  656. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  657. if (icmap_get_string("totem.cluster_name", &cluster_name) != CS_OK) {
  658. cluster_name = NULL;
  659. }
  660. totem_config->ip_version = totem_config_get_ip_version();
  661. if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
  662. /*
  663. * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
  664. */
  665. config_convert_nodelist_to_interface(totem_config);
  666. } else {
  667. free(str);
  668. }
  669. iter = icmap_iter_init("totem.interface.");
  670. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  671. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  672. if (res != 2) {
  673. continue;
  674. }
  675. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  676. continue;
  677. }
  678. member_count = 0;
  679. ringnumber = atoi(ringnumber_key);
  680. if (ringnumber >= INTERFACE_MAX) {
  681. free(cluster_name);
  682. snprintf (error_string_response, sizeof(error_string_response),
  683. "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
  684. ringnumber, INTERFACE_MAX - 1);
  685. *error_string = error_string_response;
  686. return -1;
  687. }
  688. /*
  689. * Get the bind net address
  690. */
  691. if (icmap_get_string(iter_key, &str) == CS_OK) {
  692. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  693. totem_config->interfaces[ringnumber].mcast_addr.family);
  694. free(str);
  695. }
  696. /*
  697. * Get interface multicast address
  698. */
  699. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  700. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  701. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, totem_config->ip_version);
  702. free(str);
  703. } else {
  704. /*
  705. * User not specified address -> autogenerate one from cluster_name key
  706. * (if available)
  707. */
  708. res = get_cluster_mcast_addr (cluster_name,
  709. &totem_config->interfaces[ringnumber].bindnet,
  710. ringnumber,
  711. totem_config->ip_version,
  712. &totem_config->interfaces[ringnumber].mcast_addr);
  713. }
  714. totem_config->broadcast_use = 0;
  715. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  716. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  717. if (strcmp (str, "yes") == 0) {
  718. totem_config->broadcast_use = 1;
  719. totemip_parse (
  720. &totem_config->interfaces[ringnumber].mcast_addr,
  721. "255.255.255.255", totem_config->ip_version);
  722. }
  723. free(str);
  724. }
  725. /*
  726. * Get mcast port
  727. */
  728. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  729. if (icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port) != CS_OK) {
  730. if (totem_config->broadcast_use) {
  731. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT + (2 * ringnumber);
  732. } else {
  733. totem_config->interfaces[ringnumber].ip_port = DEFAULT_PORT;
  734. }
  735. }
  736. /*
  737. * Get the TTL
  738. */
  739. totem_config->interfaces[ringnumber].ttl = 1;
  740. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  741. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  742. totem_config->interfaces[ringnumber].ttl = u8;
  743. }
  744. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  745. member_iter = icmap_iter_init(tmp_key);
  746. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  747. if (member_count == 0) {
  748. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  749. free(str);
  750. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  751. break;
  752. } else {
  753. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  754. }
  755. }
  756. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  757. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  758. str, totem_config->ip_version);
  759. }
  760. }
  761. icmap_iter_finalize(member_iter);
  762. totem_config->interfaces[ringnumber].member_count = member_count;
  763. totem_config->interface_count++;
  764. }
  765. icmap_iter_finalize(iter);
  766. /*
  767. * Store automatically generated items back to icmap
  768. */
  769. for (i = 0; i < totem_config->interface_count; i++) {
  770. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  771. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  772. free(str);
  773. } else {
  774. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  775. icmap_set_string(tmp_key, str);
  776. }
  777. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  778. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  779. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  780. }
  781. }
  782. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  783. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  784. if (strcmp (str, "udpu") == 0) {
  785. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  786. }
  787. if (strcmp (str, "iba") == 0) {
  788. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  789. }
  790. free(str);
  791. }
  792. free(cluster_name);
  793. /*
  794. * Check existence of nodelist
  795. */
  796. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  797. free(str);
  798. /*
  799. * find local node
  800. */
  801. local_node_pos = find_local_node_in_nodelist(totem_config);
  802. if (local_node_pos != -1) {
  803. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  804. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  805. nodeid_set = (totem_config->node_id != 0);
  806. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  807. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  808. }
  809. /*
  810. * Make localnode ring0_addr read only, so we can be sure that local
  811. * node never changes. If rebinding to other IP would be in future
  812. * supported, this must be changed and handled properly!
  813. */
  814. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  815. icmap_set_ro_access(tmp_key, 0, 1);
  816. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  817. }
  818. put_nodelist_members_to_config(totem_config);
  819. }
  820. /*
  821. * Get things that might change in the future (and can depend on totem_config->interfaces);
  822. */
  823. totem_volatile_config_read(totem_config, NULL);
  824. icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
  825. add_totem_config_notification(totem_config);
  826. return 0;
  827. }
  828. int totem_config_validate (
  829. struct totem_config *totem_config,
  830. const char **error_string)
  831. {
  832. static char local_error_reason[512];
  833. char parse_error[512];
  834. const char *error_reason = local_error_reason;
  835. int i;
  836. unsigned int interface_max = INTERFACE_MAX;
  837. if (totem_config->interface_count == 0) {
  838. error_reason = "No interfaces defined";
  839. goto parse_error;
  840. }
  841. for (i = 0; i < totem_config->interface_count; i++) {
  842. /*
  843. * Some error checking of parsed data to make sure its valid
  844. */
  845. struct totem_ip_address null_addr;
  846. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  847. if ((totem_config->transport_number == 0) &&
  848. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  849. sizeof (struct totem_ip_address)) == 0) {
  850. error_reason = "No multicast address specified";
  851. goto parse_error;
  852. }
  853. if (totem_config->interfaces[i].ip_port == 0) {
  854. error_reason = "No multicast port specified";
  855. goto parse_error;
  856. }
  857. if (totem_config->interfaces[i].ttl > 255) {
  858. error_reason = "Invalid TTL (should be 0..255)";
  859. goto parse_error;
  860. }
  861. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  862. totem_config->interfaces[i].ttl != 1) {
  863. error_reason = "Can only set ttl on multicast transport types";
  864. goto parse_error;
  865. }
  866. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  867. totem_config->node_id == 0) {
  868. error_reason = "An IPV6 network requires that a node ID be specified.";
  869. goto parse_error;
  870. }
  871. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  872. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  873. error_reason = "Multicast address family does not match bind address family";
  874. goto parse_error;
  875. }
  876. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  877. error_reason = "Not all bind address belong to the same IP family";
  878. goto parse_error;
  879. }
  880. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  881. error_reason = "mcastaddr is not a correct multicast address.";
  882. goto parse_error;
  883. }
  884. }
  885. }
  886. if (totem_config->version != 2) {
  887. error_reason = "This totem parser can only parse version 2 configurations.";
  888. goto parse_error;
  889. }
  890. if (totem_volatile_config_validate(totem_config, error_string) == -1) {
  891. return (-1);
  892. }
  893. /*
  894. * RRP values validation
  895. */
  896. if (strcmp (totem_config->rrp_mode, "none") &&
  897. strcmp (totem_config->rrp_mode, "active") &&
  898. strcmp (totem_config->rrp_mode, "passive")) {
  899. snprintf (local_error_reason, sizeof(local_error_reason),
  900. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  901. goto parse_error;
  902. }
  903. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  904. interface_max = 1;
  905. }
  906. if (interface_max < totem_config->interface_count) {
  907. snprintf (parse_error, sizeof(parse_error),
  908. "%d is too many configured interfaces for the rrp_mode setting %s.",
  909. totem_config->interface_count,
  910. totem_config->rrp_mode);
  911. error_reason = parse_error;
  912. goto parse_error;
  913. }
  914. if (totem_config->net_mtu == 0) {
  915. totem_config->net_mtu = 1500;
  916. }
  917. return 0;
  918. parse_error:
  919. snprintf (error_string_response, sizeof(error_string_response),
  920. "parse error in config: %s\n", error_reason);
  921. *error_string = error_string_response;
  922. return (-1);
  923. }
  924. static int read_keyfile (
  925. const char *key_location,
  926. struct totem_config *totem_config,
  927. const char **error_string)
  928. {
  929. int fd;
  930. int res;
  931. ssize_t expected_key_len = sizeof (totem_config->private_key);
  932. int saved_errno;
  933. char error_str[100];
  934. const char *error_ptr;
  935. fd = open (key_location, O_RDONLY);
  936. if (fd == -1) {
  937. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  938. snprintf (error_string_response, sizeof(error_string_response),
  939. "Could not open %s: %s\n",
  940. key_location, error_ptr);
  941. goto parse_error;
  942. }
  943. res = read (fd, totem_config->private_key, expected_key_len);
  944. saved_errno = errno;
  945. close (fd);
  946. if (res == -1) {
  947. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  948. snprintf (error_string_response, sizeof(error_string_response),
  949. "Could not read %s: %s\n",
  950. key_location, error_ptr);
  951. goto parse_error;
  952. }
  953. totem_config->private_key_len = expected_key_len;
  954. if (res != expected_key_len) {
  955. snprintf (error_string_response, sizeof(error_string_response),
  956. "Could only read %d bits of 1024 bits from %s.\n",
  957. res * 8, key_location);
  958. goto parse_error;
  959. }
  960. return 0;
  961. parse_error:
  962. *error_string = error_string_response;
  963. return (-1);
  964. }
  965. int totem_config_keyread (
  966. struct totem_config *totem_config,
  967. const char **error_string)
  968. {
  969. int got_key = 0;
  970. char *key_location = NULL;
  971. int res;
  972. size_t key_len;
  973. memset (totem_config->private_key, 0, 128);
  974. totem_config->private_key_len = 128;
  975. if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
  976. strcmp(totem_config->crypto_hash_type, "none") == 0) {
  977. return (0);
  978. }
  979. /* cmap may store the location of the key file */
  980. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  981. res = read_keyfile(key_location, totem_config, error_string);
  982. free(key_location);
  983. if (res) {
  984. goto key_error;
  985. }
  986. got_key = 1;
  987. } else { /* Or the key itself may be in the cmap */
  988. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  989. if (key_len > sizeof (totem_config->private_key)) {
  990. sprintf(error_string_response, "key is too long");
  991. goto key_error;
  992. }
  993. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  994. totem_config->private_key_len = key_len;
  995. got_key = 1;
  996. } else {
  997. sprintf(error_string_response, "can't store private key");
  998. goto key_error;
  999. }
  1000. }
  1001. }
  1002. /* In desperation we read the default filename */
  1003. if (!got_key) {
  1004. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  1005. if (!filename)
  1006. filename = COROSYSCONFDIR "/authkey";
  1007. res = read_keyfile(filename, totem_config, error_string);
  1008. if (res)
  1009. goto key_error;
  1010. }
  1011. return (0);
  1012. key_error:
  1013. *error_string = error_string_response;
  1014. return (-1);
  1015. }
  1016. static void debug_dump_totem_config(const struct totem_config *totem_config)
  1017. {
  1018. log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
  1019. totem_config->token_timeout, totem_config->token_retransmit_timeout);
  1020. log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
  1021. totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
  1022. log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
  1023. totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
  1024. totem_config->merge_timeout);
  1025. log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
  1026. totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
  1027. log_printf(LOGSYS_LEVEL_DEBUG,
  1028. "seqno unchanged const (%d rotations) Maximum network MTU %d",
  1029. totem_config->seqno_unchanged_const, totem_config->net_mtu);
  1030. log_printf(LOGSYS_LEVEL_DEBUG,
  1031. "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
  1032. totem_config->window_size, totem_config->max_messages);
  1033. log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
  1034. log_printf(LOGSYS_LEVEL_DEBUG, "RRP token expired timeout (%d ms)",
  1035. totem_config->rrp_token_expired_timeout);
  1036. log_printf(LOGSYS_LEVEL_DEBUG, "RRP token problem counter (%d ms)",
  1037. totem_config->rrp_problem_count_timeout);
  1038. log_printf(LOGSYS_LEVEL_DEBUG, "RRP threshold (%d problem count)",
  1039. totem_config->rrp_problem_count_threshold);
  1040. log_printf(LOGSYS_LEVEL_DEBUG, "RRP multicast threshold (%d problem count)",
  1041. totem_config->rrp_problem_count_mcast_threshold);
  1042. log_printf(LOGSYS_LEVEL_DEBUG, "RRP automatic recovery check timeout (%d ms)",
  1043. totem_config->rrp_autorecovery_check_timeout);
  1044. log_printf(LOGSYS_LEVEL_DEBUG, "RRP mode set to %s.",
  1045. totem_config->rrp_mode);
  1046. log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
  1047. totem_config->heartbeat_failures_allowed);
  1048. log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
  1049. }
  1050. static void totem_change_notify(
  1051. int32_t event,
  1052. const char *key_name,
  1053. struct icmap_notify_value new_val,
  1054. struct icmap_notify_value old_val,
  1055. void *user_data)
  1056. {
  1057. struct totem_config *totem_config = (struct totem_config *)user_data;
  1058. uint32_t *param;
  1059. uint8_t reloading;
  1060. const char *deleted_key = NULL;
  1061. const char *error_string;
  1062. /*
  1063. * If a full reload is in progress then don't do anything until it's done and
  1064. * can reconfigure it all atomically
  1065. */
  1066. if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
  1067. return;
  1068. param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
  1069. /*
  1070. * Process change only if changed key is found in totem_config (-> param is not NULL)
  1071. * or for special key token_coefficient. token_coefficient key is not stored in
  1072. * totem_config, but it is used for computation of token timeout.
  1073. */
  1074. if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
  1075. return;
  1076. /*
  1077. * Values other than UINT32 are not supported, or needed (yet)
  1078. */
  1079. switch (event) {
  1080. case ICMAP_TRACK_DELETE:
  1081. deleted_key = key_name;
  1082. break;
  1083. case ICMAP_TRACK_ADD:
  1084. case ICMAP_TRACK_MODIFY:
  1085. deleted_key = NULL;
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. totem_volatile_config_read (totem_config, deleted_key);
  1091. log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
  1092. debug_dump_totem_config(totem_config);
  1093. if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
  1094. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1095. /*
  1096. * TODO: Consider corosync exit and/or load defaults for volatile
  1097. * values. For now, log error seems to be enough
  1098. */
  1099. }
  1100. }
  1101. static void totem_reload_notify(
  1102. int32_t event,
  1103. const char *key_name,
  1104. struct icmap_notify_value new_val,
  1105. struct icmap_notify_value old_val,
  1106. void *user_data)
  1107. {
  1108. struct totem_config *totem_config = (struct totem_config *)user_data;
  1109. uint32_t local_node_pos;
  1110. const char *error_string;
  1111. /* Reload has completed */
  1112. if (*(uint8_t *)new_val.data == 0) {
  1113. put_nodelist_members_to_config (totem_config);
  1114. totem_volatile_config_read (totem_config, NULL);
  1115. log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
  1116. debug_dump_totem_config(totem_config);
  1117. if (totem_volatile_config_validate(totem_config, &error_string) == -1) {
  1118. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1119. /*
  1120. * TODO: Consider corosync exit and/or load defaults for volatile
  1121. * values. For now, log error seems to be enough
  1122. */
  1123. }
  1124. /* Reinstate the local_node_pos */
  1125. local_node_pos = find_local_node_in_nodelist(totem_config);
  1126. if (local_node_pos != -1) {
  1127. icmap_set_uint32("nodelist.local_node_pos", local_node_pos);
  1128. }
  1129. icmap_set_uint8("config.totemconfig_reload_in_progress", 0);
  1130. } else {
  1131. icmap_set_uint8("config.totemconfig_reload_in_progress", 1);
  1132. }
  1133. }
  1134. static void add_totem_config_notification(struct totem_config *totem_config)
  1135. {
  1136. icmap_track_t icmap_track;
  1137. icmap_track_add("totem.",
  1138. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1139. totem_change_notify,
  1140. totem_config,
  1141. &icmap_track);
  1142. icmap_track_add("config.reload_in_progress",
  1143. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  1144. totem_reload_notify,
  1145. totem_config,
  1146. &icmap_track);
  1147. }