totemknet.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*
  2. * Copyright (c) 2016-2019 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <sys/mman.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/socket.h>
  39. #include <netdb.h>
  40. #include <sys/un.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/param.h>
  43. #include <netinet/in.h>
  44. #include <net/ethernet.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <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. qb_loop_t *poll_handle;
  84. knet_handle_t knet_handle;
  85. int link_mode;
  86. void *context;
  87. void (*totemknet_deliver_fn) (
  88. void *context,
  89. const void *msg,
  90. unsigned int msg_len,
  91. const struct sockaddr_storage *system_from);
  92. void (*totemknet_iface_change_fn) (
  93. void *context,
  94. const struct totem_ip_address *iface_address,
  95. unsigned int link_no);
  96. void (*totemknet_mtu_changed) (
  97. void *context,
  98. int net_mtu);
  99. void (*totemknet_target_set_completed) (void *context);
  100. /*
  101. * Function and data used to log messages
  102. */
  103. int totemknet_log_level_security;
  104. int totemknet_log_level_error;
  105. int totemknet_log_level_warning;
  106. int totemknet_log_level_notice;
  107. int totemknet_log_level_debug;
  108. int totemknet_subsys_id;
  109. int knet_subsys_id;
  110. void (*totemknet_log_printf) (
  111. int level,
  112. int subsys,
  113. const char *function,
  114. const char *file,
  115. int line,
  116. const char *format,
  117. ...)__attribute__((format(printf, 6, 7)));
  118. void *knet_context;
  119. char iov_buffer[KNET_MAX_PACKET_SIZE];
  120. char *link_status[INTERFACE_MAX];
  121. struct totem_ip_address my_ids[INTERFACE_MAX];
  122. uint16_t ip_port[INTERFACE_MAX];
  123. int our_nodeid;
  124. int loopback_link;
  125. struct totem_config *totem_config;
  126. struct totem_ip_address token_target;
  127. qb_loop_timer_handle timer_netif_check_timeout;
  128. qb_loop_timer_handle timer_merge_detect_timeout;
  129. int send_merge_detect_message;
  130. unsigned int merge_detect_messages_sent_before_timeout;
  131. int logpipes[2];
  132. int knet_fd;
  133. #ifdef HAVE_LIBNOZZLE
  134. char *nozzle_name;
  135. char *nozzle_ipaddr;
  136. char *nozzle_prefix;
  137. char *nozzle_macaddr;
  138. nozzle_t nozzle_handle;
  139. #endif
  140. };
  141. /* Awkward. But needed to get stats from knet */
  142. struct totemknet_instance *global_instance;
  143. struct work_item {
  144. const void *msg;
  145. unsigned int msg_len;
  146. struct totemknet_instance *instance;
  147. };
  148. int totemknet_member_list_rebind_ip (
  149. void *knet_context);
  150. static void totemknet_start_merge_detect_timeout(
  151. void *knet_context);
  152. static void totemknet_stop_merge_detect_timeout(
  153. void *knet_context);
  154. static void log_flush_messages (
  155. void *knet_context);
  156. static void totemknet_instance_initialize (struct totemknet_instance *instance)
  157. {
  158. memset (instance, 0, sizeof (struct totemknet_instance));
  159. }
  160. #define knet_log_printf(level, format, args...) \
  161. do { \
  162. instance->totemknet_log_printf ( \
  163. level, instance->totemknet_subsys_id, \
  164. __FUNCTION__, __FILE__, __LINE__, \
  165. (const char *)format, ##args); \
  166. } while (0);
  167. #define libknet_log_printf(level, format, args...) \
  168. do { \
  169. instance->totemknet_log_printf ( \
  170. level, instance->knet_subsys_id, \
  171. __FUNCTION__, "libknet.h", __LINE__, \
  172. (const char *)format, ##args); \
  173. } while (0);
  174. #define KNET_LOGSYS_PERROR(err_num, level, fmt, args...) \
  175. do { \
  176. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  177. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  178. instance->totemknet_log_printf ( \
  179. level, instance->totemknet_subsys_id, \
  180. __FUNCTION__, __FILE__, __LINE__, \
  181. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  182. } while(0)
  183. #ifdef HAVE_LIBNOZZLE
  184. static inline int is_ether_addr_multicast(const uint8_t *addr)
  185. {
  186. return (addr[0] & 0x01);
  187. }
  188. static inline int is_ether_addr_zero(const uint8_t *addr)
  189. {
  190. return (!addr[0] && !addr[1] && !addr[2] && !addr[3] && !addr[4] && !addr[5]);
  191. }
  192. static int ether_host_filter_fn(void *private_data,
  193. const unsigned char *outdata,
  194. ssize_t outdata_len,
  195. uint8_t tx_rx,
  196. knet_node_id_t this_host_id,
  197. knet_node_id_t src_host_id,
  198. int8_t *channel,
  199. knet_node_id_t *dst_host_ids,
  200. size_t *dst_host_ids_entries)
  201. {
  202. struct ether_header *eth_h = (struct ether_header *)outdata;
  203. uint8_t *dst_mac = (uint8_t *)eth_h->ether_dhost;
  204. uint16_t dst_host_id;
  205. if (is_ether_addr_zero(dst_mac))
  206. return -1;
  207. if (is_ether_addr_multicast(dst_mac)) {
  208. return 1;
  209. }
  210. memmove(&dst_host_id, &dst_mac[4], 2);
  211. dst_host_ids[0] = ntohs(dst_host_id);
  212. *dst_host_ids_entries = 1;
  213. return 0;
  214. }
  215. #endif
  216. static int dst_host_filter_callback_fn(void *private_data,
  217. const unsigned char *outdata,
  218. ssize_t outdata_len,
  219. uint8_t tx_rx,
  220. knet_node_id_t this_host_id,
  221. knet_node_id_t src_host_id,
  222. int8_t *channel,
  223. knet_node_id_t *dst_host_ids,
  224. size_t *dst_host_ids_entries)
  225. {
  226. struct totem_message_header *header = (struct totem_message_header *)outdata;
  227. int res;
  228. #ifdef HAVE_LIBNOZZLE
  229. if (*channel != 0) {
  230. return ether_host_filter_fn(private_data,
  231. outdata, outdata_len,
  232. tx_rx,
  233. this_host_id, src_host_id,
  234. channel,
  235. dst_host_ids,
  236. dst_host_ids_entries);
  237. }
  238. #endif
  239. if (header->target_nodeid) {
  240. dst_host_ids[0] = header->target_nodeid;
  241. *dst_host_ids_entries = 1;
  242. res = 0; /* unicast message */
  243. }
  244. else {
  245. *dst_host_ids_entries = 0;
  246. res = 1; /* multicast message */
  247. }
  248. return res;
  249. }
  250. static void socket_error_callback_fn(void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)
  251. {
  252. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  253. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet socket ERROR notification called: txrx=%d, error=%d, errorno=%d", tx_rx, error, errorno);
  254. if ((error == -1 && errorno != EAGAIN) || (error == 0)) {
  255. knet_handle_remove_datafd(instance->knet_handle, datafd);
  256. }
  257. }
  258. static void host_change_callback_fn(void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)
  259. {
  260. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  261. // TODO: what? if anything.
  262. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet host change callback. nodeid: %d reachable: %d", host_id, reachable);
  263. }
  264. static void pmtu_change_callback_fn(void *private_data, unsigned int data_mtu)
  265. {
  266. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  267. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet pMTU change: %d", data_mtu);
  268. /* We don't need to tell corosync the actual knet MTU */
  269. // instance->totemknet_mtu_changed(instance->context, data_mtu);
  270. }
  271. int totemknet_crypto_set (
  272. void *knet_context,
  273. const char *cipher_type,
  274. const char *hash_type)
  275. {
  276. return (0);
  277. }
  278. static inline void ucast_sendmsg (
  279. struct totemknet_instance *instance,
  280. struct totem_ip_address *system_to,
  281. const void *msg,
  282. unsigned int msg_len)
  283. {
  284. int res = 0;
  285. struct totem_message_header *header = (struct totem_message_header *)msg;
  286. struct msghdr msg_ucast;
  287. struct iovec iovec;
  288. header->target_nodeid = system_to->nodeid;
  289. iovec.iov_base = (void *)msg;
  290. iovec.iov_len = msg_len;
  291. /*
  292. * Build unicast message
  293. */
  294. memset(&msg_ucast, 0, sizeof(msg_ucast));
  295. msg_ucast.msg_iov = (void *)&iovec;
  296. msg_ucast.msg_iovlen = 1;
  297. #ifdef HAVE_MSGHDR_CONTROL
  298. msg_ucast.msg_control = 0;
  299. #endif
  300. #ifdef HAVE_MSGHDR_CONTROLLEN
  301. msg_ucast.msg_controllen = 0;
  302. #endif
  303. #ifdef HAVE_MSGHDR_FLAGS
  304. msg_ucast.msg_flags = 0;
  305. #endif
  306. #ifdef HAVE_MSGHDR_ACCRIGHTS
  307. msg_ucast.msg_accrights = NULL;
  308. #endif
  309. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  310. msg_ucast.msg_accrightslen = 0;
  311. #endif
  312. /*
  313. * Transmit unicast message
  314. * An error here is recovered by totemsrp
  315. */
  316. res = sendmsg (instance->knet_fd, &msg_ucast, MSG_NOSIGNAL);
  317. if (res < 0) {
  318. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  319. "sendmsg(ucast) failed (non-critical)");
  320. }
  321. }
  322. static inline void mcast_sendmsg (
  323. struct totemknet_instance *instance,
  324. const void *msg,
  325. unsigned int msg_len,
  326. int only_active)
  327. {
  328. int res;
  329. struct totem_message_header *header = (struct totem_message_header *)msg;
  330. struct msghdr msg_mcast;
  331. struct iovec iovec;
  332. iovec.iov_base = (void *)msg;
  333. iovec.iov_len = msg_len;
  334. header->target_nodeid = 0;
  335. /*
  336. * Build multicast message
  337. */
  338. memset(&msg_mcast, 0, sizeof(msg_mcast));
  339. msg_mcast.msg_iov = (void *)&iovec;
  340. msg_mcast.msg_iovlen = 1;
  341. #ifdef HAVE_MSGHDR_CONTROL
  342. msg_mcast.msg_control = 0;
  343. #endif
  344. #ifdef HAVE_MSGHDR_CONTROLLEN
  345. msg_mcast.msg_controllen = 0;
  346. #endif
  347. #ifdef HAVE_MSGHDR_FLAGS
  348. msg_mcast.msg_flags = 0;
  349. #endif
  350. #ifdef HAVE_MSGHDR_ACCRIGHTS
  351. msg_mcast.msg_accrights = NULL;
  352. #endif
  353. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  354. msg_mcast.msg_accrightslen = 0;
  355. #endif
  356. // log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_sendmsg. only_active=%d, len=%d", only_active, msg_len);
  357. res = sendmsg (instance->knet_fd, &msg_mcast, MSG_NOSIGNAL);
  358. if (res < msg_len) {
  359. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send sendmsg returned %d", res);
  360. }
  361. if (!only_active || instance->send_merge_detect_message) {
  362. /*
  363. * Current message was sent to all nodes
  364. */
  365. instance->merge_detect_messages_sent_before_timeout++;
  366. instance->send_merge_detect_message = 0;
  367. }
  368. }
  369. static int node_compare(const void *aptr, const void *bptr)
  370. {
  371. uint16_t a,b;
  372. a = *(uint16_t *)aptr;
  373. b = *(uint16_t *)bptr;
  374. return a > b;
  375. }
  376. int totemknet_ifaces_get (void *knet_context,
  377. char ***status,
  378. unsigned int *iface_count)
  379. {
  380. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  381. struct knet_link_status link_status;
  382. knet_node_id_t host_list[KNET_MAX_HOST];
  383. uint8_t link_list[KNET_MAX_LINK];
  384. size_t num_hosts;
  385. size_t num_links;
  386. size_t link_idx;
  387. int i,j;
  388. char *ptr;
  389. int res = 0;
  390. /*
  391. * Don't do the whole 'link_info' bit if the caller just wants
  392. * a count of interfaces.
  393. */
  394. if (status) {
  395. res = knet_host_get_host_list(instance->knet_handle,
  396. host_list, &num_hosts);
  397. if (res) {
  398. return (-1);
  399. }
  400. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  401. for (i=0; i<INTERFACE_MAX; i++) {
  402. memset(instance->link_status[i], 'n', CFG_INTERFACE_STATUS_MAX_LEN-1);
  403. instance->link_status[i][num_hosts] = '\0';
  404. }
  405. /* This is all a bit "inside-out" because "status" is a set of strings per link
  406. * and knet orders things by host
  407. */
  408. for (j=0; j<num_hosts; j++) {
  409. res = knet_link_get_link_list(instance->knet_handle,
  410. host_list[j], link_list, &num_links);
  411. if (res) {
  412. return (-1);
  413. }
  414. link_idx = 0;
  415. for (i=0; i < num_links; i++) {
  416. /*
  417. * Skip over links that are unconfigured to corosync. This is basically
  418. * link0 if corosync isn't using it for comms, as we will still
  419. * have it set up for loopback.
  420. */
  421. if (!instance->totem_config->interfaces[link_list[i]].configured) {
  422. continue;
  423. }
  424. ptr = instance->link_status[link_idx++];
  425. res = knet_link_get_status(instance->knet_handle,
  426. host_list[j],
  427. link_list[i],
  428. &link_status,
  429. sizeof(link_status));
  430. if (res == 0) {
  431. ptr[j] = '0' + (link_status.enabled |
  432. link_status.connected<<1 |
  433. link_status.dynconnected<<2);
  434. }
  435. else {
  436. ptr[j] = '?';
  437. }
  438. }
  439. }
  440. *status = instance->link_status;
  441. }
  442. *iface_count = INTERFACE_MAX;
  443. return (res);
  444. }
  445. int totemknet_finalize (
  446. void *knet_context)
  447. {
  448. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  449. int res = 0;
  450. int i,j;
  451. static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */
  452. uint8_t links[KNET_MAX_LINK];
  453. size_t num_nodes;
  454. size_t num_links;
  455. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  456. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  457. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  458. res = knet_host_get_host_list(instance->knet_handle, nodes, &num_nodes);
  459. if (res) {
  460. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet node list for shutdown: %s", strerror(errno));
  461. /* Crash out anyway */
  462. goto finalise_error;
  463. }
  464. /* Tidily shut down all nodes & links. This ensures that the LEAVE message will be sent */
  465. for (i=0; i<num_nodes; i++) {
  466. res = knet_link_get_link_list(instance->knet_handle, nodes[i], links, &num_links);
  467. if (res) {
  468. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet link list for node %d: %s", nodes[i], strerror(errno));
  469. goto finalise_error;
  470. }
  471. for (j=0; j<num_links; j++) {
  472. res = knet_link_set_enable(instance->knet_handle, nodes[i], links[j], 0);
  473. if (res) {
  474. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_set_enable(node %d, link %d) failed: %s", nodes[i], links[j], strerror(errno));
  475. }
  476. res = knet_link_clear_config(instance->knet_handle, nodes[i], links[j]);
  477. if (res) {
  478. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_clear_config(node %d, link %d) failed: %s", nodes[i], links[j], strerror(errno));
  479. }
  480. }
  481. res = knet_host_remove(instance->knet_handle, nodes[i]);
  482. if (res) {
  483. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_host_remove(node %d) failed: %s", nodes[i], strerror(errno));
  484. }
  485. }
  486. finalise_error:
  487. res = knet_handle_setfwd(instance->knet_handle, 0);
  488. if (res) {
  489. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_setfwd failed: %s", strerror(errno));
  490. }
  491. res = knet_handle_free(instance->knet_handle);
  492. if (res) {
  493. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_free failed: %s", strerror(errno));
  494. }
  495. totemknet_stop_merge_detect_timeout(instance);
  496. log_flush_messages(instance);
  497. return (res);
  498. }
  499. static int log_deliver_fn (
  500. int fd,
  501. int revents,
  502. void *data)
  503. {
  504. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  505. char buffer[sizeof(struct knet_log_msg)*4];
  506. char *bufptr = buffer;
  507. int done = 0;
  508. int len;
  509. len = read(fd, buffer, sizeof(buffer));
  510. while (done < len) {
  511. struct knet_log_msg *msg = (struct knet_log_msg *)bufptr;
  512. switch (msg->msglevel) {
  513. case KNET_LOG_ERR:
  514. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s: %s",
  515. knet_log_get_subsystem_name(msg->subsystem),
  516. msg->msg);
  517. break;
  518. case KNET_LOG_WARN:
  519. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s: %s",
  520. knet_log_get_subsystem_name(msg->subsystem),
  521. msg->msg);
  522. break;
  523. case KNET_LOG_INFO:
  524. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s: %s",
  525. knet_log_get_subsystem_name(msg->subsystem),
  526. msg->msg);
  527. break;
  528. case KNET_LOG_DEBUG:
  529. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s: %s",
  530. knet_log_get_subsystem_name(msg->subsystem),
  531. msg->msg);
  532. break;
  533. }
  534. bufptr += sizeof(struct knet_log_msg);
  535. done += sizeof(struct knet_log_msg);
  536. }
  537. return 0;
  538. }
  539. static int data_deliver_fn (
  540. int fd,
  541. int revents,
  542. void *data)
  543. {
  544. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  545. struct msghdr msg_hdr;
  546. struct iovec iov_recv;
  547. struct sockaddr_storage system_from;
  548. ssize_t msg_len;
  549. int truncated_packet;
  550. iov_recv.iov_base = instance->iov_buffer;
  551. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  552. msg_hdr.msg_name = &system_from;
  553. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  554. msg_hdr.msg_iov = &iov_recv;
  555. msg_hdr.msg_iovlen = 1;
  556. #ifdef HAVE_MSGHDR_CONTROL
  557. msg_hdr.msg_control = 0;
  558. #endif
  559. #ifdef HAVE_MSGHDR_CONTROLLEN
  560. msg_hdr.msg_controllen = 0;
  561. #endif
  562. #ifdef HAVE_MSGHDR_FLAGS
  563. msg_hdr.msg_flags = 0;
  564. #endif
  565. #ifdef HAVE_MSGHDR_ACCRIGHTS
  566. msg_hdr.msg_accrights = NULL;
  567. #endif
  568. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  569. msg_hdr.msg_accrightslen = 0;
  570. #endif
  571. msg_len = recvmsg (fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  572. if (msg_len <= 0) {
  573. return (0);
  574. }
  575. truncated_packet = 0;
  576. #ifdef HAVE_MSGHDR_FLAGS
  577. if (msg_hdr.msg_flags & MSG_TRUNC) {
  578. truncated_packet = 1;
  579. }
  580. #else
  581. /*
  582. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  583. * if bytes_received == KNET_MAX_PACKET_SIZE then packet is truncated
  584. */
  585. if (bytes_received == KNET_MAX_PACKET_SIZE) {
  586. truncated_packet = 1;
  587. }
  588. #endif
  589. if (truncated_packet) {
  590. knet_log_printf(instance->totemknet_log_level_error,
  591. "Received too big message. This may be because something bad is happening"
  592. "on the network (attack?), or you tried join more nodes than corosync is"
  593. "compiled with (%u) or bug in the code (bad estimation of "
  594. "the KNET_MAX_PACKET_SIZE). Dropping packet.", PROCESSOR_COUNT_MAX);
  595. return (0);
  596. }
  597. /*
  598. * Handle incoming message
  599. */
  600. instance->totemknet_deliver_fn (
  601. instance->context,
  602. instance->iov_buffer,
  603. msg_len,
  604. &system_from);
  605. return (0);
  606. }
  607. static void timer_function_netif_check_timeout (
  608. void *data)
  609. {
  610. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  611. int i;
  612. for (i=0; i < INTERFACE_MAX; i++) {
  613. if (!instance->totem_config->interfaces[i].configured) {
  614. continue;
  615. }
  616. instance->totemknet_iface_change_fn (instance->context,
  617. &instance->my_ids[i],
  618. i);
  619. }
  620. }
  621. static void knet_set_access_list_config(struct totemknet_instance *instance)
  622. {
  623. #ifdef HAVE_KNET_ACCESS_LIST
  624. uint32_t value;
  625. cs_error_t err;
  626. value = instance->totem_config->block_unlisted_ips;
  627. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_enable access list: %d", value);
  628. err = knet_handle_enable_access_lists(instance->knet_handle, value);
  629. if (err) {
  630. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_access_lists failed");
  631. }
  632. #endif
  633. }
  634. /* NOTE: this relies on the fact that totem_reload_notify() is called first */
  635. static void totemknet_refresh_config(
  636. int32_t event,
  637. const char *key_name,
  638. struct icmap_notify_value new_val,
  639. struct icmap_notify_value old_val,
  640. void *user_data)
  641. {
  642. uint8_t reloading;
  643. uint32_t value;
  644. uint32_t link_no;
  645. size_t num_nodes;
  646. knet_node_id_t host_ids[KNET_MAX_HOST];
  647. int i;
  648. int err;
  649. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  650. ENTER();
  651. /*
  652. * If a full reload is in progress then don't do anything until it's done and
  653. * can reconfigure it all atomically
  654. */
  655. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  656. return;
  657. }
  658. knet_set_access_list_config(instance);
  659. if (icmap_get_uint32("totem.knet_pmtud_interval", &value) == CS_OK) {
  660. instance->totem_config->knet_pmtud_interval = value;
  661. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %d", value);
  662. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  663. if (err) {
  664. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  665. }
  666. }
  667. /* Configure link parameters for each node */
  668. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  669. if (err != 0) {
  670. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  671. }
  672. for (i=0; i<num_nodes; i++) {
  673. for (link_no = 0; link_no < INTERFACE_MAX; link_no++) {
  674. if (host_ids[i] == instance->our_nodeid || !instance->totem_config->interfaces[link_no].configured) {
  675. continue;
  676. }
  677. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  678. instance->totem_config->interfaces[link_no].knet_ping_interval,
  679. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  680. instance->totem_config->interfaces[link_no].knet_ping_precision);
  681. if (err) {
  682. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for node %d link %d failed", host_ids[i], link_no);
  683. }
  684. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  685. instance->totem_config->interfaces[link_no].knet_pong_count);
  686. if (err) {
  687. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for node %d link %d failed",host_ids[i], link_no);
  688. }
  689. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  690. instance->totem_config->interfaces[link_no].knet_link_priority);
  691. if (err) {
  692. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for node %d link %d failed", host_ids[i], link_no);
  693. }
  694. }
  695. }
  696. LEAVE();
  697. }
  698. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  699. {
  700. icmap_track_t icmap_track_totem = NULL;
  701. icmap_track_t icmap_track_reload = NULL;
  702. ENTER();
  703. icmap_track_add("totem.",
  704. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  705. totemknet_refresh_config,
  706. instance,
  707. &icmap_track_totem);
  708. icmap_track_add("config.totemconfig_reload_in_progress",
  709. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  710. totemknet_refresh_config,
  711. instance,
  712. &icmap_track_reload);
  713. LEAVE();
  714. }
  715. /*
  716. * Create an instance
  717. */
  718. int totemknet_initialize (
  719. qb_loop_t *poll_handle,
  720. void **knet_context,
  721. struct totem_config *totem_config,
  722. totemsrp_stats_t *stats,
  723. void *context,
  724. void (*deliver_fn) (
  725. void *context,
  726. const void *msg,
  727. unsigned int msg_len,
  728. const struct sockaddr_storage *system_from),
  729. void (*iface_change_fn) (
  730. void *context,
  731. const struct totem_ip_address *iface_address,
  732. unsigned int link_no),
  733. void (*mtu_changed) (
  734. void *context,
  735. int net_mtu),
  736. void (*target_set_completed) (
  737. void *context))
  738. {
  739. struct totemknet_instance *instance;
  740. int8_t channel=0;
  741. int res;
  742. int i;
  743. instance = malloc (sizeof (struct totemknet_instance));
  744. if (instance == NULL) {
  745. return (-1);
  746. }
  747. totemknet_instance_initialize (instance);
  748. instance->totem_config = totem_config;
  749. /*
  750. * Configure logging
  751. */
  752. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  753. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  754. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  755. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  756. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  757. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  758. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  759. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  760. /*
  761. * Initialize local variables for totemknet
  762. */
  763. instance->our_nodeid = instance->totem_config->node_id;
  764. for (i=0; i< INTERFACE_MAX; i++) {
  765. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  766. instance->my_ids[i].nodeid = instance->our_nodeid;
  767. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  768. /* Needed for totemsrp */
  769. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  770. }
  771. instance->poll_handle = poll_handle;
  772. instance->context = context;
  773. instance->totemknet_deliver_fn = deliver_fn;
  774. instance->totemknet_iface_change_fn = iface_change_fn;
  775. instance->totemknet_mtu_changed = mtu_changed;
  776. instance->totemknet_target_set_completed = target_set_completed;
  777. instance->loopback_link = 0;
  778. res = pipe(instance->logpipes);
  779. if (res == -1) {
  780. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  781. goto exit_error;
  782. }
  783. fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK);
  784. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK);
  785. #if !defined(KNET_API_VER) || (KNET_API_VER == 1)
  786. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  787. #endif
  788. #if KNET_API_VER == 2
  789. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, KNET_HANDLE_FLAG_PRIVILEGED);
  790. #endif
  791. if (!instance->knet_handle) {
  792. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  793. goto exit_error;
  794. }
  795. knet_set_access_list_config(instance);
  796. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  797. if (res) {
  798. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  799. }
  800. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  801. if (res) {
  802. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  803. }
  804. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  805. if (res) {
  806. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  807. }
  808. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  809. if (res) {
  810. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  811. }
  812. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  813. if (res) {
  814. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  815. }
  816. global_instance = instance;
  817. /* Get an fd into knet */
  818. instance->knet_fd = 0;
  819. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  820. if (res) {
  821. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  822. goto exit_error;
  823. }
  824. /* Enable crypto if requested */
  825. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  826. struct knet_handle_crypto_cfg crypto_cfg;
  827. strcpy(crypto_cfg.crypto_model, instance->totem_config->crypto_model);
  828. strcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type);
  829. strcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type);
  830. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  831. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  832. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  833. if (res == -1) {
  834. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  835. goto exit_error;
  836. }
  837. if (res == -2) {
  838. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  839. goto exit_error;
  840. }
  841. knet_log_printf(LOG_INFO, "kronosnet crypto initialized: %s/%s", crypto_cfg.crypto_cipher_type, crypto_cfg.crypto_hash_type);
  842. }
  843. /* Set up compression */
  844. totemknet_reconfigure(instance, instance->totem_config);
  845. knet_handle_setfwd(instance->knet_handle, 1);
  846. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  847. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  848. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  849. }
  850. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  851. instance->link_mode = KNET_LINK_POLICY_RR;
  852. }
  853. for (i=0; i<INTERFACE_MAX; i++) {
  854. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  855. if (!instance->link_status[i]) {
  856. goto exit_error;
  857. }
  858. }
  859. qb_loop_poll_add (instance->poll_handle,
  860. QB_LOOP_MED,
  861. instance->logpipes[0],
  862. POLLIN, instance, log_deliver_fn);
  863. qb_loop_poll_add (instance->poll_handle,
  864. QB_LOOP_HIGH,
  865. instance->knet_fd,
  866. POLLIN, instance, data_deliver_fn);
  867. /*
  868. * Upper layer isn't ready to receive message because it hasn't
  869. * initialized yet. Add short timer to check the interfaces.
  870. */
  871. qb_loop_timer_add (instance->poll_handle,
  872. QB_LOOP_MED,
  873. 100*QB_TIME_NS_IN_MSEC,
  874. (void *)instance,
  875. timer_function_netif_check_timeout,
  876. &instance->timer_netif_check_timeout);
  877. totemknet_start_merge_detect_timeout(instance);
  878. /* Start listening for config changes */
  879. totemknet_add_config_notifications(instance);
  880. /* Add stats keys to icmap */
  881. stats_knet_add_handle();
  882. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  883. *knet_context = instance;
  884. return (0);
  885. exit_error:
  886. log_flush_messages(instance);
  887. free(instance);
  888. return (-1);
  889. }
  890. void *totemknet_buffer_alloc (void)
  891. {
  892. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  893. return malloc(KNET_MAX_PACKET_SIZE + 512);
  894. }
  895. void totemknet_buffer_release (void *ptr)
  896. {
  897. return free (ptr);
  898. }
  899. int totemknet_processor_count_set (
  900. void *knet_context,
  901. int processor_count)
  902. {
  903. return (0);
  904. }
  905. int totemknet_recv_flush (void *knet_context)
  906. {
  907. return (0);
  908. }
  909. int totemknet_send_flush (void *knet_context)
  910. {
  911. return (0);
  912. }
  913. int totemknet_token_send (
  914. void *knet_context,
  915. const void *msg,
  916. unsigned int msg_len)
  917. {
  918. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  919. int res = 0;
  920. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  921. return (res);
  922. }
  923. int totemknet_mcast_flush_send (
  924. void *knet_context,
  925. const void *msg,
  926. unsigned int msg_len)
  927. {
  928. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  929. int res = 0;
  930. mcast_sendmsg (instance, msg, msg_len, 0);
  931. return (res);
  932. }
  933. int totemknet_mcast_noflush_send (
  934. void *knet_context,
  935. const void *msg,
  936. unsigned int msg_len)
  937. {
  938. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  939. int res = 0;
  940. mcast_sendmsg (instance, msg, msg_len, 1);
  941. return (res);
  942. }
  943. extern int totemknet_iface_check (void *knet_context)
  944. {
  945. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  946. int res = 0;
  947. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  948. return (res);
  949. }
  950. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  951. {
  952. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  953. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  954. }
  955. int totemknet_token_target_set (
  956. void *knet_context,
  957. unsigned int nodeid)
  958. {
  959. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  960. int res = 0;
  961. instance->token_target.nodeid = nodeid;
  962. instance->totemknet_target_set_completed (instance->context);
  963. return (res);
  964. }
  965. extern int totemknet_recv_mcast_empty (
  966. void *knet_context)
  967. {
  968. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  969. unsigned int res;
  970. struct sockaddr_storage system_from;
  971. struct msghdr msg_hdr;
  972. struct iovec iov_recv;
  973. struct pollfd ufd;
  974. int nfds;
  975. int msg_processed = 0;
  976. iov_recv.iov_base = instance->iov_buffer;
  977. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  978. msg_hdr.msg_name = &system_from;
  979. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  980. msg_hdr.msg_iov = &iov_recv;
  981. msg_hdr.msg_iovlen = 1;
  982. #ifdef HAVE_MSGHDR_CONTROL
  983. msg_hdr.msg_control = 0;
  984. #endif
  985. #ifdef HAVE_MSGHDR_CONTROLLEN
  986. msg_hdr.msg_controllen = 0;
  987. #endif
  988. #ifdef HAVE_MSGHDR_FLAGS
  989. msg_hdr.msg_flags = 0;
  990. #endif
  991. #ifdef HAVE_MSGHDR_ACCRIGHTS
  992. msg_msg_hdr.msg_accrights = NULL;
  993. #endif
  994. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  995. msg_msg_hdr.msg_accrightslen = 0;
  996. #endif
  997. do {
  998. ufd.fd = instance->knet_fd;
  999. ufd.events = POLLIN;
  1000. nfds = poll (&ufd, 1, 0);
  1001. if (nfds == 1 && ufd.revents & POLLIN) {
  1002. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  1003. if (res != -1) {
  1004. msg_processed = 1;
  1005. } else {
  1006. msg_processed = -1;
  1007. }
  1008. }
  1009. } while (nfds == 1);
  1010. return (msg_processed);
  1011. }
  1012. int totemknet_iface_set (void *knet_context,
  1013. const struct totem_ip_address *local_addr,
  1014. unsigned short ip_port,
  1015. unsigned int iface_no)
  1016. {
  1017. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1018. totemip_copy(&instance->my_ids[iface_no], local_addr);
  1019. knet_log_printf(LOG_INFO, "Configured link number %d: local addr: %s, port=%d", iface_no, totemip_print(local_addr), ip_port);
  1020. instance->ip_port[iface_no] = ip_port;
  1021. return 0;
  1022. }
  1023. int totemknet_member_add (
  1024. void *knet_context,
  1025. const struct totem_ip_address *local,
  1026. const struct totem_ip_address *member,
  1027. int link_no)
  1028. {
  1029. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1030. int err;
  1031. int port = instance->ip_port[link_no];
  1032. struct sockaddr_storage remote_ss;
  1033. struct sockaddr_storage local_ss;
  1034. int addrlen;
  1035. int i;
  1036. int host_found = 0;
  1037. knet_node_id_t host_ids[KNET_MAX_HOST];
  1038. size_t num_host_ids;
  1039. /* Only create 1 loopback link and use link 0 */
  1040. if (member->nodeid == instance->our_nodeid) {
  1041. if (!instance->loopback_link) {
  1042. link_no = 0;
  1043. instance->loopback_link = 1;
  1044. } else {
  1045. /* Already done */
  1046. return 0;
  1047. }
  1048. }
  1049. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: %d (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  1050. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: %d (%s)", local->nodeid, totemip_print(local));
  1051. /* Only add the host if it doesn't already exist in knet */
  1052. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_host_ids);
  1053. if (err) {
  1054. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list");
  1055. return -1;
  1056. }
  1057. for (i=0; i<num_host_ids; i++) {
  1058. if (host_ids[i] == member->nodeid) {
  1059. host_found = 1;
  1060. }
  1061. }
  1062. if (!host_found) {
  1063. err = knet_host_add(instance->knet_handle, member->nodeid);
  1064. if (err != 0 && errno != EEXIST) {
  1065. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  1066. return -1;
  1067. }
  1068. } else {
  1069. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nodeid %d already added", member->nodeid);
  1070. }
  1071. if (err == 0) {
  1072. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  1073. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  1074. return -1;
  1075. }
  1076. }
  1077. memset(&local_ss, 0, sizeof(local_ss));
  1078. /* Casts to remove const */
  1079. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port, &remote_ss, &addrlen);
  1080. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port, &local_ss, &addrlen);
  1081. if (member->nodeid == instance->our_nodeid) {
  1082. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: loopback link is %d\n", link_no);
  1083. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1084. KNET_TRANSPORT_LOOPBACK,
  1085. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1086. }
  1087. else {
  1088. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1089. instance->totem_config->interfaces[link_no].knet_transport,
  1090. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1091. }
  1092. if (err) {
  1093. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  1094. return -1;
  1095. }
  1096. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  1097. instance->totem_config->interfaces[link_no].knet_link_priority);
  1098. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  1099. instance->totem_config->interfaces[link_no].knet_link_priority);
  1100. if (err) {
  1101. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid %d, link %d failed", member->nodeid, link_no);
  1102. }
  1103. /* ping timeouts maybe 0 here for a newly added interface so we leave this till later, it will
  1104. get done in totemknet_refresh_config */
  1105. if (instance->totem_config->interfaces[link_no].knet_ping_interval != 0) {
  1106. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  1107. instance->totem_config->interfaces[link_no].knet_ping_interval,
  1108. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  1109. instance->totem_config->interfaces[link_no].knet_ping_precision);
  1110. if (err) {
  1111. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for nodeid %d, link %d failed", member->nodeid, link_no);
  1112. }
  1113. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  1114. instance->totem_config->interfaces[link_no].knet_pong_count);
  1115. if (err) {
  1116. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for nodeid %d, link %d failed", member->nodeid, link_no);
  1117. }
  1118. }
  1119. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1120. if (err) {
  1121. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid %d, link %d failed", member->nodeid, link_no);
  1122. return -1;
  1123. }
  1124. /* register stats */
  1125. stats_knet_add_member(member->nodeid, link_no);
  1126. return (0);
  1127. }
  1128. int totemknet_member_remove (
  1129. void *knet_context,
  1130. const struct totem_ip_address *token_target,
  1131. int link_no)
  1132. {
  1133. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1134. int res;
  1135. uint8_t link_list[KNET_MAX_LINK];
  1136. size_t num_links;
  1137. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: %d, link=%d", token_target->nodeid, link_no);
  1138. /* Don't remove the link with the loopback on it until we shut down */
  1139. if (token_target->nodeid == instance->our_nodeid) {
  1140. return 0;
  1141. }
  1142. /* Tidy stats */
  1143. stats_knet_del_member(token_target->nodeid, link_no);
  1144. /* Remove the link first */
  1145. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1146. if (res != 0) {
  1147. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set enable(off) for nodeid %d, link %d failed", token_target->nodeid, link_no);
  1148. return res;
  1149. }
  1150. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1151. if (res != 0) {
  1152. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_clear_config for nodeid %d, link %d failed", token_target->nodeid, link_no);
  1153. return res;
  1154. }
  1155. /* If this is the last link, then remove the node */
  1156. res = knet_link_get_link_list(instance->knet_handle,
  1157. token_target->nodeid, link_list, &num_links);
  1158. if (res) {
  1159. return (0); /* not really failure */
  1160. }
  1161. if (num_links == 0) {
  1162. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1163. }
  1164. return res;
  1165. }
  1166. int totemknet_member_list_rebind_ip (
  1167. void *knet_context)
  1168. {
  1169. return (0);
  1170. }
  1171. int totemknet_reconfigure (
  1172. void *knet_context,
  1173. struct totem_config *totem_config)
  1174. {
  1175. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1176. struct knet_handle_compress_cfg compress_cfg;
  1177. int res = 0;
  1178. if (totem_config->knet_compression_model) {
  1179. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1180. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1181. compress_cfg.compress_level = totem_config->knet_compression_level;
  1182. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1183. if (res) {
  1184. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1185. }
  1186. }
  1187. #ifdef HAVE_LIBNOZZLE
  1188. /* Set up nozzle device(s) */
  1189. setup_nozzle(instance);
  1190. #endif
  1191. return (res);
  1192. }
  1193. void totemknet_stats_clear (
  1194. void *knet_context)
  1195. {
  1196. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1197. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1198. }
  1199. /* For the stats module */
  1200. int totemknet_link_get_status (
  1201. knet_node_id_t node, uint8_t link_no,
  1202. struct knet_link_status *status)
  1203. {
  1204. int res;
  1205. int ret = CS_OK;
  1206. /* We are probably not using knet */
  1207. if (!global_instance) {
  1208. return CS_ERR_NOT_EXIST;
  1209. }
  1210. if (link_no >= INTERFACE_MAX) {
  1211. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1212. }
  1213. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1214. if (res) {
  1215. switch (errno) {
  1216. case EINVAL:
  1217. ret = CS_ERR_INVALID_PARAM;
  1218. break;
  1219. case EBUSY:
  1220. ret = CS_ERR_BUSY;
  1221. break;
  1222. case EDEADLK:
  1223. ret = CS_ERR_TRY_AGAIN;
  1224. break;
  1225. default:
  1226. ret = CS_ERR_LIBRARY;
  1227. break;
  1228. }
  1229. }
  1230. return (ret);
  1231. }
  1232. int totemknet_handle_get_stats (
  1233. struct knet_handle_stats *stats)
  1234. {
  1235. /* We are probably not using knet */
  1236. if (!global_instance) {
  1237. return CS_ERR_NOT_EXIST;
  1238. }
  1239. return knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1240. }
  1241. static void timer_function_merge_detect_timeout (
  1242. void *data)
  1243. {
  1244. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1245. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1246. instance->send_merge_detect_message = 1;
  1247. }
  1248. instance->merge_detect_messages_sent_before_timeout = 0;
  1249. totemknet_start_merge_detect_timeout(instance);
  1250. }
  1251. static void totemknet_start_merge_detect_timeout(
  1252. void *knet_context)
  1253. {
  1254. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1255. qb_loop_timer_add(instance->poll_handle,
  1256. QB_LOOP_MED,
  1257. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1258. (void *)instance,
  1259. timer_function_merge_detect_timeout,
  1260. &instance->timer_merge_detect_timeout);
  1261. }
  1262. static void totemknet_stop_merge_detect_timeout(
  1263. void *knet_context)
  1264. {
  1265. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1266. qb_loop_timer_del(instance->poll_handle,
  1267. instance->timer_merge_detect_timeout);
  1268. }
  1269. static void log_flush_messages (void *knet_context)
  1270. {
  1271. struct pollfd pfd;
  1272. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1273. int cont;
  1274. cont = 1;
  1275. while (cont) {
  1276. pfd.fd = instance->logpipes[0];
  1277. pfd.events = POLLIN;
  1278. pfd.revents = 0;
  1279. if ((poll(&pfd, 1, 0) > 0) &&
  1280. (pfd.revents & POLLIN) &&
  1281. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1282. cont = 1;
  1283. } else {
  1284. cont = 0;
  1285. }
  1286. }
  1287. }
  1288. #ifdef HAVE_LIBNOZZLE
  1289. #define NOZZLE_NAME "nozzle.name"
  1290. #define NOZZLE_IPADDR "nozzle.ipaddr"
  1291. #define NOZZLE_PREFIX "nozzle.ipprefix"
  1292. #define NOZZLE_MACADDR "nozzle.macaddr"
  1293. #define NOZZLE_CHANNEL 1
  1294. static char *get_nozzle_script_dir(void *knet_context)
  1295. {
  1296. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1297. char filename[PATH_MAX + FILENAME_MAX + 1];
  1298. static char updown_dirname[PATH_MAX + FILENAME_MAX + 1];
  1299. int res;
  1300. const char *dirname_res;
  1301. /*
  1302. * Build script directory based on corosync.conf file location
  1303. */
  1304. res = snprintf(filename, sizeof(filename), "%s",
  1305. corosync_get_config_file());
  1306. if (res >= sizeof(filename)) {
  1307. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1308. return NULL;
  1309. }
  1310. dirname_res = dirname(filename);
  1311. res = snprintf(updown_dirname, sizeof(updown_dirname), "%s/%s",
  1312. dirname_res, "updown.d");
  1313. if (res >= sizeof(updown_dirname)) {
  1314. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1315. return NULL;
  1316. }
  1317. return updown_dirname;
  1318. }
  1319. /*
  1320. * Deliberately doesn't return the status as caller doesn't care.
  1321. * The result will be logged though
  1322. */
  1323. static void run_nozzle_script(struct totemknet_instance *instance, int type, const char *typename)
  1324. {
  1325. int res;
  1326. char *exec_string;
  1327. res = nozzle_run_updown(instance->nozzle_handle, type, &exec_string);
  1328. if (res == -1 && errno != ENOENT) {
  1329. knet_log_printf (LOGSYS_LEVEL_INFO, "exec nozzle %s script failed: %s", typename, strerror(errno));
  1330. } else if (res == -2) {
  1331. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle %s script failed", typename);
  1332. knet_log_printf (LOGSYS_LEVEL_INFO, "%s", exec_string);
  1333. }
  1334. }
  1335. /*
  1336. * Reparse IP address to add in our node ID
  1337. * IPv6 addresses must end in '::'
  1338. * IPv4 addresses must just be valid
  1339. * '/xx' lengths are optional for IPv6, mandatory for IPv4
  1340. *
  1341. * Returns the modified IP address as a string to pass into libnozzle
  1342. */
  1343. static int reparse_nozzle_ip_address(struct totemknet_instance *instance,
  1344. const char *input_addr,
  1345. const char *prefix, int nodeid,
  1346. char *output_addr, size_t output_len)
  1347. {
  1348. char *coloncolon;
  1349. int bits;
  1350. int max_prefix = 64;
  1351. uint32_t nodeid_mask;
  1352. uint32_t addr_mask;
  1353. uint32_t masked_nodeid;
  1354. struct in_addr *addr;
  1355. struct totem_ip_address totemip;
  1356. coloncolon = strstr(input_addr, "::");
  1357. if (!coloncolon) {
  1358. max_prefix = 30;
  1359. }
  1360. bits = atoi(prefix);
  1361. if (bits < 8 || bits > max_prefix) {
  1362. knet_log_printf(LOGSYS_LEVEL_ERROR, "nozzle IP address prefix must be >= 8 and <= %d (got %d)", max_prefix, bits);
  1363. return -1;
  1364. }
  1365. /* IPv6 is easy */
  1366. if (coloncolon) {
  1367. memcpy(output_addr, input_addr, coloncolon-input_addr);
  1368. sprintf(output_addr + (coloncolon-input_addr), "::%x", nodeid);
  1369. return 0;
  1370. }
  1371. /* For IPv4 we need to parse the address into binary, mask off the required bits,
  1372. * add in the masked_nodeid and 'print' it out again
  1373. */
  1374. nodeid_mask = UINT32_MAX & ((1<<(32 - bits)) - 1);
  1375. addr_mask = UINT32_MAX ^ nodeid_mask;
  1376. masked_nodeid = nodeid & nodeid_mask;
  1377. if (totemip_parse(&totemip, input_addr, AF_INET)) {
  1378. knet_log_printf(LOGSYS_LEVEL_ERROR, "Failed to parse IPv4 nozzle IP address");
  1379. return -1;
  1380. }
  1381. addr = (struct in_addr *)&totemip.addr;
  1382. addr->s_addr &= htonl(addr_mask);
  1383. addr->s_addr |= htonl(masked_nodeid);
  1384. inet_ntop(AF_INET, addr, output_addr, output_len);
  1385. return 0;
  1386. }
  1387. static int create_nozzle_device(void *knet_context, const char *name,
  1388. const char *ipaddr, const char *prefix,
  1389. const char *macaddr)
  1390. {
  1391. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1392. char device_name[IFNAMSIZ+1];
  1393. size_t size = IFNAMSIZ;
  1394. int8_t channel = NOZZLE_CHANNEL;
  1395. nozzle_t nozzle_dev;
  1396. int nozzle_fd;
  1397. int res;
  1398. char *updown_dir;
  1399. char parsed_ipaddr[INET6_ADDRSTRLEN];
  1400. char mac[19];
  1401. memset(device_name, 0, size);
  1402. memset(&mac, 0, sizeof(mac));
  1403. strncpy(device_name, name, size);
  1404. updown_dir = get_nozzle_script_dir(knet_context);
  1405. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle script dir is %s", updown_dir);
  1406. nozzle_dev = nozzle_open(device_name, size, updown_dir);
  1407. if (!nozzle_dev) {
  1408. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to init nozzle device %s: %s", device_name, strerror(errno));
  1409. return -1;
  1410. }
  1411. instance->nozzle_handle = nozzle_dev;
  1412. if (nozzle_set_mac(nozzle_dev, macaddr) < 0) {
  1413. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle MAC to %s: %s", mac, strerror(errno));
  1414. goto out_clean;
  1415. }
  1416. if (reparse_nozzle_ip_address(instance, ipaddr, prefix, instance->our_nodeid, parsed_ipaddr, sizeof(parsed_ipaddr))) {
  1417. /* Prints its own errors */
  1418. goto out_clean;
  1419. }
  1420. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle IP address is %s / %d", parsed_ipaddr, atoi(prefix));
  1421. if (ipaddr) {
  1422. if (nozzle_add_ip(nozzle_dev, parsed_ipaddr, prefix) < 0) {
  1423. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle IP addr to %s/%s: %s", parsed_ipaddr, prefix, strerror(errno));
  1424. goto out_clean;
  1425. }
  1426. }
  1427. nozzle_fd = nozzle_get_fd(nozzle_dev);
  1428. knet_log_printf (LOGSYS_LEVEL_INFO, "Opened '%s' on fd %d", device_name, nozzle_fd);
  1429. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel);
  1430. if (res != 0) {
  1431. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add nozzle FD to knet: %s", strerror(errno));
  1432. goto out_clean;
  1433. }
  1434. run_nozzle_script(instance, NOZZLE_PREUP, "pre-up");
  1435. res = nozzle_set_up(nozzle_dev);
  1436. if (res != 0) {
  1437. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to set nozzle interface UP: %s", strerror(errno));
  1438. goto out_clean;
  1439. }
  1440. run_nozzle_script(instance, NOZZLE_UP, "up");
  1441. return 0;
  1442. out_clean:
  1443. nozzle_close(nozzle_dev);
  1444. return -1;
  1445. }
  1446. static int remove_nozzle_device(void *knet_context)
  1447. {
  1448. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1449. int res;
  1450. int datafd;
  1451. res = knet_handle_get_datafd(instance->knet_handle, NOZZLE_CHANNEL, &datafd);
  1452. if (res != 0) {
  1453. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't find datafd for channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1454. return -1;
  1455. }
  1456. res = knet_handle_remove_datafd(instance->knet_handle, datafd);
  1457. if (res != 0) {
  1458. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't remove datafd for nozzle channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1459. return -1;
  1460. }
  1461. run_nozzle_script(instance, NOZZLE_DOWN, "pre-down");
  1462. res = nozzle_set_down(instance->nozzle_handle);
  1463. if (res != 0) {
  1464. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't set nozzle device down: %s", strerror(errno));
  1465. return -1;
  1466. }
  1467. run_nozzle_script(instance, NOZZLE_POSTDOWN, "post-down");
  1468. res = nozzle_close(instance->nozzle_handle);
  1469. if (res != 0) {
  1470. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't close nozzle device: %s", strerror(errno));
  1471. return -1;
  1472. }
  1473. knet_log_printf (LOGSYS_LEVEL_INFO, "Removed nozzle device");
  1474. return 0;
  1475. }
  1476. static void free_nozzle(struct totemknet_instance *instance)
  1477. {
  1478. free(instance->nozzle_name);
  1479. free(instance->nozzle_ipaddr);
  1480. free(instance->nozzle_prefix);
  1481. free(instance->nozzle_macaddr);
  1482. instance->nozzle_name = instance->nozzle_ipaddr = instance->nozzle_prefix =
  1483. instance->nozzle_macaddr = NULL;
  1484. }
  1485. static int setup_nozzle(void *knet_context)
  1486. {
  1487. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1488. char *ipaddr_str = NULL;
  1489. char *name_str = NULL;
  1490. char *prefix_str = NULL;
  1491. char *macaddr_str = NULL;
  1492. char mac[32];
  1493. int name_res;
  1494. int macaddr_res;
  1495. int res;
  1496. icmap_get_string(NOZZLE_IPADDR, &ipaddr_str);
  1497. icmap_get_string(NOZZLE_PREFIX, &prefix_str);
  1498. macaddr_res = icmap_get_string(NOZZLE_MACADDR, &macaddr_str);
  1499. name_res = icmap_get_string(NOZZLE_NAME, &name_str);
  1500. /* Is is being removed? */
  1501. if (name_res == CS_ERR_NOT_EXIST && instance->nozzle_handle) {
  1502. remove_nozzle_device(instance);
  1503. free_nozzle(instance);
  1504. goto out_free;
  1505. }
  1506. if (!name_str) {
  1507. /* no nozzle */
  1508. goto out_free;
  1509. }
  1510. if (!ipaddr_str) {
  1511. knet_log_printf (LOGSYS_LEVEL_ERROR, "No IP address supplied for Nozzle device");
  1512. goto out_free;
  1513. }
  1514. if (!prefix_str) {
  1515. knet_log_printf (LOGSYS_LEVEL_ERROR, "No prefix supplied for Nozzle IP address");
  1516. goto out_free;
  1517. }
  1518. if (macaddr_str && strlen(macaddr_str) != 17) {
  1519. knet_log_printf (LOGSYS_LEVEL_ERROR, "macaddr for nozzle device is not in the correct format '%s'", macaddr_str);
  1520. goto out_free;
  1521. }
  1522. if (!macaddr_str) {
  1523. macaddr_str = (char*)"54:54:01:00:00:00";
  1524. }
  1525. if (instance->nozzle_name &&
  1526. (strcmp(name_str, instance->nozzle_name) == 0) &&
  1527. (strcmp(ipaddr_str, instance->nozzle_ipaddr) == 0) &&
  1528. (strcmp(prefix_str, instance->nozzle_prefix) == 0) &&
  1529. ((macaddr_str == NULL && instance->nozzle_macaddr == NULL) ||
  1530. strcmp(macaddr_str, instance->nozzle_macaddr) == 0)) {
  1531. /* Nothing has changed */
  1532. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Nozzle device info not changed");
  1533. goto out_free;
  1534. }
  1535. /* Add nodeid into MAC address */
  1536. memcpy(mac, macaddr_str, 12);
  1537. snprintf(mac+12, sizeof(mac) - 13, "%02x:%02x",
  1538. instance->our_nodeid >> 8,
  1539. instance->our_nodeid & 0xFF);
  1540. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle MAC address is %s", mac);
  1541. if (name_res == CS_OK && name_str) {
  1542. /* Reconfigure */
  1543. if (instance->nozzle_name) {
  1544. remove_nozzle_device(instance);
  1545. free_nozzle(instance);
  1546. }
  1547. res = create_nozzle_device(knet_context, name_str, ipaddr_str, prefix_str,
  1548. mac);
  1549. instance->nozzle_name = strdup(name_str);
  1550. instance->nozzle_ipaddr = strdup(ipaddr_str);
  1551. instance->nozzle_prefix = strdup(prefix_str);
  1552. instance->nozzle_macaddr = strdup(macaddr_str);
  1553. if (!instance->nozzle_name || !instance->nozzle_ipaddr ||
  1554. !instance->nozzle_prefix) {
  1555. knet_log_printf (LOGSYS_LEVEL_ERROR, "strdup failed in nozzle allocation");
  1556. /*
  1557. * This 'free' will cause a complete reconfigure of the device next time we reload
  1558. * but will also let the the current device keep working until then.
  1559. * remove_nozzle() only needs the, statically-allocated, nozzle_handle
  1560. */
  1561. free_nozzle(instance);
  1562. }
  1563. }
  1564. out_free:
  1565. free(name_str);
  1566. free(ipaddr_str);
  1567. free(prefix_str);
  1568. if (macaddr_res == CS_OK) {
  1569. free(macaddr_str);
  1570. }
  1571. return res;
  1572. }
  1573. #endif // HAVE_LIBNOZZLE