totemconfig.c 42 KB

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