totemconfig.c 57 KB

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