totemconfig.c 64 KB

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