totemconfig.c 75 KB

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