totemknet.c 38 KB

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