totemconfig.c 41 KB

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