totemknet.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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. 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];
  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. int res = 0;
  734. for (i=0; i < INTERFACE_MAX; i++) {
  735. if (!instance->totem_config->interfaces[i].configured) {
  736. continue;
  737. }
  738. res = instance->totemknet_iface_change_fn (instance->context,
  739. &instance->my_ids[i],
  740. i);
  741. }
  742. if (res != 0) {
  743. /* This is only called at startup, so we can quit here.
  744. Refresh takes a different path */
  745. corosync_exit_error(COROSYNC_DONE_MAINCONFIGREAD);
  746. }
  747. }
  748. static void knet_set_access_list_config(struct totemknet_instance *instance)
  749. {
  750. #ifdef HAVE_KNET_ACCESS_LIST
  751. uint32_t value;
  752. cs_error_t err;
  753. value = instance->totem_config->block_unlisted_ips;
  754. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_enable access list: %d", value);
  755. err = knet_handle_enable_access_lists(instance->knet_handle, value);
  756. if (err) {
  757. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_access_lists failed");
  758. }
  759. #endif
  760. }
  761. void totemknet_configure_log_level()
  762. {
  763. int logsys_log_mode;
  764. int knet_log_mode = KNET_LOG_INFO;
  765. uint8_t s;
  766. int err;
  767. if (!global_instance || !global_instance->knet_handle) {
  768. return;
  769. }
  770. /* Reconfigure logging level */
  771. logsys_log_mode = logsys_config_debug_get("KNET");
  772. switch (logsys_log_mode) {
  773. case LOGSYS_DEBUG_OFF:
  774. knet_log_mode = KNET_LOG_INFO;
  775. break;
  776. case LOGSYS_DEBUG_ON:
  777. knet_log_mode = KNET_LOG_DEBUG;
  778. break;
  779. case LOGSYS_DEBUG_TRACE:
  780. #ifdef KNET_LOG_TRACE
  781. knet_log_mode = KNET_LOG_TRACE;
  782. #else
  783. knet_log_mode = KNET_LOG_DEBUG;
  784. #endif
  785. break;
  786. }
  787. log_printf (LOGSYS_LEVEL_DEBUG, "totemknet setting log level %s", knet_log_get_loglevel_name(knet_log_mode));
  788. err = 0;
  789. for (s = 0; s<KNET_MAX_SUBSYSTEMS; s++) {
  790. err = knet_log_set_loglevel(global_instance->knet_handle, s, knet_log_mode);
  791. }
  792. /* If one fails, they all fail. no point in issuing KNET_MAX_SUBSYSTEMS errors */
  793. if (err) {
  794. log_printf (LOGSYS_LEVEL_ERROR, "totemknet failed to set log level: %s", strerror(errno));
  795. }
  796. }
  797. /* NOTE: this relies on the fact that totem_reload_notify() is called first */
  798. static void totemknet_refresh_config(
  799. int32_t event,
  800. const char *key_name,
  801. struct icmap_notify_value new_val,
  802. struct icmap_notify_value old_val,
  803. void *user_data)
  804. {
  805. uint8_t reloading;
  806. int after_reload;
  807. uint32_t link_no;
  808. size_t num_nodes;
  809. knet_node_id_t host_ids[KNET_MAX_HOST];
  810. int i;
  811. int err;
  812. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  813. ENTER();
  814. /*
  815. * If a full reload is in progress then don't do anything until it's done and
  816. * can reconfigure it all atomically
  817. */
  818. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  819. return;
  820. }
  821. after_reload = (strcmp(key_name, "config.totemconfig_reload_in_progress") == 0);
  822. knet_set_access_list_config(instance);
  823. if (strcmp(key_name, "totem.knet_pmtud_interval") == 0 || after_reload) {
  824. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %u",
  825. instance->totem_config->knet_pmtud_interval);
  826. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  827. if (err) {
  828. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  829. }
  830. }
  831. if (strcmp(key_name, "totem.knet_mtu") == 0 || after_reload) {
  832. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_mtu now %u", instance->totem_config->knet_mtu);
  833. err = knet_handle_pmtud_set(instance->knet_handle, instance->totem_config->knet_mtu);
  834. if (err) {
  835. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud failed");
  836. }
  837. }
  838. /* Configure link parameters for each node */
  839. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  840. if (err != 0) {
  841. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  842. }
  843. for (i=0; i<num_nodes; i++) {
  844. for (link_no = 0; link_no < INTERFACE_MAX; link_no++) {
  845. if (host_ids[i] == instance->our_nodeid || !instance->totem_config->interfaces[link_no].configured) {
  846. continue;
  847. }
  848. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  849. instance->totem_config->interfaces[link_no].knet_ping_interval,
  850. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  851. instance->totem_config->interfaces[link_no].knet_ping_precision);
  852. if (err) {
  853. 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);
  854. }
  855. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  856. instance->totem_config->interfaces[link_no].knet_pong_count);
  857. if (err) {
  858. 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);
  859. }
  860. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  861. instance->totem_config->interfaces[link_no].knet_link_priority);
  862. if (err) {
  863. 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);
  864. }
  865. }
  866. }
  867. /* Log levels get reconfigured from logconfig.c as that happens last in the reload */
  868. LEAVE();
  869. }
  870. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  871. {
  872. icmap_track_t icmap_track_totem = NULL;
  873. icmap_track_t icmap_track_reload = NULL;
  874. ENTER();
  875. icmap_track_add("totem.",
  876. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  877. totemknet_refresh_config,
  878. instance,
  879. &icmap_track_totem);
  880. icmap_track_add("config.totemconfig_reload_in_progress",
  881. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  882. totemknet_refresh_config,
  883. instance,
  884. &icmap_track_reload);
  885. LEAVE();
  886. }
  887. static int totemknet_is_crypto_enabled(const struct totemknet_instance *instance)
  888. {
  889. return (!(strcmp(instance->totem_config->crypto_cipher_type, "none") == 0 &&
  890. strcmp(instance->totem_config->crypto_hash_type, "none") == 0));
  891. }
  892. static int totemknet_set_knet_crypto(struct totemknet_instance *instance)
  893. {
  894. struct knet_handle_crypto_cfg crypto_cfg;
  895. int res;
  896. /* These have already been validated */
  897. memcpy(crypto_cfg.crypto_model, instance->totem_config->crypto_model, sizeof(crypto_cfg.crypto_model));
  898. memcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type, sizeof(crypto_cfg.crypto_model));
  899. memcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type, sizeof(crypto_cfg.crypto_model));
  900. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  901. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  902. #ifdef HAVE_KNET_CRYPTO_RECONF
  903. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s on index %d",
  904. crypto_cfg.crypto_model,
  905. crypto_cfg.crypto_cipher_type,
  906. crypto_cfg.crypto_hash_type,
  907. instance->totem_config->crypto_index
  908. );
  909. /* If crypto is being disabled we need to explicitly allow cleartext traffic in knet */
  910. if (!totemknet_is_crypto_enabled(instance)) {
  911. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  912. if (res) {
  913. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(ALLOW) failed %s", strerror(errno));
  914. }
  915. }
  916. /* use_config will be called later when all nodes are synced */
  917. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, instance->totem_config->crypto_index);
  918. if (res == -1) {
  919. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: %s", instance->totem_config->crypto_index, strerror(errno));
  920. goto exit_error;
  921. }
  922. if (res == -2) {
  923. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: -2", instance->totem_config->crypto_index);
  924. goto exit_error;
  925. }
  926. #else
  927. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s",
  928. crypto_cfg.crypto_model,
  929. crypto_cfg.crypto_cipher_type,
  930. crypto_cfg.crypto_hash_type
  931. );
  932. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  933. if (res == -1) {
  934. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  935. goto exit_error;
  936. }
  937. if (res == -2) {
  938. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  939. goto exit_error;
  940. }
  941. #endif
  942. exit_error:
  943. return res;
  944. }
  945. /*
  946. * Create an instance
  947. */
  948. int totemknet_initialize (
  949. qb_loop_t *poll_handle,
  950. void **knet_context,
  951. struct totem_config *totem_config,
  952. totemsrp_stats_t *stats,
  953. void *context,
  954. int (*deliver_fn) (
  955. void *context,
  956. const void *msg,
  957. unsigned int msg_len,
  958. const struct sockaddr_storage *system_from),
  959. int (*iface_change_fn) (
  960. void *context,
  961. const struct totem_ip_address *iface_address,
  962. unsigned int link_no),
  963. void (*mtu_changed) (
  964. void *context,
  965. int net_mtu),
  966. void (*target_set_completed) (
  967. void *context))
  968. {
  969. struct totemknet_instance *instance;
  970. char *tmp_str;
  971. int8_t channel=0;
  972. int allow_knet_handle_fallback=0;
  973. int res;
  974. int i;
  975. instance = malloc (sizeof (struct totemknet_instance));
  976. if (instance == NULL) {
  977. return (-1);
  978. }
  979. totemknet_instance_initialize (instance);
  980. instance->totem_config = totem_config;
  981. /*
  982. * Configure logging
  983. */
  984. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  985. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  986. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  987. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  988. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  989. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  990. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  991. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  992. /*
  993. * Initialize local variables for totemknet
  994. */
  995. instance->our_nodeid = instance->totem_config->node_id;
  996. for (i=0; i< INTERFACE_MAX; i++) {
  997. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  998. instance->my_ids[i].nodeid = instance->our_nodeid;
  999. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  1000. /* Needed for totemsrp */
  1001. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  1002. }
  1003. instance->poll_handle = poll_handle;
  1004. instance->context = context;
  1005. instance->totemknet_deliver_fn = deliver_fn;
  1006. instance->totemknet_iface_change_fn = iface_change_fn;
  1007. instance->totemknet_mtu_changed = mtu_changed;
  1008. instance->totemknet_target_set_completed = target_set_completed;
  1009. instance->loopback_link = 0;
  1010. res = pipe(instance->logpipes);
  1011. if (res == -1) {
  1012. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  1013. goto exit_error;
  1014. }
  1015. if (fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK) == -1 ||
  1016. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK) == -1) {
  1017. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to set O_NONBLOCK flag for instance->logpipes");
  1018. goto exit_error;
  1019. }
  1020. if (icmap_get_string("system.allow_knet_handle_fallback", &tmp_str) == CS_OK) {
  1021. if (strcmp(tmp_str, "yes") == 0) {
  1022. allow_knet_handle_fallback = 1;
  1023. }
  1024. free(tmp_str);
  1025. }
  1026. #if defined(KNET_API_VER) && (KNET_API_VER == 2)
  1027. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, KNET_HANDLE_FLAG_PRIVILEGED);
  1028. #else
  1029. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  1030. #endif
  1031. if (allow_knet_handle_fallback && !instance->knet_handle && errno == ENAMETOOLONG) {
  1032. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_new failed, trying unprivileged");
  1033. #if defined(KNET_API_VER) && (KNET_API_VER == 2)
  1034. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, 0);
  1035. #else
  1036. instance->knet_handle = knet_handle_new_ex(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, 0);
  1037. #endif
  1038. }
  1039. if (!instance->knet_handle) {
  1040. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  1041. goto exit_error;
  1042. }
  1043. knet_set_access_list_config(instance);
  1044. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  1045. if (res) {
  1046. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  1047. }
  1048. res = knet_handle_pmtud_set(instance->knet_handle, instance->totem_config->knet_mtu);
  1049. if (res) {
  1050. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_set failed");
  1051. }
  1052. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  1053. if (res) {
  1054. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  1055. }
  1056. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  1057. if (res) {
  1058. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  1059. }
  1060. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  1061. if (res) {
  1062. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  1063. }
  1064. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  1065. if (res) {
  1066. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  1067. }
  1068. global_instance = instance;
  1069. /* Setup knet logging level */
  1070. totemknet_configure_log_level();
  1071. /* Get an fd into knet */
  1072. instance->knet_fd = 0;
  1073. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  1074. if (res) {
  1075. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  1076. goto exit_error;
  1077. }
  1078. /* Enable crypto if requested */
  1079. #ifdef HAVE_KNET_CRYPTO_RECONF
  1080. if (totemknet_is_crypto_enabled(instance)) {
  1081. res = totemknet_set_knet_crypto(instance);
  1082. if (res == 0) {
  1083. res = knet_handle_crypto_use_config(instance->knet_handle, totem_config->crypto_index);
  1084. if (res) {
  1085. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_use_config failed: %s", strerror(errno));
  1086. goto exit_error;
  1087. }
  1088. } else {
  1089. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1090. goto exit_error;
  1091. }
  1092. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1093. if (res) {
  1094. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (DISALLOW) failed: %s", strerror(errno));
  1095. goto exit_error;
  1096. }
  1097. } else {
  1098. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  1099. if (res) {
  1100. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (ALLOW) failed: %s", strerror(errno));
  1101. goto exit_error;
  1102. }
  1103. }
  1104. #else
  1105. if (totemknet_is_crypto_enabled(instance)) {
  1106. res = totemknet_set_knet_crypto(instance);
  1107. if (res) {
  1108. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1109. goto exit_error;
  1110. }
  1111. }
  1112. #endif
  1113. /* Set up compression */
  1114. if (strcmp(totem_config->knet_compression_model, "none") != 0) {
  1115. /* Not fatal, but will log */
  1116. (void)totemknet_configure_compression(instance, totem_config);
  1117. }
  1118. knet_handle_setfwd(instance->knet_handle, 1);
  1119. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  1120. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  1121. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  1122. }
  1123. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  1124. instance->link_mode = KNET_LINK_POLICY_RR;
  1125. }
  1126. for (i=0; i<INTERFACE_MAX; i++) {
  1127. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  1128. if (!instance->link_status[i]) {
  1129. goto exit_error;
  1130. }
  1131. }
  1132. qb_loop_poll_add (instance->poll_handle,
  1133. QB_LOOP_MED,
  1134. instance->logpipes[0],
  1135. POLLIN, instance, log_deliver_fn);
  1136. qb_loop_poll_add (instance->poll_handle,
  1137. QB_LOOP_HIGH,
  1138. instance->knet_fd,
  1139. POLLIN, instance, data_deliver_fn);
  1140. /*
  1141. * Upper layer isn't ready to receive message because it hasn't
  1142. * initialized yet. Add short timer to check the interfaces.
  1143. */
  1144. qb_loop_timer_add (instance->poll_handle,
  1145. QB_LOOP_MED,
  1146. 100*QB_TIME_NS_IN_MSEC,
  1147. (void *)instance,
  1148. timer_function_netif_check_timeout,
  1149. &instance->timer_netif_check_timeout);
  1150. totemknet_start_merge_detect_timeout(instance);
  1151. /* Start listening for config changes */
  1152. totemknet_add_config_notifications(instance);
  1153. /* Add stats keys to icmap */
  1154. stats_knet_add_handle();
  1155. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  1156. *knet_context = instance;
  1157. return (0);
  1158. exit_error:
  1159. log_flush_messages(instance);
  1160. free(instance);
  1161. return (-1);
  1162. }
  1163. void *totemknet_buffer_alloc (void)
  1164. {
  1165. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  1166. return malloc(KNET_MAX_PACKET_SIZE + 512);
  1167. }
  1168. void totemknet_buffer_release (void *ptr)
  1169. {
  1170. return free (ptr);
  1171. }
  1172. int totemknet_processor_count_set (
  1173. void *knet_context,
  1174. int processor_count)
  1175. {
  1176. return (0);
  1177. }
  1178. int totemknet_recv_flush (void *knet_context)
  1179. {
  1180. return (0);
  1181. }
  1182. int totemknet_send_flush (void *knet_context)
  1183. {
  1184. return (0);
  1185. }
  1186. int totemknet_token_send (
  1187. void *knet_context,
  1188. const void *msg,
  1189. unsigned int msg_len)
  1190. {
  1191. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1192. int res = 0;
  1193. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1194. return (res);
  1195. }
  1196. int totemknet_mcast_flush_send (
  1197. void *knet_context,
  1198. const void *msg,
  1199. unsigned int msg_len)
  1200. {
  1201. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1202. int res = 0;
  1203. mcast_sendmsg (instance, msg, msg_len, 0);
  1204. return (res);
  1205. }
  1206. int totemknet_mcast_noflush_send (
  1207. void *knet_context,
  1208. const void *msg,
  1209. unsigned int msg_len)
  1210. {
  1211. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1212. int res = 0;
  1213. mcast_sendmsg (instance, msg, msg_len, 1);
  1214. return (res);
  1215. }
  1216. extern int totemknet_iface_check (void *knet_context)
  1217. {
  1218. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1219. int res = 0;
  1220. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  1221. return (res);
  1222. }
  1223. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  1224. {
  1225. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1226. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  1227. }
  1228. int totemknet_token_target_set (
  1229. void *knet_context,
  1230. unsigned int nodeid)
  1231. {
  1232. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1233. int res = 0;
  1234. instance->token_target.nodeid = nodeid;
  1235. instance->totemknet_target_set_completed (instance->context);
  1236. return (res);
  1237. }
  1238. extern int totemknet_recv_mcast_empty (
  1239. void *knet_context)
  1240. {
  1241. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1242. unsigned int res;
  1243. struct sockaddr_storage system_from;
  1244. struct msghdr msg_hdr;
  1245. struct iovec iov_recv;
  1246. struct pollfd ufd;
  1247. int nfds;
  1248. int msg_processed = 0;
  1249. iov_recv.iov_base = instance->iov_buffer;
  1250. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  1251. msg_hdr.msg_name = &system_from;
  1252. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  1253. msg_hdr.msg_iov = &iov_recv;
  1254. msg_hdr.msg_iovlen = 1;
  1255. #ifdef HAVE_MSGHDR_CONTROL
  1256. msg_hdr.msg_control = 0;
  1257. #endif
  1258. #ifdef HAVE_MSGHDR_CONTROLLEN
  1259. msg_hdr.msg_controllen = 0;
  1260. #endif
  1261. #ifdef HAVE_MSGHDR_FLAGS
  1262. msg_hdr.msg_flags = 0;
  1263. #endif
  1264. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1265. msg_msg_hdr.msg_accrights = NULL;
  1266. #endif
  1267. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1268. msg_msg_hdr.msg_accrightslen = 0;
  1269. #endif
  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. return -1;
  1394. }
  1395. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  1396. instance->totem_config->interfaces[link_no].knet_pong_count);
  1397. if (err) {
  1398. /* Flush logs before reporting this error so that the knet message prints before ours */
  1399. int saved_errno = errno;
  1400. log_flush_messages(instance);
  1401. errno = saved_errno;
  1402. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1403. return -1;
  1404. }
  1405. }
  1406. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1407. if (err) {
  1408. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1409. return -1;
  1410. }
  1411. /* register stats */
  1412. stats_knet_add_member(member->nodeid, link_no);
  1413. return (0);
  1414. }
  1415. int totemknet_member_remove (
  1416. void *knet_context,
  1417. const struct totem_ip_address *token_target,
  1418. int link_no)
  1419. {
  1420. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1421. int res;
  1422. uint8_t link_list[KNET_MAX_LINK];
  1423. size_t num_links;
  1424. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: " CS_PRI_NODE_ID ", link=%d", token_target->nodeid, link_no);
  1425. /* Don't remove the link with the loopback on it until we shut down */
  1426. if (token_target->nodeid == instance->our_nodeid) {
  1427. return 0;
  1428. }
  1429. /* Tidy stats */
  1430. stats_knet_del_member(token_target->nodeid, link_no);
  1431. /* Remove the link first */
  1432. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1433. if (res != 0) {
  1434. 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);
  1435. return res;
  1436. }
  1437. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1438. if (res != 0) {
  1439. 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);
  1440. return res;
  1441. }
  1442. /* If this is the last link, then remove the node */
  1443. res = knet_link_get_link_list(instance->knet_handle,
  1444. token_target->nodeid, link_list, &num_links);
  1445. if (res) {
  1446. return (0); /* not really failure */
  1447. }
  1448. if (num_links == 0) {
  1449. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1450. }
  1451. return res;
  1452. }
  1453. int totemknet_member_list_rebind_ip (
  1454. void *knet_context)
  1455. {
  1456. return (0);
  1457. }
  1458. static int totemknet_configure_compression (
  1459. struct totemknet_instance *instance,
  1460. struct totem_config *totem_config)
  1461. {
  1462. struct knet_handle_compress_cfg compress_cfg;
  1463. int res = 0;
  1464. assert(strlen(totem_config->knet_compression_model) < sizeof(compress_cfg.compress_model));
  1465. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1466. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1467. compress_cfg.compress_level = totem_config->knet_compression_level;
  1468. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1469. if (res) {
  1470. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1471. }
  1472. return res;
  1473. }
  1474. int totemknet_reconfigure (
  1475. void *knet_context,
  1476. struct totem_config *totem_config)
  1477. {
  1478. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1479. int res = 0;
  1480. (void)totemknet_configure_compression(instance, totem_config);
  1481. #ifdef HAVE_LIBNOZZLE
  1482. /* Set up nozzle device(s). Return code is ignored, because inability
  1483. * configure nozzle is not fatal problem, errors are logged and
  1484. * there is not much else we can do */
  1485. (void)setup_nozzle(instance);
  1486. #endif
  1487. if (totem_config->crypto_changed) {
  1488. /* Flip crypto_index */
  1489. totem_config->crypto_index = 3-totem_config->crypto_index;
  1490. res = totemknet_set_knet_crypto(instance);
  1491. knet_log_printf(LOG_INFO, "kronosnet crypto reconfigured on index %d: %s/%s/%s", totem_config->crypto_index,
  1492. totem_config->crypto_model,
  1493. totem_config->crypto_cipher_type,
  1494. totem_config->crypto_hash_type);
  1495. }
  1496. return (res);
  1497. }
  1498. int totemknet_crypto_reconfigure_phase (
  1499. void *knet_context,
  1500. struct totem_config *totem_config,
  1501. cfg_message_crypto_reconfig_phase_t phase)
  1502. {
  1503. #ifdef HAVE_KNET_CRYPTO_RECONF
  1504. int res;
  1505. int config_to_use;
  1506. int config_to_clear;
  1507. struct knet_handle_crypto_cfg crypto_cfg;
  1508. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1509. knet_log_printf(LOGSYS_LEVEL_DEBUG, "totemknet_crypto_reconfigure_phase %d, index=%d\n", phase, totem_config->crypto_index);
  1510. switch (phase) {
  1511. case CRYPTO_RECONFIG_PHASE_ACTIVATE:
  1512. config_to_use = totem_config->crypto_index;
  1513. if (!totemknet_is_crypto_enabled(instance)) {
  1514. config_to_use = 0; /* we are clearing it */
  1515. }
  1516. /* Enable the new config on this node */
  1517. res = knet_handle_crypto_use_config(instance->knet_handle, config_to_use);
  1518. if (res == -1) {
  1519. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_use_config %d failed: %s", config_to_use, strerror(errno));
  1520. }
  1521. break;
  1522. case CRYPTO_RECONFIG_PHASE_CLEANUP:
  1523. /*
  1524. * All nodes should now have the new config. clear the old one out
  1525. * OR disable crypto entirely if that's what the new config insists on.
  1526. */
  1527. config_to_clear = 3-totem_config->crypto_index;
  1528. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Clearing old knet crypto config %d\n", config_to_clear);
  1529. strcpy(crypto_cfg.crypto_model, "none");
  1530. strcpy(crypto_cfg.crypto_cipher_type, "none");
  1531. strcpy(crypto_cfg.crypto_hash_type, "none");
  1532. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, config_to_clear);
  1533. if (res == -1) {
  1534. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: %s", config_to_clear, strerror(errno));
  1535. }
  1536. if (res == -2) {
  1537. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: -2", config_to_clear);
  1538. }
  1539. /* If crypto is enabled then disable all cleartext reception */
  1540. if (totemknet_is_crypto_enabled(instance)) {
  1541. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1542. if (res) {
  1543. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(DISALLOW) failed %s", strerror(errno));
  1544. }
  1545. }
  1546. }
  1547. #endif
  1548. return 0;
  1549. }
  1550. void totemknet_stats_clear (
  1551. void *knet_context)
  1552. {
  1553. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1554. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1555. }
  1556. /* For the stats module */
  1557. int totemknet_link_get_status (
  1558. knet_node_id_t node, uint8_t link_no,
  1559. struct knet_link_status *status)
  1560. {
  1561. int res;
  1562. int ret = CS_OK;
  1563. /* We are probably not using knet */
  1564. if (!global_instance) {
  1565. return CS_ERR_NOT_EXIST;
  1566. }
  1567. if (link_no >= INTERFACE_MAX) {
  1568. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1569. }
  1570. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1571. if (res) {
  1572. switch (errno) {
  1573. case EINVAL:
  1574. ret = CS_ERR_INVALID_PARAM;
  1575. break;
  1576. case EBUSY:
  1577. ret = CS_ERR_BUSY;
  1578. break;
  1579. case EDEADLK:
  1580. ret = CS_ERR_TRY_AGAIN;
  1581. break;
  1582. default:
  1583. ret = CS_ERR_LIBRARY;
  1584. break;
  1585. }
  1586. }
  1587. return (ret);
  1588. }
  1589. int totemknet_handle_get_stats (
  1590. struct knet_handle_stats *stats)
  1591. {
  1592. int res;
  1593. /* We are probably not using knet */
  1594. if (!global_instance) {
  1595. return CS_ERR_NOT_EXIST;
  1596. }
  1597. res = knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1598. if (res != 0) {
  1599. return (qb_to_cs_error(-errno));
  1600. }
  1601. return CS_OK;
  1602. }
  1603. static void timer_function_merge_detect_timeout (
  1604. void *data)
  1605. {
  1606. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1607. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1608. instance->send_merge_detect_message = 1;
  1609. }
  1610. instance->merge_detect_messages_sent_before_timeout = 0;
  1611. totemknet_start_merge_detect_timeout(instance);
  1612. }
  1613. static void totemknet_start_merge_detect_timeout(
  1614. void *knet_context)
  1615. {
  1616. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1617. qb_loop_timer_add(instance->poll_handle,
  1618. QB_LOOP_MED,
  1619. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1620. (void *)instance,
  1621. timer_function_merge_detect_timeout,
  1622. &instance->timer_merge_detect_timeout);
  1623. }
  1624. static void totemknet_stop_merge_detect_timeout(
  1625. void *knet_context)
  1626. {
  1627. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1628. qb_loop_timer_del(instance->poll_handle,
  1629. instance->timer_merge_detect_timeout);
  1630. }
  1631. static void log_flush_messages (void *knet_context)
  1632. {
  1633. struct pollfd pfd;
  1634. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1635. int cont;
  1636. cont = 1;
  1637. while (cont) {
  1638. pfd.fd = instance->logpipes[0];
  1639. pfd.events = POLLIN;
  1640. pfd.revents = 0;
  1641. if ((poll(&pfd, 1, 0) > 0) &&
  1642. (pfd.revents & POLLIN) &&
  1643. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1644. cont = 1;
  1645. } else {
  1646. cont = 0;
  1647. }
  1648. }
  1649. }
  1650. #ifdef HAVE_LIBNOZZLE
  1651. #define NOZZLE_NAME "nozzle.name"
  1652. #define NOZZLE_IPADDR "nozzle.ipaddr"
  1653. #define NOZZLE_PREFIX "nozzle.ipprefix"
  1654. #define NOZZLE_MACADDR "nozzle.macaddr"
  1655. #define NOZZLE_CHANNEL 1
  1656. static char *get_nozzle_script_dir(void *knet_context)
  1657. {
  1658. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1659. char filename[PATH_MAX + FILENAME_MAX + 1];
  1660. static char updown_dirname[PATH_MAX + FILENAME_MAX + 1];
  1661. int res;
  1662. const char *dirname_res;
  1663. /*
  1664. * Build script directory based on corosync.conf file location
  1665. */
  1666. res = snprintf(filename, sizeof(filename), "%s",
  1667. corosync_get_config_file());
  1668. if (res >= sizeof(filename)) {
  1669. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1670. return NULL;
  1671. }
  1672. dirname_res = dirname(filename);
  1673. res = snprintf(updown_dirname, sizeof(updown_dirname), "%s/%s",
  1674. dirname_res, "updown.d");
  1675. if (res >= sizeof(updown_dirname)) {
  1676. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1677. return NULL;
  1678. }
  1679. return updown_dirname;
  1680. }
  1681. /*
  1682. * Deliberately doesn't return the status as caller doesn't care.
  1683. * The result will be logged though
  1684. */
  1685. static void run_nozzle_script(struct totemknet_instance *instance, int type, const char *typename)
  1686. {
  1687. int res;
  1688. char *exec_string;
  1689. res = nozzle_run_updown(instance->nozzle_handle, type, &exec_string);
  1690. if (res == -1 && errno != ENOENT) {
  1691. knet_log_printf (LOGSYS_LEVEL_INFO, "exec nozzle %s script failed: %s", typename, strerror(errno));
  1692. } else if (res == -2) {
  1693. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle %s script failed", typename);
  1694. knet_log_printf (LOGSYS_LEVEL_INFO, "%s", exec_string);
  1695. }
  1696. }
  1697. /*
  1698. * Reparse IP address to add in our node ID
  1699. * IPv6 addresses must end in '::'
  1700. * IPv4 addresses must just be valid
  1701. * '/xx' lengths are optional for IPv6, mandatory for IPv4
  1702. *
  1703. * Returns the modified IP address as a string to pass into libnozzle
  1704. */
  1705. static int reparse_nozzle_ip_address(struct totemknet_instance *instance,
  1706. const char *input_addr,
  1707. const char *prefix, int nodeid,
  1708. char *output_addr, size_t output_len)
  1709. {
  1710. char *coloncolon;
  1711. int bits;
  1712. int max_prefix = 64;
  1713. uint32_t nodeid_mask;
  1714. uint32_t addr_mask;
  1715. uint32_t masked_nodeid;
  1716. struct in_addr *addr;
  1717. struct totem_ip_address totemip;
  1718. coloncolon = strstr(input_addr, "::");
  1719. if (!coloncolon) {
  1720. max_prefix = 30;
  1721. }
  1722. bits = atoi(prefix);
  1723. if (bits < 8 || bits > max_prefix) {
  1724. knet_log_printf(LOGSYS_LEVEL_ERROR, "nozzle IP address prefix must be >= 8 and <= %d (got %d)", max_prefix, bits);
  1725. return -1;
  1726. }
  1727. /* IPv6 is easy */
  1728. if (coloncolon) {
  1729. memcpy(output_addr, input_addr, coloncolon-input_addr);
  1730. sprintf(output_addr + (coloncolon-input_addr), "::%x", nodeid);
  1731. return 0;
  1732. }
  1733. /* For IPv4 we need to parse the address into binary, mask off the required bits,
  1734. * add in the masked_nodeid and 'print' it out again
  1735. */
  1736. nodeid_mask = UINT32_MAX & ((1<<(32 - bits)) - 1);
  1737. addr_mask = UINT32_MAX ^ nodeid_mask;
  1738. masked_nodeid = nodeid & nodeid_mask;
  1739. if (totemip_parse(&totemip, input_addr, AF_INET)) {
  1740. knet_log_printf(LOGSYS_LEVEL_ERROR, "Failed to parse IPv4 nozzle IP address");
  1741. return -1;
  1742. }
  1743. addr = (struct in_addr *)&totemip.addr;
  1744. addr->s_addr &= htonl(addr_mask);
  1745. addr->s_addr |= htonl(masked_nodeid);
  1746. inet_ntop(AF_INET, addr, output_addr, output_len);
  1747. return 0;
  1748. }
  1749. static int create_nozzle_device(void *knet_context, const char *name,
  1750. const char *ipaddr, const char *prefix,
  1751. const char *macaddr)
  1752. {
  1753. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1754. char device_name[IFNAMSIZ+1];
  1755. size_t size = IFNAMSIZ;
  1756. int8_t channel = NOZZLE_CHANNEL;
  1757. nozzle_t nozzle_dev;
  1758. int nozzle_fd;
  1759. int res;
  1760. char *updown_dir;
  1761. char parsed_ipaddr[INET6_ADDRSTRLEN];
  1762. char mac[19];
  1763. memset(device_name, 0, size);
  1764. memset(&mac, 0, sizeof(mac));
  1765. strncpy(device_name, name, size);
  1766. updown_dir = get_nozzle_script_dir(knet_context);
  1767. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle script dir is %s", updown_dir);
  1768. nozzle_dev = nozzle_open(device_name, size, updown_dir);
  1769. if (!nozzle_dev) {
  1770. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to init nozzle device %s: %s", device_name, strerror(errno));
  1771. return -1;
  1772. }
  1773. instance->nozzle_handle = nozzle_dev;
  1774. if (nozzle_set_mac(nozzle_dev, macaddr) < 0) {
  1775. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle MAC to %s: %s", mac, strerror(errno));
  1776. goto out_clean;
  1777. }
  1778. if (reparse_nozzle_ip_address(instance, ipaddr, prefix, instance->our_nodeid, parsed_ipaddr, sizeof(parsed_ipaddr))) {
  1779. /* Prints its own errors */
  1780. goto out_clean;
  1781. }
  1782. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle IP address is %s / %d", parsed_ipaddr, atoi(prefix));
  1783. if (nozzle_add_ip(nozzle_dev, parsed_ipaddr, prefix) < 0) {
  1784. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle IP addr to %s/%s: %s", parsed_ipaddr, prefix, strerror(errno));
  1785. goto out_clean;
  1786. }
  1787. nozzle_fd = nozzle_get_fd(nozzle_dev);
  1788. knet_log_printf (LOGSYS_LEVEL_INFO, "Opened '%s' on fd %d", device_name, nozzle_fd);
  1789. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel);
  1790. if (res != 0) {
  1791. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add nozzle FD to knet: %s", strerror(errno));
  1792. goto out_clean;
  1793. }
  1794. run_nozzle_script(instance, NOZZLE_PREUP, "pre-up");
  1795. res = nozzle_set_up(nozzle_dev);
  1796. if (res != 0) {
  1797. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to set nozzle interface UP: %s", strerror(errno));
  1798. goto out_clean;
  1799. }
  1800. run_nozzle_script(instance, NOZZLE_UP, "up");
  1801. return 0;
  1802. out_clean:
  1803. nozzle_close(nozzle_dev);
  1804. return -1;
  1805. }
  1806. static int remove_nozzle_device(void *knet_context)
  1807. {
  1808. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1809. int res;
  1810. int datafd;
  1811. res = knet_handle_get_datafd(instance->knet_handle, NOZZLE_CHANNEL, &datafd);
  1812. if (res != 0) {
  1813. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't find datafd for channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1814. return -1;
  1815. }
  1816. res = knet_handle_remove_datafd(instance->knet_handle, datafd);
  1817. if (res != 0) {
  1818. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't remove datafd for nozzle channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1819. return -1;
  1820. }
  1821. run_nozzle_script(instance, NOZZLE_DOWN, "pre-down");
  1822. res = nozzle_set_down(instance->nozzle_handle);
  1823. if (res != 0) {
  1824. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't set nozzle device down: %s", strerror(errno));
  1825. return -1;
  1826. }
  1827. run_nozzle_script(instance, NOZZLE_POSTDOWN, "post-down");
  1828. res = nozzle_close(instance->nozzle_handle);
  1829. if (res != 0) {
  1830. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't close nozzle device: %s", strerror(errno));
  1831. return -1;
  1832. }
  1833. knet_log_printf (LOGSYS_LEVEL_INFO, "Removed nozzle device");
  1834. return 0;
  1835. }
  1836. static void free_nozzle(struct totemknet_instance *instance)
  1837. {
  1838. free(instance->nozzle_name);
  1839. free(instance->nozzle_ipaddr);
  1840. free(instance->nozzle_prefix);
  1841. free(instance->nozzle_macaddr);
  1842. instance->nozzle_name = instance->nozzle_ipaddr = instance->nozzle_prefix =
  1843. instance->nozzle_macaddr = NULL;
  1844. }
  1845. static int setup_nozzle(void *knet_context)
  1846. {
  1847. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1848. char *ipaddr_str = NULL;
  1849. char *name_str = NULL;
  1850. char *prefix_str = NULL;
  1851. char *macaddr_str = NULL;
  1852. char mac[32];
  1853. int name_res;
  1854. int macaddr_res;
  1855. int res = -1;
  1856. /*
  1857. * Return value ignored on purpose. icmap_get_string changes
  1858. * ipaddr_str/prefix_str only on success.
  1859. */
  1860. (void)icmap_get_string(NOZZLE_IPADDR, &ipaddr_str);
  1861. (void)icmap_get_string(NOZZLE_PREFIX, &prefix_str);
  1862. macaddr_res = icmap_get_string(NOZZLE_MACADDR, &macaddr_str);
  1863. name_res = icmap_get_string(NOZZLE_NAME, &name_str);
  1864. /* Is is being removed? */
  1865. if (name_res == CS_ERR_NOT_EXIST && instance->nozzle_handle) {
  1866. remove_nozzle_device(instance);
  1867. free_nozzle(instance);
  1868. goto out_free;
  1869. }
  1870. if (!name_str) {
  1871. /* no nozzle */
  1872. goto out_free;
  1873. }
  1874. if (!ipaddr_str) {
  1875. knet_log_printf (LOGSYS_LEVEL_ERROR, "No IP address supplied for Nozzle device");
  1876. goto out_free;
  1877. }
  1878. if (!prefix_str) {
  1879. knet_log_printf (LOGSYS_LEVEL_ERROR, "No prefix supplied for Nozzle IP address");
  1880. goto out_free;
  1881. }
  1882. if (macaddr_str && strlen(macaddr_str) != 17) {
  1883. knet_log_printf (LOGSYS_LEVEL_ERROR, "macaddr for nozzle device is not in the correct format '%s'", macaddr_str);
  1884. goto out_free;
  1885. }
  1886. if (!macaddr_str) {
  1887. macaddr_str = (char*)"54:54:01:00:00:00";
  1888. }
  1889. if (instance->nozzle_name &&
  1890. (strcmp(name_str, instance->nozzle_name) == 0) &&
  1891. (strcmp(ipaddr_str, instance->nozzle_ipaddr) == 0) &&
  1892. (strcmp(prefix_str, instance->nozzle_prefix) == 0) &&
  1893. (instance->nozzle_macaddr == NULL ||
  1894. strcmp(macaddr_str, instance->nozzle_macaddr) == 0)) {
  1895. /* Nothing has changed */
  1896. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Nozzle device info not changed");
  1897. goto out_free;
  1898. }
  1899. /* Add nodeid into MAC address */
  1900. memcpy(mac, macaddr_str, 12);
  1901. snprintf(mac+12, sizeof(mac) - 13, "%02x:%02x",
  1902. instance->our_nodeid >> 8,
  1903. instance->our_nodeid & 0xFF);
  1904. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle MAC address is %s", mac);
  1905. if (name_res == CS_OK && name_str) {
  1906. /* Reconfigure */
  1907. if (instance->nozzle_name) {
  1908. remove_nozzle_device(instance);
  1909. free_nozzle(instance);
  1910. }
  1911. res = create_nozzle_device(knet_context, name_str, ipaddr_str, prefix_str,
  1912. mac);
  1913. instance->nozzle_name = strdup(name_str);
  1914. instance->nozzle_ipaddr = strdup(ipaddr_str);
  1915. instance->nozzle_prefix = strdup(prefix_str);
  1916. instance->nozzle_macaddr = strdup(macaddr_str);
  1917. if (!instance->nozzle_name || !instance->nozzle_ipaddr ||
  1918. !instance->nozzle_prefix) {
  1919. knet_log_printf (LOGSYS_LEVEL_ERROR, "strdup failed in nozzle allocation");
  1920. /*
  1921. * This 'free' will cause a complete reconfigure of the device next time we reload
  1922. * but will also let the the current device keep working until then.
  1923. * remove_nozzle() only needs the, statically-allocated, nozzle_handle
  1924. */
  1925. free_nozzle(instance);
  1926. }
  1927. }
  1928. out_free:
  1929. free(name_str);
  1930. free(ipaddr_str);
  1931. free(prefix_str);
  1932. if (macaddr_res == CS_OK) {
  1933. free(macaddr_str);
  1934. }
  1935. return res;
  1936. }
  1937. #endif // HAVE_LIBNOZZLE