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