totemconfig.c 49 KB

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