totemknet.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * Copyright (c) 2016 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 <sched.h>
  51. #include <time.h>
  52. #include <sys/time.h>
  53. #include <sys/poll.h>
  54. #include <sys/uio.h>
  55. #include <limits.h>
  56. #include <qb/qbdefs.h>
  57. #include <qb/qbloop.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/logsys.h>
  61. #include <corosync/icmap.h>
  62. #include <corosync/totem/totemip.h>
  63. #include "totemknet.h"
  64. #include "util.h"
  65. #include <nss.h>
  66. #include <pk11pub.h>
  67. #include <pkcs11.h>
  68. #include <prerror.h>
  69. #include <libknet.h>
  70. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. /* Should match that used by cfg */
  74. #define CFG_INTERFACE_STATUS_MAX_LEN 512
  75. struct totemknet_instance {
  76. struct crypto_instance *crypto_inst;
  77. qb_loop_t *poll_handle;
  78. knet_handle_t knet_handle;
  79. int link_mode;
  80. void *context;
  81. void (*totemknet_deliver_fn) (
  82. void *context,
  83. const void *msg,
  84. unsigned int msg_len);
  85. void (*totemknet_iface_change_fn) (
  86. void *context,
  87. const struct totem_ip_address *iface_address,
  88. unsigned int link_no);
  89. void (*totemknet_mtu_changed) (
  90. void *context,
  91. int net_mtu);
  92. void (*totemknet_target_set_completed) (void *context);
  93. /*
  94. * Function and data used to log messages
  95. */
  96. int totemknet_log_level_security;
  97. int totemknet_log_level_error;
  98. int totemknet_log_level_warning;
  99. int totemknet_log_level_notice;
  100. int totemknet_log_level_debug;
  101. int totemknet_subsys_id;
  102. int knet_subsys_id;
  103. void (*totemknet_log_printf) (
  104. int level,
  105. int subsys,
  106. const char *function,
  107. const char *file,
  108. int line,
  109. const char *format,
  110. ...)__attribute__((format(printf, 6, 7)));
  111. void *knet_context;
  112. char iov_buffer[KNET_MAX_PACKET_SIZE];
  113. int stats_sent;
  114. int stats_recv;
  115. int stats_delv;
  116. int stats_remcasts;
  117. int stats_orf_token;
  118. struct timeval stats_tv_start;
  119. char *link_status[INTERFACE_MAX];
  120. int num_links;
  121. struct totem_ip_address my_ids[INTERFACE_MAX];
  122. uint16_t ip_port[INTERFACE_MAX];
  123. int our_nodeid;
  124. struct totem_config *totem_config;
  125. totemsrp_stats_t *stats;
  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. };
  134. struct work_item {
  135. const void *msg;
  136. unsigned int msg_len;
  137. struct totemknet_instance *instance;
  138. };
  139. int totemknet_member_list_rebind_ip (
  140. void *knet_context);
  141. static void totemknet_start_merge_detect_timeout(
  142. void *knet_context);
  143. static void totemknet_stop_merge_detect_timeout(
  144. void *knet_context);
  145. static void log_flush_messages (
  146. void *knet_context);
  147. static void totemknet_instance_initialize (struct totemknet_instance *instance)
  148. {
  149. memset (instance, 0, sizeof (struct totemknet_instance));
  150. }
  151. #define knet_log_printf(level, format, args...) \
  152. do { \
  153. instance->totemknet_log_printf ( \
  154. level, instance->totemknet_subsys_id, \
  155. __FUNCTION__, __FILE__, __LINE__, \
  156. (const char *)format, ##args); \
  157. } while (0);
  158. #define libknet_log_printf(level, format, args...) \
  159. do { \
  160. instance->totemknet_log_printf ( \
  161. level, instance->knet_subsys_id, \
  162. __FUNCTION__, "libknet.h", __LINE__, \
  163. (const char *)format, ##args); \
  164. } while (0);
  165. #define KNET_LOGSYS_PERROR(err_num, level, fmt, args...) \
  166. do { \
  167. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  168. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  169. instance->totemknet_log_printf ( \
  170. level, instance->totemknet_subsys_id, \
  171. __FUNCTION__, __FILE__, __LINE__, \
  172. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  173. } while(0)
  174. static int dst_host_filter_callback_fn(void *private_data,
  175. const unsigned char *outdata,
  176. ssize_t outdata_len,
  177. uint8_t tx_rx,
  178. knet_node_id_t this_host_id,
  179. knet_node_id_t src_host_id,
  180. int8_t *channel,
  181. knet_node_id_t *dst_host_ids,
  182. size_t *dst_host_ids_entries)
  183. {
  184. struct totem_message_header *header = (struct totem_message_header *)outdata;
  185. int res;
  186. *channel = 0;
  187. if (header->target_nodeid) {
  188. dst_host_ids[0] = header->target_nodeid;
  189. *dst_host_ids_entries = 1;
  190. res = 0; /* unicast message */
  191. }
  192. else {
  193. *dst_host_ids_entries = 0;
  194. res = 1; /* multicast message */
  195. }
  196. return res;
  197. }
  198. static void socket_error_callback_fn(void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)
  199. {
  200. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  201. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet socket ERROR notification called: txrx=%d, error=%d, errorno=%d", tx_rx, error, errorno);
  202. if ((error == -1 && errorno != EAGAIN) || (error == 0)) {
  203. knet_handle_remove_datafd(instance->knet_handle, datafd);
  204. }
  205. }
  206. static void host_change_callback_fn(void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)
  207. {
  208. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  209. // TODO: what? if anything.
  210. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet host change callback. nodeid: %d reachable: %d", host_id, reachable);
  211. }
  212. static void pmtu_change_callback_fn(void *private_data, unsigned int data_mtu)
  213. {
  214. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  215. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet pMTU change: %d", data_mtu);
  216. /* We don't need to tell corosync the actual knet MTU */
  217. // instance->totemknet_mtu_changed(instance->context, data_mtu);
  218. }
  219. int totemknet_crypto_set (
  220. void *knet_context,
  221. const char *cipher_type,
  222. const char *hash_type)
  223. {
  224. return (0);
  225. }
  226. static inline void ucast_sendmsg (
  227. struct totemknet_instance *instance,
  228. struct totem_ip_address *system_to,
  229. const void *msg,
  230. unsigned int msg_len)
  231. {
  232. int res = 0;
  233. struct totem_message_header *header = (struct totem_message_header *)msg;
  234. struct msghdr msg_ucast;
  235. struct iovec iovec;
  236. header->target_nodeid = system_to->nodeid;
  237. iovec.iov_base = (void *)msg;
  238. iovec.iov_len = msg_len;
  239. /*
  240. * Build unicast message
  241. */
  242. memset(&msg_ucast, 0, sizeof(msg_ucast));
  243. msg_ucast.msg_iov = (void *)&iovec;
  244. msg_ucast.msg_iovlen = 1;
  245. #ifdef HAVE_MSGHDR_CONTROL
  246. msg_ucast.msg_control = 0;
  247. #endif
  248. #ifdef HAVE_MSGHDR_CONTROLLEN
  249. msg_ucast.msg_controllen = 0;
  250. #endif
  251. #ifdef HAVE_MSGHDR_FLAGS
  252. msg_ucast.msg_flags = 0;
  253. #endif
  254. #ifdef HAVE_MSGHDR_ACCRIGHTS
  255. msg_ucast.msg_accrights = NULL;
  256. #endif
  257. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  258. msg_ucast.msg_accrightslen = 0;
  259. #endif
  260. /*
  261. * Transmit unicast message
  262. * An error here is recovered by totemsrp
  263. */
  264. /*
  265. * If sending to ourself then just pass it through knet back to
  266. * the receive fn. knet does not do local->local delivery
  267. */
  268. if (system_to->nodeid == instance->our_nodeid) {
  269. res = sendmsg (instance->knet_fd+1, &msg_ucast, MSG_NOSIGNAL);
  270. if (res < 0) {
  271. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  272. "sendmsg(ucast-local) failed (non-critical)");
  273. }
  274. }
  275. else {
  276. res = sendmsg (instance->knet_fd, &msg_ucast, MSG_NOSIGNAL);
  277. if (res < 0) {
  278. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  279. "sendmsg(ucast) failed (non-critical)");
  280. }
  281. }
  282. }
  283. static inline void mcast_sendmsg (
  284. struct totemknet_instance *instance,
  285. const void *msg,
  286. unsigned int msg_len,
  287. int only_active)
  288. {
  289. int res;
  290. struct totem_message_header *header = (struct totem_message_header *)msg;
  291. struct msghdr msg_mcast;
  292. struct iovec iovec;
  293. iovec.iov_base = (void *)msg;
  294. iovec.iov_len = msg_len;
  295. header->target_nodeid = 0;
  296. /*
  297. * Build multicast message
  298. */
  299. memset(&msg_mcast, 0, sizeof(msg_mcast));
  300. msg_mcast.msg_iov = (void *)&iovec;
  301. msg_mcast.msg_iovlen = 1;
  302. #ifdef HAVE_MSGHDR_CONTROL
  303. msg_mcast.msg_control = 0;
  304. #endif
  305. #ifdef HAVE_MSGHDR_CONTROLLEN
  306. msg_mcast.msg_controllen = 0;
  307. #endif
  308. #ifdef HAVE_MSGHDR_FLAGS
  309. msg_mcast.msg_flags = 0;
  310. #endif
  311. #ifdef HAVE_MSGHDR_ACCRIGHTS
  312. msg_mcast.msg_accrights = NULL;
  313. #endif
  314. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  315. msg_mcast.msg_accrightslen = 0;
  316. #endif
  317. // log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_sendmsg. only_active=%d, len=%d", only_active, msg_len);
  318. res = sendmsg (instance->knet_fd, &msg_mcast, MSG_NOSIGNAL);
  319. if (res < msg_len) {
  320. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send sendmsg returned %d", res);
  321. }
  322. /*
  323. * Also send it to ourself, directly into
  324. * the receive fn. knet does not to local->local delivery
  325. */
  326. res = sendmsg (instance->knet_fd+1, &msg_mcast, MSG_NOSIGNAL);
  327. if (res < msg_len) {
  328. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send sendmsg (local) returned %d", res);
  329. }
  330. if (!only_active || instance->send_merge_detect_message) {
  331. /*
  332. * Current message was sent to all nodes
  333. */
  334. instance->merge_detect_messages_sent_before_timeout++;
  335. instance->send_merge_detect_message = 0;
  336. }
  337. }
  338. static int node_compare(const void *aptr, const void *bptr)
  339. {
  340. uint16_t a,b;
  341. a = *(uint16_t *)aptr;
  342. b = *(uint16_t *)bptr;
  343. return a > b;
  344. }
  345. int totemknet_ifaces_get (void *knet_context,
  346. char ***status,
  347. unsigned int *iface_count)
  348. {
  349. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  350. struct knet_link_status link_status;
  351. knet_node_id_t host_list[KNET_MAX_HOST];
  352. size_t num_hosts;
  353. int i,j;
  354. char *ptr;
  355. int res = 0;
  356. /*
  357. * Don't do the whole 'link_info' bit if the caller just wants
  358. * a count of interfaces.
  359. */
  360. if (status) {
  361. res = knet_host_get_host_list(instance->knet_handle,
  362. host_list, &num_hosts);
  363. if (res) {
  364. return (-1);
  365. }
  366. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  367. /* num_links is actually the highest link ID */
  368. for (i=0; i <= instance->num_links; i++) {
  369. ptr = instance->link_status[i];
  370. for (j=0; j<num_hosts; j++) {
  371. res = knet_link_get_status(instance->knet_handle,
  372. host_list[j],
  373. i,
  374. &link_status,
  375. sizeof(link_status));
  376. if (res == 0) {
  377. ptr[j] = '0' + (link_status.enabled |
  378. link_status.connected<<1 |
  379. link_status.dynconnected<<2);
  380. }
  381. else {
  382. ptr[j] += '?';
  383. }
  384. }
  385. ptr[num_hosts] = '\0';
  386. }
  387. *status = instance->link_status;
  388. }
  389. *iface_count = instance->num_links+1;
  390. return (res);
  391. }
  392. int totemknet_finalize (
  393. void *knet_context)
  394. {
  395. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  396. int res = 0;
  397. int i,j;
  398. static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */
  399. uint8_t links[KNET_MAX_LINK];
  400. size_t num_nodes;
  401. size_t num_links;
  402. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  403. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  404. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  405. res = knet_host_get_host_list(instance->knet_handle, nodes, &num_nodes);
  406. if (res) {
  407. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet node list for shutdown: %s", strerror(errno));
  408. /* Crash out anyway */
  409. goto finalise_error;
  410. }
  411. /* Tidily shut down all nodes & links. This ensures that the LEAVE message will be sent */
  412. for (i=0; i<num_nodes; i++) {
  413. /* Don't try to shut local link down, there isn't one */
  414. if (nodes[i] == instance->our_nodeid) {
  415. continue;
  416. }
  417. res = knet_link_get_link_list(instance->knet_handle, nodes[i], links, &num_links);
  418. if (res) {
  419. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet link list for node %d: %s", nodes[i], strerror(errno));
  420. goto finalise_error;
  421. }
  422. for (j=0; j<num_links; j++) {
  423. res = knet_link_set_enable(instance->knet_handle, nodes[i], links[j], 0);
  424. if (res) {
  425. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_set_enable(node %d, link %d) failed: %s", nodes[i], links[j], strerror(errno));
  426. }
  427. res = knet_link_clear_config(instance->knet_handle, nodes[i], links[j]);
  428. if (res) {
  429. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_clear_config(node %d, link %d) failed: %s", nodes[i], links[j], strerror(errno));
  430. }
  431. }
  432. res = knet_host_remove(instance->knet_handle, nodes[i]);
  433. if (res) {
  434. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_host_remove(node %d) failed: %s", nodes[i], strerror(errno));
  435. }
  436. }
  437. finalise_error:
  438. res = knet_handle_setfwd(instance->knet_handle, 0);
  439. if (res) {
  440. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_setfwd failed: %s", strerror(errno));
  441. }
  442. res = knet_handle_free(instance->knet_handle);
  443. if (res) {
  444. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_free failed: %s", strerror(errno));
  445. }
  446. totemknet_stop_merge_detect_timeout(instance);
  447. log_flush_messages(instance);
  448. return (res);
  449. }
  450. static int log_deliver_fn (
  451. int fd,
  452. int revents,
  453. void *data)
  454. {
  455. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  456. char buffer[KNET_MAX_LOG_MSG_SIZE*4];
  457. char *bufptr = buffer;
  458. int done = 0;
  459. int len;
  460. len = read(fd, buffer, sizeof(buffer));
  461. while (done < len) {
  462. struct knet_log_msg *msg = (struct knet_log_msg *)bufptr;
  463. switch (msg->msglevel) {
  464. case KNET_LOG_ERR:
  465. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s: %s",
  466. knet_log_get_subsystem_name(msg->subsystem),
  467. msg->msg);
  468. break;
  469. case KNET_LOG_WARN:
  470. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s: %s",
  471. knet_log_get_subsystem_name(msg->subsystem),
  472. msg->msg);
  473. break;
  474. case KNET_LOG_INFO:
  475. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s: %s",
  476. knet_log_get_subsystem_name(msg->subsystem),
  477. msg->msg);
  478. break;
  479. case KNET_LOG_DEBUG:
  480. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s: %s",
  481. knet_log_get_subsystem_name(msg->subsystem),
  482. msg->msg);
  483. break;
  484. }
  485. bufptr += KNET_MAX_LOG_MSG_SIZE;
  486. done += KNET_MAX_LOG_MSG_SIZE;
  487. }
  488. return 0;
  489. }
  490. static int data_deliver_fn (
  491. int fd,
  492. int revents,
  493. void *data)
  494. {
  495. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  496. struct msghdr msg_hdr;
  497. struct iovec iov_recv;
  498. struct sockaddr_storage system_from;
  499. ssize_t msg_len;
  500. iov_recv.iov_base = instance->iov_buffer;
  501. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  502. msg_hdr.msg_name = &system_from;
  503. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  504. msg_hdr.msg_iov = &iov_recv;
  505. msg_hdr.msg_iovlen = 1;
  506. #ifdef HAVE_MSGHDR_CONTROL
  507. msg_hdr.msg_control = 0;
  508. #endif
  509. #ifdef HAVE_MSGHDR_CONTROLLEN
  510. msg_hdr.msg_controllen = 0;
  511. #endif
  512. #ifdef HAVE_MSGHDR_FLAGS
  513. msg_hdr.msg_flags = 0;
  514. #endif
  515. #ifdef HAVE_MSGHDR_ACCRIGHTS
  516. msg_hdr.msg_accrights = NULL;
  517. #endif
  518. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  519. msg_hdr.msg_accrightslen = 0;
  520. #endif
  521. msg_len = recvmsg (fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  522. if (msg_len <= 0) {
  523. return (0);
  524. }
  525. instance->stats_recv += msg_len;
  526. /*
  527. * Handle incoming message
  528. */
  529. instance->totemknet_deliver_fn (
  530. instance->context,
  531. instance->iov_buffer,
  532. msg_len);
  533. return (0);
  534. }
  535. static void timer_function_netif_check_timeout (
  536. void *data)
  537. {
  538. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  539. int i;
  540. for (i=0; i<instance->totem_config->interface_count; i++)
  541. instance->totemknet_iface_change_fn (instance->context,
  542. &instance->my_ids[i],
  543. i);
  544. }
  545. static void totemknet_refresh_config(
  546. int32_t event,
  547. const char *key_name,
  548. struct icmap_notify_value new_val,
  549. struct icmap_notify_value old_val,
  550. void *user_data)
  551. {
  552. uint8_t reloading;
  553. uint32_t value;
  554. uint32_t link_no;
  555. size_t num_nodes;
  556. knet_node_id_t host_ids[KNET_MAX_HOST];
  557. int i;
  558. int err;
  559. char path[ICMAP_KEYNAME_MAXLEN];
  560. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  561. ENTER();
  562. /*
  563. * If a full reload is in progress then don't do anything until it's done and
  564. * can reconfigure it all atomically
  565. */
  566. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  567. return;
  568. }
  569. if (icmap_get_uint32("totem.knet_pmtud_interval", &value) == CS_OK) {
  570. instance->totem_config->knet_pmtud_interval = value;
  571. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %d", value);
  572. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  573. if (err) {
  574. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  575. }
  576. }
  577. /* Get link parameters */
  578. for (i = 0; i <= instance->num_links; i++) {
  579. sprintf(path, "totem.interface.%d.knet_link_priority", i);
  580. if (icmap_get_uint32(path, &value) == CS_OK) {
  581. instance->totem_config->interfaces[i].knet_link_priority = value;
  582. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_link_priority on link %d now %d", i, value);
  583. }
  584. sprintf(path, "totem.interface.%d.knet_ping_interval", i);
  585. if (icmap_get_uint32(path, &value) == CS_OK) {
  586. instance->totem_config->interfaces[i].knet_ping_interval = value;
  587. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_ping_interval on link %d now %d", i, value);
  588. }
  589. sprintf(path, "totem.interface.%d.knet_ping_timeout", i);
  590. if (icmap_get_uint32(path, &value) == CS_OK) {
  591. instance->totem_config->interfaces[i].knet_ping_timeout = value;
  592. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_ping_timeout on link %d now %d", i, value);
  593. }
  594. sprintf(path, "totem.interface.%d.knet_ping_precision", i);
  595. if (icmap_get_uint32(path, &value) == CS_OK) {
  596. instance->totem_config->interfaces[i].knet_ping_precision = value;
  597. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_ping_precision on link %d now %d", i, value);
  598. }
  599. sprintf(path, "totem.interface.%d.knet_pong_count", i);
  600. if (icmap_get_uint32(path, &value) == CS_OK) {
  601. instance->totem_config->interfaces[i].knet_pong_count = value;
  602. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pong_count on link %d now %d", i, value);
  603. }
  604. }
  605. /* Configure link parameters for each node */
  606. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  607. if (err != 0) {
  608. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  609. }
  610. for (i=0; i<num_nodes; i++) {
  611. for (link_no = 0; link_no < instance->num_links; link_no++) {
  612. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  613. instance->totem_config->interfaces[link_no].knet_ping_interval,
  614. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  615. instance->totem_config->interfaces[link_no].knet_ping_precision);
  616. if (err) {
  617. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for node %d link %d failed", host_ids[i], link_no);
  618. }
  619. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  620. instance->totem_config->interfaces[link_no].knet_pong_count);
  621. if (err) {
  622. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for node %d link %d failed",host_ids[i], link_no);
  623. }
  624. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  625. instance->totem_config->interfaces[link_no].knet_link_priority);
  626. if (err) {
  627. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for node %d link %d failed", host_ids[i], link_no);
  628. }
  629. }
  630. }
  631. LEAVE();
  632. }
  633. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  634. {
  635. icmap_track_t icmap_track_totem = NULL;
  636. icmap_track_t icmap_track_reload = NULL;
  637. ENTER();
  638. icmap_track_add("totem.",
  639. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  640. totemknet_refresh_config,
  641. instance,
  642. &icmap_track_totem);
  643. icmap_track_add("config.totemconfig_reload_in_progress",
  644. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  645. totemknet_refresh_config,
  646. instance,
  647. &icmap_track_reload);
  648. LEAVE();
  649. }
  650. /*
  651. * Create an instance
  652. */
  653. int totemknet_initialize (
  654. qb_loop_t *poll_handle,
  655. void **knet_context,
  656. struct totem_config *totem_config,
  657. totemsrp_stats_t *stats,
  658. void *context,
  659. void (*deliver_fn) (
  660. void *context,
  661. const void *msg,
  662. unsigned int msg_len),
  663. void (*iface_change_fn) (
  664. void *context,
  665. const struct totem_ip_address *iface_address,
  666. unsigned int link_no),
  667. void (*mtu_changed) (
  668. void *context,
  669. int net_mtu),
  670. void (*target_set_completed) (
  671. void *context))
  672. {
  673. struct totemknet_instance *instance;
  674. int8_t channel=0;
  675. int res;
  676. int i;
  677. instance = malloc (sizeof (struct totemknet_instance));
  678. if (instance == NULL) {
  679. return (-1);
  680. }
  681. totemknet_instance_initialize (instance);
  682. instance->totem_config = totem_config;
  683. instance->stats = stats;
  684. /*
  685. * Configure logging
  686. */
  687. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  688. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  689. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  690. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  691. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  692. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  693. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  694. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  695. /*
  696. * Initialize local variables for totemknet
  697. */
  698. instance->our_nodeid = instance->totem_config->node_id;
  699. for (i=0; i< instance->totem_config->interface_count; i++) {
  700. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  701. instance->my_ids[i].nodeid = instance->our_nodeid;
  702. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  703. /* Needed for totemsrp */
  704. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  705. }
  706. instance->poll_handle = poll_handle;
  707. instance->context = context;
  708. instance->totemknet_deliver_fn = deliver_fn;
  709. instance->totemknet_iface_change_fn = iface_change_fn;
  710. instance->totemknet_mtu_changed = mtu_changed;
  711. instance->totemknet_target_set_completed = target_set_completed;
  712. res = pipe(instance->logpipes);
  713. if (res == -1) {
  714. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  715. goto exit_error;
  716. }
  717. fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK);
  718. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK);
  719. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  720. if (!instance->knet_handle) {
  721. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  722. goto exit_error;
  723. }
  724. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  725. if (res) {
  726. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  727. }
  728. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  729. if (res) {
  730. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  731. }
  732. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  733. if (res) {
  734. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  735. }
  736. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  737. if (res) {
  738. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  739. }
  740. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  741. if (res) {
  742. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  743. }
  744. /* Get an fd into knet */
  745. instance->knet_fd = 0;
  746. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  747. if (res) {
  748. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  749. goto exit_error;
  750. }
  751. /* Enable crypto if requested */
  752. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  753. struct knet_handle_crypto_cfg crypto_cfg;
  754. strcpy(crypto_cfg.crypto_model, "nss");
  755. strcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type);
  756. strcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type);
  757. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  758. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  759. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  760. if (res == -1) {
  761. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  762. goto exit_error;
  763. }
  764. if (res == -2) {
  765. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  766. goto exit_error;
  767. }
  768. knet_log_printf(LOG_INFO, "kronosnet crypto initialized: %s/%s", crypto_cfg.crypto_cipher_type, crypto_cfg.crypto_hash_type);
  769. }
  770. knet_handle_setfwd(instance->knet_handle, 1);
  771. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  772. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  773. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  774. }
  775. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  776. instance->link_mode = KNET_LINK_POLICY_RR;
  777. }
  778. for (i=0; i<INTERFACE_MAX; i++) {
  779. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  780. if (!instance->link_status[i]) {
  781. goto exit_error;
  782. }
  783. }
  784. qb_loop_poll_add (instance->poll_handle,
  785. QB_LOOP_MED,
  786. instance->logpipes[0],
  787. POLLIN, instance, log_deliver_fn);
  788. qb_loop_poll_add (instance->poll_handle,
  789. QB_LOOP_HIGH,
  790. instance->knet_fd,
  791. POLLIN, instance, data_deliver_fn);
  792. /*
  793. * Upper layer isn't ready to receive message because it hasn't
  794. * initialized yet. Add short timer to check the interfaces.
  795. */
  796. qb_loop_timer_add (instance->poll_handle,
  797. QB_LOOP_MED,
  798. 100*QB_TIME_NS_IN_MSEC,
  799. (void *)instance,
  800. timer_function_netif_check_timeout,
  801. &instance->timer_netif_check_timeout);
  802. totemknet_start_merge_detect_timeout(instance);
  803. /* Start listening for config changes */
  804. totemknet_add_config_notifications(instance);
  805. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  806. *knet_context = instance;
  807. return (0);
  808. exit_error:
  809. log_flush_messages(instance);
  810. return (-1);
  811. }
  812. void *totemknet_buffer_alloc (void)
  813. {
  814. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  815. return malloc(KNET_MAX_PACKET_SIZE + 512);
  816. }
  817. void totemknet_buffer_release (void *ptr)
  818. {
  819. return free (ptr);
  820. }
  821. int totemknet_processor_count_set (
  822. void *knet_context,
  823. int processor_count)
  824. {
  825. return (0);
  826. }
  827. int totemknet_recv_flush (void *knet_context)
  828. {
  829. return (0);
  830. }
  831. int totemknet_send_flush (void *knet_context)
  832. {
  833. return (0);
  834. }
  835. int totemknet_token_send (
  836. void *knet_context,
  837. const void *msg,
  838. unsigned int msg_len)
  839. {
  840. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  841. int res = 0;
  842. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  843. return (res);
  844. }
  845. int totemknet_mcast_flush_send (
  846. void *knet_context,
  847. const void *msg,
  848. unsigned int msg_len)
  849. {
  850. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  851. int res = 0;
  852. mcast_sendmsg (instance, msg, msg_len, 0);
  853. return (res);
  854. }
  855. int totemknet_mcast_noflush_send (
  856. void *knet_context,
  857. const void *msg,
  858. unsigned int msg_len)
  859. {
  860. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  861. int res = 0;
  862. mcast_sendmsg (instance, msg, msg_len, 1);
  863. return (res);
  864. }
  865. extern int totemknet_iface_check (void *knet_context)
  866. {
  867. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  868. int res = 0;
  869. knet_log_printf(LOG_DEBUG, "totmeknet: iface_check");
  870. return (res);
  871. }
  872. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  873. {
  874. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  875. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  876. }
  877. int totemknet_token_target_set (
  878. void *knet_context,
  879. const struct totem_ip_address *token_target)
  880. {
  881. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  882. int res = 0;
  883. memcpy (&instance->token_target, token_target,
  884. sizeof (struct totem_ip_address));
  885. instance->totemknet_target_set_completed (instance->context);
  886. return (res);
  887. }
  888. extern int totemknet_recv_mcast_empty (
  889. void *knet_context)
  890. {
  891. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  892. unsigned int res;
  893. struct sockaddr_storage system_from;
  894. struct msghdr msg_hdr;
  895. struct iovec iov_recv;
  896. struct pollfd ufd;
  897. int nfds;
  898. int msg_processed = 0;
  899. iov_recv.iov_base = instance->iov_buffer;
  900. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  901. msg_hdr.msg_name = &system_from;
  902. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  903. msg_hdr.msg_iov = &iov_recv;
  904. msg_hdr.msg_iovlen = 1;
  905. #ifdef HAVE_MSGHDR_CONTROL
  906. msg_hdr.msg_control = 0;
  907. #endif
  908. #ifdef HAVE_MSGHDR_CONTROLLEN
  909. msg_hdr.msg_controllen = 0;
  910. #endif
  911. #ifdef HAVE_MSGHDR_FLAGS
  912. msg_hdr.msg_flags = 0;
  913. #endif
  914. #ifdef HAVE_MSGHDR_ACCRIGHTS
  915. msg_msg_hdr.msg_accrights = NULL;
  916. #endif
  917. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  918. msg_msg_hdr.msg_accrightslen = 0;
  919. #endif
  920. do {
  921. ufd.fd = instance->knet_fd;
  922. ufd.events = POLLIN;
  923. nfds = poll (&ufd, 1, 0);
  924. if (nfds == 1 && ufd.revents & POLLIN) {
  925. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  926. if (res != -1) {
  927. msg_processed = 1;
  928. } else {
  929. msg_processed = -1;
  930. }
  931. }
  932. } while (nfds == 1);
  933. return (msg_processed);
  934. }
  935. int totemknet_member_add (
  936. void *knet_context,
  937. const struct totem_ip_address *local,
  938. const struct totem_ip_address *member,
  939. int link_no)
  940. {
  941. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  942. int err;
  943. int port = instance->ip_port[link_no];
  944. struct sockaddr_storage remote_ss;
  945. struct sockaddr_storage local_ss;
  946. int addrlen;
  947. if (member->nodeid == instance->our_nodeid) {
  948. return 0; /* Don't add ourself, we send loopback messages directly */
  949. }
  950. /* Keep track of the number of links */
  951. if (link_no > instance->num_links) {
  952. instance->num_links = link_no;
  953. }
  954. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: %d (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  955. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: %d (%s)", local->nodeid, totemip_print(local));
  956. if (link_no == 0) {
  957. if (knet_host_add(instance->knet_handle, member->nodeid)) {
  958. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  959. return -1;
  960. }
  961. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  962. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  963. return -1;
  964. }
  965. }
  966. /* Casts to remove const */
  967. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port+link_no, &remote_ss, &addrlen);
  968. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port+link_no, &local_ss, &addrlen);
  969. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  970. instance->totem_config->interfaces[link_no].knet_transport,
  971. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  972. if (err) {
  973. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  974. return -1;
  975. }
  976. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  977. instance->totem_config->interfaces[link_no].knet_link_priority);
  978. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  979. instance->totem_config->interfaces[link_no].knet_link_priority);
  980. if (err) {
  981. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid %d, link %d failed", member->nodeid, link_no);
  982. }
  983. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  984. instance->totem_config->interfaces[link_no].knet_ping_interval,
  985. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  986. instance->totem_config->interfaces[link_no].knet_ping_precision);
  987. if (err) {
  988. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for nodeid %d, link %d failed", member->nodeid, link_no);
  989. }
  990. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  991. instance->totem_config->interfaces[link_no].knet_pong_count);
  992. if (err) {
  993. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for nodeid %d, link %d failed", member->nodeid, link_no);
  994. }
  995. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  996. if (err) {
  997. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid %d, link %d failed", member->nodeid, link_no);
  998. return -1;
  999. }
  1000. return (0);
  1001. }
  1002. int totemknet_member_remove (
  1003. void *knet_context,
  1004. const struct totem_ip_address *token_target,
  1005. int link_no)
  1006. {
  1007. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1008. int res;
  1009. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: %d, link=%d", token_target->nodeid, link_no);
  1010. if (token_target->nodeid == instance->our_nodeid) {
  1011. return 0; /* Don't remove ourself */
  1012. }
  1013. /* Remove the link first */
  1014. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1015. if (res != 0) {
  1016. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set enable(off) for nodeid %d, link %d failed", token_target->nodeid, link_no);
  1017. return res;
  1018. }
  1019. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1020. if (res != 0) {
  1021. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_clear_config for nodeid %d, link %d failed", token_target->nodeid, link_no);
  1022. return res;
  1023. }
  1024. return knet_host_remove(instance->knet_handle, token_target->nodeid);
  1025. }
  1026. int totemknet_member_list_rebind_ip (
  1027. void *knet_context)
  1028. {
  1029. return (0);
  1030. }
  1031. static void timer_function_merge_detect_timeout (
  1032. void *data)
  1033. {
  1034. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1035. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1036. instance->send_merge_detect_message = 1;
  1037. }
  1038. instance->merge_detect_messages_sent_before_timeout = 0;
  1039. totemknet_start_merge_detect_timeout(instance);
  1040. }
  1041. static void totemknet_start_merge_detect_timeout(
  1042. void *knet_context)
  1043. {
  1044. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1045. qb_loop_timer_add(instance->poll_handle,
  1046. QB_LOOP_MED,
  1047. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1048. (void *)instance,
  1049. timer_function_merge_detect_timeout,
  1050. &instance->timer_merge_detect_timeout);
  1051. }
  1052. static void totemknet_stop_merge_detect_timeout(
  1053. void *knet_context)
  1054. {
  1055. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1056. qb_loop_timer_del(instance->poll_handle,
  1057. instance->timer_merge_detect_timeout);
  1058. }
  1059. static void log_flush_messages (void *knet_context)
  1060. {
  1061. struct pollfd pfd;
  1062. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1063. pfd.fd = instance->logpipes[0];
  1064. pfd.events = POLLIN;
  1065. pfd.revents = 0;
  1066. while (poll(&pfd, 1, 0) > 0) {
  1067. if (pfd.revents & POLLIN) {
  1068. (void)log_deliver_fn(instance->logpipes[0], POLLIN, instance);
  1069. }
  1070. }
  1071. }