totemknet.c 38 KB

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