totemknet.c 68 KB

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