totemknet.c 68 KB

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