totemknet.c 40 KB

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