totemknet.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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, KNET_TRANSPORT_UDP, &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. int res;
  904. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: %d, link=%d", token_target->nodeid, link_no);
  905. if (token_target->nodeid == instance->our_nodeid) {
  906. return 0; /* Don't remove ourself */
  907. }
  908. /* Remove the link first */
  909. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  910. if (res != 0) {
  911. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set enable(off) for nodeid %d, link %d failed", token_target->nodeid, link_no);
  912. return res;
  913. }
  914. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  915. if (res != 0) {
  916. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_clear_config for nodeid %d, link %d failed", token_target->nodeid, link_no);
  917. return res;
  918. }
  919. return knet_host_remove(instance->knet_handle, token_target->nodeid);
  920. }
  921. int totemknet_member_list_rebind_ip (
  922. void *knet_context)
  923. {
  924. return (0);
  925. }
  926. static void timer_function_merge_detect_timeout (
  927. void *data)
  928. {
  929. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  930. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  931. instance->send_merge_detect_message = 1;
  932. }
  933. instance->merge_detect_messages_sent_before_timeout = 0;
  934. totemknet_start_merge_detect_timeout(instance);
  935. }
  936. static void totemknet_start_merge_detect_timeout(
  937. void *knet_context)
  938. {
  939. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  940. qb_loop_timer_add(instance->poll_handle,
  941. QB_LOOP_MED,
  942. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  943. (void *)instance,
  944. timer_function_merge_detect_timeout,
  945. &instance->timer_merge_detect_timeout);
  946. }
  947. static void totemknet_stop_merge_detect_timeout(
  948. void *knet_context)
  949. {
  950. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  951. qb_loop_timer_del(instance->poll_handle,
  952. instance->timer_merge_detect_timeout);
  953. }