totemconfig.c 63 KB

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