totemknet.c 40 KB

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