totemknet.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Copyright (c) 2016 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/list.h>
  60. #include <corosync/swab.h>
  61. #include <corosync/logsys.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. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  74. /* Buffers for sendmmsg/recvmmsg */
  75. #define MAX_BUFFERS 10
  76. /* Should match that used by cfg */
  77. #define CFG_INTERFACE_STATUS_MAX_LEN 512
  78. /* Log messages received from libknet */
  79. #define LOG_BUFFER_SIZE 8192
  80. struct totemknet_instance {
  81. struct crypto_instance *crypto_inst;
  82. qb_loop_t *poll_handle;
  83. knet_handle_t knet_handle;
  84. int link_mode;
  85. void *context;
  86. void (*totemknet_deliver_fn) (
  87. void *context,
  88. const void *msg,
  89. unsigned int msg_len);
  90. void (*totemknet_iface_change_fn) (
  91. void *context,
  92. const struct totem_ip_address *iface_address,
  93. unsigned int link_no);
  94. void (*totemknet_mtu_changed) (
  95. void *context,
  96. int net_mtu);
  97. void (*totemknet_target_set_completed) (void *context);
  98. /*
  99. * Function and data used to log messages
  100. */
  101. int totemknet_log_level_security;
  102. int totemknet_log_level_error;
  103. int totemknet_log_level_warning;
  104. int totemknet_log_level_notice;
  105. int totemknet_log_level_debug;
  106. int totemknet_subsys_id;
  107. int knet_subsys_id;
  108. void (*totemknet_log_printf) (
  109. int level,
  110. int subsys,
  111. const char *function,
  112. const char *file,
  113. int line,
  114. const char *format,
  115. ...)__attribute__((format(printf, 6, 7)));
  116. void *knet_context;
  117. char iov_buffer[MAX_BUFFERS][FRAME_SIZE_MAX];
  118. int stats_sent;
  119. int stats_recv;
  120. int stats_delv;
  121. int stats_remcasts;
  122. int stats_orf_token;
  123. struct timeval stats_tv_start;
  124. char *link_status[INTERFACE_MAX];
  125. int num_links;
  126. struct totem_ip_address my_ids[INTERFACE_MAX];
  127. uint16_t ip_port[INTERFACE_MAX];
  128. int our_nodeid;
  129. struct totem_config *totem_config;
  130. totemsrp_stats_t *stats;
  131. struct totem_ip_address token_target;
  132. qb_loop_timer_handle timer_netif_check_timeout;
  133. qb_loop_timer_handle timer_merge_detect_timeout;
  134. int send_merge_detect_message;
  135. unsigned int merge_detect_messages_sent_before_timeout;
  136. int logpipes[2];
  137. int knet_fd;
  138. };
  139. struct work_item {
  140. const void *msg;
  141. unsigned int msg_len;
  142. struct totemknet_instance *instance;
  143. };
  144. int totemknet_member_list_rebind_ip (
  145. void *knet_context);
  146. static void totemknet_start_merge_detect_timeout(
  147. void *knet_context);
  148. static void totemknet_stop_merge_detect_timeout(
  149. void *knet_context);
  150. static void totemknet_instance_initialize (struct totemknet_instance *instance)
  151. {
  152. memset (instance, 0, sizeof (struct totemknet_instance));
  153. }
  154. #define knet_log_printf(level, format, args...) \
  155. do { \
  156. instance->totemknet_log_printf ( \
  157. level, instance->totemknet_subsys_id, \
  158. __FUNCTION__, __FILE__, __LINE__, \
  159. (const char *)format, ##args); \
  160. } while (0);
  161. #define libknet_log_printf(level, format, args...) \
  162. do { \
  163. instance->totemknet_log_printf ( \
  164. level, instance->knet_subsys_id, \
  165. __FUNCTION__, "libknet.h", __LINE__, \
  166. (const char *)format, ##args); \
  167. } while (0);
  168. #define KNET_LOGSYS_PERROR(err_num, level, fmt, args...) \
  169. do { \
  170. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  171. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  172. instance->totemknet_log_printf ( \
  173. level, instance->totemknet_subsys_id, \
  174. __FUNCTION__, __FILE__, __LINE__, \
  175. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  176. } while(0)
  177. static int dst_host_filter_callback_fn(void *private_data,
  178. const unsigned char *outdata,
  179. ssize_t outdata_len,
  180. uint8_t tx_rx,
  181. uint16_t this_host_id,
  182. uint16_t src_host_id,
  183. int8_t *channel,
  184. uint16_t *dst_host_ids,
  185. size_t *dst_host_ids_entries)
  186. {
  187. struct totem_message_header *header = (struct totem_message_header *)outdata;
  188. // struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  189. int res;
  190. // knet_log_printf (LOGSYS_LEVEL_DEBUG, "Filter notification called: %s target nodeid=%d, len=%ld", tx_rx==KNET_NOTIFY_RX?"RX":"TX", header->target_nodeid, outdata_len);
  191. *channel = 0;
  192. if (header->target_nodeid) {
  193. dst_host_ids[0] = header->target_nodeid;
  194. *dst_host_ids_entries = 1;
  195. res = 0; /* unicast message */
  196. }
  197. else {
  198. *dst_host_ids_entries = 0;
  199. res = 1; /* multicast message */
  200. }
  201. return res;
  202. }
  203. static void socket_error_callback_fn(void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)
  204. {
  205. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  206. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet socket ERROR notification called: txrx=%d, error=%d, errorno=%d", tx_rx, error, errorno);
  207. if ((error == -1 && errorno != EAGAIN) || (error == 0)) {
  208. knet_handle_remove_datafd(instance->knet_handle, datafd);
  209. }
  210. }
  211. static void host_change_callback_fn(void *private_data, uint16_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)
  212. {
  213. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  214. // TODO: what? if anything.
  215. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet host change callback. nodeid: %d reachable: %d", host_id, reachable);
  216. }
  217. static void pmtu_change_callback_fn(void *private_data, unsigned int data_mtu)
  218. {
  219. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  220. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet pMTU change: %d", data_mtu);
  221. // TODO: Check this
  222. instance->totemknet_mtu_changed(instance->context, data_mtu - totemip_udpip_header_size(AF_INET));
  223. }
  224. int totemknet_crypto_set (
  225. void *knet_context,
  226. const char *cipher_type,
  227. const char *hash_type)
  228. {
  229. return (0);
  230. }
  231. static inline void ucast_sendmsg (
  232. struct totemknet_instance *instance,
  233. struct totem_ip_address *system_to,
  234. const void *msg,
  235. unsigned int msg_len)
  236. {
  237. int res = 0;
  238. struct totem_message_header *header = (struct totem_message_header *)msg;
  239. header->target_nodeid = system_to->nodeid;
  240. /*
  241. * Transmit unicast message
  242. * An error here is recovered by totemsrp
  243. */
  244. /*
  245. * If sending to ourself then just pass it through knet back to
  246. * the receive fn. knet does not do local->local delivery
  247. */
  248. if (system_to->nodeid == instance->our_nodeid) {
  249. res = write (instance->knet_fd+1, msg, msg_len);
  250. if (res < 0) {
  251. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  252. "sendmsg(ucast-local) failed (non-critical)");
  253. }
  254. }
  255. else {
  256. res = write (instance->knet_fd, msg, msg_len);
  257. if (res < 0) {
  258. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  259. "sendmsg(ucast) failed (non-critical)");
  260. }
  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. header->target_nodeid = 0;
  272. // log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_sendmsg. only_active=%d, len=%d", only_active, msg_len);
  273. res = write (instance->knet_fd, msg, msg_len);
  274. if (res < msg_len) {
  275. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send writev returned %d", res);
  276. }
  277. /*
  278. * Also send it to ourself, directly into
  279. * the receive fn. knet does not to local->local delivery
  280. */
  281. res = write (instance->knet_fd+1, msg, msg_len);
  282. if (res < msg_len) {
  283. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send writev (local) returned %d", res);
  284. }
  285. if (!only_active || instance->send_merge_detect_message) {
  286. /*
  287. * Current message was sent to all nodes
  288. */
  289. instance->merge_detect_messages_sent_before_timeout++;
  290. instance->send_merge_detect_message = 0;
  291. }
  292. }
  293. static int node_compare(const void *aptr, const void *bptr)
  294. {
  295. uint16_t a,b;
  296. a = *(uint16_t *)aptr;
  297. b = *(uint16_t *)bptr;
  298. return a > b;
  299. }
  300. int totemknet_ifaces_get (void *knet_context,
  301. char ***status,
  302. unsigned int *iface_count)
  303. {
  304. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  305. struct knet_link_status link_status;
  306. uint16_t host_list[KNET_MAX_HOST];
  307. size_t num_hosts;
  308. int i,j;
  309. char *ptr;
  310. int res = 0;
  311. /*
  312. * Don't do the whole 'link_info' bit if the caller just wants
  313. * a count of interfaces.
  314. */
  315. if (status) {
  316. res = knet_host_get_host_list(instance->knet_handle,
  317. host_list, &num_hosts);
  318. if (res) {
  319. return (-1);
  320. }
  321. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  322. /* num_links is actually the highest link ID */
  323. for (i=0; i <= instance->num_links; i++) {
  324. ptr = instance->link_status[i];
  325. for (j=0; j<num_hosts; j++) {
  326. res = knet_link_get_status(instance->knet_handle,
  327. host_list[j],
  328. i,
  329. &link_status);
  330. if (res == 0) {
  331. ptr[j] = '0' + (link_status.enabled |
  332. link_status.connected<<1 |
  333. link_status.dynconnected<<2);
  334. }
  335. else {
  336. ptr[j] += '?';
  337. }
  338. }
  339. ptr[num_hosts] = '\0';
  340. }
  341. *status = instance->link_status;
  342. }
  343. *iface_count = instance->num_links+1;
  344. return (res);
  345. }
  346. int totemknet_finalize (
  347. void *knet_context)
  348. {
  349. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  350. int res = 0;
  351. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  352. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  353. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  354. knet_handle_free(instance->knet_handle);
  355. totemknet_stop_merge_detect_timeout(instance);
  356. return (res);
  357. }
  358. static int log_deliver_fn (
  359. int fd,
  360. int revents,
  361. void *data)
  362. {
  363. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  364. char buffer[LOG_BUFFER_SIZE];
  365. int len;
  366. len = read(fd, buffer, sizeof(buffer));
  367. if (len) {
  368. struct knet_log_msg *msg = (struct knet_log_msg *)buffer;
  369. switch (msg->msglevel) {
  370. case KNET_LOG_ERR:
  371. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s", msg->msg);
  372. break;
  373. case KNET_LOG_WARN:
  374. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s", msg->msg);
  375. break;
  376. case KNET_LOG_INFO:
  377. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s", msg->msg);
  378. break;
  379. case KNET_LOG_DEBUG:
  380. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s", msg->msg);
  381. break;
  382. }
  383. }
  384. return 0;
  385. }
  386. static int data_deliver_fn (
  387. int fd,
  388. int revents,
  389. void *data)
  390. {
  391. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  392. struct mmsghdr msg_recv[MAX_BUFFERS];
  393. struct iovec iov_recv[MAX_BUFFERS];
  394. struct sockaddr_storage system_from;
  395. int msgs_received;
  396. int i;
  397. for (i=0; i<MAX_BUFFERS; i++) {
  398. iov_recv[i].iov_base = instance->iov_buffer[i];
  399. iov_recv[i].iov_len = FRAME_SIZE_MAX;
  400. msg_recv[i].msg_hdr.msg_name = &system_from;
  401. msg_recv[i].msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  402. msg_recv[i].msg_hdr.msg_iov = &iov_recv[i];
  403. msg_recv[i].msg_hdr.msg_iovlen = 1;
  404. #ifdef HAVE_MSGHDR_CONTROL
  405. msg_recv[i].msg_hdr.msg_control = 0;
  406. #endif
  407. #ifdef HAVE_MSGHDR_CONTROLLEN
  408. msg_recv[i].msg_hdr.msg_controllen = 0;
  409. #endif
  410. #ifdef HAVE_MSGHDR_FLAGS
  411. msg_recv[i].msg_hdr.msg_flags = 0;
  412. #endif
  413. #ifdef HAVE_MSGHDR_ACCRIGHTS
  414. msg_recv[i].msg_hdr.msg_accrights = NULL;
  415. #endif
  416. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  417. msg_recv[i].msg_hdr.msg_accrightslen = 0;
  418. #endif
  419. }
  420. msgs_received = recvmmsg (fd, msg_recv, MAX_BUFFERS, MSG_NOSIGNAL | MSG_DONTWAIT, NULL);
  421. if (msgs_received == -1) {
  422. return (0);
  423. }
  424. for (i=0; i<msgs_received; i++) {
  425. instance->stats_recv += msg_recv[i].msg_len;
  426. /*
  427. * Handle incoming message
  428. */
  429. instance->totemknet_deliver_fn (
  430. instance->context,
  431. instance->iov_buffer[i],
  432. msg_recv[i].msg_len);
  433. }
  434. return (0);
  435. }
  436. static void timer_function_netif_check_timeout (
  437. void *data)
  438. {
  439. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  440. int i;
  441. for (i=0; i<instance->totem_config->interface_count; i++)
  442. instance->totemknet_iface_change_fn (instance->context,
  443. &instance->my_ids[i],
  444. i);
  445. }
  446. /*
  447. * Create an instance
  448. */
  449. int totemknet_initialize (
  450. qb_loop_t *poll_handle,
  451. void **knet_context,
  452. struct totem_config *totem_config,
  453. totemsrp_stats_t *stats,
  454. void *context,
  455. void (*deliver_fn) (
  456. void *context,
  457. const void *msg,
  458. unsigned int msg_len),
  459. void (*iface_change_fn) (
  460. void *context,
  461. const struct totem_ip_address *iface_address,
  462. unsigned int link_no),
  463. void (*mtu_changed) (
  464. void *context,
  465. int net_mtu),
  466. void (*target_set_completed) (
  467. void *context))
  468. {
  469. struct totemknet_instance *instance;
  470. int8_t channel=0;
  471. int res;
  472. int i;
  473. instance = malloc (sizeof (struct totemknet_instance));
  474. if (instance == NULL) {
  475. return (-1);
  476. }
  477. totemknet_instance_initialize (instance);
  478. instance->totem_config = totem_config;
  479. instance->stats = stats;
  480. /*
  481. * Configure logging
  482. */
  483. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  484. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  485. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  486. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  487. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  488. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  489. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  490. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  491. /*
  492. * Initialize local variables for totemknet
  493. */
  494. instance->our_nodeid = instance->totem_config->node_id;
  495. for (i=0; i< instance->totem_config->interface_count; i++) {
  496. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  497. instance->my_ids[i].nodeid = instance->our_nodeid;
  498. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  499. /* Needed for totemsrp */
  500. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  501. }
  502. instance->poll_handle = poll_handle;
  503. instance->context = context;
  504. instance->totemknet_deliver_fn = deliver_fn;
  505. instance->totemknet_iface_change_fn = iface_change_fn;
  506. instance->totemknet_mtu_changed = mtu_changed;
  507. instance->totemknet_target_set_completed = target_set_completed;
  508. pipe(instance->logpipes);
  509. fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK);
  510. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK);
  511. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  512. if (!instance->knet_handle) {
  513. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  514. return (-1);
  515. }
  516. res = knet_handle_pmtud_setfreq(instance->knet_handle, 5);
  517. if (res) {
  518. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  519. }
  520. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  521. if (res) {
  522. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  523. }
  524. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  525. if (res) {
  526. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  527. }
  528. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  529. if (res) {
  530. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  531. }
  532. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  533. if (res) {
  534. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  535. }
  536. /* Get an fd into knet */
  537. instance->knet_fd = 0;
  538. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  539. if (res) {
  540. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  541. return -1;
  542. }
  543. /* Enable crypto if requested */
  544. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  545. struct knet_handle_crypto_cfg crypto_cfg;
  546. strcpy(crypto_cfg.crypto_model, "nss");
  547. strcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type);
  548. strcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type);
  549. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  550. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  551. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  552. if (res == -1) {
  553. knet_log_printf(LOG_ERR, "knet_handle_crypto failed: %s", strerror(errno));
  554. return -1;
  555. }
  556. if (res == -2) {
  557. knet_log_printf(LOG_ERR, "knet_handle_crypto failed: -2");
  558. return -1;
  559. }
  560. knet_log_printf(LOG_INFO, "kronosnet crypto initialized: %s/%s", crypto_cfg.crypto_cipher_type, crypto_cfg.crypto_hash_type);
  561. }
  562. knet_handle_setfwd(instance->knet_handle, 1);
  563. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  564. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  565. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  566. }
  567. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  568. instance->link_mode = KNET_LINK_POLICY_RR;
  569. }
  570. for (i=0; i<INTERFACE_MAX; i++) {
  571. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  572. if (!instance->link_status[i]) {
  573. return -1;
  574. }
  575. }
  576. qb_loop_poll_add (instance->poll_handle,
  577. QB_LOOP_MED,
  578. instance->logpipes[0],
  579. POLLIN, instance, log_deliver_fn);
  580. qb_loop_poll_add (instance->poll_handle,
  581. QB_LOOP_HIGH,
  582. instance->knet_fd,
  583. POLLIN, instance, data_deliver_fn);
  584. /*
  585. * Upper layer isn't ready to receive message because it hasn't
  586. * initialized yet. Add short timer to check the interfaces.
  587. */
  588. qb_loop_timer_add (instance->poll_handle,
  589. QB_LOOP_MED,
  590. 100*QB_TIME_NS_IN_MSEC,
  591. (void *)instance,
  592. timer_function_netif_check_timeout,
  593. &instance->timer_netif_check_timeout);
  594. totemknet_start_merge_detect_timeout(instance);
  595. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  596. *knet_context = instance;
  597. return (0);
  598. }
  599. void *totemknet_buffer_alloc (void)
  600. {
  601. return malloc (FRAME_SIZE_MAX);
  602. }
  603. void totemknet_buffer_release (void *ptr)
  604. {
  605. return free (ptr);
  606. }
  607. int totemknet_processor_count_set (
  608. void *knet_context,
  609. int processor_count)
  610. {
  611. return (0);
  612. }
  613. int totemknet_recv_flush (void *knet_context)
  614. {
  615. return (0);
  616. }
  617. int totemknet_send_flush (void *knet_context)
  618. {
  619. return (0);
  620. }
  621. int totemknet_token_send (
  622. void *knet_context,
  623. const void *msg,
  624. unsigned int msg_len)
  625. {
  626. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  627. int res = 0;
  628. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  629. return (res);
  630. }
  631. int totemknet_mcast_flush_send (
  632. void *knet_context,
  633. const void *msg,
  634. unsigned int msg_len)
  635. {
  636. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  637. int res = 0;
  638. mcast_sendmsg (instance, msg, msg_len, 0);
  639. return (res);
  640. }
  641. int totemknet_mcast_noflush_send (
  642. void *knet_context,
  643. const void *msg,
  644. unsigned int msg_len)
  645. {
  646. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  647. int res = 0;
  648. mcast_sendmsg (instance, msg, msg_len, 1);
  649. return (res);
  650. }
  651. extern int totemknet_iface_check (void *knet_context)
  652. {
  653. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  654. int res = 0;
  655. knet_log_printf(LOG_DEBUG, "totmeknet: iface_check");
  656. return (res);
  657. }
  658. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  659. {
  660. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  661. totem_config->net_mtu -= totemip_udpip_header_size(AF_INET) + 23;
  662. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  663. }
  664. int totemknet_token_target_set (
  665. void *knet_context,
  666. const struct totem_ip_address *token_target)
  667. {
  668. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  669. int res = 0;
  670. memcpy (&instance->token_target, token_target,
  671. sizeof (struct totem_ip_address));
  672. instance->totemknet_target_set_completed (instance->context);
  673. return (res);
  674. }
  675. extern int totemknet_recv_mcast_empty (
  676. void *knet_context)
  677. {
  678. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  679. unsigned int res;
  680. struct sockaddr_storage system_from;
  681. struct mmsghdr msg_recv[MAX_BUFFERS];
  682. struct iovec iov_recv[MAX_BUFFERS];
  683. struct pollfd ufd;
  684. int nfds;
  685. int msg_processed = 0;
  686. int i;
  687. for (i=0; i<MAX_BUFFERS; i++) {
  688. iov_recv[i].iov_base = instance->iov_buffer[i];
  689. iov_recv[i].iov_len = FRAME_SIZE_MAX;
  690. msg_recv[i].msg_hdr.msg_name = &system_from;
  691. msg_recv[i].msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  692. msg_recv[i].msg_hdr.msg_iov = &iov_recv[i];
  693. msg_recv[i].msg_hdr.msg_iovlen = 1;
  694. #ifdef HAVE_MSGHDR_CONTROL
  695. msg_recv[i].msg_hdr.msg_control = 0;
  696. #endif
  697. #ifdef HAVE_MSGHDR_CONTROLLEN
  698. msg_recv[i].msg_hdr.msg_controllen = 0;
  699. #endif
  700. #ifdef HAVE_MSGHDR_FLAGS
  701. msg_recv[i].msg_hdr.msg_flags = 0;
  702. #endif
  703. #ifdef HAVE_MSGHDR_ACCRIGHTS
  704. msg_recv[i].msg_hdr.msg_accrights = NULL;
  705. #endif
  706. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  707. msg_recv[i].msg_hdr.msg_accrightslen = 0;
  708. #endif
  709. }
  710. do {
  711. ufd.fd = instance->knet_fd;
  712. ufd.events = POLLIN;
  713. nfds = poll (&ufd, 1, 0);
  714. if (nfds == 1 && ufd.revents & POLLIN) {
  715. res = recvmmsg (instance->knet_fd, msg_recv, MAX_BUFFERS, MSG_NOSIGNAL | MSG_DONTWAIT, NULL);
  716. if (res != -1) {
  717. msg_processed = 1;
  718. } else {
  719. msg_processed = -1;
  720. }
  721. }
  722. } while (nfds == 1);
  723. return (msg_processed);
  724. }
  725. int totemknet_member_add (
  726. void *knet_context,
  727. const struct totem_ip_address *local,
  728. const struct totem_ip_address *member,
  729. int link_no)
  730. {
  731. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  732. int err;
  733. int port = instance->ip_port[link_no];
  734. struct sockaddr_storage remote_ss;
  735. struct sockaddr_storage local_ss;
  736. int addrlen;
  737. if (member->nodeid == instance->our_nodeid) {
  738. return 0; /* Don't add ourself, we send loopback messages directly */
  739. }
  740. /* Keep track of the number of links */
  741. if (link_no > instance->num_links) {
  742. instance->num_links = link_no;
  743. }
  744. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: %d (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  745. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: %d (%s)", local->nodeid, totemip_print(local));
  746. if (link_no == 0) {
  747. if (knet_host_add(instance->knet_handle, member->nodeid)) {
  748. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  749. return -1;
  750. }
  751. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  752. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  753. return -1;
  754. }
  755. }
  756. /* Casts to remove const */
  757. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port+link_no, &remote_ss, &addrlen);
  758. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port+link_no, &local_ss, &addrlen);
  759. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no, &local_ss, &remote_ss);
  760. if (err) {
  761. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  762. return -1;
  763. }
  764. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  765. instance->totem_config->interfaces[link_no].knet_link_priority);
  766. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  767. instance->totem_config->interfaces[link_no].knet_link_priority);
  768. if (err) {
  769. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid %d, link %d failed", member->nodeid, link_no);
  770. }
  771. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  772. instance->totem_config->interfaces[link_no].knet_ping_interval,
  773. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  774. instance->totem_config->interfaces[link_no].knet_ping_precision);
  775. if (err) {
  776. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for nodeid %d, link %d failed", member->nodeid, link_no);
  777. }
  778. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  779. if (err) {
  780. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid %d, link %d failed", member->nodeid, link_no);
  781. return -1;
  782. }
  783. return (0);
  784. }
  785. int totemknet_member_remove (
  786. void *knet_context,
  787. const struct totem_ip_address *token_target,
  788. int link_no)
  789. {
  790. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  791. return knet_host_remove(instance->knet_handle, token_target->nodeid);
  792. }
  793. int totemknet_member_list_rebind_ip (
  794. void *knet_context)
  795. {
  796. return (0);
  797. }
  798. static void timer_function_merge_detect_timeout (
  799. void *data)
  800. {
  801. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  802. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  803. instance->send_merge_detect_message = 1;
  804. }
  805. instance->merge_detect_messages_sent_before_timeout = 0;
  806. totemknet_start_merge_detect_timeout(instance);
  807. }
  808. static void totemknet_start_merge_detect_timeout(
  809. void *knet_context)
  810. {
  811. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  812. qb_loop_timer_add(instance->poll_handle,
  813. QB_LOOP_MED,
  814. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  815. (void *)instance,
  816. timer_function_merge_detect_timeout,
  817. &instance->timer_merge_detect_timeout);
  818. }
  819. static void totemknet_stop_merge_detect_timeout(
  820. void *knet_context)
  821. {
  822. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  823. qb_loop_timer_del(instance->poll_handle,
  824. instance->timer_merge_detect_timeout);
  825. }