totemknet.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. * Copyright (c) 2016-2017 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <sys/mman.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/socket.h>
  39. #include <netdb.h>
  40. #include <sys/un.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/param.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <errno.h>
  50. #include <sched.h>
  51. #include <time.h>
  52. #include <sys/time.h>
  53. #include <sys/poll.h>
  54. #include <sys/uio.h>
  55. #include <limits.h>
  56. #include <qb/qbdefs.h>
  57. #include <qb/qbloop.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/logsys.h>
  61. #include <corosync/icmap.h>
  62. #include <corosync/totem/totemip.h>
  63. #include "totemknet.h"
  64. #include "util.h"
  65. #include <libknet.h>
  66. #include <corosync/totem/totemstats.h>
  67. #ifndef MSG_NOSIGNAL
  68. #define MSG_NOSIGNAL 0
  69. #endif
  70. /* Should match that used by cfg */
  71. #define CFG_INTERFACE_STATUS_MAX_LEN 512
  72. struct totemknet_instance {
  73. struct crypto_instance *crypto_inst;
  74. qb_loop_t *poll_handle;
  75. knet_handle_t knet_handle;
  76. int link_mode;
  77. void *context;
  78. void (*totemknet_deliver_fn) (
  79. void *context,
  80. const void *msg,
  81. unsigned int msg_len);
  82. void (*totemknet_iface_change_fn) (
  83. void *context,
  84. const struct totem_ip_address *iface_address,
  85. unsigned int link_no);
  86. void (*totemknet_mtu_changed) (
  87. void *context,
  88. int net_mtu);
  89. void (*totemknet_target_set_completed) (void *context);
  90. /*
  91. * Function and data used to log messages
  92. */
  93. int totemknet_log_level_security;
  94. int totemknet_log_level_error;
  95. int totemknet_log_level_warning;
  96. int totemknet_log_level_notice;
  97. int totemknet_log_level_debug;
  98. int totemknet_subsys_id;
  99. int knet_subsys_id;
  100. void (*totemknet_log_printf) (
  101. int level,
  102. int subsys,
  103. const char *function,
  104. const char *file,
  105. int line,
  106. const char *format,
  107. ...)__attribute__((format(printf, 6, 7)));
  108. void *knet_context;
  109. char iov_buffer[KNET_MAX_PACKET_SIZE];
  110. char *link_status[INTERFACE_MAX];
  111. struct totem_ip_address my_ids[INTERFACE_MAX];
  112. uint16_t ip_port[INTERFACE_MAX];
  113. int our_nodeid;
  114. 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. int i,j;
  327. char *ptr;
  328. int res = 0;
  329. /*
  330. * Don't do the whole 'link_info' bit if the caller just wants
  331. * a count of interfaces.
  332. */
  333. if (status) {
  334. res = knet_host_get_host_list(instance->knet_handle,
  335. host_list, &num_hosts);
  336. if (res) {
  337. return (-1);
  338. }
  339. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  340. for (i=0; i<INTERFACE_MAX; i++) {
  341. memset(instance->link_status[i], 'n', CFG_INTERFACE_STATUS_MAX_LEN-1);
  342. instance->link_status[i][num_hosts] = '\0';
  343. }
  344. /* This is all a bit "inside-out" because "status" is a set of strings per link
  345. * and knet orders things by host
  346. */
  347. for (j=0; j<num_hosts; j++) {
  348. res = knet_link_get_link_list(instance->knet_handle,
  349. host_list[j], link_list, &num_links);
  350. if (res) {
  351. return (-1);
  352. }
  353. for (i=0; i < num_links; i++) {
  354. ptr = instance->link_status[link_list[i]];
  355. res = knet_link_get_status(instance->knet_handle,
  356. host_list[j],
  357. link_list[i],
  358. &link_status,
  359. sizeof(link_status));
  360. if (res == 0) {
  361. ptr[j] = '0' + (link_status.enabled |
  362. link_status.connected<<1 |
  363. link_status.dynconnected<<2);
  364. }
  365. else {
  366. ptr[j] = '?';
  367. }
  368. }
  369. }
  370. *status = instance->link_status;
  371. }
  372. *iface_count = INTERFACE_MAX;
  373. return (res);
  374. }
  375. int totemknet_finalize (
  376. void *knet_context)
  377. {
  378. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  379. int res = 0;
  380. int i,j;
  381. static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */
  382. uint8_t links[KNET_MAX_LINK];
  383. size_t num_nodes;
  384. size_t num_links;
  385. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  386. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  387. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  388. res = knet_host_get_host_list(instance->knet_handle, nodes, &num_nodes);
  389. if (res) {
  390. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet node list for shutdown: %s", strerror(errno));
  391. /* Crash out anyway */
  392. goto finalise_error;
  393. }
  394. /* Tidily shut down all nodes & links. This ensures that the LEAVE message will be sent */
  395. for (i=0; i<num_nodes; i++) {
  396. res = knet_link_get_link_list(instance->knet_handle, nodes[i], links, &num_links);
  397. if (res) {
  398. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet link list for node %d: %s", nodes[i], strerror(errno));
  399. goto finalise_error;
  400. }
  401. for (j=0; j<num_links; j++) {
  402. res = knet_link_set_enable(instance->knet_handle, nodes[i], links[j], 0);
  403. if (res) {
  404. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_set_enable(node %d, link %d) failed: %s", nodes[i], links[j], strerror(errno));
  405. }
  406. res = knet_link_clear_config(instance->knet_handle, nodes[i], links[j]);
  407. if (res) {
  408. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_clear_config(node %d, link %d) failed: %s", nodes[i], links[j], strerror(errno));
  409. }
  410. }
  411. res = knet_host_remove(instance->knet_handle, nodes[i]);
  412. if (res) {
  413. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_host_remove(node %d) failed: %s", nodes[i], strerror(errno));
  414. }
  415. }
  416. finalise_error:
  417. res = knet_handle_setfwd(instance->knet_handle, 0);
  418. if (res) {
  419. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_setfwd failed: %s", strerror(errno));
  420. }
  421. res = knet_handle_free(instance->knet_handle);
  422. if (res) {
  423. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_free failed: %s", strerror(errno));
  424. }
  425. totemknet_stop_merge_detect_timeout(instance);
  426. log_flush_messages(instance);
  427. return (res);
  428. }
  429. static int log_deliver_fn (
  430. int fd,
  431. int revents,
  432. void *data)
  433. {
  434. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  435. char buffer[KNET_MAX_LOG_MSG_SIZE*4];
  436. char *bufptr = buffer;
  437. int done = 0;
  438. int len;
  439. len = read(fd, buffer, sizeof(buffer));
  440. while (done < len) {
  441. struct knet_log_msg *msg = (struct knet_log_msg *)bufptr;
  442. switch (msg->msglevel) {
  443. case KNET_LOG_ERR:
  444. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s: %s",
  445. knet_log_get_subsystem_name(msg->subsystem),
  446. msg->msg);
  447. break;
  448. case KNET_LOG_WARN:
  449. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s: %s",
  450. knet_log_get_subsystem_name(msg->subsystem),
  451. msg->msg);
  452. break;
  453. case KNET_LOG_INFO:
  454. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s: %s",
  455. knet_log_get_subsystem_name(msg->subsystem),
  456. msg->msg);
  457. break;
  458. case KNET_LOG_DEBUG:
  459. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s: %s",
  460. knet_log_get_subsystem_name(msg->subsystem),
  461. msg->msg);
  462. break;
  463. }
  464. bufptr += KNET_MAX_LOG_MSG_SIZE;
  465. done += KNET_MAX_LOG_MSG_SIZE;
  466. }
  467. return 0;
  468. }
  469. static int data_deliver_fn (
  470. int fd,
  471. int revents,
  472. void *data)
  473. {
  474. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  475. struct msghdr msg_hdr;
  476. struct iovec iov_recv;
  477. struct sockaddr_storage system_from;
  478. ssize_t msg_len;
  479. int truncated_packet;
  480. iov_recv.iov_base = instance->iov_buffer;
  481. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  482. msg_hdr.msg_name = &system_from;
  483. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  484. msg_hdr.msg_iov = &iov_recv;
  485. msg_hdr.msg_iovlen = 1;
  486. #ifdef HAVE_MSGHDR_CONTROL
  487. msg_hdr.msg_control = 0;
  488. #endif
  489. #ifdef HAVE_MSGHDR_CONTROLLEN
  490. msg_hdr.msg_controllen = 0;
  491. #endif
  492. #ifdef HAVE_MSGHDR_FLAGS
  493. msg_hdr.msg_flags = 0;
  494. #endif
  495. #ifdef HAVE_MSGHDR_ACCRIGHTS
  496. msg_hdr.msg_accrights = NULL;
  497. #endif
  498. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  499. msg_hdr.msg_accrightslen = 0;
  500. #endif
  501. msg_len = recvmsg (fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  502. if (msg_len <= 0) {
  503. return (0);
  504. }
  505. truncated_packet = 0;
  506. #ifdef HAVE_MSGHDR_FLAGS
  507. if (msg_hdr.msg_flags & MSG_TRUNC) {
  508. truncated_packet = 1;
  509. }
  510. #else
  511. /*
  512. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  513. * if bytes_received == KNET_MAX_PACKET_SIZE then packet is truncated
  514. */
  515. if (bytes_received == KNET_MAX_PACKET_SIZE) {
  516. truncated_packet = 1;
  517. }
  518. #endif
  519. if (truncated_packet) {
  520. knet_log_printf(instance->totemknet_log_level_error,
  521. "Received too big message. This may be because something bad is happening"
  522. "on the network (attack?), or you tried join more nodes than corosync is"
  523. "compiled with (%u) or bug in the code (bad estimation of "
  524. "the KNET_MAX_PACKET_SIZE). Dropping packet.", PROCESSOR_COUNT_MAX);
  525. return (0);
  526. }
  527. /*
  528. * Handle incoming message
  529. */
  530. instance->totemknet_deliver_fn (
  531. instance->context,
  532. instance->iov_buffer,
  533. msg_len);
  534. return (0);
  535. }
  536. static void timer_function_netif_check_timeout (
  537. void *data)
  538. {
  539. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  540. int i;
  541. for (i=0; i < INTERFACE_MAX; i++) {
  542. if (!instance->totem_config->interfaces[i].configured) {
  543. continue;
  544. }
  545. instance->totemknet_iface_change_fn (instance->context,
  546. &instance->my_ids[i],
  547. i);
  548. }
  549. }
  550. /* NOTE: this relies on the fact that totem_reload_notify() is called first */
  551. static void totemknet_refresh_config(
  552. int32_t event,
  553. const char *key_name,
  554. struct icmap_notify_value new_val,
  555. struct icmap_notify_value old_val,
  556. void *user_data)
  557. {
  558. uint8_t reloading;
  559. uint32_t value;
  560. uint32_t link_no;
  561. size_t num_nodes;
  562. knet_node_id_t host_ids[KNET_MAX_HOST];
  563. int i;
  564. int err;
  565. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  566. ENTER();
  567. /*
  568. * If a full reload is in progress then don't do anything until it's done and
  569. * can reconfigure it all atomically
  570. */
  571. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  572. return;
  573. }
  574. if (icmap_get_uint32("totem.knet_pmtud_interval", &value) == CS_OK) {
  575. instance->totem_config->knet_pmtud_interval = value;
  576. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %d", value);
  577. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  578. if (err) {
  579. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  580. }
  581. }
  582. /* Configure link parameters for each node */
  583. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  584. if (err != 0) {
  585. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  586. }
  587. for (i=0; i<num_nodes; i++) {
  588. for (link_no = 0; link_no < INTERFACE_MAX; link_no++) {
  589. if (host_ids[i] == instance->our_nodeid || !instance->totem_config->interfaces[link_no].configured) {
  590. continue;
  591. }
  592. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  593. instance->totem_config->interfaces[link_no].knet_ping_interval,
  594. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  595. instance->totem_config->interfaces[link_no].knet_ping_precision);
  596. if (err) {
  597. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for node %d link %d failed", host_ids[i], link_no);
  598. }
  599. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  600. instance->totem_config->interfaces[link_no].knet_pong_count);
  601. if (err) {
  602. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for node %d link %d failed",host_ids[i], link_no);
  603. }
  604. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  605. instance->totem_config->interfaces[link_no].knet_link_priority);
  606. if (err) {
  607. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for node %d link %d failed", host_ids[i], link_no);
  608. }
  609. }
  610. }
  611. LEAVE();
  612. }
  613. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  614. {
  615. icmap_track_t icmap_track_totem = NULL;
  616. icmap_track_t icmap_track_reload = NULL;
  617. ENTER();
  618. icmap_track_add("totem.",
  619. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  620. totemknet_refresh_config,
  621. instance,
  622. &icmap_track_totem);
  623. icmap_track_add("config.totemconfig_reload_in_progress",
  624. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  625. totemknet_refresh_config,
  626. instance,
  627. &icmap_track_reload);
  628. LEAVE();
  629. }
  630. /*
  631. * Create an instance
  632. */
  633. int totemknet_initialize (
  634. qb_loop_t *poll_handle,
  635. void **knet_context,
  636. struct totem_config *totem_config,
  637. totemsrp_stats_t *stats,
  638. void *context,
  639. void (*deliver_fn) (
  640. void *context,
  641. const void *msg,
  642. unsigned int msg_len),
  643. void (*iface_change_fn) (
  644. void *context,
  645. const struct totem_ip_address *iface_address,
  646. unsigned int link_no),
  647. void (*mtu_changed) (
  648. void *context,
  649. int net_mtu),
  650. void (*target_set_completed) (
  651. void *context))
  652. {
  653. struct totemknet_instance *instance;
  654. int8_t channel=0;
  655. int res;
  656. int i;
  657. instance = malloc (sizeof (struct totemknet_instance));
  658. if (instance == NULL) {
  659. return (-1);
  660. }
  661. totemknet_instance_initialize (instance);
  662. instance->totem_config = totem_config;
  663. /*
  664. * Configure logging
  665. */
  666. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  667. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  668. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  669. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  670. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  671. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  672. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  673. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  674. /*
  675. * Initialize local variables for totemknet
  676. */
  677. instance->our_nodeid = instance->totem_config->node_id;
  678. for (i=0; i< INTERFACE_MAX; i++) {
  679. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  680. instance->my_ids[i].nodeid = instance->our_nodeid;
  681. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  682. /* Needed for totemsrp */
  683. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  684. }
  685. instance->poll_handle = poll_handle;
  686. instance->context = context;
  687. instance->totemknet_deliver_fn = deliver_fn;
  688. instance->totemknet_iface_change_fn = iface_change_fn;
  689. instance->totemknet_mtu_changed = mtu_changed;
  690. instance->totemknet_target_set_completed = target_set_completed;
  691. instance->loopback_link = -1;
  692. res = pipe(instance->logpipes);
  693. if (res == -1) {
  694. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  695. goto exit_error;
  696. }
  697. fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK);
  698. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK);
  699. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  700. if (!instance->knet_handle) {
  701. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  702. goto exit_error;
  703. }
  704. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  705. if (res) {
  706. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  707. }
  708. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  709. if (res) {
  710. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  711. }
  712. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  713. if (res) {
  714. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  715. }
  716. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  717. if (res) {
  718. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  719. }
  720. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  721. if (res) {
  722. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  723. }
  724. global_instance = instance;
  725. /* Get an fd into knet */
  726. instance->knet_fd = 0;
  727. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  728. if (res) {
  729. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  730. goto exit_error;
  731. }
  732. /* Enable crypto if requested */
  733. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  734. struct knet_handle_crypto_cfg crypto_cfg;
  735. strcpy(crypto_cfg.crypto_model, instance->totem_config->crypto_model);
  736. strcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type);
  737. strcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type);
  738. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  739. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  740. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  741. if (res == -1) {
  742. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  743. goto exit_error;
  744. }
  745. if (res == -2) {
  746. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  747. goto exit_error;
  748. }
  749. knet_log_printf(LOG_INFO, "kronosnet crypto initialized: %s/%s", crypto_cfg.crypto_cipher_type, crypto_cfg.crypto_hash_type);
  750. }
  751. /* Set up compression */
  752. totemknet_reconfigure(instance, instance->totem_config);
  753. knet_handle_setfwd(instance->knet_handle, 1);
  754. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  755. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  756. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  757. }
  758. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  759. instance->link_mode = KNET_LINK_POLICY_RR;
  760. }
  761. for (i=0; i<INTERFACE_MAX; i++) {
  762. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  763. if (!instance->link_status[i]) {
  764. goto exit_error;
  765. }
  766. }
  767. qb_loop_poll_add (instance->poll_handle,
  768. QB_LOOP_MED,
  769. instance->logpipes[0],
  770. POLLIN, instance, log_deliver_fn);
  771. qb_loop_poll_add (instance->poll_handle,
  772. QB_LOOP_HIGH,
  773. instance->knet_fd,
  774. POLLIN, instance, data_deliver_fn);
  775. /*
  776. * Upper layer isn't ready to receive message because it hasn't
  777. * initialized yet. Add short timer to check the interfaces.
  778. */
  779. qb_loop_timer_add (instance->poll_handle,
  780. QB_LOOP_MED,
  781. 100*QB_TIME_NS_IN_MSEC,
  782. (void *)instance,
  783. timer_function_netif_check_timeout,
  784. &instance->timer_netif_check_timeout);
  785. totemknet_start_merge_detect_timeout(instance);
  786. /* Start listening for config changes */
  787. totemknet_add_config_notifications(instance);
  788. /* Add stats keys to icmap */
  789. stats_knet_add_handle();
  790. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  791. *knet_context = instance;
  792. return (0);
  793. exit_error:
  794. log_flush_messages(instance);
  795. return (-1);
  796. }
  797. void *totemknet_buffer_alloc (void)
  798. {
  799. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  800. return malloc(KNET_MAX_PACKET_SIZE + 512);
  801. }
  802. void totemknet_buffer_release (void *ptr)
  803. {
  804. return free (ptr);
  805. }
  806. int totemknet_processor_count_set (
  807. void *knet_context,
  808. int processor_count)
  809. {
  810. return (0);
  811. }
  812. int totemknet_recv_flush (void *knet_context)
  813. {
  814. return (0);
  815. }
  816. int totemknet_send_flush (void *knet_context)
  817. {
  818. return (0);
  819. }
  820. int totemknet_token_send (
  821. void *knet_context,
  822. const void *msg,
  823. unsigned int msg_len)
  824. {
  825. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  826. int res = 0;
  827. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  828. return (res);
  829. }
  830. int totemknet_mcast_flush_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. mcast_sendmsg (instance, msg, msg_len, 0);
  838. return (res);
  839. }
  840. int totemknet_mcast_noflush_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, 1);
  848. return (res);
  849. }
  850. extern int totemknet_iface_check (void *knet_context)
  851. {
  852. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  853. int res = 0;
  854. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  855. return (res);
  856. }
  857. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  858. {
  859. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  860. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  861. }
  862. int totemknet_token_target_set (
  863. void *knet_context,
  864. unsigned int nodeid)
  865. {
  866. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  867. int res = 0;
  868. instance->token_target.nodeid = nodeid;
  869. instance->totemknet_target_set_completed (instance->context);
  870. return (res);
  871. }
  872. extern int totemknet_recv_mcast_empty (
  873. void *knet_context)
  874. {
  875. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  876. unsigned int res;
  877. struct sockaddr_storage system_from;
  878. struct msghdr msg_hdr;
  879. struct iovec iov_recv;
  880. struct pollfd ufd;
  881. int nfds;
  882. int msg_processed = 0;
  883. iov_recv.iov_base = instance->iov_buffer;
  884. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  885. msg_hdr.msg_name = &system_from;
  886. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  887. msg_hdr.msg_iov = &iov_recv;
  888. msg_hdr.msg_iovlen = 1;
  889. #ifdef HAVE_MSGHDR_CONTROL
  890. msg_hdr.msg_control = 0;
  891. #endif
  892. #ifdef HAVE_MSGHDR_CONTROLLEN
  893. msg_hdr.msg_controllen = 0;
  894. #endif
  895. #ifdef HAVE_MSGHDR_FLAGS
  896. msg_hdr.msg_flags = 0;
  897. #endif
  898. #ifdef HAVE_MSGHDR_ACCRIGHTS
  899. msg_msg_hdr.msg_accrights = NULL;
  900. #endif
  901. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  902. msg_msg_hdr.msg_accrightslen = 0;
  903. #endif
  904. do {
  905. ufd.fd = instance->knet_fd;
  906. ufd.events = POLLIN;
  907. nfds = poll (&ufd, 1, 0);
  908. if (nfds == 1 && ufd.revents & POLLIN) {
  909. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  910. if (res != -1) {
  911. msg_processed = 1;
  912. } else {
  913. msg_processed = -1;
  914. }
  915. }
  916. } while (nfds == 1);
  917. return (msg_processed);
  918. }
  919. int totemknet_iface_set (void *knet_context,
  920. const struct totem_ip_address *local_addr,
  921. unsigned short ip_port,
  922. unsigned int iface_no)
  923. {
  924. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  925. totemip_copy(&instance->my_ids[iface_no], local_addr);
  926. knet_log_printf(LOG_INFO, "Configured link number %d: local addr: %s, port=%d", iface_no, totemip_print(local_addr), ip_port);
  927. instance->ip_port[iface_no] = ip_port;
  928. return 0;
  929. }
  930. int totemknet_member_add (
  931. void *knet_context,
  932. const struct totem_ip_address *local,
  933. const struct totem_ip_address *member,
  934. int link_no)
  935. {
  936. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  937. int err;
  938. int port = instance->ip_port[link_no];
  939. struct sockaddr_storage remote_ss;
  940. struct sockaddr_storage local_ss;
  941. int addrlen;
  942. /* Only create 1 loopback link */
  943. // NOTE: THis depends on member_remove being run before member_add when reeconfiguring
  944. // otherwise we could be left with no loopback.
  945. if (member->nodeid == instance->our_nodeid && instance->loopback_link > -1) {
  946. return 0;
  947. }
  948. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: %d (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  949. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: %d (%s)", local->nodeid, totemip_print(local));
  950. // TODO FIXME - prints errors when host already exists
  951. err = knet_host_add(instance->knet_handle, member->nodeid);
  952. if (err != 0 && errno != EEXIST) {
  953. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  954. return -1;
  955. }
  956. if (err == 0) {
  957. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  958. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  959. return -1;
  960. }
  961. }
  962. memset(&local_ss, 0, sizeof(local_ss));
  963. /* Casts to remove const */
  964. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port, &remote_ss, &addrlen);
  965. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port, &local_ss, &addrlen);
  966. if (member->nodeid == instance->our_nodeid) {
  967. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: loopback link is %d\n", link_no);
  968. instance->loopback_link = link_no;
  969. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  970. KNET_TRANSPORT_LOOPBACK,
  971. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  972. }
  973. else {
  974. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  975. instance->totem_config->interfaces[link_no].knet_transport,
  976. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  977. }
  978. if (err) {
  979. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  980. return -1;
  981. }
  982. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  983. instance->totem_config->interfaces[link_no].knet_link_priority);
  984. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  985. instance->totem_config->interfaces[link_no].knet_link_priority);
  986. if (err) {
  987. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid %d, link %d failed", member->nodeid, link_no);
  988. }
  989. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  990. instance->totem_config->interfaces[link_no].knet_ping_interval,
  991. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  992. instance->totem_config->interfaces[link_no].knet_ping_precision);
  993. if (err) {
  994. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for nodeid %d, link %d failed", member->nodeid, link_no);
  995. }
  996. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  997. instance->totem_config->interfaces[link_no].knet_pong_count);
  998. if (err) {
  999. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for nodeid %d, link %d failed", member->nodeid, link_no);
  1000. }
  1001. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1002. if (err) {
  1003. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid %d, link %d failed", member->nodeid, link_no);
  1004. return -1;
  1005. }
  1006. /* register stats */
  1007. stats_knet_add_member(member->nodeid, link_no);
  1008. return (0);
  1009. }
  1010. int totemknet_member_remove (
  1011. void *knet_context,
  1012. const struct totem_ip_address *token_target,
  1013. int link_no)
  1014. {
  1015. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1016. int res;
  1017. uint8_t link_list[KNET_MAX_LINK];
  1018. size_t num_links;
  1019. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: %d, link=%d", token_target->nodeid, link_no);
  1020. /* Removing link with the loopback on it */
  1021. if (token_target->nodeid == instance->our_nodeid && link_no == instance->loopback_link) {
  1022. instance->loopback_link= -1;
  1023. }
  1024. /* Tidy stats */
  1025. stats_knet_del_member(token_target->nodeid, link_no);
  1026. /* Remove the link first */
  1027. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1028. if (res != 0) {
  1029. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set enable(off) for nodeid %d, link %d failed", token_target->nodeid, link_no);
  1030. return res;
  1031. }
  1032. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1033. if (res != 0) {
  1034. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_clear_config for nodeid %d, link %d failed", token_target->nodeid, link_no);
  1035. return res;
  1036. }
  1037. /* If this is the last link, then remove the node */
  1038. res = knet_link_get_link_list(instance->knet_handle,
  1039. token_target->nodeid, link_list, &num_links);
  1040. if (res) {
  1041. return (0); /* not really failure */
  1042. }
  1043. if (num_links == 0) {
  1044. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1045. }
  1046. return res;
  1047. }
  1048. int totemknet_member_list_rebind_ip (
  1049. void *knet_context)
  1050. {
  1051. return (0);
  1052. }
  1053. int totemknet_reconfigure (
  1054. void *knet_context,
  1055. struct totem_config *totem_config)
  1056. {
  1057. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1058. struct knet_handle_compress_cfg compress_cfg;
  1059. int res = 0;
  1060. if (totem_config->knet_compression_model) {
  1061. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1062. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1063. compress_cfg.compress_level = totem_config->knet_compression_level;
  1064. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1065. if (res) {
  1066. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1067. }
  1068. }
  1069. return (res);
  1070. }
  1071. void totemknet_stats_clear (
  1072. void *knet_context)
  1073. {
  1074. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1075. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1076. }
  1077. /* For the stats module */
  1078. int totemknet_link_get_status (
  1079. knet_node_id_t node, uint8_t link_no,
  1080. struct knet_link_status *status)
  1081. {
  1082. int res;
  1083. int ret = CS_OK;
  1084. /* We are probably not using knet */
  1085. if (!global_instance) {
  1086. return CS_ERR_NOT_EXIST;
  1087. }
  1088. if (link_no >= INTERFACE_MAX) {
  1089. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1090. }
  1091. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1092. if (res) {
  1093. switch (errno) {
  1094. case EINVAL:
  1095. ret = CS_ERR_INVALID_PARAM;
  1096. break;
  1097. case EBUSY:
  1098. ret = CS_ERR_BUSY;
  1099. break;
  1100. case EDEADLK:
  1101. ret = CS_ERR_TRY_AGAIN;
  1102. break;
  1103. default:
  1104. ret = CS_ERR_LIBRARY;
  1105. break;
  1106. }
  1107. }
  1108. return (ret);
  1109. }
  1110. int totemknet_handle_get_stats (
  1111. struct knet_handle_stats *stats)
  1112. {
  1113. /* We are probably not using knet */
  1114. if (!global_instance) {
  1115. return CS_ERR_NOT_EXIST;
  1116. }
  1117. return knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1118. }
  1119. static void timer_function_merge_detect_timeout (
  1120. void *data)
  1121. {
  1122. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1123. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1124. instance->send_merge_detect_message = 1;
  1125. }
  1126. instance->merge_detect_messages_sent_before_timeout = 0;
  1127. totemknet_start_merge_detect_timeout(instance);
  1128. }
  1129. static void totemknet_start_merge_detect_timeout(
  1130. void *knet_context)
  1131. {
  1132. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1133. qb_loop_timer_add(instance->poll_handle,
  1134. QB_LOOP_MED,
  1135. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1136. (void *)instance,
  1137. timer_function_merge_detect_timeout,
  1138. &instance->timer_merge_detect_timeout);
  1139. }
  1140. static void totemknet_stop_merge_detect_timeout(
  1141. void *knet_context)
  1142. {
  1143. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1144. qb_loop_timer_del(instance->poll_handle,
  1145. instance->timer_merge_detect_timeout);
  1146. }
  1147. static void log_flush_messages (void *knet_context)
  1148. {
  1149. struct pollfd pfd;
  1150. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1151. int cont;
  1152. cont = 1;
  1153. while (cont) {
  1154. pfd.fd = instance->logpipes[0];
  1155. pfd.events = POLLIN;
  1156. pfd.revents = 0;
  1157. if ((poll(&pfd, 1, 0) > 0) &&
  1158. (pfd.revents & POLLIN) &&
  1159. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1160. cont = 1;
  1161. } else {
  1162. cont = 0;
  1163. }
  1164. }
  1165. }