totemknet.c 39 KB

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