totemconfig.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2019 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. * Chrissie Caulfield (ccaulfie@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <errno.h>
  42. #include <unistd.h>
  43. #include <sys/socket.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <fcntl.h>
  47. #include <ifaddrs.h>
  48. #include <netdb.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <sys/param.h>
  52. #include <sys/utsname.h>
  53. #include <corosync/swab.h>
  54. #include <qb/qblist.h>
  55. #include <qb/qbdefs.h>
  56. #include <libknet.h>
  57. #include <corosync/totem/totem.h>
  58. #include <corosync/config.h>
  59. #include <corosync/logsys.h>
  60. #include <corosync/icmap.h>
  61. #include "util.h"
  62. #include "totemconfig.h"
  63. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  64. #define TOKEN_TIMEOUT 1000
  65. #define TOKEN_WARNING 75
  66. #define TOKEN_COEFFICIENT 650
  67. #define JOIN_TIMEOUT 50
  68. #define MERGE_TIMEOUT 200
  69. #define DOWNCHECK_TIMEOUT 1000
  70. #define FAIL_TO_RECV_CONST 2500
  71. #define SEQNO_UNCHANGED_CONST 30
  72. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  73. #define MINIMUM_TIMEOUT_HOLD (int)(MINIMUM_TIMEOUT * 0.8 - (1000/HZ))
  74. #define MAX_NETWORK_DELAY 50
  75. #define WINDOW_SIZE 50
  76. #define MAX_MESSAGES 17
  77. #define MISS_COUNT_CONST 5
  78. #define BLOCK_UNLISTED_IPS 1
  79. /* Currently all but PONG_COUNT match the defaults in libknet.h */
  80. #define KNET_PING_INTERVAL 1000
  81. #define KNET_PING_TIMEOUT 2000
  82. #define KNET_PING_PRECISION 2048
  83. #define KNET_PONG_COUNT 2
  84. #define KNET_PMTUD_INTERVAL 30
  85. #define KNET_DEFAULT_TRANSPORT KNET_TRANSPORT_UDP
  86. #define DEFAULT_PORT 5405
  87. static char error_string_response[768];
  88. static void add_totem_config_notification(struct totem_config *totem_config);
  89. static void *totem_get_param_by_name(struct totem_config *totem_config, const char *param_name)
  90. {
  91. if (strcmp(param_name, "totem.token") == 0)
  92. return &totem_config->token_timeout;
  93. if (strcmp(param_name, "totem.token_warning") == 0)
  94. return &totem_config->token_warning;
  95. if (strcmp(param_name, "totem.token_retransmit") == 0)
  96. return &totem_config->token_retransmit_timeout;
  97. if (strcmp(param_name, "totem.hold") == 0)
  98. return &totem_config->token_hold_timeout;
  99. if (strcmp(param_name, "totem.token_retransmits_before_loss_const") == 0)
  100. return &totem_config->token_retransmits_before_loss_const;
  101. if (strcmp(param_name, "totem.join") == 0)
  102. return &totem_config->join_timeout;
  103. if (strcmp(param_name, "totem.send_join") == 0)
  104. return &totem_config->send_join_timeout;
  105. if (strcmp(param_name, "totem.consensus") == 0)
  106. return &totem_config->consensus_timeout;
  107. if (strcmp(param_name, "totem.merge") == 0)
  108. return &totem_config->merge_timeout;
  109. if (strcmp(param_name, "totem.downcheck") == 0)
  110. return &totem_config->downcheck_timeout;
  111. if (strcmp(param_name, "totem.fail_recv_const") == 0)
  112. return &totem_config->fail_to_recv_const;
  113. if (strcmp(param_name, "totem.seqno_unchanged_const") == 0)
  114. return &totem_config->seqno_unchanged_const;
  115. if (strcmp(param_name, "totem.heartbeat_failures_allowed") == 0)
  116. return &totem_config->heartbeat_failures_allowed;
  117. if (strcmp(param_name, "totem.max_network_delay") == 0)
  118. return &totem_config->max_network_delay;
  119. if (strcmp(param_name, "totem.window_size") == 0)
  120. return &totem_config->window_size;
  121. if (strcmp(param_name, "totem.max_messages") == 0)
  122. return &totem_config->max_messages;
  123. if (strcmp(param_name, "totem.miss_count_const") == 0)
  124. return &totem_config->miss_count_const;
  125. if (strcmp(param_name, "totem.knet_pmtud_interval") == 0)
  126. return &totem_config->knet_pmtud_interval;
  127. if (strcmp(param_name, "totem.knet_compression_threshold") == 0)
  128. return &totem_config->knet_compression_threshold;
  129. if (strcmp(param_name, "totem.knet_compression_level") == 0)
  130. return &totem_config->knet_compression_level;
  131. if (strcmp(param_name, "totem.knet_compression_model") == 0)
  132. return totem_config->knet_compression_model;
  133. if (strcmp(param_name, "totem.block_unlisted_ips") == 0)
  134. return &totem_config->block_unlisted_ips;
  135. return NULL;
  136. }
  137. /*
  138. * Read key_name from icmap. If key is not found or key_name == delete_key or if allow_zero is false
  139. * and readed value is zero, default value is used and stored into totem_config.
  140. */
  141. static void totem_volatile_config_set_uint32_value (struct totem_config *totem_config, icmap_map_t map,
  142. const char *key_name, const char *deleted_key, unsigned int default_value,
  143. int allow_zero_value)
  144. {
  145. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  146. if (icmap_get_uint32_r(map, key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
  147. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
  148. (!allow_zero_value && *(uint32_t *)totem_get_param_by_name(totem_config, key_name) == 0)) {
  149. *(uint32_t *)totem_get_param_by_name(totem_config, key_name) = default_value;
  150. }
  151. /*
  152. * Store totem_config value to cmap runtime section
  153. */
  154. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  155. /*
  156. * This shouldn't happen
  157. */
  158. return ;
  159. }
  160. strcpy(runtime_key_name, "runtime.config.");
  161. strcat(runtime_key_name, key_name);
  162. icmap_set_uint32_r(map, runtime_key_name, *(uint32_t *)totem_get_param_by_name(totem_config, key_name));
  163. }
  164. static void totem_volatile_config_set_int32_value (struct totem_config *totem_config, icmap_map_t map,
  165. const char *key_name, const char *deleted_key, int default_value,
  166. int allow_zero_value)
  167. {
  168. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  169. if (icmap_get_int32_r(map, key_name, totem_get_param_by_name(totem_config, key_name)) != CS_OK ||
  170. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
  171. (!allow_zero_value && *(int32_t *)totem_get_param_by_name(totem_config, key_name) == 0)) {
  172. *(int32_t *)totem_get_param_by_name(totem_config, key_name) = default_value;
  173. }
  174. /*
  175. * Store totem_config value to cmap runtime section
  176. */
  177. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  178. /*
  179. * This shouldn't happen
  180. */
  181. return ;
  182. }
  183. strcpy(runtime_key_name, "runtime.config.");
  184. strcat(runtime_key_name, key_name);
  185. icmap_set_int32_r(map, runtime_key_name, *(int32_t *)totem_get_param_by_name(totem_config, key_name));
  186. }
  187. static void totem_volatile_config_set_string_value (struct totem_config *totem_config, icmap_map_t map,
  188. const char *key_name, const char *deleted_key, const char *default_value)
  189. {
  190. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  191. int res;
  192. char *new_config_value;
  193. const void *config_value;
  194. config_value = totem_get_param_by_name(totem_config, key_name);
  195. res = icmap_get_string_r(map, key_name, (char **)&new_config_value);
  196. if (res != CS_OK ||
  197. (deleted_key != NULL && strcmp(deleted_key, key_name) == 0)) {
  198. /* Slightly pointless use of strncpy but it keeps coverity happy */
  199. strncpy((char *)config_value, default_value, CONFIG_STRING_LEN_MAX);
  200. } else {
  201. strncpy((char *)config_value, new_config_value, CONFIG_STRING_LEN_MAX);
  202. }
  203. if (res == CS_OK) {
  204. free(new_config_value);
  205. }
  206. /*
  207. * Store totem_config value to cmap runtime section
  208. */
  209. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  210. /*
  211. * This shouldn't happen
  212. */
  213. return ;
  214. }
  215. strcpy(runtime_key_name, "runtime.config.");
  216. strcat(runtime_key_name, key_name);
  217. (void)icmap_set_string_r(map, runtime_key_name, (char *)config_value);
  218. }
  219. /*
  220. * Read string value stored in key_name from icmap, use it as a boolean (yes/no) type, convert it
  221. * to integer value (1/0) and store into totem_config.
  222. *
  223. * If key is not found or key_name == delete_key default value is used
  224. * and stored into totem_config.
  225. */
  226. static void totem_volatile_config_set_boolean_value (struct totem_config *totem_config, icmap_map_t map,
  227. const char *key_name, const char *deleted_key, unsigned int default_value)
  228. {
  229. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  230. char *str;
  231. int val;
  232. str = NULL;
  233. val = default_value;
  234. if ((deleted_key != NULL && strcmp(deleted_key, key_name) == 0) ||
  235. (icmap_get_string_r(map, key_name, &str) != CS_OK)) {
  236. /*
  237. * Do nothing. str is NULL (icmap_get_string ether not called or
  238. * not changed str).
  239. */
  240. } else {
  241. if (strcmp(str, "yes") == 0) {
  242. val = 1;
  243. } else if (strcmp(str, "no") == 0) {
  244. val = 0;
  245. }
  246. free(str);
  247. }
  248. /*
  249. * Store totem_config value to cmap runtime section
  250. */
  251. if (strlen("runtime.config.") + strlen(key_name) >= ICMAP_KEYNAME_MAXLEN) {
  252. /*
  253. * This shouldn't happen
  254. */
  255. return ;
  256. }
  257. strcpy(runtime_key_name, "runtime.config.");
  258. strcat(runtime_key_name, key_name);
  259. *(uint32_t *)totem_get_param_by_name(totem_config, key_name) = val;
  260. icmap_set_uint32_r(map, runtime_key_name, val);
  261. }
  262. /*
  263. * Read and validate config values from cmap and store them into totem_config. If key doesn't exists,
  264. * default value is stored. deleted_key is name of key beeing processed by delete operation
  265. * from cmap. It is considered as non existing even if it can be read. Can be NULL.
  266. */
  267. void totem_volatile_config_read (struct totem_config *totem_config, icmap_map_t temp_map, const char *deleted_key)
  268. {
  269. uint32_t u32;
  270. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.token_retransmits_before_loss_const", deleted_key,
  271. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST, 0);
  272. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.token", deleted_key, TOKEN_TIMEOUT, 0);
  273. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.token_warning", deleted_key, TOKEN_WARNING, 1);
  274. if (totem_config->interfaces[0].member_count > 2) {
  275. u32 = TOKEN_COEFFICIENT;
  276. icmap_get_uint32_r(temp_map, "totem.token_coefficient", &u32);
  277. totem_config->token_timeout += (totem_config->interfaces[0].member_count - 2) * u32;
  278. /*
  279. * Store totem_config value to cmap runtime section
  280. */
  281. icmap_set_uint32_r(temp_map, "runtime.config.totem.token", totem_config->token_timeout);
  282. }
  283. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.max_network_delay", deleted_key, MAX_NETWORK_DELAY, 0);
  284. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.window_size", deleted_key, WINDOW_SIZE, 0);
  285. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.max_messages", deleted_key, MAX_MESSAGES, 0);
  286. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.miss_count_const", deleted_key, MISS_COUNT_CONST, 0);
  287. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.knet_pmtud_interval", deleted_key, KNET_PMTUD_INTERVAL, 0);
  288. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.token_retransmit", deleted_key,
  289. (int)(totem_config->token_timeout / (totem_config->token_retransmits_before_loss_const + 0.2)), 0);
  290. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.hold", deleted_key,
  291. (int)(totem_config->token_retransmit_timeout * 0.8 - (1000/HZ)), 0);
  292. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.join", deleted_key, JOIN_TIMEOUT, 0);
  293. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.consensus", deleted_key,
  294. (int)(float)(1.2 * totem_config->token_timeout), 0);
  295. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.merge", deleted_key, MERGE_TIMEOUT, 0);
  296. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.downcheck", deleted_key, DOWNCHECK_TIMEOUT, 0);
  297. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.fail_recv_const", deleted_key, FAIL_TO_RECV_CONST, 0);
  298. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.seqno_unchanged_const", deleted_key,
  299. SEQNO_UNCHANGED_CONST, 0);
  300. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.send_join", deleted_key, 0, 1);
  301. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.heartbeat_failures_allowed", deleted_key, 0, 1);
  302. totem_volatile_config_set_uint32_value(totem_config, temp_map, "totem.knet_compression_threshold", deleted_key, 0, 1);
  303. totem_volatile_config_set_int32_value(totem_config, temp_map, "totem.knet_compression_level", deleted_key, 0, 1);
  304. totem_volatile_config_set_string_value(totem_config, temp_map, "totem.knet_compression_model", deleted_key, "none");
  305. totem_volatile_config_set_boolean_value(totem_config, temp_map, "totem.block_unlisted_ips", deleted_key,
  306. BLOCK_UNLISTED_IPS);
  307. }
  308. int totem_volatile_config_validate (
  309. struct totem_config *totem_config,
  310. icmap_map_t temp_map,
  311. const char **error_string)
  312. {
  313. /* Static just to keep them off the stack */
  314. static char local_error_reason[512];
  315. static char addr_str_buf[INET6_ADDRSTRLEN];
  316. const char *error_reason = local_error_reason;
  317. char name_key[ICMAP_KEYNAME_MAXLEN];
  318. char *name_str;
  319. int i, j, num_configured, members;
  320. uint32_t tmp_config_value;
  321. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  322. snprintf (local_error_reason, sizeof(local_error_reason),
  323. "The max_network_delay parameter (%d ms) may not be less than (%d ms).",
  324. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  325. goto parse_error;
  326. }
  327. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  328. snprintf (local_error_reason, sizeof(local_error_reason),
  329. "The token timeout parameter (%d ms) may not be less than (%d ms).",
  330. totem_config->token_timeout, MINIMUM_TIMEOUT);
  331. goto parse_error;
  332. }
  333. if (totem_config->token_warning > 100 || totem_config->token_warning < 0) {
  334. snprintf (local_error_reason, sizeof(local_error_reason),
  335. "The token warning parameter (%d%%) must be between 0 (disabled) and 100.",
  336. totem_config->token_warning);
  337. goto parse_error;
  338. }
  339. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  340. if (icmap_get_uint32_r(temp_map, "totem.token_retransmit", &tmp_config_value) == CS_OK) {
  341. snprintf (local_error_reason, sizeof(local_error_reason),
  342. "The token retransmit timeout parameter (%d ms) may not be less than (%d ms).",
  343. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  344. goto parse_error;
  345. } else {
  346. snprintf (local_error_reason, sizeof(local_error_reason),
  347. "Not appropriate token or token_retransmits_before_loss_const value set");
  348. goto parse_error;
  349. }
  350. }
  351. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT_HOLD) {
  352. snprintf (local_error_reason, sizeof(local_error_reason),
  353. "The token hold timeout parameter (%d ms) may not be less than (%d ms).",
  354. totem_config->token_hold_timeout, MINIMUM_TIMEOUT_HOLD);
  355. goto parse_error;
  356. }
  357. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  358. snprintf (local_error_reason, sizeof(local_error_reason),
  359. "The join timeout parameter (%d ms) may not be less than (%d ms).",
  360. totem_config->join_timeout, MINIMUM_TIMEOUT);
  361. goto parse_error;
  362. }
  363. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  364. snprintf (local_error_reason, sizeof(local_error_reason),
  365. "The consensus timeout parameter (%d ms) may not be less than (%d ms).",
  366. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  367. goto parse_error;
  368. }
  369. if (totem_config->consensus_timeout < totem_config->join_timeout) {
  370. snprintf (local_error_reason, sizeof(local_error_reason),
  371. "The consensus timeout parameter (%d ms) may not be less than join timeout (%d ms).",
  372. totem_config->consensus_timeout, totem_config->join_timeout);
  373. goto parse_error;
  374. }
  375. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  376. snprintf (local_error_reason, sizeof(local_error_reason),
  377. "The merge timeout parameter (%d ms) may not be less than (%d ms).",
  378. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  379. goto parse_error;
  380. }
  381. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  382. snprintf (local_error_reason, sizeof(local_error_reason),
  383. "The downcheck timeout parameter (%d ms) may not be less than (%d ms).",
  384. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  385. goto parse_error;
  386. }
  387. /* Check that we have nodelist 'name' if there is more than one link */
  388. num_configured = 0;
  389. members = -1;
  390. for (i = 0; i < INTERFACE_MAX; i++) {
  391. if (totem_config->interfaces[i].configured) {
  392. if (num_configured == 0) {
  393. members = totem_config->interfaces[i].member_count;
  394. }
  395. num_configured++;
  396. }
  397. }
  398. if (num_configured > 1) {
  399. /*
  400. * This assert is here just to make compiler happy
  401. */
  402. assert(members != -1);
  403. for (i=0; i < members; i++) {
  404. snprintf(name_key, sizeof(name_key), "nodelist.node.%d.name", i);
  405. if (icmap_get_string_r(temp_map, name_key, &name_str) != CS_OK) {
  406. snprintf (local_error_reason, sizeof(local_error_reason),
  407. "for a multi-link configuration, all nodes must have a 'name' attribute");
  408. goto parse_error;
  409. }
  410. free(name_str);
  411. }
  412. for (i=0; i < INTERFACE_MAX; i++) {
  413. if (!totem_config->interfaces[i].configured) {
  414. continue;
  415. }
  416. if (totem_config->interfaces[i].member_count != members) {
  417. snprintf (local_error_reason, sizeof(local_error_reason),
  418. "Not all nodes have the same number of links");
  419. goto parse_error;
  420. }
  421. }
  422. }
  423. /* Verify that all nodes on the same link have the same IP family */
  424. for (i=0; i < INTERFACE_MAX; i++) {
  425. for (j=1; j<totem_config->interfaces[i].member_count; j++) {
  426. if (totem_config->interfaces[i].configured) {
  427. if (totem_config->interfaces[i].member_list[j].family !=
  428. totem_config->interfaces[i].member_list[0].family) {
  429. memcpy(addr_str_buf,
  430. totemip_print(&(totem_config->interfaces[i].member_list[j])),
  431. sizeof(addr_str_buf));
  432. snprintf (local_error_reason, sizeof(local_error_reason),
  433. "Nodes for link %d have different IP families "
  434. "(compared %s with %s)", i,
  435. addr_str_buf,
  436. totemip_print(&(totem_config->interfaces[i].member_list[0])));
  437. goto parse_error;
  438. }
  439. }
  440. }
  441. }
  442. return 0;
  443. parse_error:
  444. snprintf (error_string_response, sizeof(error_string_response),
  445. "parse error in config: %s\n", error_reason);
  446. *error_string = error_string_response;
  447. return (-1);
  448. }
  449. static int totem_get_crypto(struct totem_config *totem_config, icmap_map_t map, const char **error_string)
  450. {
  451. char *str;
  452. const char *tmp_cipher;
  453. const char *tmp_hash;
  454. const char *tmp_model;
  455. tmp_hash = "none";
  456. tmp_cipher = "none";
  457. tmp_model = "none";
  458. if (icmap_get_string_r(map, "totem.crypto_model", &str) == CS_OK) {
  459. if (strcmp(str, "nss") == 0) {
  460. tmp_model = "nss";
  461. }
  462. if (strcmp(str, "openssl") == 0) {
  463. tmp_model = "openssl";
  464. }
  465. free(str);
  466. } else {
  467. tmp_model = "nss";
  468. }
  469. if (icmap_get_string_r(map, "totem.secauth", &str) == CS_OK) {
  470. if (strcmp(str, "on") == 0) {
  471. tmp_cipher = "aes256";
  472. tmp_hash = "sha256";
  473. }
  474. free(str);
  475. }
  476. if (icmap_get_string_r(map, "totem.crypto_cipher", &str) == CS_OK) {
  477. if (strcmp(str, "none") == 0) {
  478. tmp_cipher = "none";
  479. }
  480. if (strcmp(str, "aes256") == 0) {
  481. tmp_cipher = "aes256";
  482. }
  483. if (strcmp(str, "aes192") == 0) {
  484. tmp_cipher = "aes192";
  485. }
  486. if (strcmp(str, "aes128") == 0) {
  487. tmp_cipher = "aes128";
  488. }
  489. free(str);
  490. }
  491. if (icmap_get_string_r(map, "totem.crypto_hash", &str) == CS_OK) {
  492. if (strcmp(str, "none") == 0) {
  493. tmp_hash = "none";
  494. }
  495. if (strcmp(str, "md5") == 0) {
  496. tmp_hash = "md5";
  497. }
  498. if (strcmp(str, "sha1") == 0) {
  499. tmp_hash = "sha1";
  500. }
  501. if (strcmp(str, "sha256") == 0) {
  502. tmp_hash = "sha256";
  503. }
  504. if (strcmp(str, "sha384") == 0) {
  505. tmp_hash = "sha384";
  506. }
  507. if (strcmp(str, "sha512") == 0) {
  508. tmp_hash = "sha512";
  509. }
  510. free(str);
  511. }
  512. if ((strcmp(tmp_cipher, "none") != 0) &&
  513. (strcmp(tmp_hash, "none") == 0)) {
  514. *error_string = "crypto_cipher requires crypto_hash with value other than none";
  515. return -1;
  516. }
  517. if (strcmp(tmp_model, "none") == 0) {
  518. *error_string = "crypto_model should be 'nss' or 'openssl'";
  519. return -1;
  520. }
  521. if (strcmp(tmp_cipher, totem_config->crypto_cipher_type) ||
  522. strcmp(tmp_hash, totem_config->crypto_hash_type) ||
  523. strcmp(tmp_model, totem_config->crypto_model)) {
  524. totem_config->crypto_changed = 1;
  525. }
  526. strncpy(totem_config->crypto_cipher_type, tmp_cipher, CONFIG_STRING_LEN_MAX);
  527. strncpy(totem_config->crypto_hash_type, tmp_hash, CONFIG_STRING_LEN_MAX);
  528. strncpy(totem_config->crypto_model, tmp_model, CONFIG_STRING_LEN_MAX);
  529. return 0;
  530. }
  531. static int nodelist_byname(icmap_map_t map, const char *find_name, int strip_domain)
  532. {
  533. icmap_iter_t iter;
  534. const char *iter_key;
  535. char name_str[ICMAP_KEYNAME_MAXLEN];
  536. int res = 0;
  537. unsigned int node_pos;
  538. char *name;
  539. unsigned int namelen;
  540. iter = icmap_iter_init_r(map, "nodelist.node.");
  541. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  542. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, name_str);
  543. if (res != 2) {
  544. continue;
  545. }
  546. /* ring0_addr is allowed as a fallback */
  547. if (strcmp(name_str, "name") && strcmp(name_str, "ring0_addr")) {
  548. continue;
  549. }
  550. if (icmap_get_string_r(map, iter_key, &name) != CS_OK) {
  551. continue;
  552. }
  553. namelen = strlen(name);
  554. if (strip_domain) {
  555. char *dot;
  556. dot = strchr(name, '.');
  557. if (dot) {
  558. namelen = name - dot - 1;
  559. }
  560. }
  561. if (strncmp(find_name, name, namelen) == 0 &&
  562. strlen(find_name) == strlen(name)) {
  563. icmap_iter_finalize(iter);
  564. return node_pos;
  565. }
  566. }
  567. icmap_iter_finalize(iter);
  568. return -1;
  569. }
  570. /* Compare two addresses - only address part (sin_addr/sin6_addr) is checked */
  571. static int ipaddr_equal(const struct sockaddr *addr1, const struct sockaddr *addr2)
  572. {
  573. int addrlen = 0;
  574. const void *addr1p, *addr2p;
  575. if (addr1->sa_family != addr2->sa_family)
  576. return 0;
  577. switch (addr1->sa_family) {
  578. case AF_INET:
  579. addrlen = sizeof(struct in_addr);
  580. addr1p = &((struct sockaddr_in *)addr1)->sin_addr;
  581. addr2p = &((struct sockaddr_in *)addr2)->sin_addr;
  582. break;
  583. case AF_INET6:
  584. addrlen = sizeof(struct in6_addr);
  585. addr1p = &((struct sockaddr_in6 *)addr1)->sin6_addr;
  586. addr2p = &((struct sockaddr_in6 *)addr2)->sin6_addr;
  587. break;
  588. default:
  589. assert(0);
  590. }
  591. return (memcmp(addr1p, addr2p, addrlen) == 0);
  592. }
  593. /* Finds the local node and returns its position in the nodelist.
  594. * Uses nodelist.local_node_pos as a cache to save effort
  595. */
  596. static int find_local_node(icmap_map_t map, int use_cache)
  597. {
  598. char nodename2[PATH_MAX];
  599. char name_str[ICMAP_KEYNAME_MAXLEN];
  600. icmap_iter_t iter;
  601. const char *iter_key;
  602. unsigned int cached_pos;
  603. char *dot = NULL;
  604. const char *node;
  605. struct ifaddrs *ifa, *ifa_list;
  606. struct sockaddr *sa;
  607. int found = 0;
  608. int node_pos = -1;
  609. int res;
  610. struct utsname utsname;
  611. /* Check for cached value first */
  612. if (use_cache) {
  613. if (icmap_get_uint32("nodelist.local_node_pos", &cached_pos) == CS_OK) {
  614. return cached_pos;
  615. }
  616. }
  617. res = uname(&utsname);
  618. if (res) {
  619. return -1;
  620. }
  621. node = utsname.nodename;
  622. /* 1. Exact match */
  623. node_pos = nodelist_byname(map, node, 0);
  624. if (node_pos > -1) {
  625. found = 1;
  626. goto ret_found;
  627. }
  628. /* 2. Try to match with increasingly more
  629. * specific versions of it
  630. */
  631. strcpy(nodename2, node);
  632. dot = strrchr(nodename2, '.');
  633. while (dot) {
  634. *dot = '\0';
  635. node_pos = nodelist_byname(map, nodename2, 0);
  636. if (node_pos > -1) {
  637. found = 1;
  638. goto ret_found;
  639. }
  640. dot = strrchr(nodename2, '.');
  641. }
  642. node_pos = nodelist_byname(map, nodename2, 1);
  643. if (node_pos > -1) {
  644. found = 1;
  645. goto ret_found;
  646. }
  647. /*
  648. * The corosync.conf name may not be related to uname at all,
  649. * they may match a hostname on some network interface.
  650. */
  651. if (getifaddrs(&ifa_list))
  652. return -1;
  653. for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
  654. socklen_t salen = 0;
  655. /* Restore this */
  656. strcpy(nodename2, node);
  657. sa = ifa->ifa_addr;
  658. if (!sa) {
  659. continue;
  660. }
  661. if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) {
  662. continue;
  663. }
  664. if (sa->sa_family == AF_INET) {
  665. salen = sizeof(struct sockaddr_in);
  666. }
  667. if (sa->sa_family == AF_INET6) {
  668. salen = sizeof(struct sockaddr_in6);
  669. }
  670. if (getnameinfo(sa, salen,
  671. nodename2, sizeof(nodename2),
  672. NULL, 0, 0) == 0) {
  673. node_pos = nodelist_byname(map, nodename2, 0);
  674. if (node_pos > -1) {
  675. found = 1;
  676. goto out;
  677. }
  678. /* Truncate this name and try again */
  679. dot = strchr(nodename2, '.');
  680. if (dot) {
  681. *dot = '\0';
  682. node_pos = nodelist_byname(map, nodename2, 0);
  683. if (node_pos > -1) {
  684. found = 1;
  685. goto out;
  686. }
  687. }
  688. }
  689. /* See if it's the IP address that's in corosync.conf */
  690. if (getnameinfo(sa, sizeof(*sa),
  691. nodename2, sizeof(nodename2),
  692. NULL, 0, NI_NUMERICHOST))
  693. continue;
  694. node_pos = nodelist_byname(map, nodename2, 0);
  695. if (node_pos > -1) {
  696. found = 1;
  697. goto out;
  698. }
  699. }
  700. out:
  701. if (found) {
  702. freeifaddrs(ifa_list);
  703. goto ret_found;
  704. }
  705. /*
  706. * This section covers the usecase where the nodename specified in cluster.conf
  707. * is an alias specified in /etc/hosts. For example:
  708. * <ipaddr> hostname alias1 alias2
  709. * and <clusternode name="alias2">
  710. * the above calls use uname and getnameinfo does not return aliases.
  711. * here we take the name specified in cluster.conf, resolve it to an address
  712. * and then compare against all known local ip addresses.
  713. * if we have a match, we found our nodename. In theory this chunk of code
  714. * could replace all the checks above, but let's avoid any possible regressions
  715. * and use it as last.
  716. */
  717. iter = icmap_iter_init_r(map, "nodelist.node.");
  718. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  719. char *dbnodename = NULL;
  720. struct addrinfo hints;
  721. struct addrinfo *result = NULL, *rp = NULL;
  722. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, name_str);
  723. if (res != 2) {
  724. continue;
  725. }
  726. /* 'ring0_addr' is allowed as a fallback, but 'name' will be found first
  727. * because the names are in alpha order.
  728. */
  729. if (strcmp(name_str, "name") && strcmp(name_str, "ring0_addr")) {
  730. continue;
  731. }
  732. if (icmap_get_string_r(map, iter_key, &dbnodename) != CS_OK) {
  733. continue;
  734. }
  735. memset(&hints, 0, sizeof(struct addrinfo));
  736. hints.ai_family = AF_UNSPEC;
  737. hints.ai_socktype = SOCK_DGRAM;
  738. hints.ai_flags = 0;
  739. hints.ai_protocol = IPPROTO_UDP;
  740. if (getaddrinfo(dbnodename, NULL, &hints, &result)) {
  741. continue;
  742. }
  743. for (rp = result; rp != NULL; rp = rp->ai_next) {
  744. for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
  745. if (ifa->ifa_addr &&
  746. ipaddr_equal(rp->ai_addr, ifa->ifa_addr)) {
  747. freeaddrinfo(result);
  748. found = 1;
  749. goto out2;
  750. }
  751. }
  752. }
  753. freeaddrinfo(result);
  754. }
  755. out2:
  756. icmap_iter_finalize(iter);
  757. freeifaddrs(ifa_list);
  758. ret_found:
  759. if (found) {
  760. res = icmap_set_uint32_r(map, "nodelist.local_node_pos", node_pos);
  761. }
  762. return node_pos;
  763. }
  764. static enum totem_ip_version_enum totem_config_get_ip_version(struct totem_config *totem_config)
  765. {
  766. enum totem_ip_version_enum res;
  767. char *str;
  768. res = TOTEM_IP_VERSION_6_4;
  769. if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  770. res = TOTEM_IP_VERSION_4;
  771. }
  772. if (icmap_get_string("totem.ip_version", &str) == CS_OK) {
  773. if (strcmp(str, "ipv4") == 0) {
  774. res = TOTEM_IP_VERSION_4;
  775. }
  776. if (strcmp(str, "ipv6") == 0) {
  777. res = TOTEM_IP_VERSION_6;
  778. }
  779. if (strcmp(str, "ipv6-4") == 0) {
  780. res = TOTEM_IP_VERSION_6_4;
  781. }
  782. if (strcmp(str, "ipv4-6") == 0) {
  783. res = TOTEM_IP_VERSION_4_6;
  784. }
  785. free(str);
  786. }
  787. return (res);
  788. }
  789. static uint16_t generate_cluster_id (const char *cluster_name)
  790. {
  791. int i;
  792. int value = 0;
  793. for (i = 0; i < strlen(cluster_name); i++) {
  794. value <<= 1;
  795. value += cluster_name[i];
  796. }
  797. return (value & 0xFFFF);
  798. }
  799. static int get_cluster_mcast_addr (
  800. const char *cluster_name,
  801. unsigned int linknumber,
  802. enum totem_ip_version_enum ip_version,
  803. struct totem_ip_address *res)
  804. {
  805. uint16_t clusterid;
  806. char addr[INET6_ADDRSTRLEN + 1];
  807. int err;
  808. if (cluster_name == NULL) {
  809. return (-1);
  810. }
  811. clusterid = generate_cluster_id(cluster_name) + linknumber;
  812. memset (res, 0, sizeof(*res));
  813. switch (ip_version) {
  814. case TOTEM_IP_VERSION_4:
  815. case TOTEM_IP_VERSION_4_6:
  816. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  817. break;
  818. case TOTEM_IP_VERSION_6:
  819. case TOTEM_IP_VERSION_6_4:
  820. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  821. break;
  822. default:
  823. /*
  824. * Unknown family
  825. */
  826. return (-1);
  827. }
  828. err = totemip_parse (res, addr, ip_version);
  829. return (err);
  830. }
  831. static unsigned int generate_nodeid(
  832. struct totem_config *totem_config,
  833. char *addr)
  834. {
  835. unsigned int nodeid;
  836. struct totem_ip_address totemip;
  837. /* AF_INET hard-coded here because auto-generated nodeids
  838. are only for IPv4 */
  839. if (totemip_parse(&totemip, addr, TOTEM_IP_VERSION_4) != 0)
  840. return -1;
  841. memcpy (&nodeid, &totemip.addr, sizeof (unsigned int));
  842. #if __BYTE_ORDER == __LITTLE_ENDIAN
  843. nodeid = swab32 (nodeid);
  844. #endif
  845. if (totem_config->clear_node_high_bit) {
  846. nodeid &= 0x7FFFFFFF;
  847. }
  848. return nodeid;
  849. }
  850. static int check_for_duplicate_nodeids(
  851. struct totem_config *totem_config,
  852. const char **error_string)
  853. {
  854. icmap_iter_t iter;
  855. icmap_iter_t subiter;
  856. const char *iter_key;
  857. int res = 0;
  858. int retval = 0;
  859. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  860. char *ring0_addr=NULL;
  861. char *ring0_addr1=NULL;
  862. unsigned int node_pos;
  863. unsigned int node_pos1;
  864. unsigned int last_node_pos = -1;
  865. unsigned int nodeid;
  866. unsigned int nodeid1;
  867. int autogenerated;
  868. iter = icmap_iter_init("nodelist.node.");
  869. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  870. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  871. if (res != 2) {
  872. continue;
  873. }
  874. /*
  875. * This relies on the fact the icmap keys are always returned in order
  876. * so all of the keys for a node will be grouped together. We're basically
  877. * just running the code below once for each node.
  878. */
  879. if (last_node_pos == node_pos) {
  880. continue;
  881. }
  882. last_node_pos = node_pos;
  883. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  884. autogenerated = 0;
  885. /* Generated nodeids are only allowed for UDP/UDPU so ring0_addr is valid here */
  886. if (icmap_get_uint32(tmp_key, &nodeid) != CS_OK) {
  887. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  888. if (icmap_get_string(tmp_key, &ring0_addr) != CS_OK) {
  889. continue;
  890. }
  891. /* Generate nodeid so we can check that auto-generated nodeids don't clash either */
  892. nodeid = generate_nodeid(totem_config, ring0_addr);
  893. if (nodeid == -1) {
  894. continue;
  895. }
  896. autogenerated = 1;
  897. }
  898. node_pos1 = 0;
  899. subiter = icmap_iter_init("nodelist.node.");
  900. while (((iter_key = icmap_iter_next(subiter, NULL, NULL)) != NULL) && (node_pos1 < node_pos)) {
  901. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos1, tmp_key);
  902. if ((res != 2) || (node_pos1 >= node_pos)) {
  903. continue;
  904. }
  905. if (strcmp(tmp_key, "nodeid") != 0) {
  906. continue;
  907. }
  908. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos1);
  909. if (icmap_get_uint32(tmp_key, &nodeid1) != CS_OK) {
  910. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos1);
  911. if (icmap_get_string(tmp_key, &ring0_addr1) != CS_OK) {
  912. continue;
  913. }
  914. nodeid1 = generate_nodeid(totem_config, ring0_addr1);
  915. if (nodeid1 == -1) {
  916. continue;
  917. }
  918. }
  919. if (nodeid == nodeid1) {
  920. retval = -1;
  921. snprintf (error_string_response, sizeof(error_string_response),
  922. "Nodeid %u%s%s%s appears twice in corosync.conf", nodeid,
  923. autogenerated?"(autogenerated from ":"",
  924. autogenerated?ring0_addr:"",
  925. autogenerated?")":"");
  926. log_printf (LOGSYS_LEVEL_ERROR, error_string_response);
  927. *error_string = error_string_response;
  928. break;
  929. }
  930. }
  931. icmap_iter_finalize(subiter);
  932. }
  933. icmap_iter_finalize(iter);
  934. return retval;
  935. }
  936. /*
  937. * This needs to be done last of all. It would be nice to do it when reading the
  938. * interface params, but the totem params need to have them to be read first. We
  939. * need both, so this is a way round that circular dependancy.
  940. */
  941. static void calc_knet_ping_timers(struct totem_config *totem_config)
  942. {
  943. char runtime_key_name[ICMAP_KEYNAME_MAXLEN];
  944. int interface;
  945. for (interface = 0; interface < INTERFACE_MAX; interface++) {
  946. if (totem_config->interfaces[interface].configured) {
  947. if (!totem_config->interfaces[interface].knet_pong_count) {
  948. totem_config->interfaces[interface].knet_pong_count = KNET_PONG_COUNT;
  949. }
  950. if (!totem_config->interfaces[interface].knet_ping_timeout) {
  951. totem_config->interfaces[interface].knet_ping_timeout =
  952. totem_config->token_timeout / totem_config->interfaces[interface].knet_pong_count;
  953. }
  954. snprintf(runtime_key_name, sizeof(runtime_key_name),
  955. "runtime.config.totem.interface.%d.knet_ping_timeout", interface);
  956. icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_timeout);
  957. if (!totem_config->interfaces[interface].knet_ping_interval) {
  958. totem_config->interfaces[interface].knet_ping_interval =
  959. totem_config->token_timeout / (totem_config->interfaces[interface].knet_pong_count * 2);
  960. }
  961. snprintf(runtime_key_name, sizeof(runtime_key_name),
  962. "runtime.config.totem.interface.%d.knet_ping_interval", interface);
  963. icmap_set_uint32(runtime_key_name, totem_config->interfaces[interface].knet_ping_interval);
  964. }
  965. }
  966. }
  967. /*
  968. * Compute difference between two set of totem interface arrays and commit it.
  969. * set1 and set2
  970. * are changed so for same ring, ip existing in both set1 and set2 are cleared
  971. * (set to 0), and ips which are only in set1 or set2 remains untouched.
  972. * totempg_node_add/remove is called.
  973. */
  974. static void compute_and_set_totempg_interfaces(struct totem_interface *set1,
  975. struct totem_interface *set2)
  976. {
  977. int ring_no, set1_pos, set2_pos;
  978. struct totem_ip_address empty_ip_address;
  979. memset(&empty_ip_address, 0, sizeof(empty_ip_address));
  980. for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
  981. if (!set1[ring_no].configured && !set2[ring_no].configured) {
  982. continue;
  983. }
  984. for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
  985. for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
  986. /*
  987. * For current ring_no remove all set1 items existing
  988. * in set2
  989. */
  990. if (memcmp(&set1[ring_no].member_list[set1_pos],
  991. &set2[ring_no].member_list[set2_pos],
  992. sizeof(struct totem_ip_address)) == 0) {
  993. memset(&set1[ring_no].member_list[set1_pos], 0,
  994. sizeof(struct totem_ip_address));
  995. memset(&set2[ring_no].member_list[set2_pos], 0,
  996. sizeof(struct totem_ip_address));
  997. }
  998. }
  999. }
  1000. }
  1001. for (ring_no = 0; ring_no < INTERFACE_MAX; ring_no++) {
  1002. for (set1_pos = 0; set1_pos < set1[ring_no].member_count; set1_pos++) {
  1003. /*
  1004. * All items which remain in set1 and don't exist in set2 any more
  1005. * have to be removed.
  1006. */
  1007. if (memcmp(&set1[ring_no].member_list[set1_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
  1008. log_printf(LOGSYS_LEVEL_DEBUG,
  1009. "removing dynamic member %s for ring %u",
  1010. totemip_print(&set1[ring_no].member_list[set1_pos]),
  1011. ring_no);
  1012. totempg_member_remove(&set1[ring_no].member_list[set1_pos], ring_no);
  1013. }
  1014. }
  1015. if (!set2[ring_no].configured) {
  1016. continue;
  1017. }
  1018. for (set2_pos = 0; set2_pos < set2[ring_no].member_count; set2_pos++) {
  1019. /*
  1020. * All items which remain in set2 and don't exist in set1 are new nodes
  1021. * and have to be added.
  1022. */
  1023. if (memcmp(&set2[ring_no].member_list[set2_pos], &empty_ip_address, sizeof(empty_ip_address)) != 0) {
  1024. log_printf(LOGSYS_LEVEL_DEBUG,
  1025. "adding dynamic member %s for ring %u",
  1026. totemip_print(&set2[ring_no].member_list[set2_pos]),
  1027. ring_no);
  1028. totempg_member_add(&set2[ring_no].member_list[set2_pos], ring_no);
  1029. }
  1030. }
  1031. }
  1032. }
  1033. /*
  1034. * Configure parameters for links
  1035. */
  1036. static void configure_link_params(struct totem_config *totem_config, icmap_map_t map)
  1037. {
  1038. int i;
  1039. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  1040. char *addr_string;
  1041. int err;
  1042. int local_node_pos = find_local_node(map, 0);
  1043. for (i = 0; i<INTERFACE_MAX; i++) {
  1044. if (!totem_config->interfaces[i].configured) {
  1045. continue;
  1046. }
  1047. log_printf(LOGSYS_LEVEL_DEBUG, "Configuring link %d params\n", i);
  1048. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring%u_addr", local_node_pos, i);
  1049. if (icmap_get_string_r(map, tmp_key, &addr_string) != CS_OK) {
  1050. continue;
  1051. }
  1052. err = totemip_parse(&totem_config->interfaces[i].local_ip, addr_string, totem_config->ip_version);
  1053. if (err != 0) {
  1054. continue;
  1055. }
  1056. totem_config->interfaces[i].local_ip.nodeid = totem_config->node_id;
  1057. /* In case this is a new link, fill in the defaults if there was no interface{} section for it */
  1058. if (!totem_config->interfaces[i].knet_link_priority)
  1059. totem_config->interfaces[i].knet_link_priority = 1;
  1060. /* knet_ping_interval & knet_ping_timeout are set later once we know all the other params */
  1061. if (!totem_config->interfaces[i].knet_ping_precision)
  1062. totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
  1063. if (!totem_config->interfaces[i].knet_pong_count)
  1064. totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
  1065. if (!totem_config->interfaces[i].knet_transport)
  1066. totem_config->interfaces[i].knet_transport = KNET_TRANSPORT_UDP;
  1067. if (!totem_config->interfaces[i].ip_port)
  1068. totem_config->interfaces[i].ip_port = DEFAULT_PORT + i;
  1069. }
  1070. }
  1071. static void configure_totem_links(struct totem_config *totem_config, icmap_map_t map)
  1072. {
  1073. int i;
  1074. for (i = 0; i<INTERFACE_MAX; i++) {
  1075. if (!totem_config->interfaces[i].configured) {
  1076. continue;
  1077. }
  1078. log_printf(LOGSYS_LEVEL_INFO, "Configuring link %d\n", i);
  1079. totempg_iface_set(&totem_config->interfaces[i].local_ip, totem_config->interfaces[i].ip_port, i);
  1080. }
  1081. }
  1082. /* Check for differences in config that can't be done on-the-fly and print an error */
  1083. static int check_things_have_not_changed(struct totem_config *totem_config, const char **error_string)
  1084. {
  1085. int i,j,k;
  1086. const char *ip_str;
  1087. char addr_buf[INET6_ADDRSTRLEN];
  1088. int changed = 0;
  1089. for (i = 0; i<INTERFACE_MAX; i++) {
  1090. if (totem_config->interfaces[i].configured &&
  1091. totem_config->orig_interfaces[i].configured) {
  1092. if (totem_config->interfaces[i].knet_transport !=
  1093. totem_config->orig_interfaces[i].knet_transport) {
  1094. log_printf(LOGSYS_LEVEL_ERROR,
  1095. "New config has different knet transport for link %d. Internal value was NOT changed.\n", i);
  1096. changed = 1;
  1097. }
  1098. /* Check each nodeid in the new configuration and make sure its IP address on this link has not changed */
  1099. for (j=0; j < totem_config->interfaces[i].member_count; j++) {
  1100. for (k=0; k < totem_config->orig_interfaces[i].member_count; k++) {
  1101. if (totem_config->interfaces[i].member_list[j].nodeid ==
  1102. totem_config->orig_interfaces[i].member_list[k].nodeid) {
  1103. /* Found our nodeid - check the IP address */
  1104. if (memcmp(&totem_config->interfaces[i].member_list[j],
  1105. &totem_config->orig_interfaces[i].member_list[k],
  1106. sizeof(struct totem_ip_address))) {
  1107. ip_str = totemip_print(&totem_config->orig_interfaces[i].member_list[k]);
  1108. /* if ip_str is NULL then the old address was invalid and is allowed to change */
  1109. if (ip_str) {
  1110. strncpy(addr_buf, ip_str, sizeof(addr_buf));
  1111. addr_buf[sizeof(addr_buf) - 1] = '\0';
  1112. log_printf(LOGSYS_LEVEL_ERROR,
  1113. "new config has different address for link %d (addr changed from %s to %s). Internal value was NOT changed.\n",
  1114. i, addr_buf, totemip_print(&totem_config->interfaces[i].member_list[j]));
  1115. changed = 1;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. }
  1122. }
  1123. if (changed) {
  1124. snprintf (error_string_response, sizeof(error_string_response),
  1125. "To reconfigure an interface it must be deleted and recreated. A working interface needs to be available to corosync at all times");
  1126. *error_string = error_string_response;
  1127. return -1;
  1128. }
  1129. return 0;
  1130. }
  1131. static int put_nodelist_members_to_config(struct totem_config *totem_config, icmap_map_t map,
  1132. int reload, const char **error_string)
  1133. {
  1134. icmap_iter_t iter, iter2;
  1135. const char *iter_key, *iter_key2;
  1136. int res = 0;
  1137. unsigned int node_pos;
  1138. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  1139. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  1140. char *node_addr_str;
  1141. int member_count;
  1142. unsigned int linknumber = 0;
  1143. int i, j;
  1144. int last_node_pos = -1;
  1145. /* Clear out nodelist so we can put the new one in if needed */
  1146. for (i = 0; i < INTERFACE_MAX; i++) {
  1147. for (j = 0; j < PROCESSOR_COUNT_MAX; j++) {
  1148. memset(&totem_config->interfaces[i].member_list[j], 0, sizeof(struct totem_ip_address));
  1149. }
  1150. totem_config->interfaces[i].member_count = 0;
  1151. }
  1152. iter = icmap_iter_init_r(map, "nodelist.node.");
  1153. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  1154. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  1155. if (res != 2) {
  1156. continue;
  1157. }
  1158. /* If it's the same as the last node_pos then skip it */
  1159. if (node_pos == last_node_pos) {
  1160. continue;
  1161. }
  1162. last_node_pos = node_pos;
  1163. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  1164. iter2 = icmap_iter_init_r(map, tmp_key);
  1165. while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
  1166. unsigned int nodeid;
  1167. char *str;
  1168. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  1169. if (icmap_get_uint32_r(map, tmp_key, &nodeid) != CS_OK) {
  1170. nodeid = 0;
  1171. }
  1172. res = sscanf(iter_key2, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
  1173. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  1174. continue;
  1175. }
  1176. if (linknumber >= INTERFACE_MAX) {
  1177. snprintf (error_string_response, sizeof(error_string_response),
  1178. "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
  1179. linknumber, INTERFACE_MAX - 1);
  1180. *error_string = error_string_response;
  1181. icmap_iter_finalize(iter2);
  1182. icmap_iter_finalize(iter);
  1183. return (-1);
  1184. }
  1185. if (icmap_get_string_r(map, iter_key2, &node_addr_str) != CS_OK) {
  1186. continue;
  1187. }
  1188. /* Generate nodeids if they are not provided and transport is UDP/U */
  1189. if (!nodeid &&
  1190. (totem_config->transport_number == TOTEM_TRANSPORT_UDP ||
  1191. totem_config->transport_number == TOTEM_TRANSPORT_UDPU)) {
  1192. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  1193. if (icmap_get_string_r(map, tmp_key, &str) == CS_OK) {
  1194. nodeid = generate_nodeid(totem_config, str);
  1195. if (nodeid == -1) {
  1196. sprintf(error_string_response,
  1197. "An IPV6 network requires that a node ID be specified "
  1198. "for address '%s'.", node_addr_str);
  1199. *error_string = error_string_response;
  1200. free(str);
  1201. return (-1);
  1202. }
  1203. log_printf(LOGSYS_LEVEL_DEBUG,
  1204. "Generated nodeid = " CS_PRI_NODE_ID " for %s", nodeid, str);
  1205. free(str);
  1206. }
  1207. }
  1208. member_count = totem_config->interfaces[linknumber].member_count;
  1209. res = totemip_parse(&totem_config->interfaces[linknumber].member_list[member_count],
  1210. node_addr_str, totem_config->ip_version);
  1211. if (res == 0) {
  1212. totem_config->interfaces[linknumber].member_list[member_count].nodeid = nodeid;
  1213. totem_config->interfaces[linknumber].member_count++;
  1214. totem_config->interfaces[linknumber].configured = 1;
  1215. } else {
  1216. sprintf(error_string_response, "failed to parse node address '%s'\n", node_addr_str);
  1217. *error_string = error_string_response;
  1218. memset(&totem_config->interfaces[linknumber].member_list[member_count], 0,
  1219. sizeof(struct totem_ip_address));
  1220. free(node_addr_str);
  1221. icmap_iter_finalize(iter2);
  1222. icmap_iter_finalize(iter);
  1223. return -1;
  1224. }
  1225. free(node_addr_str);
  1226. }
  1227. icmap_iter_finalize(iter2);
  1228. }
  1229. icmap_iter_finalize(iter);
  1230. configure_link_params(totem_config, map);
  1231. if (reload) {
  1232. log_printf(LOGSYS_LEVEL_DEBUG, "About to reconfigure links from nodelist.\n");
  1233. if (check_things_have_not_changed(totem_config, error_string) == -1) {
  1234. return -1;
  1235. }
  1236. }
  1237. return 0;
  1238. }
  1239. static void config_convert_nodelist_to_interface(icmap_map_t map, struct totem_config *totem_config)
  1240. {
  1241. int res = 0;
  1242. int node_pos;
  1243. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  1244. char tmp_key2[ICMAP_KEYNAME_MAXLEN];
  1245. char *node_addr_str;
  1246. unsigned int linknumber = 0;
  1247. icmap_iter_t iter;
  1248. const char *iter_key;
  1249. node_pos = find_local_node(map, 1);
  1250. if (node_pos > -1) {
  1251. /*
  1252. * We found node, so create interface section
  1253. */
  1254. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.", node_pos);
  1255. iter = icmap_iter_init_r(map, tmp_key);
  1256. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  1257. res = sscanf(iter_key, "nodelist.node.%u.ring%u%s", &node_pos, &linknumber, tmp_key2);
  1258. if (res != 3 || strcmp(tmp_key2, "_addr") != 0) {
  1259. continue ;
  1260. }
  1261. if (icmap_get_string_r(map, iter_key, &node_addr_str) != CS_OK) {
  1262. continue;
  1263. }
  1264. snprintf(tmp_key2, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
  1265. icmap_set_string_r(map, tmp_key2, node_addr_str);
  1266. free(node_addr_str);
  1267. }
  1268. icmap_iter_finalize(iter);
  1269. }
  1270. }
  1271. static int get_interface_params(struct totem_config *totem_config, icmap_map_t map,
  1272. const char **error_string, uint64_t *warnings,
  1273. int reload)
  1274. {
  1275. int res = 0;
  1276. unsigned int linknumber = 0;
  1277. int member_count = 0;
  1278. int i;
  1279. icmap_iter_t iter, member_iter;
  1280. const char *iter_key;
  1281. const char *member_iter_key;
  1282. char linknumber_key[ICMAP_KEYNAME_MAXLEN];
  1283. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  1284. uint8_t u8;
  1285. uint32_t u32;
  1286. char *str;
  1287. char *cluster_name = NULL;
  1288. enum totem_ip_version_enum tmp_ip_version = TOTEM_IP_VERSION_4;
  1289. int ret = 0;
  1290. if (reload) {
  1291. for (i=0; i<INTERFACE_MAX; i++) {
  1292. /*
  1293. * Set back to defaults things that might have been configured and
  1294. * now have been taken out of corosync.conf. These won't be caught by the
  1295. * code below which only looks at interface{} sections that actually exist.
  1296. */
  1297. totem_config->interfaces[i].configured = 0;
  1298. totem_config->interfaces[i].knet_ping_timeout = 0;
  1299. totem_config->interfaces[i].knet_ping_interval = 0;
  1300. totem_config->interfaces[i].knet_ping_precision = KNET_PING_PRECISION;
  1301. totem_config->interfaces[i].knet_pong_count = KNET_PONG_COUNT;
  1302. }
  1303. }
  1304. if (icmap_get_string_r(map, "totem.cluster_name", &cluster_name) != CS_OK) {
  1305. cluster_name = NULL;
  1306. }
  1307. iter = icmap_iter_init_r(map, "totem.interface.");
  1308. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  1309. res = sscanf(iter_key, "totem.interface.%[^.].%s", linknumber_key, tmp_key);
  1310. if (res != 2) {
  1311. continue;
  1312. }
  1313. if (strcmp(tmp_key, "bindnetaddr") != 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  1314. continue;
  1315. }
  1316. member_count = 0;
  1317. linknumber = atoi(linknumber_key);
  1318. if (linknumber >= INTERFACE_MAX) {
  1319. snprintf (error_string_response, sizeof(error_string_response),
  1320. "parse error in config: interface ring number %u is bigger than allowed maximum %u\n",
  1321. linknumber, INTERFACE_MAX - 1);
  1322. *error_string = error_string_response;
  1323. ret = -1;
  1324. goto out;
  1325. }
  1326. /* These things are only valid for the initial read */
  1327. if (!reload) {
  1328. /*
  1329. * Get the bind net address
  1330. */
  1331. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.bindnetaddr", linknumber);
  1332. if (icmap_get_string_r(map, tmp_key, &str) == CS_OK) {
  1333. res = totemip_parse (&totem_config->interfaces[linknumber].bindnet, str,
  1334. totem_config->ip_version);
  1335. if (res) {
  1336. sprintf(error_string_response, "failed to parse bindnet address '%s'\n", str);
  1337. *error_string = error_string_response;
  1338. free(str);
  1339. ret = -1;
  1340. goto out;
  1341. }
  1342. free(str);
  1343. }
  1344. /*
  1345. * Get interface multicast address
  1346. */
  1347. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", linknumber);
  1348. if (icmap_get_string_r(map, tmp_key, &str) == CS_OK) {
  1349. res = totemip_parse (&totem_config->interfaces[linknumber].mcast_addr, str,
  1350. totem_config->ip_version);
  1351. if (res) {
  1352. sprintf(error_string_response, "failed to parse mcast address '%s'\n", str);
  1353. *error_string = error_string_response;
  1354. free(str);
  1355. ret = -1;
  1356. goto out;
  1357. }
  1358. free(str);
  1359. } else if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  1360. /*
  1361. * User not specified address -> autogenerate one from cluster_name key
  1362. * (if available). Return code is intentionally ignored, because
  1363. * udpu doesn't need mcastaddr and validity of mcastaddr for udp is
  1364. * checked later anyway.
  1365. */
  1366. if (totem_config->interfaces[0].bindnet.family == AF_INET) {
  1367. tmp_ip_version = TOTEM_IP_VERSION_4;
  1368. } else if (totem_config->interfaces[0].bindnet.family == AF_INET6) {
  1369. tmp_ip_version = TOTEM_IP_VERSION_6;
  1370. }
  1371. (void)get_cluster_mcast_addr (cluster_name,
  1372. linknumber,
  1373. tmp_ip_version,
  1374. &totem_config->interfaces[linknumber].mcast_addr);
  1375. }
  1376. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", linknumber);
  1377. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  1378. if (strcmp (str, "yes") == 0) {
  1379. totem_config->broadcast_use = 1;
  1380. }
  1381. free(str);
  1382. }
  1383. }
  1384. /* These things are only valid for the initial read OR a newly-defined link */
  1385. if (!reload || (totem_config->interfaces[linknumber].configured == 0)) {
  1386. /*
  1387. * Get mcast port
  1388. */
  1389. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", linknumber);
  1390. if (icmap_get_uint16_r(map, tmp_key, &totem_config->interfaces[linknumber].ip_port) != CS_OK) {
  1391. if (totem_config->broadcast_use) {
  1392. totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + (2 * linknumber);
  1393. } else {
  1394. totem_config->interfaces[linknumber].ip_port = DEFAULT_PORT + linknumber;
  1395. }
  1396. }
  1397. /*
  1398. * Get the TTL
  1399. */
  1400. totem_config->interfaces[linknumber].ttl = 1;
  1401. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", linknumber);
  1402. if (icmap_get_uint8_r(map, tmp_key, &u8) == CS_OK) {
  1403. totem_config->interfaces[linknumber].ttl = u8;
  1404. }
  1405. totem_config->interfaces[linknumber].knet_transport = KNET_DEFAULT_TRANSPORT;
  1406. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_transport", linknumber);
  1407. if (icmap_get_string_r(map, tmp_key, &str) == CS_OK) {
  1408. if (strcmp(str, "sctp") == 0) {
  1409. totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_SCTP;
  1410. }
  1411. else if (strcmp(str, "udp") == 0) {
  1412. totem_config->interfaces[linknumber].knet_transport = KNET_TRANSPORT_UDP;
  1413. }
  1414. else {
  1415. *error_string = "Unrecognised knet_transport. expected 'udp' or 'sctp'";
  1416. ret = -1;
  1417. goto out;
  1418. }
  1419. }
  1420. }
  1421. totem_config->interfaces[linknumber].configured = 1;
  1422. /*
  1423. * Get the knet link params
  1424. */
  1425. totem_config->interfaces[linknumber].knet_link_priority = 1;
  1426. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_link_priority", linknumber);
  1427. if (icmap_get_uint8_r(map, tmp_key, &u8) == CS_OK) {
  1428. totem_config->interfaces[linknumber].knet_link_priority = u8;
  1429. }
  1430. totem_config->interfaces[linknumber].knet_ping_interval = 0; /* real default applied later */
  1431. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_interval", linknumber);
  1432. if (icmap_get_uint32_r(map, tmp_key, &u32) == CS_OK) {
  1433. totem_config->interfaces[linknumber].knet_ping_interval = u32;
  1434. }
  1435. totem_config->interfaces[linknumber].knet_ping_timeout = 0; /* real default applied later */
  1436. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_timeout", linknumber);
  1437. if (icmap_get_uint32_r(map, tmp_key, &u32) == CS_OK) {
  1438. totem_config->interfaces[linknumber].knet_ping_timeout = u32;
  1439. }
  1440. totem_config->interfaces[linknumber].knet_ping_precision = KNET_PING_PRECISION;
  1441. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_ping_precision", linknumber);
  1442. if (icmap_get_uint32_r(map, tmp_key, &u32) == CS_OK) {
  1443. totem_config->interfaces[linknumber].knet_ping_precision = u32;
  1444. }
  1445. totem_config->interfaces[linknumber].knet_pong_count = KNET_PONG_COUNT;
  1446. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.knet_pong_count", linknumber);
  1447. if (icmap_get_uint32_r(map, tmp_key, &u32) == CS_OK) {
  1448. totem_config->interfaces[linknumber].knet_pong_count = u32;
  1449. }
  1450. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", linknumber);
  1451. member_iter = icmap_iter_init_r(map, tmp_key);
  1452. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  1453. if (member_count == 0) {
  1454. if (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK) {
  1455. free(str);
  1456. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_IGNORED;
  1457. break;
  1458. } else {
  1459. *warnings |= TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED;
  1460. }
  1461. }
  1462. if (icmap_get_string_r(map, member_iter_key, &str) == CS_OK) {
  1463. res = totemip_parse (&totem_config->interfaces[linknumber].member_list[member_count++],
  1464. str, totem_config->ip_version);
  1465. if (res) {
  1466. sprintf(error_string_response, "failed to parse node address '%s'\n", str);
  1467. *error_string = error_string_response;
  1468. icmap_iter_finalize(member_iter);
  1469. free(str);
  1470. ret = -1;
  1471. goto out;
  1472. }
  1473. free(str);
  1474. }
  1475. }
  1476. icmap_iter_finalize(member_iter);
  1477. totem_config->interfaces[linknumber].member_count = member_count;
  1478. }
  1479. out:
  1480. icmap_iter_finalize(iter);
  1481. free(cluster_name);
  1482. return (ret);
  1483. }
  1484. extern int totem_config_read (
  1485. struct totem_config *totem_config,
  1486. const char **error_string,
  1487. uint64_t *warnings)
  1488. {
  1489. int res = 0;
  1490. char *str, *ring0_addr_str;
  1491. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  1492. uint16_t u16;
  1493. int i;
  1494. int local_node_pos;
  1495. int nodeid_set;
  1496. *warnings = 0;
  1497. memset (totem_config, 0, sizeof (struct totem_config));
  1498. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  1499. if (totem_config->interfaces == 0) {
  1500. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  1501. return -1;
  1502. }
  1503. totem_config->transport_number = TOTEM_TRANSPORT_KNET;
  1504. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  1505. if (strcmp (str, "udpu") == 0) {
  1506. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  1507. } else if (strcmp (str, "udp") == 0) {
  1508. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  1509. } else if (strcmp (str, "knet") == 0) {
  1510. totem_config->transport_number = TOTEM_TRANSPORT_KNET;
  1511. } else {
  1512. *error_string = "Invalid transport type. Should be udpu, udp or knet";
  1513. free(str);
  1514. return -1;
  1515. }
  1516. free(str);
  1517. }
  1518. memset (totem_config->interfaces, 0,
  1519. sizeof (struct totem_interface) * INTERFACE_MAX);
  1520. strcpy (totem_config->link_mode, "passive");
  1521. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  1522. /* initial crypto load */
  1523. if (totem_get_crypto(totem_config, icmap_get_global_map(), error_string) != 0) {
  1524. return -1;
  1525. }
  1526. if (totem_config_keyread(totem_config, icmap_get_global_map(), error_string) != 0) {
  1527. return -1;
  1528. }
  1529. totem_config->crypto_index = 1;
  1530. totem_config->crypto_changed = 0;
  1531. if (icmap_get_string("totem.link_mode", &str) == CS_OK) {
  1532. if (strlen(str) >= TOTEM_LINK_MODE_BYTES) {
  1533. *error_string = "totem.link_mode is too long";
  1534. free(str);
  1535. return -1;
  1536. }
  1537. strcpy (totem_config->link_mode, str);
  1538. free(str);
  1539. }
  1540. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  1541. totem_config->clear_node_high_bit = 0;
  1542. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  1543. if (strcmp (str, "yes") == 0) {
  1544. totem_config->clear_node_high_bit = 1;
  1545. }
  1546. free(str);
  1547. }
  1548. icmap_get_uint32("totem.threads", &totem_config->threads);
  1549. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  1550. totem_config->ip_version = totem_config_get_ip_version(totem_config);
  1551. if (icmap_get_string("totem.interface.0.bindnetaddr", &str) != CS_OK) {
  1552. /*
  1553. * We were not able to find ring 0 bindnet addr. Try to use nodelist informations
  1554. */
  1555. config_convert_nodelist_to_interface(icmap_get_global_map(), totem_config);
  1556. } else {
  1557. if (icmap_get_string("nodelist.node.0.ring0_addr", &ring0_addr_str) == CS_OK) {
  1558. /*
  1559. * Both bindnetaddr and ring0_addr are set.
  1560. * Log warning information, and use nodelist instead
  1561. */
  1562. *warnings |= TOTEM_CONFIG_BINDNETADDR_NODELIST_SET;
  1563. config_convert_nodelist_to_interface(icmap_get_global_map(), totem_config);
  1564. free(ring0_addr_str);
  1565. }
  1566. free(str);
  1567. }
  1568. /*
  1569. * Broadcast option is global but set in interface section,
  1570. * so reset before processing interfaces.
  1571. */
  1572. totem_config->broadcast_use = 0;
  1573. res = get_interface_params(totem_config, icmap_get_global_map(), error_string, warnings, 0);
  1574. if (res < 0) {
  1575. return res;
  1576. }
  1577. /*
  1578. * Use broadcast is global, so if set, make sure to fill mcast addr correctly
  1579. * broadcast is only supported for UDP so just do interface 0;
  1580. */
  1581. if (totem_config->broadcast_use) {
  1582. totemip_parse (&totem_config->interfaces[0].mcast_addr,
  1583. "255.255.255.255", TOTEM_IP_VERSION_4);
  1584. }
  1585. /*
  1586. * Store automatically generated items back to icmap only for UDP
  1587. */
  1588. if (totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  1589. for (i = 0; i < INTERFACE_MAX; i++) {
  1590. if (!totem_config->interfaces[i].configured) {
  1591. continue;
  1592. }
  1593. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  1594. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  1595. free(str);
  1596. } else {
  1597. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  1598. icmap_set_string(tmp_key, str);
  1599. }
  1600. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  1601. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  1602. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  1603. }
  1604. }
  1605. }
  1606. /*
  1607. * Check existence of nodelist
  1608. */
  1609. if ((icmap_get_string("nodelist.node.0.name", &str) == CS_OK) ||
  1610. (icmap_get_string("nodelist.node.0.ring0_addr", &str) == CS_OK)) {
  1611. free(str);
  1612. /*
  1613. * find local node
  1614. */
  1615. local_node_pos = find_local_node(icmap_get_global_map(), 1);
  1616. if (local_node_pos != -1) {
  1617. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", local_node_pos);
  1618. nodeid_set = (totem_config->node_id != 0);
  1619. if (icmap_get_uint32(tmp_key, &totem_config->node_id) == CS_OK && nodeid_set) {
  1620. *warnings |= TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED;
  1621. }
  1622. if ((totem_config->transport_number == TOTEM_TRANSPORT_KNET) && (!totem_config->node_id)) {
  1623. *error_string = "Knet requires an explicit nodeid for the local node";
  1624. return -1;
  1625. }
  1626. if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP ||
  1627. totem_config->transport_number == TOTEM_TRANSPORT_UDPU) && (!totem_config->node_id)) {
  1628. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", local_node_pos);
  1629. icmap_get_string(tmp_key, &str);
  1630. totem_config->node_id = generate_nodeid(totem_config, str);
  1631. if (totem_config->node_id == -1) {
  1632. *error_string = "An IPV6 network requires that a node ID be specified";
  1633. free(str);
  1634. return (-1);
  1635. }
  1636. totem_config->interfaces[0].member_list[local_node_pos].nodeid = totem_config->node_id;
  1637. free(str);
  1638. }
  1639. /* Users must not change this */
  1640. icmap_set_ro_access("nodelist.local_node_pos", 0, 1);
  1641. }
  1642. if (put_nodelist_members_to_config(totem_config, icmap_get_global_map(), 0, error_string)) {
  1643. return -1;
  1644. }
  1645. }
  1646. /*
  1647. * Get things that might change in the future (and can depend on totem_config->interfaces);
  1648. */
  1649. totem_volatile_config_read(totem_config, icmap_get_global_map(), NULL);
  1650. calc_knet_ping_timers(totem_config);
  1651. /* This is now done in the totemknet interface callback */
  1652. /* configure_totem_links(totem_config, icmap_get_global_map()); */
  1653. add_totem_config_notification(totem_config);
  1654. return 0;
  1655. }
  1656. int totem_config_validate (
  1657. struct totem_config *totem_config,
  1658. const char **error_string)
  1659. {
  1660. static char local_error_reason[512];
  1661. char parse_error[512];
  1662. const char *error_reason = local_error_reason;
  1663. int i;
  1664. uint32_t u32;
  1665. int num_configured = 0;
  1666. unsigned int interface_max = INTERFACE_MAX;
  1667. for (i = 0; i < INTERFACE_MAX; i++) {
  1668. if (totem_config->interfaces[i].configured) {
  1669. num_configured++;
  1670. }
  1671. }
  1672. if (num_configured == 0) {
  1673. error_reason = "No interfaces defined";
  1674. goto parse_error;
  1675. }
  1676. /* Check we found a local node name */
  1677. if (icmap_get_uint32("nodelist.local_node_pos", &u32) != CS_OK) {
  1678. error_reason = "No valid name found for local host";
  1679. goto parse_error;
  1680. }
  1681. for (i = 0; i < INTERFACE_MAX; i++) {
  1682. /*
  1683. * Some error checking of parsed data to make sure its valid
  1684. */
  1685. struct totem_ip_address null_addr;
  1686. if (!totem_config->interfaces[i].configured) {
  1687. continue;
  1688. }
  1689. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  1690. if ((totem_config->transport_number == TOTEM_TRANSPORT_UDP) &&
  1691. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  1692. sizeof (struct totem_ip_address)) == 0) {
  1693. snprintf (local_error_reason, sizeof(local_error_reason),
  1694. "No multicast address specified for interface %u", i);
  1695. goto parse_error;
  1696. }
  1697. if (totem_config->interfaces[i].ip_port == 0) {
  1698. snprintf (local_error_reason, sizeof(local_error_reason),
  1699. "No multicast port specified for interface %u", i);
  1700. goto parse_error;
  1701. }
  1702. if (totem_config->interfaces[i].ttl > 255) {
  1703. snprintf (local_error_reason, sizeof(local_error_reason),
  1704. "Invalid TTL (should be 0..255) for interface %u", i);
  1705. goto parse_error;
  1706. }
  1707. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  1708. totem_config->interfaces[i].ttl != 1) {
  1709. snprintf (local_error_reason, sizeof(local_error_reason),
  1710. "Can only set ttl on multicast transport types for interface %u", i);
  1711. goto parse_error;
  1712. }
  1713. if (totem_config->interfaces[i].knet_link_priority > 255) {
  1714. snprintf (local_error_reason, sizeof(local_error_reason),
  1715. "Invalid link priority (should be 0..255) for interface %u", i);
  1716. goto parse_error;
  1717. }
  1718. if (totem_config->transport_number != TOTEM_TRANSPORT_KNET &&
  1719. totem_config->interfaces[i].knet_link_priority != 1) {
  1720. snprintf (local_error_reason, sizeof(local_error_reason),
  1721. "Can only set link priority on knet transport type for interface %u", i);
  1722. goto parse_error;
  1723. }
  1724. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  1725. totem_config->node_id == 0) {
  1726. snprintf (local_error_reason, sizeof(local_error_reason),
  1727. "An IPV6 network requires that a node ID be specified for interface %u", i);
  1728. goto parse_error;
  1729. }
  1730. if (totem_config->broadcast_use == 0 && totem_config->transport_number == TOTEM_TRANSPORT_UDP) {
  1731. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  1732. snprintf (local_error_reason, sizeof(local_error_reason),
  1733. "Multicast address family does not match bind address family for interface %u", i);
  1734. goto parse_error;
  1735. }
  1736. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  1737. snprintf (local_error_reason, sizeof(local_error_reason),
  1738. "mcastaddr is not a correct multicast address for interface %u", i);
  1739. goto parse_error;
  1740. }
  1741. }
  1742. }
  1743. if (totem_config->version != 2) {
  1744. error_reason = "This totem parser can only parse version 2 configurations.";
  1745. goto parse_error;
  1746. }
  1747. if (totem_volatile_config_validate(totem_config, icmap_get_global_map(), error_string) == -1) {
  1748. return (-1);
  1749. }
  1750. if (check_for_duplicate_nodeids(totem_config, error_string) == -1) {
  1751. return (-1);
  1752. }
  1753. /*
  1754. * KNET Link values validation
  1755. */
  1756. if (strcmp (totem_config->link_mode, "active") &&
  1757. strcmp (totem_config->link_mode, "rr") &&
  1758. strcmp (totem_config->link_mode, "passive")) {
  1759. snprintf (local_error_reason, sizeof(local_error_reason),
  1760. "The Knet link mode \"%s\" specified is invalid. It must be active, passive or rr.\n", totem_config->link_mode);
  1761. goto parse_error;
  1762. }
  1763. /* Only Knet does multiple interfaces */
  1764. if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
  1765. interface_max = 1;
  1766. }
  1767. if (interface_max < num_configured) {
  1768. snprintf (parse_error, sizeof(parse_error),
  1769. "%d is too many configured interfaces for non-Knet transport.",
  1770. num_configured);
  1771. error_reason = parse_error;
  1772. goto parse_error;
  1773. }
  1774. /* Only knet allows crypto */
  1775. if (totem_config->transport_number != TOTEM_TRANSPORT_KNET) {
  1776. if ((strcmp(totem_config->crypto_cipher_type, "none") != 0) ||
  1777. (strcmp(totem_config->crypto_hash_type, "none") != 0)) {
  1778. snprintf (parse_error, sizeof(parse_error),
  1779. "crypto_cipher & crypto_hash are only valid for the Knet transport.");
  1780. error_reason = parse_error;
  1781. goto parse_error;
  1782. }
  1783. }
  1784. if (totem_config->net_mtu == 0) {
  1785. if (totem_config->transport_number == TOTEM_TRANSPORT_KNET) {
  1786. totem_config->net_mtu = KNET_MAX_PACKET_SIZE;
  1787. }
  1788. else {
  1789. totem_config->net_mtu = 1500;
  1790. }
  1791. }
  1792. return 0;
  1793. parse_error:
  1794. snprintf (error_string_response, sizeof(error_string_response),
  1795. "parse error in config: %s\n", error_reason);
  1796. *error_string = error_string_response;
  1797. return (-1);
  1798. }
  1799. static int read_keyfile (
  1800. const char *key_location,
  1801. struct totem_config *totem_config,
  1802. const char **error_string)
  1803. {
  1804. int fd;
  1805. int res;
  1806. int saved_errno;
  1807. char error_str[100];
  1808. const char *error_ptr;
  1809. fd = open (key_location, O_RDONLY);
  1810. if (fd == -1) {
  1811. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  1812. snprintf (error_string_response, sizeof(error_string_response),
  1813. "Could not open %s: %s\n",
  1814. key_location, error_ptr);
  1815. goto parse_error;
  1816. }
  1817. res = read (fd, totem_config->private_key, TOTEM_PRIVATE_KEY_LEN_MAX);
  1818. saved_errno = errno;
  1819. close (fd);
  1820. if (res == -1) {
  1821. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  1822. snprintf (error_string_response, sizeof(error_string_response),
  1823. "Could not read %s: %s\n",
  1824. key_location, error_ptr);
  1825. goto parse_error;
  1826. }
  1827. if (res < TOTEM_PRIVATE_KEY_LEN_MIN) {
  1828. snprintf (error_string_response, sizeof(error_string_response),
  1829. "Could only read %d bits of minimum %u bits from %s.\n",
  1830. res * 8, TOTEM_PRIVATE_KEY_LEN_MIN * 8, key_location);
  1831. goto parse_error;
  1832. }
  1833. totem_config->private_key_len = res;
  1834. return 0;
  1835. parse_error:
  1836. *error_string = error_string_response;
  1837. return (-1);
  1838. }
  1839. int totem_config_keyread (
  1840. struct totem_config *totem_config,
  1841. icmap_map_t map,
  1842. const char **error_string)
  1843. {
  1844. int got_key = 0;
  1845. char *key_location = NULL;
  1846. int res;
  1847. size_t key_len;
  1848. char old_key[TOTEM_PRIVATE_KEY_LEN_MAX];
  1849. size_t old_key_len;
  1850. /* Take a copy so we can see if it has changed */
  1851. memcpy(old_key, totem_config->private_key, sizeof(totem_config->private_key));
  1852. old_key_len = totem_config->private_key_len;
  1853. memset (totem_config->private_key, 0, sizeof(totem_config->private_key));
  1854. totem_config->private_key_len = 0;
  1855. if (strcmp(totem_config->crypto_cipher_type, "none") == 0 &&
  1856. strcmp(totem_config->crypto_hash_type, "none") == 0) {
  1857. return (0);
  1858. }
  1859. /* cmap may store the location of the key file */
  1860. if (icmap_get_string_r(map, "totem.keyfile", &key_location) == CS_OK) {
  1861. res = read_keyfile(key_location, totem_config, error_string);
  1862. free(key_location);
  1863. if (res) {
  1864. goto key_error;
  1865. }
  1866. got_key = 1;
  1867. } else { /* Or the key itself may be in the cmap */
  1868. if (icmap_get_r(map, "totem.key", NULL, &key_len, NULL) == CS_OK) {
  1869. if (key_len > sizeof(totem_config->private_key)) {
  1870. sprintf(error_string_response, "key is too long");
  1871. goto key_error;
  1872. }
  1873. if (key_len < TOTEM_PRIVATE_KEY_LEN_MIN) {
  1874. sprintf(error_string_response, "key is too short");
  1875. goto key_error;
  1876. }
  1877. if (icmap_get_r(map, "totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  1878. totem_config->private_key_len = key_len;
  1879. got_key = 1;
  1880. } else {
  1881. sprintf(error_string_response, "can't load private key");
  1882. goto key_error;
  1883. }
  1884. }
  1885. }
  1886. /* In desperation we read the default filename */
  1887. if (!got_key) {
  1888. res = read_keyfile(COROSYSCONFDIR "/authkey", totem_config, error_string);
  1889. if (res)
  1890. goto key_error;
  1891. }
  1892. if (old_key_len != totem_config->private_key_len ||
  1893. memcmp(old_key, totem_config->private_key, sizeof(totem_config->private_key))) {
  1894. totem_config->crypto_changed = 1;
  1895. }
  1896. return (0);
  1897. key_error:
  1898. *error_string = error_string_response;
  1899. return (-1);
  1900. }
  1901. int totem_reread_crypto_config(struct totem_config *totem_config, icmap_map_t map, const char **error_string)
  1902. {
  1903. if (totem_get_crypto(totem_config, map, error_string) != 0) {
  1904. return -1;
  1905. }
  1906. if (totem_config_keyread(totem_config, map, error_string) != 0) {
  1907. return -1;
  1908. }
  1909. return 0;
  1910. }
  1911. static void debug_dump_totem_config(const struct totem_config *totem_config)
  1912. {
  1913. log_printf(LOGSYS_LEVEL_DEBUG, "Token Timeout (%d ms) retransmit timeout (%d ms)",
  1914. totem_config->token_timeout, totem_config->token_retransmit_timeout);
  1915. if (totem_config->token_warning) {
  1916. uint32_t token_warning_ms = totem_config->token_warning * totem_config->token_timeout / 100;
  1917. log_printf(LOGSYS_LEVEL_DEBUG, "Token warning every %d ms (%d%% of Token Timeout)",
  1918. token_warning_ms, totem_config->token_warning);
  1919. if (token_warning_ms < totem_config->token_retransmit_timeout)
  1920. log_printf (LOGSYS_LEVEL_DEBUG,
  1921. "The token warning interval (%d ms) is less than the token retransmit timeout (%d ms) "
  1922. "which can lead to spurious token warnings. Consider increasing the token_warning parameter.",
  1923. token_warning_ms, totem_config->token_retransmit_timeout);
  1924. } else
  1925. log_printf(LOGSYS_LEVEL_DEBUG, "Token warnings disabled");
  1926. log_printf(LOGSYS_LEVEL_DEBUG, "token hold (%d ms) retransmits before loss (%d retrans)",
  1927. totem_config->token_hold_timeout, totem_config->token_retransmits_before_loss_const);
  1928. log_printf(LOGSYS_LEVEL_DEBUG, "join (%d ms) send_join (%d ms) consensus (%d ms) merge (%d ms)",
  1929. totem_config->join_timeout, totem_config->send_join_timeout, totem_config->consensus_timeout,
  1930. totem_config->merge_timeout);
  1931. log_printf(LOGSYS_LEVEL_DEBUG, "downcheck (%d ms) fail to recv const (%d msgs)",
  1932. totem_config->downcheck_timeout, totem_config->fail_to_recv_const);
  1933. log_printf(LOGSYS_LEVEL_DEBUG,
  1934. "seqno unchanged const (%d rotations) Maximum network MTU %d",
  1935. totem_config->seqno_unchanged_const, totem_config->net_mtu);
  1936. log_printf(LOGSYS_LEVEL_DEBUG,
  1937. "window size per rotation (%d messages) maximum messages per rotation (%d messages)",
  1938. totem_config->window_size, totem_config->max_messages);
  1939. log_printf(LOGSYS_LEVEL_DEBUG, "missed count const (%d messages)", totem_config->miss_count_const);
  1940. log_printf(LOGSYS_LEVEL_DEBUG, "heartbeat_failures_allowed (%d)",
  1941. totem_config->heartbeat_failures_allowed);
  1942. log_printf(LOGSYS_LEVEL_DEBUG, "max_network_delay (%d ms)", totem_config->max_network_delay);
  1943. }
  1944. static void totem_change_notify(
  1945. int32_t event,
  1946. const char *key_name,
  1947. struct icmap_notify_value new_val,
  1948. struct icmap_notify_value old_val,
  1949. void *user_data)
  1950. {
  1951. struct totem_config *totem_config = (struct totem_config *)user_data;
  1952. uint32_t *param;
  1953. uint8_t reloading;
  1954. const char *deleted_key = NULL;
  1955. const char *error_string;
  1956. /*
  1957. * If a full reload is in progress then don't do anything until it's done and
  1958. * can reconfigure it all atomically
  1959. */
  1960. if (icmap_get_uint8("config.reload_in_progress", &reloading) == CS_OK && reloading)
  1961. return;
  1962. param = totem_get_param_by_name((struct totem_config *)user_data, key_name);
  1963. /*
  1964. * Process change only if changed key is found in totem_config (-> param is not NULL)
  1965. * or for special key token_coefficient. token_coefficient key is not stored in
  1966. * totem_config, but it is used for computation of token timeout.
  1967. */
  1968. if (!param && strcmp(key_name, "totem.token_coefficient") != 0)
  1969. return;
  1970. /*
  1971. * Values other than UINT32 are not supported, or needed (yet)
  1972. */
  1973. switch (event) {
  1974. case ICMAP_TRACK_DELETE:
  1975. deleted_key = key_name;
  1976. break;
  1977. case ICMAP_TRACK_ADD:
  1978. case ICMAP_TRACK_MODIFY:
  1979. deleted_key = NULL;
  1980. break;
  1981. default:
  1982. break;
  1983. }
  1984. totem_volatile_config_read (totem_config, icmap_get_global_map(), deleted_key);
  1985. log_printf(LOGSYS_LEVEL_DEBUG, "Totem related config key changed. Dumping actual totem config.");
  1986. debug_dump_totem_config(totem_config);
  1987. if (totem_volatile_config_validate(totem_config, icmap_get_global_map(), &error_string) == -1) {
  1988. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1989. /*
  1990. * TODO: Consider corosync exit and/or load defaults for volatile
  1991. * values. For now, log error seems to be enough
  1992. */
  1993. }
  1994. }
  1995. int totemconfig_configure_new_params(
  1996. struct totem_config *totem_config,
  1997. icmap_map_t map,
  1998. const char **error_string)
  1999. {
  2000. uint64_t warnings = 0LL;
  2001. get_interface_params(totem_config, map, error_string, &warnings, 1);
  2002. if (put_nodelist_members_to_config (totem_config, map, 1, error_string)) {
  2003. return -1;
  2004. }
  2005. calc_knet_ping_timers(totem_config);
  2006. log_printf(LOGSYS_LEVEL_DEBUG, "Configuration reloaded. Dumping actual totem config.");
  2007. debug_dump_totem_config(totem_config);
  2008. /* Reinstate the local_node_pos */
  2009. (void)find_local_node(map, 0);
  2010. return 0;
  2011. }
  2012. void totemconfig_commit_new_params(
  2013. struct totem_config *totem_config,
  2014. icmap_map_t map)
  2015. {
  2016. struct totem_interface *new_interfaces = NULL;
  2017. new_interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  2018. assert(new_interfaces != NULL);
  2019. memcpy(new_interfaces, totem_config->interfaces, sizeof (struct totem_interface) * INTERFACE_MAX);
  2020. /* Set link parameters including local_ip */
  2021. configure_totem_links(totem_config, map);
  2022. /* Add & remove nodes */
  2023. compute_and_set_totempg_interfaces(totem_config->orig_interfaces, new_interfaces);
  2024. /* Does basic global params (like compression) */
  2025. totempg_reconfigure();
  2026. free(new_interfaces);
  2027. }
  2028. static void add_totem_config_notification(struct totem_config *totem_config)
  2029. {
  2030. icmap_track_t icmap_track;
  2031. icmap_track_add("totem.",
  2032. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  2033. totem_change_notify,
  2034. totem_config,
  2035. &icmap_track);
  2036. }