totemconfig.c 61 KB

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