totemconfig.c 68 KB

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