totemknet.c 37 KB

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