totemconfig.c 75 KB

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