4
0

totemconfig.c 50 KB

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