totemknet.c 32 KB

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