totemconfig.c 56 KB

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