totemknet.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  1. /*
  2. * Copyright (c) 2016-2022 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <sys/mman.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/socket.h>
  39. #include <netdb.h>
  40. #include <sys/un.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/param.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <pthread.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <sys/uio.h>
  56. #include <limits.h>
  57. #include <qb/qbdefs.h>
  58. #include <qb/qbloop.h>
  59. #ifdef HAVE_LIBNOZZLE
  60. #include <libgen.h>
  61. #include <net/ethernet.h>
  62. #include <libnozzle.h>
  63. #endif
  64. #include <corosync/sq.h>
  65. #include <corosync/swab.h>
  66. #include <corosync/logsys.h>
  67. #include <corosync/icmap.h>
  68. #include <corosync/totem/totemip.h>
  69. #include "totemknet.h"
  70. #include "main.h"
  71. #include "util.h"
  72. #include <libknet.h>
  73. #include <corosync/totem/totemstats.h>
  74. #ifndef MSG_NOSIGNAL
  75. #define MSG_NOSIGNAL 0
  76. #endif
  77. #ifdef HAVE_LIBNOZZLE
  78. static int setup_nozzle(void *knet_context);
  79. #endif
  80. /* Should match that used by cfg */
  81. #define CFG_INTERFACE_STATUS_MAX_LEN 512
  82. struct totemknet_instance {
  83. struct crypto_instance *crypto_inst;
  84. struct knet_handle_crypto_cfg last_good_crypto_cfg;
  85. qb_loop_t *poll_handle;
  86. knet_handle_t knet_handle;
  87. int link_mode;
  88. void *context;
  89. int (*totemknet_deliver_fn) (
  90. void *context,
  91. const void *msg,
  92. unsigned int msg_len,
  93. const struct sockaddr_storage *system_from);
  94. int (*totemknet_iface_change_fn) (
  95. void *context,
  96. const struct totem_ip_address *iface_address,
  97. unsigned int link_no);
  98. void (*totemknet_mtu_changed) (
  99. void *context,
  100. int net_mtu);
  101. void (*totemknet_target_set_completed) (void *context);
  102. /*
  103. * Function and data used to log messages
  104. */
  105. int totemknet_log_level_security;
  106. int totemknet_log_level_error;
  107. int totemknet_log_level_warning;
  108. int totemknet_log_level_notice;
  109. int totemknet_log_level_debug;
  110. int totemknet_subsys_id;
  111. int knet_subsys_id;
  112. void (*totemknet_log_printf) (
  113. int level,
  114. int subsys,
  115. const char *function,
  116. const char *file,
  117. int line,
  118. const char *format,
  119. ...)__attribute__((format(printf, 6, 7)));
  120. void *knet_context;
  121. char iov_buffer[KNET_MAX_PACKET_SIZE];
  122. char *link_status[INTERFACE_MAX];
  123. struct totem_ip_address my_ids[INTERFACE_MAX];
  124. uint16_t ip_port[INTERFACE_MAX];
  125. int our_nodeid;
  126. int loopback_link;
  127. struct totem_config *totem_config;
  128. struct totem_ip_address token_target;
  129. qb_loop_timer_handle timer_netif_check_timeout;
  130. qb_loop_timer_handle timer_merge_detect_timeout;
  131. int send_merge_detect_message;
  132. unsigned int merge_detect_messages_sent_before_timeout;
  133. int logpipes[2];
  134. int knet_fd;
  135. pthread_mutex_t log_mutex;
  136. #ifdef HAVE_LIBNOZZLE
  137. char *nozzle_name;
  138. char *nozzle_ipaddr;
  139. char *nozzle_prefix;
  140. char *nozzle_macaddr;
  141. nozzle_t nozzle_handle;
  142. #endif
  143. };
  144. /* Awkward. But needed to get stats from knet */
  145. struct totemknet_instance *global_instance;
  146. struct work_item {
  147. const void *msg;
  148. unsigned int msg_len;
  149. struct totemknet_instance *instance;
  150. };
  151. int totemknet_member_list_rebind_ip (
  152. void *knet_context);
  153. static int totemknet_configure_compression (
  154. struct totemknet_instance *instance,
  155. struct totem_config *totem_config);
  156. static void totemknet_start_merge_detect_timeout(
  157. void *knet_context);
  158. static void totemknet_stop_merge_detect_timeout(
  159. void *knet_context);
  160. static void log_flush_messages (
  161. void *knet_context);
  162. static void totemknet_instance_initialize (struct totemknet_instance *instance)
  163. {
  164. int res;
  165. memset (instance, 0, sizeof (struct totemknet_instance));
  166. res = pthread_mutex_init(&instance->log_mutex, NULL);
  167. /*
  168. * There is not too much else what can be done.
  169. */
  170. assert(res == 0);
  171. }
  172. #define knet_log_printf_lock(level, subsys, function, file, line, format, args...) \
  173. do { \
  174. (void)pthread_mutex_lock(&instance->log_mutex); \
  175. instance->totemknet_log_printf ( \
  176. level, subsys, function, file, line, \
  177. (const char *)format, ##args); \
  178. (void)pthread_mutex_unlock(&instance->log_mutex); \
  179. } while (0);
  180. #define knet_log_printf(level, format, args...) \
  181. do { \
  182. knet_log_printf_lock ( \
  183. level, instance->totemknet_subsys_id, \
  184. __FUNCTION__, __FILE__, __LINE__, \
  185. (const char *)format, ##args); \
  186. } while (0);
  187. #define libknet_log_printf(level, format, args...) \
  188. do { \
  189. knet_log_printf_lock ( \
  190. level, instance->knet_subsys_id, \
  191. __FUNCTION__, "libknet.h", __LINE__, \
  192. (const char *)format, ##args); \
  193. } while (0);
  194. #define KNET_LOGSYS_PERROR(err_num, level, fmt, args...) \
  195. do { \
  196. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  197. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  198. instance->totemknet_log_printf ( \
  199. level, instance->totemknet_subsys_id, \
  200. __FUNCTION__, __FILE__, __LINE__, \
  201. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  202. } while(0)
  203. #ifdef HAVE_LIBNOZZLE
  204. static inline int is_ether_addr_multicast(const uint8_t *addr)
  205. {
  206. return (addr[0] & 0x01);
  207. }
  208. static inline int is_ether_addr_zero(const uint8_t *addr)
  209. {
  210. return (!addr[0] && !addr[1] && !addr[2] && !addr[3] && !addr[4] && !addr[5]);
  211. }
  212. static int ether_host_filter_fn(void *private_data,
  213. const unsigned char *outdata,
  214. ssize_t outdata_len,
  215. uint8_t tx_rx,
  216. knet_node_id_t this_host_id,
  217. knet_node_id_t src_host_id,
  218. int8_t *channel,
  219. knet_node_id_t *dst_host_ids,
  220. size_t *dst_host_ids_entries)
  221. {
  222. struct ether_header *eth_h = (struct ether_header *)outdata;
  223. uint8_t *dst_mac = (uint8_t *)eth_h->ether_dhost;
  224. uint16_t dst_host_id;
  225. if (is_ether_addr_zero(dst_mac))
  226. return -1;
  227. if (is_ether_addr_multicast(dst_mac)) {
  228. return 1;
  229. }
  230. memmove(&dst_host_id, &dst_mac[4], 2);
  231. dst_host_ids[0] = ntohs(dst_host_id);
  232. *dst_host_ids_entries = 1;
  233. return 0;
  234. }
  235. #endif
  236. static int dst_host_filter_callback_fn(void *private_data,
  237. const unsigned char *outdata,
  238. ssize_t outdata_len,
  239. uint8_t tx_rx,
  240. knet_node_id_t this_host_id,
  241. knet_node_id_t src_host_id,
  242. int8_t *channel,
  243. knet_node_id_t *dst_host_ids,
  244. size_t *dst_host_ids_entries)
  245. {
  246. struct totem_message_header *header = (struct totem_message_header *)outdata;
  247. int res;
  248. #ifdef HAVE_LIBNOZZLE
  249. if (*channel != 0) {
  250. return ether_host_filter_fn(private_data,
  251. outdata, outdata_len,
  252. tx_rx,
  253. this_host_id, src_host_id,
  254. channel,
  255. dst_host_ids,
  256. dst_host_ids_entries);
  257. }
  258. #endif
  259. if (header->target_nodeid) {
  260. dst_host_ids[0] = header->target_nodeid;
  261. *dst_host_ids_entries = 1;
  262. res = 0; /* unicast message */
  263. }
  264. else {
  265. *dst_host_ids_entries = 0;
  266. res = 1; /* multicast message */
  267. }
  268. return res;
  269. }
  270. static void socket_error_callback_fn(void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)
  271. {
  272. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  273. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet socket ERROR notification called: txrx=%d, error=%d, errorno=%d", tx_rx, error, errorno);
  274. if ((error == -1 && errorno != EAGAIN) || (error == 0)) {
  275. knet_handle_remove_datafd(instance->knet_handle, datafd);
  276. }
  277. }
  278. static void host_change_callback_fn(void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)
  279. {
  280. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  281. // TODO: what? if anything.
  282. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet host change callback. nodeid: " CS_PRI_NODE_ID " reachable: %d", host_id, reachable);
  283. }
  284. static void pmtu_change_callback_fn(void *private_data, unsigned int data_mtu)
  285. {
  286. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  287. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet pMTU change: %d", data_mtu);
  288. /* We don't need to tell corosync the actual knet MTU */
  289. // instance->totemknet_mtu_changed(instance->context, data_mtu);
  290. }
  291. int totemknet_crypto_set (
  292. void *knet_context,
  293. const char *cipher_type,
  294. const char *hash_type)
  295. {
  296. return (0);
  297. }
  298. static inline void ucast_sendmsg (
  299. struct totemknet_instance *instance,
  300. struct totem_ip_address *system_to,
  301. const void *msg,
  302. unsigned int msg_len)
  303. {
  304. int res = 0;
  305. struct totem_message_header *header = (struct totem_message_header *)msg;
  306. struct msghdr msg_ucast;
  307. struct iovec iovec;
  308. header->target_nodeid = system_to->nodeid;
  309. iovec.iov_base = (void *)msg;
  310. iovec.iov_len = msg_len;
  311. /*
  312. * Build unicast message
  313. */
  314. memset(&msg_ucast, 0, sizeof(msg_ucast));
  315. msg_ucast.msg_iov = (void *)&iovec;
  316. msg_ucast.msg_iovlen = 1;
  317. #ifdef HAVE_MSGHDR_CONTROL
  318. msg_ucast.msg_control = 0;
  319. #endif
  320. #ifdef HAVE_MSGHDR_CONTROLLEN
  321. msg_ucast.msg_controllen = 0;
  322. #endif
  323. #ifdef HAVE_MSGHDR_FLAGS
  324. msg_ucast.msg_flags = 0;
  325. #endif
  326. #ifdef HAVE_MSGHDR_ACCRIGHTS
  327. msg_ucast.msg_accrights = NULL;
  328. #endif
  329. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  330. msg_ucast.msg_accrightslen = 0;
  331. #endif
  332. /*
  333. * Transmit unicast message
  334. * An error here is recovered by totemsrp
  335. */
  336. res = sendmsg (instance->knet_fd, &msg_ucast, MSG_NOSIGNAL);
  337. if (res < 0) {
  338. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  339. "sendmsg(ucast) failed (non-critical)");
  340. }
  341. }
  342. static inline void mcast_sendmsg (
  343. struct totemknet_instance *instance,
  344. const void *msg,
  345. unsigned int msg_len,
  346. int only_active)
  347. {
  348. int res;
  349. struct totem_message_header *header = (struct totem_message_header *)msg;
  350. struct msghdr msg_mcast;
  351. struct iovec iovec;
  352. iovec.iov_base = (void *)msg;
  353. iovec.iov_len = msg_len;
  354. header->target_nodeid = 0;
  355. /*
  356. * Build multicast message
  357. */
  358. memset(&msg_mcast, 0, sizeof(msg_mcast));
  359. msg_mcast.msg_iov = (void *)&iovec;
  360. msg_mcast.msg_iovlen = 1;
  361. #ifdef HAVE_MSGHDR_CONTROL
  362. msg_mcast.msg_control = 0;
  363. #endif
  364. #ifdef HAVE_MSGHDR_CONTROLLEN
  365. msg_mcast.msg_controllen = 0;
  366. #endif
  367. #ifdef HAVE_MSGHDR_FLAGS
  368. msg_mcast.msg_flags = 0;
  369. #endif
  370. #ifdef HAVE_MSGHDR_ACCRIGHTS
  371. msg_mcast.msg_accrights = NULL;
  372. #endif
  373. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  374. msg_mcast.msg_accrightslen = 0;
  375. #endif
  376. // log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_sendmsg. only_active=%d, len=%d", only_active, msg_len);
  377. res = sendmsg (instance->knet_fd, &msg_mcast, MSG_NOSIGNAL);
  378. if (res < msg_len) {
  379. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send sendmsg returned %d", res);
  380. }
  381. if (!only_active || instance->send_merge_detect_message) {
  382. /*
  383. * Current message was sent to all nodes
  384. */
  385. instance->merge_detect_messages_sent_before_timeout++;
  386. instance->send_merge_detect_message = 0;
  387. }
  388. }
  389. static int node_compare(const void *aptr, const void *bptr)
  390. {
  391. uint16_t a,b;
  392. a = *(uint16_t *)aptr;
  393. b = *(uint16_t *)bptr;
  394. return a > b;
  395. }
  396. #ifndef OWN_INDEX_NONE
  397. #define OWN_INDEX_NONE -1
  398. #endif
  399. int totemknet_nodestatus_get (
  400. void *knet_context,
  401. unsigned int nodeid,
  402. struct totem_node_status *node_status)
  403. {
  404. int i;
  405. int res = 0;
  406. struct knet_link_status link_status;
  407. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  408. struct knet_host_status knet_host_status;
  409. uint8_t link_list[KNET_MAX_LINK];
  410. size_t num_links;
  411. if (!instance->knet_handle) {
  412. return CS_ERR_NOT_EXIST; /* Not using knet */
  413. }
  414. if (!node_status) {
  415. return CS_ERR_INVALID_PARAM;
  416. }
  417. res = knet_host_get_status(instance->knet_handle,
  418. nodeid,
  419. &knet_host_status);
  420. if (res) {
  421. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_handle_get_host_status(%d) failed: %d", nodeid, res);
  422. return (-1);
  423. }
  424. node_status->nodeid = nodeid;
  425. node_status->reachable = knet_host_status.reachable;
  426. node_status->remote = knet_host_status.remote;
  427. node_status->external = knet_host_status.external;
  428. #ifdef HAVE_KNET_ONWIRE_VER
  429. res = knet_handle_get_onwire_ver(instance->knet_handle,
  430. nodeid,
  431. &node_status->onwire_min,
  432. &node_status->onwire_max,
  433. &node_status->onwire_ver);
  434. if (res) {
  435. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_handle_get_onwire_ver(%d) failed: %d", nodeid, res);
  436. return (-1);
  437. }
  438. #endif
  439. /* Get link info */
  440. res = knet_link_get_link_list(instance->knet_handle,
  441. nodeid, link_list, &num_links);
  442. if (res) {
  443. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_link_get_link_list(%d) failed: %d", nodeid, res);
  444. return (-1);
  445. }
  446. /* node_status[] has been zeroed for us in totempg.c */
  447. for (i=0; i < num_links; i++) {
  448. if (!instance->totem_config->interfaces[link_list[i]].configured) {
  449. continue;
  450. }
  451. res = knet_link_get_status(instance->knet_handle,
  452. nodeid,
  453. link_list[i],
  454. &link_status,
  455. sizeof(link_status));
  456. if (res == 0) {
  457. node_status->link_status[link_list[i]].enabled = link_status.enabled;
  458. node_status->link_status[link_list[i]].connected = link_status.connected;
  459. node_status->link_status[link_list[i]].dynconnected = link_status.dynconnected;
  460. node_status->link_status[link_list[i]].mtu = link_status.mtu;
  461. memcpy(node_status->link_status[link_list[i]].src_ipaddr, link_status.src_ipaddr, KNET_MAX_HOST_LEN);
  462. memcpy(node_status->link_status[link_list[i]].dst_ipaddr, link_status.dst_ipaddr, KNET_MAX_HOST_LEN);
  463. } else {
  464. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_link_get_link_status(%d, %d) failed: %d", nodeid, link_list[i], res);
  465. }
  466. }
  467. return res;
  468. }
  469. int totemknet_ifaces_get (void *knet_context,
  470. char ***status,
  471. unsigned int *iface_count)
  472. {
  473. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  474. struct knet_link_status link_status;
  475. knet_node_id_t host_list[KNET_MAX_HOST];
  476. uint8_t link_list[KNET_MAX_LINK];
  477. size_t num_hosts;
  478. size_t num_links;
  479. size_t link_idx;
  480. int i,j;
  481. char *ptr;
  482. int res = 0;
  483. /*
  484. * Don't do the whole 'link_info' bit if the caller just wants
  485. * a count of interfaces.
  486. */
  487. if (status) {
  488. int own_idx = OWN_INDEX_NONE;
  489. res = knet_host_get_host_list(instance->knet_handle,
  490. host_list, &num_hosts);
  491. if (res) {
  492. return (-1);
  493. }
  494. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  495. for (j=0; j<num_hosts; j++) {
  496. if (host_list[j] == instance->our_nodeid) {
  497. own_idx = j;
  498. break;
  499. }
  500. }
  501. for (i=0; i<INTERFACE_MAX; i++) {
  502. memset(instance->link_status[i], 'd', CFG_INTERFACE_STATUS_MAX_LEN-1);
  503. if (own_idx != OWN_INDEX_NONE) {
  504. instance->link_status[i][own_idx] = 'n';
  505. }
  506. instance->link_status[i][num_hosts] = '\0';
  507. }
  508. /* This is all a bit "inside-out" because "status" is a set of strings per link
  509. * and knet orders things by host
  510. */
  511. for (j=0; j<num_hosts; j++) {
  512. if (own_idx != OWN_INDEX_NONE && j == own_idx) {
  513. continue ;
  514. }
  515. res = knet_link_get_link_list(instance->knet_handle,
  516. host_list[j], link_list, &num_links);
  517. if (res) {
  518. return (-1);
  519. }
  520. link_idx = 0;
  521. for (i=0; i < num_links; i++) {
  522. /*
  523. * Skip over links that are unconfigured to corosync. This is basically
  524. * link0 if corosync isn't using it for comms, as we will still
  525. * have it set up for loopback.
  526. */
  527. if (!instance->totem_config->interfaces[link_list[i]].configured) {
  528. continue;
  529. }
  530. ptr = instance->link_status[link_idx++];
  531. res = knet_link_get_status(instance->knet_handle,
  532. host_list[j],
  533. link_list[i],
  534. &link_status,
  535. sizeof(link_status));
  536. if (res == 0) {
  537. ptr[j] = '0' + (link_status.enabled |
  538. link_status.connected<<1 |
  539. link_status.dynconnected<<2);
  540. }
  541. else {
  542. knet_log_printf (LOGSYS_LEVEL_ERROR,
  543. "totemknet_ifaces_get: Cannot get link status: %s", strerror(errno));
  544. ptr[j] = '?';
  545. }
  546. }
  547. }
  548. *status = instance->link_status;
  549. }
  550. *iface_count = INTERFACE_MAX;
  551. return (res);
  552. }
  553. int totemknet_finalize (
  554. void *knet_context)
  555. {
  556. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  557. int res = 0;
  558. int i,j;
  559. static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */
  560. uint8_t links[KNET_MAX_LINK];
  561. size_t num_nodes;
  562. size_t num_links;
  563. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  564. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  565. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  566. /*
  567. * Disable forwarding to make knet flush send queue. This ensures that the LEAVE message will be sent.
  568. */
  569. res = knet_handle_setfwd(instance->knet_handle, 0);
  570. if (res) {
  571. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_setfwd failed: %s", strerror(errno));
  572. }
  573. res = knet_host_get_host_list(instance->knet_handle, nodes, &num_nodes);
  574. if (res) {
  575. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet node list for shutdown: %s", strerror(errno));
  576. /* Crash out anyway */
  577. goto finalise_error;
  578. }
  579. /* Tidily shut down all nodes & links. */
  580. for (i=0; i<num_nodes; i++) {
  581. res = knet_link_get_link_list(instance->knet_handle, nodes[i], links, &num_links);
  582. if (res) {
  583. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet link list for node " CS_PRI_NODE_ID ": %s", nodes[i], strerror(errno));
  584. goto finalise_error;
  585. }
  586. for (j=0; j<num_links; j++) {
  587. res = knet_link_set_enable(instance->knet_handle, nodes[i], links[j], 0);
  588. if (res) {
  589. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_set_enable(node " CS_PRI_NODE_ID ", link %d) failed: %s", nodes[i], links[j], strerror(errno));
  590. }
  591. res = knet_link_clear_config(instance->knet_handle, nodes[i], links[j]);
  592. if (res) {
  593. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_clear_config(node " CS_PRI_NODE_ID ", link %d) failed: %s", nodes[i], links[j], strerror(errno));
  594. }
  595. }
  596. res = knet_host_remove(instance->knet_handle, nodes[i]);
  597. if (res) {
  598. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_host_remove(node " CS_PRI_NODE_ID ") failed: %s", nodes[i], strerror(errno));
  599. }
  600. }
  601. finalise_error:
  602. res = knet_handle_free(instance->knet_handle);
  603. if (res) {
  604. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_free failed: %s", strerror(errno));
  605. }
  606. totemknet_stop_merge_detect_timeout(instance);
  607. log_flush_messages(instance);
  608. /*
  609. * Error is deliberately ignored
  610. */
  611. (void)pthread_mutex_destroy(&instance->log_mutex);
  612. return (res);
  613. }
  614. static int log_deliver_fn (
  615. int fd,
  616. int revents,
  617. void *data)
  618. {
  619. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  620. char buffer[sizeof(struct knet_log_msg)*4];
  621. char *bufptr = buffer;
  622. int done = 0;
  623. int len;
  624. len = read(fd, buffer, sizeof(buffer));
  625. while (done < len) {
  626. struct knet_log_msg *msg = (struct knet_log_msg *)bufptr;
  627. switch (msg->msglevel) {
  628. case KNET_LOG_ERR:
  629. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s: %s",
  630. knet_log_get_subsystem_name(msg->subsystem),
  631. msg->msg);
  632. break;
  633. case KNET_LOG_WARN:
  634. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s: %s",
  635. knet_log_get_subsystem_name(msg->subsystem),
  636. msg->msg);
  637. break;
  638. case KNET_LOG_INFO:
  639. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s: %s",
  640. knet_log_get_subsystem_name(msg->subsystem),
  641. msg->msg);
  642. break;
  643. case KNET_LOG_DEBUG:
  644. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s: %s",
  645. knet_log_get_subsystem_name(msg->subsystem),
  646. msg->msg);
  647. break;
  648. #ifdef KNET_LOG_TRACE
  649. case KNET_LOG_TRACE:
  650. libknet_log_printf (LOGSYS_LEVEL_TRACE, "%s: %s",
  651. knet_log_get_subsystem_name(msg->subsystem),
  652. msg->msg);
  653. break;
  654. #endif
  655. }
  656. bufptr += sizeof(struct knet_log_msg);
  657. done += sizeof(struct knet_log_msg);
  658. }
  659. return 0;
  660. }
  661. static int data_deliver_fn (
  662. int fd,
  663. int revents,
  664. void *data)
  665. {
  666. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  667. struct msghdr msg_hdr;
  668. struct iovec iov_recv;
  669. struct sockaddr_storage system_from;
  670. ssize_t msg_len;
  671. char *data_ptr = instance->iov_buffer;
  672. int truncated_packet;
  673. iov_recv.iov_base = instance->iov_buffer;
  674. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  675. msg_hdr.msg_name = &system_from;
  676. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  677. msg_hdr.msg_iov = &iov_recv;
  678. msg_hdr.msg_iovlen = 1;
  679. #ifdef HAVE_MSGHDR_CONTROL
  680. msg_hdr.msg_control = 0;
  681. #endif
  682. #ifdef HAVE_MSGHDR_CONTROLLEN
  683. msg_hdr.msg_controllen = 0;
  684. #endif
  685. #ifdef HAVE_MSGHDR_FLAGS
  686. msg_hdr.msg_flags = 0;
  687. #endif
  688. #ifdef HAVE_MSGHDR_ACCRIGHTS
  689. msg_hdr.msg_accrights = NULL;
  690. #endif
  691. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  692. msg_hdr.msg_accrightslen = 0;
  693. #endif
  694. msg_len = recvmsg (fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  695. if (msg_len <= 0) {
  696. return (0);
  697. }
  698. truncated_packet = 0;
  699. /*
  700. * If it's from the knet fd then it will have the optional knet header on it
  701. */
  702. #ifdef KNET_DATAFD_FLAG_RX_RETURN_INFO
  703. if (fd == instance->knet_fd) {
  704. struct knet_datafd_header *datafd_header = (struct knet_datafd_header *)data_ptr;
  705. /* knet_log_printf (LOGSYS_LEVEL_DEBUG, "Packet from knet_fd nodeid: %d\n", datafd_header->src_nodeid); */
  706. /* Advance past the ACTUAL header size, not the size we think it might be */
  707. data_ptr += datafd_header->size;
  708. msg_len -= datafd_header->size;
  709. }
  710. #endif
  711. #ifdef HAVE_MSGHDR_FLAGS
  712. if (msg_hdr.msg_flags & MSG_TRUNC) {
  713. truncated_packet = 1;
  714. }
  715. #else
  716. /*
  717. * Checking of received number of bytes doesn't work as with UDP(U) because knet might
  718. * send KNET_MAX_PACKET_SIZE. It is also bug if message is truncated because
  719. * knet always sends packet with maximum size of KNET_MAX_PACKET_SIZE.
  720. *
  721. * It probably doesn't make too much sense to force having msg_hdr.msg_flags, but
  722. * it is also good to know platform (or configuration) doesn't support them, so
  723. * just issue compiler warning.
  724. */
  725. #warning Platform without msg_hdr.msg_flags
  726. #endif
  727. if (truncated_packet) {
  728. /*
  729. * It this happens it is real bug, because knet always sends packet with maximum size
  730. * of KNET_MAX_PACKET_SIZE.
  731. */
  732. knet_log_printf(instance->totemknet_log_level_error,
  733. "Received truncated packet. Please report this bug. Dropping packet.");
  734. return (0);
  735. }
  736. /*
  737. * Handle incoming message
  738. */
  739. instance->totemknet_deliver_fn (
  740. instance->context,
  741. data_ptr,
  742. msg_len,
  743. &system_from);
  744. return (0);
  745. }
  746. static void timer_function_netif_check_timeout (
  747. void *data)
  748. {
  749. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  750. int i;
  751. int res = 0;
  752. for (i=0; i < INTERFACE_MAX; i++) {
  753. if (!instance->totem_config->interfaces[i].configured) {
  754. continue;
  755. }
  756. res = instance->totemknet_iface_change_fn (instance->context,
  757. &instance->my_ids[i],
  758. i);
  759. }
  760. if (res != 0) {
  761. /* This is only called at startup, so we can quit here.
  762. Refresh takes a different path */
  763. corosync_exit_error(COROSYNC_DONE_MAINCONFIGREAD);
  764. }
  765. }
  766. static void knet_set_access_list_config(struct totemknet_instance *instance)
  767. {
  768. #ifdef HAVE_KNET_ACCESS_LIST
  769. uint32_t value;
  770. cs_error_t err;
  771. value = instance->totem_config->block_unlisted_ips;
  772. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_enable access list: %d", value);
  773. err = knet_handle_enable_access_lists(instance->knet_handle, value);
  774. if (err) {
  775. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_access_lists failed");
  776. }
  777. #endif
  778. }
  779. void totemknet_configure_log_level()
  780. {
  781. int logsys_log_mode;
  782. int knet_log_mode = KNET_LOG_INFO;
  783. uint8_t s;
  784. int err;
  785. if (!global_instance || !global_instance->knet_handle) {
  786. return;
  787. }
  788. /* Reconfigure logging level */
  789. logsys_log_mode = logsys_config_debug_get("KNET");
  790. switch (logsys_log_mode) {
  791. case LOGSYS_DEBUG_OFF:
  792. knet_log_mode = KNET_LOG_INFO;
  793. break;
  794. case LOGSYS_DEBUG_ON:
  795. knet_log_mode = KNET_LOG_DEBUG;
  796. break;
  797. case LOGSYS_DEBUG_TRACE:
  798. #ifdef KNET_LOG_TRACE
  799. knet_log_mode = KNET_LOG_TRACE;
  800. #else
  801. knet_log_mode = KNET_LOG_DEBUG;
  802. #endif
  803. break;
  804. }
  805. log_printf (LOGSYS_LEVEL_DEBUG, "totemknet setting log level %s", knet_log_get_loglevel_name(knet_log_mode));
  806. err = 0;
  807. for (s = 0; s<KNET_MAX_SUBSYSTEMS; s++) {
  808. err = knet_log_set_loglevel(global_instance->knet_handle, s, knet_log_mode);
  809. }
  810. /* If one fails, they all fail. no point in issuing KNET_MAX_SUBSYSTEMS errors */
  811. if (err) {
  812. log_printf (LOGSYS_LEVEL_ERROR, "totemknet failed to set log level: %s", strerror(errno));
  813. }
  814. }
  815. /* NOTE: this relies on the fact that totem_reload_notify() is called first */
  816. static void totemknet_refresh_config(
  817. int32_t event,
  818. const char *key_name,
  819. struct icmap_notify_value new_val,
  820. struct icmap_notify_value old_val,
  821. void *user_data)
  822. {
  823. uint8_t reloading;
  824. int after_reload;
  825. uint32_t link_no;
  826. size_t num_nodes;
  827. knet_node_id_t host_ids[KNET_MAX_HOST];
  828. int i;
  829. int err;
  830. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  831. ENTER();
  832. /*
  833. * If a full reload is in progress then don't do anything until it's done and
  834. * can reconfigure it all atomically
  835. */
  836. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  837. return;
  838. }
  839. after_reload = (strcmp(key_name, "config.totemconfig_reload_in_progress") == 0);
  840. knet_set_access_list_config(instance);
  841. if (strcmp(key_name, "totem.knet_pmtud_interval") == 0 || after_reload) {
  842. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %u",
  843. instance->totem_config->knet_pmtud_interval);
  844. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  845. if (err) {
  846. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  847. }
  848. }
  849. if (strcmp(key_name, "totem.knet_mtu") == 0 || after_reload) {
  850. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_mtu now %u", instance->totem_config->knet_mtu);
  851. err = knet_handle_pmtud_set(instance->knet_handle, instance->totem_config->knet_mtu);
  852. if (err) {
  853. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud failed");
  854. }
  855. }
  856. /* Configure link parameters for each node */
  857. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  858. if (err != 0) {
  859. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  860. }
  861. for (i=0; i<num_nodes; i++) {
  862. int linkerr = 0;
  863. for (link_no = 0; link_no < INTERFACE_MAX; link_no++) {
  864. if (host_ids[i] == instance->our_nodeid || !instance->totem_config->interfaces[link_no].configured) {
  865. continue;
  866. }
  867. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  868. instance->totem_config->interfaces[link_no].knet_ping_interval,
  869. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  870. instance->totem_config->interfaces[link_no].knet_ping_precision);
  871. if (err) {
  872. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for node " CS_PRI_NODE_ID " link %d failed", host_ids[i], link_no);
  873. linkerr = err;
  874. }
  875. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  876. instance->totem_config->interfaces[link_no].knet_pong_count);
  877. if (err) {
  878. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for node " CS_PRI_NODE_ID " link %d failed",host_ids[i], link_no);
  879. linkerr = err;
  880. }
  881. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  882. instance->totem_config->interfaces[link_no].knet_link_priority);
  883. if (err) {
  884. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for node " CS_PRI_NODE_ID " link %d failed", host_ids[i], link_no);
  885. linkerr = err;
  886. }
  887. }
  888. if (linkerr) {
  889. icmap_set_string("config.reload_error_message", "Failed to set knet ping timers(2)");
  890. }
  891. }
  892. /* Log levels get reconfigured from logconfig.c as that happens last in the reload */
  893. LEAVE();
  894. }
  895. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  896. {
  897. icmap_track_t icmap_track_totem = NULL;
  898. icmap_track_t icmap_track_reload = NULL;
  899. ENTER();
  900. icmap_track_add("totem.",
  901. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  902. totemknet_refresh_config,
  903. instance,
  904. &icmap_track_totem);
  905. icmap_track_add("config.totemconfig_reload_in_progress",
  906. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  907. totemknet_refresh_config,
  908. instance,
  909. &icmap_track_reload);
  910. LEAVE();
  911. }
  912. static int totemknet_is_crypto_enabled(const struct totemknet_instance *instance)
  913. {
  914. return (!(strcmp(instance->totem_config->crypto_cipher_type, "none") == 0 &&
  915. strcmp(instance->totem_config->crypto_hash_type, "none") == 0));
  916. }
  917. static int totemknet_set_knet_crypto(struct totemknet_instance *instance)
  918. {
  919. struct knet_handle_crypto_cfg crypto_cfg;
  920. int res;
  921. /* These have already been validated */
  922. memcpy(crypto_cfg.crypto_model, instance->totem_config->crypto_model, sizeof(crypto_cfg.crypto_model));
  923. memcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type, sizeof(crypto_cfg.crypto_model));
  924. memcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type, sizeof(crypto_cfg.crypto_model));
  925. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  926. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  927. #ifdef HAVE_KNET_CRYPTO_RECONF
  928. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s on index %d",
  929. crypto_cfg.crypto_model,
  930. crypto_cfg.crypto_cipher_type,
  931. crypto_cfg.crypto_hash_type,
  932. instance->totem_config->crypto_index
  933. );
  934. /* If crypto is being disabled we need to explicitly allow cleartext traffic in knet */
  935. if (!totemknet_is_crypto_enabled(instance)) {
  936. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  937. if (res) {
  938. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(ALLOW) failed %s", strerror(errno));
  939. }
  940. }
  941. /* use_config will be called later when all nodes are synced */
  942. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, instance->totem_config->crypto_index);
  943. if (res == 0) {
  944. /* Keep a copy in case it fails in future */
  945. memcpy(&instance->last_good_crypto_cfg, &crypto_cfg, sizeof(crypto_cfg));
  946. }
  947. if (res == -1) {
  948. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: %s", instance->totem_config->crypto_index, strerror(errno));
  949. goto exit_error;
  950. }
  951. if (res == -2) {
  952. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: -2", instance->totem_config->crypto_index);
  953. goto exit_error;
  954. }
  955. #else
  956. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s",
  957. crypto_cfg.crypto_model,
  958. crypto_cfg.crypto_cipher_type,
  959. crypto_cfg.crypto_hash_type
  960. );
  961. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  962. if (res == -1) {
  963. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  964. goto exit_error;
  965. }
  966. if (res == -2) {
  967. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  968. goto exit_error;
  969. }
  970. #endif
  971. exit_error:
  972. #ifdef HAVE_KNET_CRYPTO_RECONF
  973. if (res) {
  974. icmap_set_string("config.reload_error_message", "Failed to set crypto parameters");
  975. /* Restore the old values in cmap & totem_config */
  976. icmap_set_string("totem.crypto_cipher", instance->last_good_crypto_cfg.crypto_cipher_type);
  977. icmap_set_string("totem.crypto_hash", instance->last_good_crypto_cfg.crypto_hash_type);
  978. icmap_set_string("totem.crypto_model", instance->last_good_crypto_cfg.crypto_model);
  979. memcpy(instance->totem_config->crypto_hash_type, instance->last_good_crypto_cfg.crypto_hash_type,
  980. sizeof(instance->last_good_crypto_cfg.crypto_hash_type));
  981. memcpy(instance->totem_config->crypto_cipher_type, instance->last_good_crypto_cfg.crypto_cipher_type,
  982. sizeof(instance->last_good_crypto_cfg.crypto_cipher_type));
  983. memcpy(instance->totem_config->crypto_model, instance->last_good_crypto_cfg.crypto_model,
  984. sizeof(instance->last_good_crypto_cfg.crypto_model));
  985. }
  986. #endif
  987. return res;
  988. }
  989. /*
  990. * Create an instance
  991. */
  992. int totemknet_initialize (
  993. qb_loop_t *poll_handle,
  994. void **knet_context,
  995. struct totem_config *totem_config,
  996. totemsrp_stats_t *stats,
  997. void *context,
  998. int (*deliver_fn) (
  999. void *context,
  1000. const void *msg,
  1001. unsigned int msg_len,
  1002. const struct sockaddr_storage *system_from),
  1003. int (*iface_change_fn) (
  1004. void *context,
  1005. const struct totem_ip_address *iface_address,
  1006. unsigned int link_no),
  1007. void (*mtu_changed) (
  1008. void *context,
  1009. int net_mtu),
  1010. void (*target_set_completed) (
  1011. void *context))
  1012. {
  1013. struct totemknet_instance *instance;
  1014. char *tmp_str;
  1015. int8_t channel=0;
  1016. int allow_knet_handle_fallback=0;
  1017. int res;
  1018. int i;
  1019. instance = malloc (sizeof (struct totemknet_instance));
  1020. if (instance == NULL) {
  1021. return (-1);
  1022. }
  1023. totemknet_instance_initialize (instance);
  1024. instance->totem_config = totem_config;
  1025. /*
  1026. * Configure logging
  1027. */
  1028. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1029. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1030. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1031. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1032. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1033. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1034. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  1035. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  1036. /*
  1037. * Initialize local variables for totemknet
  1038. */
  1039. instance->our_nodeid = instance->totem_config->node_id;
  1040. for (i=0; i< INTERFACE_MAX; i++) {
  1041. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  1042. instance->my_ids[i].nodeid = instance->our_nodeid;
  1043. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  1044. /* Needed for totemsrp */
  1045. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  1046. }
  1047. instance->poll_handle = poll_handle;
  1048. instance->context = context;
  1049. instance->totemknet_deliver_fn = deliver_fn;
  1050. instance->totemknet_iface_change_fn = iface_change_fn;
  1051. instance->totemknet_mtu_changed = mtu_changed;
  1052. instance->totemknet_target_set_completed = target_set_completed;
  1053. instance->loopback_link = 0;
  1054. res = pipe(instance->logpipes);
  1055. if (res == -1) {
  1056. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  1057. goto exit_error;
  1058. }
  1059. if (fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK) == -1 ||
  1060. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK) == -1) {
  1061. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to set O_NONBLOCK flag for instance->logpipes");
  1062. goto exit_error;
  1063. }
  1064. if (icmap_get_string("system.allow_knet_handle_fallback", &tmp_str) == CS_OK) {
  1065. if (strcmp(tmp_str, "yes") == 0) {
  1066. allow_knet_handle_fallback = 1;
  1067. }
  1068. free(tmp_str);
  1069. }
  1070. #if defined(KNET_API_VER) && (KNET_API_VER == 2)
  1071. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, KNET_HANDLE_FLAG_PRIVILEGED);
  1072. #else
  1073. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  1074. #endif
  1075. if (allow_knet_handle_fallback && !instance->knet_handle && errno == ENAMETOOLONG) {
  1076. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_new failed, trying unprivileged");
  1077. #if defined(KNET_API_VER) && (KNET_API_VER == 2)
  1078. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, 0);
  1079. #else
  1080. instance->knet_handle = knet_handle_new_ex(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, 0);
  1081. #endif
  1082. }
  1083. if (!instance->knet_handle) {
  1084. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  1085. goto exit_error;
  1086. }
  1087. knet_set_access_list_config(instance);
  1088. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  1089. if (res) {
  1090. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  1091. }
  1092. res = knet_handle_pmtud_set(instance->knet_handle, instance->totem_config->knet_mtu);
  1093. if (res) {
  1094. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_set failed");
  1095. }
  1096. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  1097. if (res) {
  1098. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  1099. }
  1100. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  1101. if (res) {
  1102. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  1103. }
  1104. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  1105. if (res) {
  1106. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  1107. }
  1108. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  1109. if (res) {
  1110. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  1111. }
  1112. global_instance = instance;
  1113. /* Setup knet logging level */
  1114. totemknet_configure_log_level();
  1115. /* Get an fd into knet */
  1116. instance->knet_fd = 0;
  1117. #ifdef KNET_DATAFD_FLAG_RX_RETURN_INFO
  1118. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel, KNET_DATAFD_FLAG_RX_RETURN_INFO);
  1119. #else
  1120. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  1121. #endif
  1122. if (res) {
  1123. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  1124. goto exit_error;
  1125. }
  1126. /* Enable crypto if requested */
  1127. #ifdef HAVE_KNET_CRYPTO_RECONF
  1128. if (totemknet_is_crypto_enabled(instance)) {
  1129. res = totemknet_set_knet_crypto(instance);
  1130. if (res == 0) {
  1131. res = knet_handle_crypto_use_config(instance->knet_handle, totem_config->crypto_index);
  1132. if (res) {
  1133. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_use_config failed: %s", strerror(errno));
  1134. goto exit_error;
  1135. }
  1136. } else {
  1137. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1138. goto exit_error;
  1139. }
  1140. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1141. if (res) {
  1142. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (DISALLOW) failed: %s", strerror(errno));
  1143. goto exit_error;
  1144. }
  1145. } else {
  1146. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  1147. if (res) {
  1148. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (ALLOW) failed: %s", strerror(errno));
  1149. goto exit_error;
  1150. }
  1151. }
  1152. #else
  1153. if (totemknet_is_crypto_enabled(instance)) {
  1154. res = totemknet_set_knet_crypto(instance);
  1155. if (res) {
  1156. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1157. goto exit_error;
  1158. }
  1159. }
  1160. #endif
  1161. /* Set up compression */
  1162. if (strcmp(totem_config->knet_compression_model, "none") != 0) {
  1163. /* Not fatal, but will log */
  1164. (void)totemknet_configure_compression(instance, totem_config);
  1165. }
  1166. knet_handle_setfwd(instance->knet_handle, 1);
  1167. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  1168. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  1169. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  1170. }
  1171. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  1172. instance->link_mode = KNET_LINK_POLICY_RR;
  1173. }
  1174. for (i=0; i<INTERFACE_MAX; i++) {
  1175. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  1176. if (!instance->link_status[i]) {
  1177. goto exit_error;
  1178. }
  1179. }
  1180. qb_loop_poll_add (instance->poll_handle,
  1181. QB_LOOP_MED,
  1182. instance->logpipes[0],
  1183. POLLIN, instance, log_deliver_fn);
  1184. qb_loop_poll_add (instance->poll_handle,
  1185. QB_LOOP_HIGH,
  1186. instance->knet_fd,
  1187. POLLIN, instance, data_deliver_fn);
  1188. /*
  1189. * Upper layer isn't ready to receive message because it hasn't
  1190. * initialized yet. Add short timer to check the interfaces.
  1191. */
  1192. qb_loop_timer_add (instance->poll_handle,
  1193. QB_LOOP_MED,
  1194. 100*QB_TIME_NS_IN_MSEC,
  1195. (void *)instance,
  1196. timer_function_netif_check_timeout,
  1197. &instance->timer_netif_check_timeout);
  1198. totemknet_start_merge_detect_timeout(instance);
  1199. /* Start listening for config changes */
  1200. totemknet_add_config_notifications(instance);
  1201. /* Add stats keys to icmap */
  1202. stats_knet_add_handle();
  1203. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  1204. *knet_context = instance;
  1205. return (0);
  1206. exit_error:
  1207. log_flush_messages(instance);
  1208. free(instance);
  1209. return (-1);
  1210. }
  1211. void *totemknet_buffer_alloc (void)
  1212. {
  1213. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  1214. return malloc(KNET_MAX_PACKET_SIZE + 512);
  1215. }
  1216. void totemknet_buffer_release (void *ptr)
  1217. {
  1218. return free (ptr);
  1219. }
  1220. int totemknet_processor_count_set (
  1221. void *knet_context,
  1222. int processor_count)
  1223. {
  1224. return (0);
  1225. }
  1226. int totemknet_recv_flush (void *knet_context)
  1227. {
  1228. return (0);
  1229. }
  1230. int totemknet_send_flush (void *knet_context)
  1231. {
  1232. return (0);
  1233. }
  1234. int totemknet_token_send (
  1235. void *knet_context,
  1236. const void *msg,
  1237. unsigned int msg_len)
  1238. {
  1239. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1240. int res = 0;
  1241. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1242. return (res);
  1243. }
  1244. int totemknet_mcast_flush_send (
  1245. void *knet_context,
  1246. const void *msg,
  1247. unsigned int msg_len)
  1248. {
  1249. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1250. int res = 0;
  1251. mcast_sendmsg (instance, msg, msg_len, 0);
  1252. return (res);
  1253. }
  1254. int totemknet_mcast_noflush_send (
  1255. void *knet_context,
  1256. const void *msg,
  1257. unsigned int msg_len)
  1258. {
  1259. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1260. int res = 0;
  1261. mcast_sendmsg (instance, msg, msg_len, 1);
  1262. return (res);
  1263. }
  1264. extern int totemknet_iface_check (void *knet_context)
  1265. {
  1266. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1267. int res = 0;
  1268. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  1269. return (res);
  1270. }
  1271. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  1272. {
  1273. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1274. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  1275. }
  1276. int totemknet_token_target_set (
  1277. void *knet_context,
  1278. unsigned int nodeid)
  1279. {
  1280. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1281. int res = 0;
  1282. instance->token_target.nodeid = nodeid;
  1283. instance->totemknet_target_set_completed (instance->context);
  1284. return (res);
  1285. }
  1286. extern int totemknet_recv_mcast_empty (
  1287. void *knet_context)
  1288. {
  1289. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1290. unsigned int res;
  1291. struct sockaddr_storage system_from;
  1292. struct msghdr msg_hdr;
  1293. struct iovec iov_recv;
  1294. struct pollfd ufd;
  1295. int nfds;
  1296. int msg_processed = 0;
  1297. iov_recv.iov_base = instance->iov_buffer;
  1298. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  1299. msg_hdr.msg_name = &system_from;
  1300. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  1301. msg_hdr.msg_iov = &iov_recv;
  1302. msg_hdr.msg_iovlen = 1;
  1303. #ifdef HAVE_MSGHDR_CONTROL
  1304. msg_hdr.msg_control = 0;
  1305. #endif
  1306. #ifdef HAVE_MSGHDR_CONTROLLEN
  1307. msg_hdr.msg_controllen = 0;
  1308. #endif
  1309. #ifdef HAVE_MSGHDR_FLAGS
  1310. msg_hdr.msg_flags = 0;
  1311. #endif
  1312. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1313. msg_hdr.msg_accrights = NULL;
  1314. #endif
  1315. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1316. msg_hdr.msg_accrightslen = 0;
  1317. #endif
  1318. do {
  1319. ufd.fd = instance->knet_fd;
  1320. ufd.events = POLLIN;
  1321. nfds = poll (&ufd, 1, 0);
  1322. if (nfds == 1 && ufd.revents & POLLIN) {
  1323. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  1324. if (res != -1) {
  1325. msg_processed = 1;
  1326. } else {
  1327. msg_processed = -1;
  1328. }
  1329. }
  1330. } while (nfds == 1);
  1331. return (msg_processed);
  1332. }
  1333. int totemknet_iface_set (void *knet_context,
  1334. const struct totem_ip_address *local_addr,
  1335. unsigned short ip_port,
  1336. unsigned int iface_no)
  1337. {
  1338. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1339. totemip_copy(&instance->my_ids[iface_no], local_addr);
  1340. knet_log_printf(LOG_INFO, "Configured link number %d: local addr: %s, port=%d", iface_no, totemip_print(local_addr), ip_port);
  1341. instance->ip_port[iface_no] = ip_port;
  1342. return 0;
  1343. }
  1344. int totemknet_member_add (
  1345. void *knet_context,
  1346. const struct totem_ip_address *local,
  1347. const struct totem_ip_address *member,
  1348. int link_no)
  1349. {
  1350. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1351. int err;
  1352. int port = instance->ip_port[link_no];
  1353. struct sockaddr_storage remote_ss;
  1354. struct sockaddr_storage local_ss;
  1355. int addrlen;
  1356. int i;
  1357. int host_found = 0;
  1358. knet_node_id_t host_ids[KNET_MAX_HOST];
  1359. size_t num_host_ids;
  1360. /* Only create 1 loopback link and use link 0 */
  1361. if (member->nodeid == instance->our_nodeid) {
  1362. if (!instance->loopback_link) {
  1363. link_no = 0;
  1364. instance->loopback_link = 1;
  1365. } else {
  1366. /* Already done */
  1367. return 0;
  1368. }
  1369. }
  1370. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: " CS_PRI_NODE_ID " (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  1371. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: " CS_PRI_NODE_ID " (%s)", local->nodeid, totemip_print(local));
  1372. /* Only add the host if it doesn't already exist in knet */
  1373. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_host_ids);
  1374. if (err) {
  1375. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list");
  1376. return -1;
  1377. }
  1378. for (i=0; i<num_host_ids; i++) {
  1379. if (host_ids[i] == member->nodeid) {
  1380. host_found = 1;
  1381. }
  1382. }
  1383. if (!host_found) {
  1384. err = knet_host_add(instance->knet_handle, member->nodeid);
  1385. if (err != 0 && errno != EEXIST) {
  1386. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  1387. return -1;
  1388. }
  1389. } else {
  1390. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nodeid " CS_PRI_NODE_ID " already added", member->nodeid);
  1391. }
  1392. if (err == 0) {
  1393. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  1394. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  1395. return -1;
  1396. }
  1397. }
  1398. memset(&local_ss, 0, sizeof(local_ss));
  1399. memset(&remote_ss, 0, sizeof(remote_ss));
  1400. /* Casts to remove const */
  1401. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port, &remote_ss, &addrlen);
  1402. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port, &local_ss, &addrlen);
  1403. if (member->nodeid == instance->our_nodeid) {
  1404. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: loopback link is %d\n", link_no);
  1405. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1406. KNET_TRANSPORT_LOOPBACK,
  1407. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1408. }
  1409. else {
  1410. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1411. instance->totem_config->interfaces[link_no].knet_transport,
  1412. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1413. }
  1414. if (err) {
  1415. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  1416. return -1;
  1417. }
  1418. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  1419. instance->totem_config->interfaces[link_no].knet_link_priority);
  1420. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  1421. instance->totem_config->interfaces[link_no].knet_link_priority);
  1422. if (err) {
  1423. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1424. }
  1425. /*
  1426. * Ping timeouts may be 0 here for a newly added interface (on a reload),
  1427. * so we leave this till later, it will get done in totemknet_refresh_config.
  1428. * For the initial startup, we are all preset and ready to go from here.
  1429. */
  1430. if (instance->totem_config->interfaces[link_no].knet_ping_interval != 0) {
  1431. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  1432. instance->totem_config->interfaces[link_no].knet_ping_interval,
  1433. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  1434. instance->totem_config->interfaces[link_no].knet_ping_precision);
  1435. if (err) {
  1436. /* Flush logs before reporting this error so that the knet message prints before ours */
  1437. int saved_errno = errno;
  1438. log_flush_messages(instance);
  1439. errno = saved_errno;
  1440. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1441. icmap_set_string("config.reload_error_message", "Failed to set knet ping timers");
  1442. return -1;
  1443. }
  1444. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  1445. instance->totem_config->interfaces[link_no].knet_pong_count);
  1446. if (err) {
  1447. /* Flush logs before reporting this error so that the knet message prints before ours */
  1448. int saved_errno = errno;
  1449. log_flush_messages(instance);
  1450. errno = saved_errno;
  1451. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1452. icmap_set_string("config.reload_error_message", "Failed to set knet pong count");
  1453. return -1;
  1454. }
  1455. }
  1456. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1457. if (err) {
  1458. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1459. return -1;
  1460. }
  1461. /* register stats */
  1462. stats_knet_add_member(member->nodeid, link_no);
  1463. return (0);
  1464. }
  1465. int totemknet_member_remove (
  1466. void *knet_context,
  1467. const struct totem_ip_address *token_target,
  1468. int link_no)
  1469. {
  1470. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1471. int res;
  1472. uint8_t link_list[KNET_MAX_LINK];
  1473. size_t num_links;
  1474. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: " CS_PRI_NODE_ID ", link=%d", token_target->nodeid, link_no);
  1475. /* Don't remove the link with the loopback on it until we shut down */
  1476. if (token_target->nodeid == instance->our_nodeid) {
  1477. return 0;
  1478. }
  1479. /* Tidy stats */
  1480. stats_knet_del_member(token_target->nodeid, link_no);
  1481. /* Remove the link first */
  1482. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1483. if (res != 0) {
  1484. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set enable(off) for nodeid " CS_PRI_NODE_ID ", link %d failed", token_target->nodeid, link_no);
  1485. return res;
  1486. }
  1487. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1488. if (res != 0) {
  1489. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_clear_config for nodeid " CS_PRI_NODE_ID ", link %d failed", token_target->nodeid, link_no);
  1490. return res;
  1491. }
  1492. /* If this is the last link, then remove the node */
  1493. res = knet_link_get_link_list(instance->knet_handle,
  1494. token_target->nodeid, link_list, &num_links);
  1495. if (res) {
  1496. return (0); /* not really failure */
  1497. }
  1498. if (num_links == 0) {
  1499. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1500. }
  1501. return res;
  1502. }
  1503. int totemknet_member_list_rebind_ip (
  1504. void *knet_context)
  1505. {
  1506. return (0);
  1507. }
  1508. static int totemknet_configure_compression (
  1509. struct totemknet_instance *instance,
  1510. struct totem_config *totem_config)
  1511. {
  1512. struct knet_handle_compress_cfg compress_cfg;
  1513. int res = 0;
  1514. assert(strlen(totem_config->knet_compression_model) < sizeof(compress_cfg.compress_model));
  1515. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1516. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1517. compress_cfg.compress_level = totem_config->knet_compression_level;
  1518. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1519. if (res) {
  1520. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1521. }
  1522. return res;
  1523. }
  1524. int totemknet_reconfigure (
  1525. void *knet_context,
  1526. struct totem_config *totem_config)
  1527. {
  1528. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1529. int res = 0;
  1530. (void)totemknet_configure_compression(instance, totem_config);
  1531. #ifdef HAVE_LIBNOZZLE
  1532. /* Set up nozzle device(s). Return code is ignored, because inability
  1533. * configure nozzle is not fatal problem, errors are logged and
  1534. * there is not much else we can do */
  1535. (void)setup_nozzle(instance);
  1536. #endif
  1537. if (totem_config->crypto_changed) {
  1538. /* Flip crypto_index */
  1539. totem_config->crypto_index = 3-totem_config->crypto_index;
  1540. res = totemknet_set_knet_crypto(instance);
  1541. if (res == 0) {
  1542. knet_log_printf(LOG_INFO, "kronosnet crypto reconfigured on index %d: %s/%s/%s", totem_config->crypto_index,
  1543. totem_config->crypto_model,
  1544. totem_config->crypto_cipher_type,
  1545. totem_config->crypto_hash_type);
  1546. } else {
  1547. icmap_set_string("config.reload_error_message", "Failed to set knet crypto");
  1548. }
  1549. }
  1550. return (res);
  1551. }
  1552. int totemknet_crypto_reconfigure_phase (
  1553. void *knet_context,
  1554. struct totem_config *totem_config,
  1555. cfg_message_crypto_reconfig_phase_t phase)
  1556. {
  1557. #ifdef HAVE_KNET_CRYPTO_RECONF
  1558. int res;
  1559. int config_to_use;
  1560. int config_to_clear;
  1561. struct knet_handle_crypto_cfg crypto_cfg;
  1562. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1563. knet_log_printf(LOGSYS_LEVEL_DEBUG, "totemknet_crypto_reconfigure_phase %d, index=%d\n", phase, totem_config->crypto_index);
  1564. switch (phase) {
  1565. case CRYPTO_RECONFIG_PHASE_ACTIVATE:
  1566. config_to_use = totem_config->crypto_index;
  1567. if (!totemknet_is_crypto_enabled(instance)) {
  1568. config_to_use = 0; /* we are clearing it */
  1569. }
  1570. /* Enable the new config on this node */
  1571. res = knet_handle_crypto_use_config(instance->knet_handle, config_to_use);
  1572. if (res == -1) {
  1573. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_use_config %d failed: %s", config_to_use, strerror(errno));
  1574. }
  1575. break;
  1576. case CRYPTO_RECONFIG_PHASE_CLEANUP:
  1577. /*
  1578. * All nodes should now have the new config. clear the old one out
  1579. * OR disable crypto entirely if that's what the new config insists on.
  1580. */
  1581. config_to_clear = 3-totem_config->crypto_index;
  1582. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Clearing old knet crypto config %d\n", config_to_clear);
  1583. strcpy(crypto_cfg.crypto_model, "none");
  1584. strcpy(crypto_cfg.crypto_cipher_type, "none");
  1585. strcpy(crypto_cfg.crypto_hash_type, "none");
  1586. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, config_to_clear);
  1587. if (res == -1) {
  1588. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: %s", config_to_clear, strerror(errno));
  1589. }
  1590. if (res == -2) {
  1591. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: -2", config_to_clear);
  1592. }
  1593. /* If crypto is enabled then disable all cleartext reception */
  1594. if (totemknet_is_crypto_enabled(instance)) {
  1595. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1596. if (res) {
  1597. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(DISALLOW) failed %s", strerror(errno));
  1598. }
  1599. }
  1600. }
  1601. #endif
  1602. return 0;
  1603. }
  1604. void totemknet_stats_clear (
  1605. void *knet_context)
  1606. {
  1607. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1608. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1609. }
  1610. /* For the stats module */
  1611. int totemknet_link_get_status (
  1612. knet_node_id_t node, uint8_t link_no,
  1613. struct knet_link_status *status)
  1614. {
  1615. int res;
  1616. int ret = CS_OK;
  1617. /* We are probably not using knet */
  1618. if (!global_instance) {
  1619. return CS_ERR_NOT_EXIST;
  1620. }
  1621. if (link_no >= INTERFACE_MAX) {
  1622. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1623. }
  1624. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1625. if (res) {
  1626. switch (errno) {
  1627. case EINVAL:
  1628. ret = CS_ERR_INVALID_PARAM;
  1629. break;
  1630. case EBUSY:
  1631. ret = CS_ERR_BUSY;
  1632. break;
  1633. case EDEADLK:
  1634. ret = CS_ERR_TRY_AGAIN;
  1635. break;
  1636. default:
  1637. ret = CS_ERR_LIBRARY;
  1638. break;
  1639. }
  1640. }
  1641. return (ret);
  1642. }
  1643. int totemknet_handle_get_stats (
  1644. struct knet_handle_stats *stats)
  1645. {
  1646. int res;
  1647. /* We are probably not using knet */
  1648. if (!global_instance) {
  1649. return CS_ERR_NOT_EXIST;
  1650. }
  1651. res = knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1652. if (res != 0) {
  1653. return (qb_to_cs_error(-errno));
  1654. }
  1655. return CS_OK;
  1656. }
  1657. static void timer_function_merge_detect_timeout (
  1658. void *data)
  1659. {
  1660. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1661. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1662. instance->send_merge_detect_message = 1;
  1663. }
  1664. instance->merge_detect_messages_sent_before_timeout = 0;
  1665. totemknet_start_merge_detect_timeout(instance);
  1666. }
  1667. static void totemknet_start_merge_detect_timeout(
  1668. void *knet_context)
  1669. {
  1670. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1671. qb_loop_timer_add(instance->poll_handle,
  1672. QB_LOOP_MED,
  1673. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1674. (void *)instance,
  1675. timer_function_merge_detect_timeout,
  1676. &instance->timer_merge_detect_timeout);
  1677. }
  1678. static void totemknet_stop_merge_detect_timeout(
  1679. void *knet_context)
  1680. {
  1681. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1682. qb_loop_timer_del(instance->poll_handle,
  1683. instance->timer_merge_detect_timeout);
  1684. }
  1685. static void log_flush_messages (void *knet_context)
  1686. {
  1687. struct pollfd pfd;
  1688. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1689. int cont;
  1690. cont = 1;
  1691. while (cont) {
  1692. pfd.fd = instance->logpipes[0];
  1693. pfd.events = POLLIN;
  1694. pfd.revents = 0;
  1695. if ((poll(&pfd, 1, 0) > 0) &&
  1696. (pfd.revents & POLLIN) &&
  1697. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1698. cont = 1;
  1699. } else {
  1700. cont = 0;
  1701. }
  1702. }
  1703. }
  1704. #ifdef HAVE_LIBNOZZLE
  1705. #define NOZZLE_NAME "nozzle.name"
  1706. #define NOZZLE_IPADDR "nozzle.ipaddr"
  1707. #define NOZZLE_PREFIX "nozzle.ipprefix"
  1708. #define NOZZLE_MACADDR "nozzle.macaddr"
  1709. #define NOZZLE_CHANNEL 1
  1710. static char *get_nozzle_script_dir(void *knet_context)
  1711. {
  1712. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1713. char filename[PATH_MAX + FILENAME_MAX + 1];
  1714. static char updown_dirname[PATH_MAX + FILENAME_MAX + 1];
  1715. int res;
  1716. const char *dirname_res;
  1717. /*
  1718. * Build script directory based on corosync.conf file location
  1719. */
  1720. res = snprintf(filename, sizeof(filename), "%s",
  1721. corosync_get_config_file());
  1722. if (res >= sizeof(filename)) {
  1723. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1724. return NULL;
  1725. }
  1726. dirname_res = dirname(filename);
  1727. res = snprintf(updown_dirname, sizeof(updown_dirname), "%s/%s",
  1728. dirname_res, "updown.d");
  1729. if (res >= sizeof(updown_dirname)) {
  1730. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1731. return NULL;
  1732. }
  1733. return updown_dirname;
  1734. }
  1735. /*
  1736. * Deliberately doesn't return the status as caller doesn't care.
  1737. * The result will be logged though
  1738. */
  1739. static void run_nozzle_script(struct totemknet_instance *instance, int type, const char *typename)
  1740. {
  1741. int res;
  1742. char *exec_string;
  1743. res = nozzle_run_updown(instance->nozzle_handle, type, &exec_string);
  1744. if (res == -1 && errno != ENOENT) {
  1745. knet_log_printf (LOGSYS_LEVEL_INFO, "exec nozzle %s script failed: %s", typename, strerror(errno));
  1746. } else if (res == -2) {
  1747. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle %s script failed", typename);
  1748. knet_log_printf (LOGSYS_LEVEL_INFO, "%s", exec_string);
  1749. }
  1750. }
  1751. /*
  1752. * Reparse IP address to add in our node ID
  1753. * IPv6 addresses must end in '::'
  1754. * IPv4 addresses must just be valid
  1755. * '/xx' lengths are optional for IPv6, mandatory for IPv4
  1756. *
  1757. * Returns the modified IP address as a string to pass into libnozzle
  1758. */
  1759. static int reparse_nozzle_ip_address(struct totemknet_instance *instance,
  1760. const char *input_addr,
  1761. const char *prefix, int nodeid,
  1762. char *output_addr, size_t output_len)
  1763. {
  1764. char *coloncolon;
  1765. int bits;
  1766. int max_prefix = 64;
  1767. uint32_t nodeid_mask;
  1768. uint32_t addr_mask;
  1769. uint32_t masked_nodeid;
  1770. struct in_addr *addr;
  1771. struct totem_ip_address totemip;
  1772. coloncolon = strstr(input_addr, "::");
  1773. if (!coloncolon) {
  1774. max_prefix = 30;
  1775. }
  1776. bits = atoi(prefix);
  1777. if (bits < 8 || bits > max_prefix) {
  1778. knet_log_printf(LOGSYS_LEVEL_ERROR, "nozzle IP address prefix must be >= 8 and <= %d (got %d)", max_prefix, bits);
  1779. return -1;
  1780. }
  1781. /* IPv6 is easy */
  1782. if (coloncolon) {
  1783. memcpy(output_addr, input_addr, coloncolon-input_addr);
  1784. sprintf(output_addr + (coloncolon-input_addr), "::%x", nodeid);
  1785. return 0;
  1786. }
  1787. /* For IPv4 we need to parse the address into binary, mask off the required bits,
  1788. * add in the masked_nodeid and 'print' it out again
  1789. */
  1790. nodeid_mask = UINT32_MAX & ((1<<(32 - bits)) - 1);
  1791. addr_mask = UINT32_MAX ^ nodeid_mask;
  1792. masked_nodeid = nodeid & nodeid_mask;
  1793. if (totemip_parse(&totemip, input_addr, AF_INET)) {
  1794. knet_log_printf(LOGSYS_LEVEL_ERROR, "Failed to parse IPv4 nozzle IP address");
  1795. return -1;
  1796. }
  1797. addr = (struct in_addr *)&totemip.addr;
  1798. addr->s_addr &= htonl(addr_mask);
  1799. addr->s_addr |= htonl(masked_nodeid);
  1800. inet_ntop(AF_INET, addr, output_addr, output_len);
  1801. return 0;
  1802. }
  1803. static int create_nozzle_device(void *knet_context, const char *name,
  1804. const char *ipaddr, const char *prefix,
  1805. const char *macaddr)
  1806. {
  1807. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1808. char device_name[IFNAMSIZ+1];
  1809. size_t size = IFNAMSIZ;
  1810. int8_t channel = NOZZLE_CHANNEL;
  1811. nozzle_t nozzle_dev;
  1812. int nozzle_fd;
  1813. int res;
  1814. char *updown_dir;
  1815. char parsed_ipaddr[INET6_ADDRSTRLEN];
  1816. char mac[19];
  1817. memset(device_name, 0, size);
  1818. memset(&mac, 0, sizeof(mac));
  1819. strncpy(device_name, name, size);
  1820. updown_dir = get_nozzle_script_dir(knet_context);
  1821. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle script dir is %s", updown_dir);
  1822. nozzle_dev = nozzle_open(device_name, size, updown_dir);
  1823. if (!nozzle_dev) {
  1824. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to init nozzle device %s: %s", device_name, strerror(errno));
  1825. return -1;
  1826. }
  1827. instance->nozzle_handle = nozzle_dev;
  1828. if (nozzle_set_mac(nozzle_dev, macaddr) < 0) {
  1829. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle MAC to %s: %s", mac, strerror(errno));
  1830. goto out_clean;
  1831. }
  1832. if (reparse_nozzle_ip_address(instance, ipaddr, prefix, instance->our_nodeid, parsed_ipaddr, sizeof(parsed_ipaddr))) {
  1833. /* Prints its own errors */
  1834. goto out_clean;
  1835. }
  1836. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle IP address is %s / %d", parsed_ipaddr, atoi(prefix));
  1837. if (nozzle_add_ip(nozzle_dev, parsed_ipaddr, prefix) < 0) {
  1838. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle IP addr to %s/%s: %s", parsed_ipaddr, prefix, strerror(errno));
  1839. goto out_clean;
  1840. }
  1841. nozzle_fd = nozzle_get_fd(nozzle_dev);
  1842. knet_log_printf (LOGSYS_LEVEL_INFO, "Opened '%s' on fd %d", device_name, nozzle_fd);
  1843. #ifdef KNET_DATAFD_FLAG_RX_RETURN_INFO
  1844. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel, 0);
  1845. #else
  1846. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel);
  1847. #endif
  1848. if (res != 0) {
  1849. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add nozzle FD to knet: %s", strerror(errno));
  1850. goto out_clean;
  1851. }
  1852. run_nozzle_script(instance, NOZZLE_PREUP, "pre-up");
  1853. res = nozzle_set_up(nozzle_dev);
  1854. if (res != 0) {
  1855. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to set nozzle interface UP: %s", strerror(errno));
  1856. goto out_clean;
  1857. }
  1858. run_nozzle_script(instance, NOZZLE_UP, "up");
  1859. return 0;
  1860. out_clean:
  1861. nozzle_close(nozzle_dev);
  1862. return -1;
  1863. }
  1864. static int remove_nozzle_device(void *knet_context)
  1865. {
  1866. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1867. int res;
  1868. int datafd;
  1869. res = knet_handle_get_datafd(instance->knet_handle, NOZZLE_CHANNEL, &datafd);
  1870. if (res != 0) {
  1871. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't find datafd for channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1872. return -1;
  1873. }
  1874. res = knet_handle_remove_datafd(instance->knet_handle, datafd);
  1875. if (res != 0) {
  1876. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't remove datafd for nozzle channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1877. return -1;
  1878. }
  1879. run_nozzle_script(instance, NOZZLE_DOWN, "pre-down");
  1880. res = nozzle_set_down(instance->nozzle_handle);
  1881. if (res != 0) {
  1882. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't set nozzle device down: %s", strerror(errno));
  1883. return -1;
  1884. }
  1885. run_nozzle_script(instance, NOZZLE_POSTDOWN, "post-down");
  1886. res = nozzle_close(instance->nozzle_handle);
  1887. if (res != 0) {
  1888. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't close nozzle device: %s", strerror(errno));
  1889. return -1;
  1890. }
  1891. knet_log_printf (LOGSYS_LEVEL_INFO, "Removed nozzle device");
  1892. return 0;
  1893. }
  1894. static void free_nozzle(struct totemknet_instance *instance)
  1895. {
  1896. free(instance->nozzle_name);
  1897. free(instance->nozzle_ipaddr);
  1898. free(instance->nozzle_prefix);
  1899. free(instance->nozzle_macaddr);
  1900. instance->nozzle_name = instance->nozzle_ipaddr = instance->nozzle_prefix =
  1901. instance->nozzle_macaddr = NULL;
  1902. }
  1903. static int setup_nozzle(void *knet_context)
  1904. {
  1905. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1906. char *ipaddr_str = NULL;
  1907. char *name_str = NULL;
  1908. char *prefix_str = NULL;
  1909. char *macaddr_str = NULL;
  1910. char mac[32];
  1911. int name_res;
  1912. int macaddr_res;
  1913. int res = -1;
  1914. /*
  1915. * Return value ignored on purpose. icmap_get_string changes
  1916. * ipaddr_str/prefix_str only on success.
  1917. */
  1918. (void)icmap_get_string(NOZZLE_IPADDR, &ipaddr_str);
  1919. (void)icmap_get_string(NOZZLE_PREFIX, &prefix_str);
  1920. macaddr_res = icmap_get_string(NOZZLE_MACADDR, &macaddr_str);
  1921. name_res = icmap_get_string(NOZZLE_NAME, &name_str);
  1922. /* Is is being removed? */
  1923. if (name_res == CS_ERR_NOT_EXIST && instance->nozzle_handle) {
  1924. remove_nozzle_device(instance);
  1925. free_nozzle(instance);
  1926. goto out_free;
  1927. }
  1928. if (!name_str) {
  1929. /* no nozzle */
  1930. goto out_free;
  1931. }
  1932. if (!ipaddr_str) {
  1933. knet_log_printf (LOGSYS_LEVEL_ERROR, "No IP address supplied for Nozzle device");
  1934. goto out_free;
  1935. }
  1936. if (!prefix_str) {
  1937. knet_log_printf (LOGSYS_LEVEL_ERROR, "No prefix supplied for Nozzle IP address");
  1938. goto out_free;
  1939. }
  1940. if (macaddr_str && strlen(macaddr_str) != 17) {
  1941. knet_log_printf (LOGSYS_LEVEL_ERROR, "macaddr for nozzle device is not in the correct format '%s'", macaddr_str);
  1942. goto out_free;
  1943. }
  1944. if (!macaddr_str) {
  1945. macaddr_str = (char*)"54:54:01:00:00:00";
  1946. }
  1947. if (instance->nozzle_name &&
  1948. (strcmp(name_str, instance->nozzle_name) == 0) &&
  1949. (strcmp(ipaddr_str, instance->nozzle_ipaddr) == 0) &&
  1950. (strcmp(prefix_str, instance->nozzle_prefix) == 0) &&
  1951. (instance->nozzle_macaddr == NULL ||
  1952. strcmp(macaddr_str, instance->nozzle_macaddr) == 0)) {
  1953. /* Nothing has changed */
  1954. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Nozzle device info not changed");
  1955. goto out_free;
  1956. }
  1957. /* Add nodeid into MAC address */
  1958. memcpy(mac, macaddr_str, 12);
  1959. snprintf(mac+12, sizeof(mac) - 13, "%02x:%02x",
  1960. instance->our_nodeid >> 8,
  1961. instance->our_nodeid & 0xFF);
  1962. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle MAC address is %s", mac);
  1963. if (name_res == CS_OK && name_str) {
  1964. /* Reconfigure */
  1965. if (instance->nozzle_name) {
  1966. remove_nozzle_device(instance);
  1967. free_nozzle(instance);
  1968. }
  1969. res = create_nozzle_device(knet_context, name_str, ipaddr_str, prefix_str,
  1970. mac);
  1971. instance->nozzle_name = strdup(name_str);
  1972. instance->nozzle_ipaddr = strdup(ipaddr_str);
  1973. instance->nozzle_prefix = strdup(prefix_str);
  1974. instance->nozzle_macaddr = strdup(macaddr_str);
  1975. if (!instance->nozzle_name || !instance->nozzle_ipaddr ||
  1976. !instance->nozzle_prefix) {
  1977. knet_log_printf (LOGSYS_LEVEL_ERROR, "strdup failed in nozzle allocation");
  1978. /*
  1979. * This 'free' will cause a complete reconfigure of the device next time we reload
  1980. * but will also let the the current device keep working until then.
  1981. * remove_nozzle() only needs the, statically-allocated, nozzle_handle
  1982. */
  1983. free_nozzle(instance);
  1984. }
  1985. }
  1986. out_free:
  1987. free(name_str);
  1988. free(ipaddr_str);
  1989. free(prefix_str);
  1990. if (macaddr_res == CS_OK) {
  1991. free(macaddr_str);
  1992. }
  1993. return res;
  1994. }
  1995. #endif // HAVE_LIBNOZZLE