4
0

totemconfig.c 73 KB

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