totemknet.c 68 KB

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