totemconfig.c 50 KB

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