totemknet.c 34 KB

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