totemconfig.c 75 KB

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