4
0

totemknet.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  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. #endif
  1067. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  1068. if (res) {
  1069. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  1070. }
  1071. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  1072. if (res) {
  1073. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  1074. }
  1075. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  1076. if (res) {
  1077. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  1078. }
  1079. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  1080. if (res) {
  1081. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  1082. }
  1083. global_instance = instance;
  1084. /* Setup knet logging level */
  1085. totemknet_configure_log_level();
  1086. /* Get an fd into knet */
  1087. instance->knet_fd = 0;
  1088. #ifdef KNET_DATAFD_FLAG_RX_RETURN_INFO
  1089. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel, KNET_DATAFD_FLAG_RX_RETURN_INFO);
  1090. #else
  1091. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  1092. #endif
  1093. if (res) {
  1094. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  1095. goto exit_error;
  1096. }
  1097. /* Enable crypto if requested */
  1098. #ifdef HAVE_KNET_CRYPTO_RECONF
  1099. if (totemknet_is_crypto_enabled(instance)) {
  1100. res = totemknet_set_knet_crypto(instance);
  1101. if (res == 0) {
  1102. res = knet_handle_crypto_use_config(instance->knet_handle, totem_config->crypto_index);
  1103. if (res) {
  1104. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_use_config failed: %s", strerror(errno));
  1105. goto exit_error;
  1106. }
  1107. } else {
  1108. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1109. goto exit_error;
  1110. }
  1111. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1112. if (res) {
  1113. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (DISALLOW) failed: %s", strerror(errno));
  1114. goto exit_error;
  1115. }
  1116. } else {
  1117. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  1118. if (res) {
  1119. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (ALLOW) failed: %s", strerror(errno));
  1120. goto exit_error;
  1121. }
  1122. }
  1123. #else
  1124. if (totemknet_is_crypto_enabled(instance)) {
  1125. res = totemknet_set_knet_crypto(instance);
  1126. if (res) {
  1127. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1128. goto exit_error;
  1129. }
  1130. }
  1131. #endif
  1132. /* Set up compression */
  1133. if (strcmp(totem_config->knet_compression_model, "none") != 0) {
  1134. /* Not fatal, but will log */
  1135. (void)totemknet_configure_compression(instance, totem_config);
  1136. }
  1137. knet_handle_setfwd(instance->knet_handle, 1);
  1138. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  1139. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  1140. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  1141. }
  1142. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  1143. instance->link_mode = KNET_LINK_POLICY_RR;
  1144. }
  1145. for (i=0; i<INTERFACE_MAX; i++) {
  1146. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  1147. if (!instance->link_status[i]) {
  1148. goto exit_error;
  1149. }
  1150. }
  1151. qb_loop_poll_add (instance->poll_handle,
  1152. QB_LOOP_MED,
  1153. instance->logpipes[0],
  1154. POLLIN, instance, log_deliver_fn);
  1155. qb_loop_poll_add (instance->poll_handle,
  1156. QB_LOOP_HIGH,
  1157. instance->knet_fd,
  1158. POLLIN, instance, data_deliver_fn);
  1159. /*
  1160. * Upper layer isn't ready to receive message because it hasn't
  1161. * initialized yet. Add short timer to check the interfaces.
  1162. */
  1163. qb_loop_timer_add (instance->poll_handle,
  1164. QB_LOOP_MED,
  1165. 100*QB_TIME_NS_IN_MSEC,
  1166. (void *)instance,
  1167. timer_function_netif_check_timeout,
  1168. &instance->timer_netif_check_timeout);
  1169. totemknet_start_merge_detect_timeout(instance);
  1170. /* Start listening for config changes */
  1171. totemknet_add_config_notifications(instance);
  1172. /* Add stats keys to icmap */
  1173. stats_knet_add_handle();
  1174. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  1175. *knet_context = instance;
  1176. return (0);
  1177. exit_error:
  1178. log_flush_messages(instance);
  1179. free(instance);
  1180. return (-1);
  1181. }
  1182. void *totemknet_buffer_alloc (void)
  1183. {
  1184. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  1185. return malloc(KNET_MAX_PACKET_SIZE + 512);
  1186. }
  1187. void totemknet_buffer_release (void *ptr)
  1188. {
  1189. return free (ptr);
  1190. }
  1191. int totemknet_processor_count_set (
  1192. void *knet_context,
  1193. int processor_count)
  1194. {
  1195. return (0);
  1196. }
  1197. int totemknet_recv_flush (void *knet_context)
  1198. {
  1199. return (0);
  1200. }
  1201. int totemknet_send_flush (void *knet_context)
  1202. {
  1203. return (0);
  1204. }
  1205. int totemknet_token_send (
  1206. void *knet_context,
  1207. const void *msg,
  1208. unsigned int msg_len)
  1209. {
  1210. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1211. int res = 0;
  1212. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1213. return (res);
  1214. }
  1215. int totemknet_mcast_flush_send (
  1216. void *knet_context,
  1217. const void *msg,
  1218. unsigned int msg_len)
  1219. {
  1220. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1221. int res = 0;
  1222. mcast_sendmsg (instance, msg, msg_len, 0);
  1223. return (res);
  1224. }
  1225. int totemknet_mcast_noflush_send (
  1226. void *knet_context,
  1227. const void *msg,
  1228. unsigned int msg_len)
  1229. {
  1230. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1231. int res = 0;
  1232. mcast_sendmsg (instance, msg, msg_len, 1);
  1233. return (res);
  1234. }
  1235. extern int totemknet_iface_check (void *knet_context)
  1236. {
  1237. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1238. int res = 0;
  1239. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  1240. return (res);
  1241. }
  1242. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  1243. {
  1244. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1245. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  1246. }
  1247. int totemknet_token_target_set (
  1248. void *knet_context,
  1249. unsigned int nodeid)
  1250. {
  1251. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1252. int res = 0;
  1253. instance->token_target.nodeid = nodeid;
  1254. instance->totemknet_target_set_completed (instance->context);
  1255. return (res);
  1256. }
  1257. extern int totemknet_recv_mcast_empty (
  1258. void *knet_context)
  1259. {
  1260. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1261. unsigned int res;
  1262. struct sockaddr_storage system_from;
  1263. struct msghdr msg_hdr;
  1264. struct iovec iov_recv;
  1265. struct pollfd ufd;
  1266. int nfds;
  1267. int msg_processed = 0;
  1268. iov_recv.iov_base = instance->iov_buffer;
  1269. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  1270. memset(&msg_hdr, 0, sizeof(msg_hdr));
  1271. msg_hdr.msg_name = &system_from;
  1272. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  1273. msg_hdr.msg_iov = &iov_recv;
  1274. msg_hdr.msg_iovlen = 1;
  1275. do {
  1276. ufd.fd = instance->knet_fd;
  1277. ufd.events = POLLIN;
  1278. nfds = poll (&ufd, 1, 0);
  1279. if (nfds == 1 && ufd.revents & POLLIN) {
  1280. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  1281. if (res != -1) {
  1282. msg_processed = 1;
  1283. } else {
  1284. msg_processed = -1;
  1285. }
  1286. }
  1287. } while (nfds == 1);
  1288. return (msg_processed);
  1289. }
  1290. int totemknet_iface_set (void *knet_context,
  1291. const struct totem_ip_address *local_addr,
  1292. unsigned short ip_port,
  1293. unsigned int iface_no)
  1294. {
  1295. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1296. totemip_copy(&instance->my_ids[iface_no], local_addr);
  1297. knet_log_printf(LOG_INFO, "Configured link number %d: local addr: %s, port=%d", iface_no, totemip_print(local_addr), ip_port);
  1298. instance->ip_port[iface_no] = ip_port;
  1299. return 0;
  1300. }
  1301. int totemknet_member_add (
  1302. void *knet_context,
  1303. const struct totem_ip_address *local,
  1304. const struct totem_ip_address *member,
  1305. int link_no)
  1306. {
  1307. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1308. int err;
  1309. int port = instance->ip_port[link_no];
  1310. struct sockaddr_storage remote_ss;
  1311. struct sockaddr_storage local_ss;
  1312. int addrlen;
  1313. int i;
  1314. int host_found = 0;
  1315. knet_node_id_t host_ids[KNET_MAX_HOST];
  1316. size_t num_host_ids;
  1317. /* Only create 1 loopback link and use link 0 */
  1318. if (member->nodeid == instance->our_nodeid) {
  1319. if (!instance->loopback_link) {
  1320. link_no = 0;
  1321. instance->loopback_link = 1;
  1322. } else {
  1323. /* Already done */
  1324. return 0;
  1325. }
  1326. }
  1327. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: " CS_PRI_NODE_ID " (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  1328. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: " CS_PRI_NODE_ID " (%s)", local->nodeid, totemip_print(local));
  1329. /* Only add the host if it doesn't already exist in knet */
  1330. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_host_ids);
  1331. if (err) {
  1332. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list");
  1333. return -1;
  1334. }
  1335. for (i=0; i<num_host_ids; i++) {
  1336. if (host_ids[i] == member->nodeid) {
  1337. host_found = 1;
  1338. }
  1339. }
  1340. if (!host_found) {
  1341. err = knet_host_add(instance->knet_handle, member->nodeid);
  1342. if (err != 0 && errno != EEXIST) {
  1343. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  1344. return -1;
  1345. }
  1346. } else {
  1347. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nodeid " CS_PRI_NODE_ID " already added", member->nodeid);
  1348. }
  1349. if (err == 0) {
  1350. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  1351. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  1352. return -1;
  1353. }
  1354. }
  1355. memset(&local_ss, 0, sizeof(local_ss));
  1356. memset(&remote_ss, 0, sizeof(remote_ss));
  1357. /* Casts to remove const */
  1358. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port, &remote_ss, &addrlen);
  1359. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port, &local_ss, &addrlen);
  1360. if (member->nodeid == instance->our_nodeid) {
  1361. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: loopback link is %d\n", link_no);
  1362. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1363. KNET_TRANSPORT_LOOPBACK,
  1364. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1365. }
  1366. else {
  1367. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1368. instance->totem_config->interfaces[link_no].knet_transport,
  1369. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1370. }
  1371. if (err) {
  1372. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  1373. return -1;
  1374. }
  1375. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  1376. instance->totem_config->interfaces[link_no].knet_link_priority);
  1377. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  1378. instance->totem_config->interfaces[link_no].knet_link_priority);
  1379. if (err) {
  1380. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1381. }
  1382. /*
  1383. * Ping timeouts may be 0 here for a newly added interface (on a reload),
  1384. * so we leave this till later, it will get done in totemknet_refresh_config.
  1385. * For the initial startup, we are all preset and ready to go from here.
  1386. */
  1387. if (instance->totem_config->interfaces[link_no].knet_ping_interval != 0) {
  1388. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  1389. instance->totem_config->interfaces[link_no].knet_ping_interval,
  1390. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  1391. instance->totem_config->interfaces[link_no].knet_ping_precision);
  1392. if (err) {
  1393. /* Flush logs before reporting this error so that the knet message prints before ours */
  1394. int saved_errno = errno;
  1395. log_flush_messages(instance);
  1396. errno = saved_errno;
  1397. 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);
  1398. icmap_set_string("config.reload_error_message", "Failed to set knet ping timers");
  1399. return -1;
  1400. }
  1401. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  1402. instance->totem_config->interfaces[link_no].knet_pong_count);
  1403. if (err) {
  1404. /* Flush logs before reporting this error so that the knet message prints before ours */
  1405. int saved_errno = errno;
  1406. log_flush_messages(instance);
  1407. errno = saved_errno;
  1408. 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);
  1409. icmap_set_string("config.reload_error_message", "Failed to set knet pong count");
  1410. return -1;
  1411. }
  1412. }
  1413. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1414. if (err) {
  1415. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1416. return -1;
  1417. }
  1418. /* register stats */
  1419. stats_knet_add_member(member->nodeid, link_no);
  1420. return (0);
  1421. }
  1422. int totemknet_member_remove (
  1423. void *knet_context,
  1424. const struct totem_ip_address *token_target,
  1425. int link_no)
  1426. {
  1427. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1428. int res;
  1429. uint8_t link_list[KNET_MAX_LINK];
  1430. size_t num_links;
  1431. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: " CS_PRI_NODE_ID ", link=%d", token_target->nodeid, link_no);
  1432. /* Don't remove the link with the loopback on it until we shut down */
  1433. if (token_target->nodeid == instance->our_nodeid) {
  1434. return 0;
  1435. }
  1436. /* Tidy stats */
  1437. stats_knet_del_member(token_target->nodeid, link_no);
  1438. /* Remove the link first */
  1439. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1440. if (res != 0) {
  1441. 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);
  1442. return res;
  1443. }
  1444. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1445. if (res != 0) {
  1446. 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);
  1447. return res;
  1448. }
  1449. /* If this is the last link, then remove the node */
  1450. res = knet_link_get_link_list(instance->knet_handle,
  1451. token_target->nodeid, link_list, &num_links);
  1452. if (res) {
  1453. return (0); /* not really failure */
  1454. }
  1455. if (num_links == 0) {
  1456. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1457. }
  1458. return res;
  1459. }
  1460. int totemknet_member_list_rebind_ip (
  1461. void *knet_context)
  1462. {
  1463. return (0);
  1464. }
  1465. static int totemknet_configure_compression (
  1466. struct totemknet_instance *instance,
  1467. struct totem_config *totem_config)
  1468. {
  1469. struct knet_handle_compress_cfg compress_cfg;
  1470. int res = 0;
  1471. assert(strlen(totem_config->knet_compression_model) < sizeof(compress_cfg.compress_model));
  1472. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1473. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1474. compress_cfg.compress_level = totem_config->knet_compression_level;
  1475. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1476. if (res) {
  1477. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1478. }
  1479. return res;
  1480. }
  1481. int totemknet_reconfigure (
  1482. void *knet_context,
  1483. struct totem_config *totem_config)
  1484. {
  1485. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1486. int res = 0;
  1487. (void)totemknet_configure_compression(instance, totem_config);
  1488. #ifdef HAVE_LIBNOZZLE
  1489. /* Set up nozzle device(s). Return code is ignored, because inability
  1490. * configure nozzle is not fatal problem, errors are logged and
  1491. * there is not much else we can do */
  1492. (void)setup_nozzle(instance);
  1493. #endif
  1494. if (totem_config->crypto_changed) {
  1495. /* Flip crypto_index */
  1496. totem_config->crypto_index = 3-totem_config->crypto_index;
  1497. res = totemknet_set_knet_crypto(instance);
  1498. if (res == 0) {
  1499. knet_log_printf(LOG_INFO, "kronosnet crypto reconfigured on index %d: %s/%s/%s", totem_config->crypto_index,
  1500. totem_config->crypto_model,
  1501. totem_config->crypto_cipher_type,
  1502. totem_config->crypto_hash_type);
  1503. } else {
  1504. icmap_set_string("config.reload_error_message", "Failed to set knet crypto");
  1505. }
  1506. }
  1507. return (res);
  1508. }
  1509. int totemknet_crypto_reconfigure_phase (
  1510. void *knet_context,
  1511. struct totem_config *totem_config,
  1512. cfg_message_crypto_reconfig_phase_t phase)
  1513. {
  1514. #ifdef HAVE_KNET_CRYPTO_RECONF
  1515. int res;
  1516. int config_to_use;
  1517. int config_to_clear;
  1518. struct knet_handle_crypto_cfg crypto_cfg;
  1519. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1520. knet_log_printf(LOGSYS_LEVEL_DEBUG, "totemknet_crypto_reconfigure_phase %d, index=%d\n", phase, totem_config->crypto_index);
  1521. switch (phase) {
  1522. case CRYPTO_RECONFIG_PHASE_ACTIVATE:
  1523. config_to_use = totem_config->crypto_index;
  1524. if (!totemknet_is_crypto_enabled(instance)) {
  1525. config_to_use = 0; /* we are clearing it */
  1526. }
  1527. /* Enable the new config on this node */
  1528. res = knet_handle_crypto_use_config(instance->knet_handle, config_to_use);
  1529. if (res == -1) {
  1530. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_use_config %d failed: %s", config_to_use, strerror(errno));
  1531. }
  1532. break;
  1533. case CRYPTO_RECONFIG_PHASE_CLEANUP:
  1534. /*
  1535. * All nodes should now have the new config. clear the old one out
  1536. * OR disable crypto entirely if that's what the new config insists on.
  1537. */
  1538. config_to_clear = 3-totem_config->crypto_index;
  1539. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Clearing old knet crypto config %d\n", config_to_clear);
  1540. strcpy(crypto_cfg.crypto_model, "none");
  1541. strcpy(crypto_cfg.crypto_cipher_type, "none");
  1542. strcpy(crypto_cfg.crypto_hash_type, "none");
  1543. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, config_to_clear);
  1544. if (res == -1) {
  1545. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: %s", config_to_clear, strerror(errno));
  1546. }
  1547. if (res == -2) {
  1548. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: -2", config_to_clear);
  1549. }
  1550. /* If crypto is enabled then disable all cleartext reception */
  1551. if (totemknet_is_crypto_enabled(instance)) {
  1552. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1553. if (res) {
  1554. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(DISALLOW) failed %s", strerror(errno));
  1555. }
  1556. }
  1557. }
  1558. #endif
  1559. return 0;
  1560. }
  1561. void totemknet_stats_clear (
  1562. void *knet_context)
  1563. {
  1564. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1565. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1566. }
  1567. /* For the stats module */
  1568. int totemknet_link_get_status (
  1569. knet_node_id_t node, uint8_t link_no,
  1570. struct knet_link_status *status)
  1571. {
  1572. int res;
  1573. int ret = CS_OK;
  1574. /* We are probably not using knet */
  1575. if (!global_instance) {
  1576. return CS_ERR_NOT_EXIST;
  1577. }
  1578. if (link_no >= INTERFACE_MAX) {
  1579. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1580. }
  1581. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1582. if (res) {
  1583. switch (errno) {
  1584. case EINVAL:
  1585. ret = CS_ERR_INVALID_PARAM;
  1586. break;
  1587. case EBUSY:
  1588. ret = CS_ERR_BUSY;
  1589. break;
  1590. case EDEADLK:
  1591. ret = CS_ERR_TRY_AGAIN;
  1592. break;
  1593. default:
  1594. ret = CS_ERR_LIBRARY;
  1595. break;
  1596. }
  1597. }
  1598. return (ret);
  1599. }
  1600. int totemknet_handle_get_stats (
  1601. struct knet_handle_stats *stats)
  1602. {
  1603. int res;
  1604. /* We are probably not using knet */
  1605. if (!global_instance) {
  1606. return CS_ERR_NOT_EXIST;
  1607. }
  1608. res = knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1609. if (res != 0) {
  1610. return (qb_to_cs_error(-errno));
  1611. }
  1612. return CS_OK;
  1613. }
  1614. static void timer_function_merge_detect_timeout (
  1615. void *data)
  1616. {
  1617. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1618. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1619. instance->send_merge_detect_message = 1;
  1620. }
  1621. instance->merge_detect_messages_sent_before_timeout = 0;
  1622. totemknet_start_merge_detect_timeout(instance);
  1623. }
  1624. static void totemknet_start_merge_detect_timeout(
  1625. void *knet_context)
  1626. {
  1627. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1628. qb_loop_timer_add(instance->poll_handle,
  1629. QB_LOOP_MED,
  1630. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1631. (void *)instance,
  1632. timer_function_merge_detect_timeout,
  1633. &instance->timer_merge_detect_timeout);
  1634. }
  1635. static void totemknet_stop_merge_detect_timeout(
  1636. void *knet_context)
  1637. {
  1638. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1639. qb_loop_timer_del(instance->poll_handle,
  1640. instance->timer_merge_detect_timeout);
  1641. }
  1642. static void log_flush_messages (void *knet_context)
  1643. {
  1644. struct pollfd pfd;
  1645. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1646. int cont;
  1647. cont = 1;
  1648. while (cont) {
  1649. pfd.fd = instance->logpipes[0];
  1650. pfd.events = POLLIN;
  1651. pfd.revents = 0;
  1652. if ((poll(&pfd, 1, 0) > 0) &&
  1653. (pfd.revents & POLLIN) &&
  1654. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1655. cont = 1;
  1656. } else {
  1657. cont = 0;
  1658. }
  1659. }
  1660. }
  1661. #ifdef HAVE_LIBNOZZLE
  1662. #define NOZZLE_NAME "nozzle.name"
  1663. #define NOZZLE_IPADDR "nozzle.ipaddr"
  1664. #define NOZZLE_PREFIX "nozzle.ipprefix"
  1665. #define NOZZLE_MACADDR "nozzle.macaddr"
  1666. #define NOZZLE_CHANNEL 1
  1667. static char *get_nozzle_script_dir(void *knet_context)
  1668. {
  1669. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1670. char filename[PATH_MAX + FILENAME_MAX + 1];
  1671. static char updown_dirname[PATH_MAX + FILENAME_MAX + 1];
  1672. int res;
  1673. const char *dirname_res;
  1674. /*
  1675. * Build script directory based on corosync.conf file location
  1676. */
  1677. res = snprintf(filename, sizeof(filename), "%s",
  1678. corosync_get_config_file());
  1679. if (res >= sizeof(filename)) {
  1680. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1681. return NULL;
  1682. }
  1683. dirname_res = dirname(filename);
  1684. res = snprintf(updown_dirname, sizeof(updown_dirname), "%s/%s",
  1685. dirname_res, "updown.d");
  1686. if (res >= sizeof(updown_dirname)) {
  1687. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1688. return NULL;
  1689. }
  1690. return updown_dirname;
  1691. }
  1692. /*
  1693. * Deliberately doesn't return the status as caller doesn't care.
  1694. * The result will be logged though
  1695. */
  1696. static void run_nozzle_script(struct totemknet_instance *instance, int type, const char *typename)
  1697. {
  1698. int res;
  1699. char *exec_string;
  1700. res = nozzle_run_updown(instance->nozzle_handle, type, &exec_string);
  1701. if (res == -1 && errno != ENOENT) {
  1702. knet_log_printf (LOGSYS_LEVEL_INFO, "exec nozzle %s script failed: %s", typename, strerror(errno));
  1703. } else if (res == -2) {
  1704. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle %s script failed", typename);
  1705. knet_log_printf (LOGSYS_LEVEL_INFO, "%s", exec_string);
  1706. }
  1707. }
  1708. /*
  1709. * Reparse IP address to add in our node ID
  1710. * IPv6 addresses must end in '::'
  1711. * IPv4 addresses must just be valid
  1712. * '/xx' lengths are optional for IPv6, mandatory for IPv4
  1713. *
  1714. * Returns the modified IP address as a string to pass into libnozzle
  1715. */
  1716. static int reparse_nozzle_ip_address(struct totemknet_instance *instance,
  1717. const char *input_addr,
  1718. const char *prefix, int nodeid,
  1719. char *output_addr, size_t output_len)
  1720. {
  1721. const char *coloncolon;
  1722. int bits;
  1723. int max_prefix = 64;
  1724. uint32_t nodeid_mask;
  1725. uint32_t addr_mask;
  1726. uint32_t masked_nodeid;
  1727. struct in_addr *addr;
  1728. struct totem_ip_address totemip;
  1729. coloncolon = strstr(input_addr, "::");
  1730. if (!coloncolon) {
  1731. max_prefix = 30;
  1732. }
  1733. bits = atoi(prefix);
  1734. if (bits < 8 || bits > max_prefix) {
  1735. knet_log_printf(LOGSYS_LEVEL_ERROR, "nozzle IP address prefix must be >= 8 and <= %d (got %d)", max_prefix, bits);
  1736. return -1;
  1737. }
  1738. /* IPv6 is easy */
  1739. if (coloncolon) {
  1740. memcpy(output_addr, input_addr, coloncolon-input_addr);
  1741. sprintf(output_addr + (coloncolon-input_addr), "::%x", nodeid);
  1742. return 0;
  1743. }
  1744. /* For IPv4 we need to parse the address into binary, mask off the required bits,
  1745. * add in the masked_nodeid and 'print' it out again
  1746. */
  1747. nodeid_mask = UINT32_MAX & ((1<<(32 - bits)) - 1);
  1748. addr_mask = UINT32_MAX ^ nodeid_mask;
  1749. masked_nodeid = nodeid & nodeid_mask;
  1750. if (totemip_parse(&totemip, input_addr, AF_INET)) {
  1751. knet_log_printf(LOGSYS_LEVEL_ERROR, "Failed to parse IPv4 nozzle IP address");
  1752. return -1;
  1753. }
  1754. addr = (struct in_addr *)&totemip.addr;
  1755. addr->s_addr &= htonl(addr_mask);
  1756. addr->s_addr |= htonl(masked_nodeid);
  1757. inet_ntop(AF_INET, addr, output_addr, output_len);
  1758. return 0;
  1759. }
  1760. static int create_nozzle_device(void *knet_context, const char *name,
  1761. const char *ipaddr, const char *prefix,
  1762. const char *macaddr)
  1763. {
  1764. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1765. char device_name[IFNAMSIZ+1];
  1766. size_t size = IFNAMSIZ;
  1767. int8_t channel = NOZZLE_CHANNEL;
  1768. nozzle_t nozzle_dev;
  1769. int nozzle_fd;
  1770. int res;
  1771. char *updown_dir;
  1772. char parsed_ipaddr[INET6_ADDRSTRLEN];
  1773. char mac[19];
  1774. memset(device_name, 0, size);
  1775. memset(&mac, 0, sizeof(mac));
  1776. strncpy(device_name, name, size);
  1777. updown_dir = get_nozzle_script_dir(knet_context);
  1778. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle script dir is %s", updown_dir);
  1779. nozzle_dev = nozzle_open(device_name, size, updown_dir);
  1780. if (!nozzle_dev) {
  1781. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to init nozzle device %s: %s", device_name, strerror(errno));
  1782. return -1;
  1783. }
  1784. instance->nozzle_handle = nozzle_dev;
  1785. if (nozzle_set_mac(nozzle_dev, macaddr) < 0) {
  1786. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle MAC to %s: %s", mac, strerror(errno));
  1787. goto out_clean;
  1788. }
  1789. if (reparse_nozzle_ip_address(instance, ipaddr, prefix, instance->our_nodeid, parsed_ipaddr, sizeof(parsed_ipaddr))) {
  1790. /* Prints its own errors */
  1791. goto out_clean;
  1792. }
  1793. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle IP address is %s / %d", parsed_ipaddr, atoi(prefix));
  1794. if (nozzle_add_ip(nozzle_dev, parsed_ipaddr, prefix) < 0) {
  1795. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle IP addr to %s/%s: %s", parsed_ipaddr, prefix, strerror(errno));
  1796. goto out_clean;
  1797. }
  1798. nozzle_fd = nozzle_get_fd(nozzle_dev);
  1799. knet_log_printf (LOGSYS_LEVEL_INFO, "Opened '%s' on fd %d", device_name, nozzle_fd);
  1800. #ifdef KNET_DATAFD_FLAG_RX_RETURN_INFO
  1801. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel, 0);
  1802. #else
  1803. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel);
  1804. #endif
  1805. if (res != 0) {
  1806. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add nozzle FD to knet: %s", strerror(errno));
  1807. goto out_clean;
  1808. }
  1809. run_nozzle_script(instance, NOZZLE_PREUP, "pre-up");
  1810. res = nozzle_set_up(nozzle_dev);
  1811. if (res != 0) {
  1812. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to set nozzle interface UP: %s", strerror(errno));
  1813. goto out_clean;
  1814. }
  1815. run_nozzle_script(instance, NOZZLE_UP, "up");
  1816. return 0;
  1817. out_clean:
  1818. nozzle_close(nozzle_dev);
  1819. return -1;
  1820. }
  1821. static int remove_nozzle_device(void *knet_context)
  1822. {
  1823. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1824. int res;
  1825. int datafd;
  1826. res = knet_handle_get_datafd(instance->knet_handle, NOZZLE_CHANNEL, &datafd);
  1827. if (res != 0) {
  1828. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't find datafd for channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1829. return -1;
  1830. }
  1831. res = knet_handle_remove_datafd(instance->knet_handle, datafd);
  1832. if (res != 0) {
  1833. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't remove datafd for nozzle channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1834. return -1;
  1835. }
  1836. run_nozzle_script(instance, NOZZLE_DOWN, "pre-down");
  1837. res = nozzle_set_down(instance->nozzle_handle);
  1838. if (res != 0) {
  1839. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't set nozzle device down: %s", strerror(errno));
  1840. return -1;
  1841. }
  1842. run_nozzle_script(instance, NOZZLE_POSTDOWN, "post-down");
  1843. res = nozzle_close(instance->nozzle_handle);
  1844. if (res != 0) {
  1845. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't close nozzle device: %s", strerror(errno));
  1846. return -1;
  1847. }
  1848. knet_log_printf (LOGSYS_LEVEL_INFO, "Removed nozzle device");
  1849. return 0;
  1850. }
  1851. static void free_nozzle(struct totemknet_instance *instance)
  1852. {
  1853. free(instance->nozzle_name);
  1854. free(instance->nozzle_ipaddr);
  1855. free(instance->nozzle_prefix);
  1856. free(instance->nozzle_macaddr);
  1857. instance->nozzle_name = instance->nozzle_ipaddr = instance->nozzle_prefix =
  1858. instance->nozzle_macaddr = NULL;
  1859. }
  1860. static int setup_nozzle(void *knet_context)
  1861. {
  1862. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1863. char *ipaddr_str = NULL;
  1864. char *name_str = NULL;
  1865. char *prefix_str = NULL;
  1866. char *macaddr_str = NULL;
  1867. char mac[32];
  1868. int name_res;
  1869. int macaddr_res;
  1870. int res = -1;
  1871. /*
  1872. * Return value ignored on purpose. icmap_get_string changes
  1873. * ipaddr_str/prefix_str only on success.
  1874. */
  1875. (void)icmap_get_string(NOZZLE_IPADDR, &ipaddr_str);
  1876. (void)icmap_get_string(NOZZLE_PREFIX, &prefix_str);
  1877. macaddr_res = icmap_get_string(NOZZLE_MACADDR, &macaddr_str);
  1878. name_res = icmap_get_string(NOZZLE_NAME, &name_str);
  1879. /* Is is being removed? */
  1880. if (name_res == CS_ERR_NOT_EXIST && instance->nozzle_handle) {
  1881. remove_nozzle_device(instance);
  1882. free_nozzle(instance);
  1883. goto out_free;
  1884. }
  1885. if (!name_str) {
  1886. /* no nozzle */
  1887. goto out_free;
  1888. }
  1889. if (!ipaddr_str) {
  1890. knet_log_printf (LOGSYS_LEVEL_ERROR, "No IP address supplied for Nozzle device");
  1891. goto out_free;
  1892. }
  1893. if (!prefix_str) {
  1894. knet_log_printf (LOGSYS_LEVEL_ERROR, "No prefix supplied for Nozzle IP address");
  1895. goto out_free;
  1896. }
  1897. if (macaddr_str && strlen(macaddr_str) != 17) {
  1898. knet_log_printf (LOGSYS_LEVEL_ERROR, "macaddr for nozzle device is not in the correct format '%s'", macaddr_str);
  1899. goto out_free;
  1900. }
  1901. if (!macaddr_str) {
  1902. macaddr_str = (char*)"54:54:01:00:00:00";
  1903. }
  1904. if (instance->nozzle_name &&
  1905. (strcmp(name_str, instance->nozzle_name) == 0) &&
  1906. (strcmp(ipaddr_str, instance->nozzle_ipaddr) == 0) &&
  1907. (strcmp(prefix_str, instance->nozzle_prefix) == 0) &&
  1908. (instance->nozzle_macaddr == NULL ||
  1909. strcmp(macaddr_str, instance->nozzle_macaddr) == 0)) {
  1910. /* Nothing has changed */
  1911. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Nozzle device info not changed");
  1912. goto out_free;
  1913. }
  1914. /* Add nodeid into MAC address */
  1915. memcpy(mac, macaddr_str, 12);
  1916. snprintf(mac+12, sizeof(mac) - 13, "%02x:%02x",
  1917. instance->our_nodeid >> 8,
  1918. instance->our_nodeid & 0xFF);
  1919. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle MAC address is %s", mac);
  1920. if (name_res == CS_OK && name_str) {
  1921. /* Reconfigure */
  1922. if (instance->nozzle_name) {
  1923. remove_nozzle_device(instance);
  1924. free_nozzle(instance);
  1925. }
  1926. res = create_nozzle_device(knet_context, name_str, ipaddr_str, prefix_str,
  1927. mac);
  1928. instance->nozzle_name = strdup(name_str);
  1929. instance->nozzle_ipaddr = strdup(ipaddr_str);
  1930. instance->nozzle_prefix = strdup(prefix_str);
  1931. instance->nozzle_macaddr = strdup(macaddr_str);
  1932. if (!instance->nozzle_name || !instance->nozzle_ipaddr ||
  1933. !instance->nozzle_prefix) {
  1934. knet_log_printf (LOGSYS_LEVEL_ERROR, "strdup failed in nozzle allocation");
  1935. /*
  1936. * This 'free' will cause a complete reconfigure of the device next time we reload
  1937. * but will also let the the current device keep working until then.
  1938. * remove_nozzle() only needs the, statically-allocated, nozzle_handle
  1939. */
  1940. free_nozzle(instance);
  1941. }
  1942. }
  1943. out_free:
  1944. free(name_str);
  1945. free(ipaddr_str);
  1946. free(prefix_str);
  1947. if (macaddr_res == CS_OK) {
  1948. free(macaddr_str);
  1949. }
  1950. return res;
  1951. }
  1952. #endif // HAVE_LIBNOZZLE