4
0

totemknet.c 67 KB

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