4
0

totemknet.c 67 KB

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