4
0

totemconfig.c 67 KB

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