totemknet.c 69 KB

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