totemknet.c 28 KB

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