totemknet.c 38 KB

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