totemknet.c 65 KB

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