totemudpu.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <assert.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.h>
  56. #include <qb/qbdefs.h>
  57. #include <qb/qbloop.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/list.h>
  60. #include <corosync/swab.h>
  61. #define LOGSYS_UTILS_ONLY 1
  62. #include <corosync/logsys.h>
  63. #include "totemudpu.h"
  64. #include "util.h"
  65. #include "totemcrypto.h"
  66. #include <nss.h>
  67. #include <pk11pub.h>
  68. #include <pkcs11.h>
  69. #include <prerror.h>
  70. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  74. #define NETIF_STATE_REPORT_UP 1
  75. #define NETIF_STATE_REPORT_DOWN 2
  76. #define BIND_STATE_UNBOUND 0
  77. #define BIND_STATE_REGULAR 1
  78. #define BIND_STATE_LOOPBACK 2
  79. struct totemudpu_member {
  80. struct list_head list;
  81. struct totem_ip_address member;
  82. int fd;
  83. };
  84. struct totemudpu_instance {
  85. struct crypto_instance *crypto_inst;
  86. qb_loop_t *totemudpu_poll_handle;
  87. struct totem_interface *totem_interface;
  88. int netif_state_report;
  89. int netif_bind_state;
  90. void *context;
  91. void (*totemudpu_deliver_fn) (
  92. void *context,
  93. const void *msg,
  94. unsigned int msg_len);
  95. void (*totemudpu_iface_change_fn) (
  96. void *context,
  97. const struct totem_ip_address *iface_address);
  98. void (*totemudpu_target_set_completed) (void *context);
  99. /*
  100. * Function and data used to log messages
  101. */
  102. int totemudpu_log_level_security;
  103. int totemudpu_log_level_error;
  104. int totemudpu_log_level_warning;
  105. int totemudpu_log_level_notice;
  106. int totemudpu_log_level_debug;
  107. int totemudpu_subsys_id;
  108. void (*totemudpu_log_printf) (
  109. int level,
  110. int subsys,
  111. const char *function,
  112. const char *file,
  113. int line,
  114. const char *format,
  115. ...)__attribute__((format(printf, 6, 7)));
  116. void *udpu_context;
  117. char iov_buffer[FRAME_SIZE_MAX];
  118. struct iovec totemudpu_iov_recv;
  119. struct list_head member_list;
  120. int stats_sent;
  121. int stats_recv;
  122. int stats_delv;
  123. int stats_remcasts;
  124. int stats_orf_token;
  125. struct timeval stats_tv_start;
  126. struct totem_ip_address my_id;
  127. int firstrun;
  128. qb_loop_timer_handle timer_netif_check_timeout;
  129. unsigned int my_memb_entries;
  130. struct totem_config *totem_config;
  131. struct totem_ip_address token_target;
  132. int token_socket;
  133. };
  134. struct work_item {
  135. const void *msg;
  136. unsigned int msg_len;
  137. struct totemudpu_instance *instance;
  138. };
  139. static int totemudpu_build_sockets (
  140. struct totemudpu_instance *instance,
  141. struct totem_ip_address *bindnet_address,
  142. struct totem_ip_address *bound_to);
  143. static struct totem_ip_address localhost;
  144. static void totemudpu_instance_initialize (struct totemudpu_instance *instance)
  145. {
  146. memset (instance, 0, sizeof (struct totemudpu_instance));
  147. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  148. instance->totemudpu_iov_recv.iov_base = instance->iov_buffer;
  149. instance->totemudpu_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  150. /*
  151. * There is always atleast 1 processor
  152. */
  153. instance->my_memb_entries = 1;
  154. list_init (&instance->member_list);
  155. }
  156. #define log_printf(level, format, args...) \
  157. do { \
  158. instance->totemudpu_log_printf ( \
  159. level, instance->totemudpu_subsys_id, \
  160. __FUNCTION__, __FILE__, __LINE__, \
  161. (const char *)format, ##args); \
  162. } while (0);
  163. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  164. do { \
  165. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  166. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  167. instance->totemudpu_log_printf ( \
  168. level, instance->totemudpu_subsys_id, \
  169. __FUNCTION__, __FILE__, __LINE__, \
  170. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  171. } while(0)
  172. int totemudpu_crypto_set (
  173. void *udpu_context,
  174. unsigned int type)
  175. {
  176. return (0);
  177. }
  178. static inline void ucast_sendmsg (
  179. struct totemudpu_instance *instance,
  180. struct totem_ip_address *system_to,
  181. const void *msg,
  182. unsigned int msg_len)
  183. {
  184. struct msghdr msg_ucast;
  185. int res = 0;
  186. size_t buf_out_len;
  187. unsigned char buf_out[FRAME_SIZE_MAX];
  188. struct sockaddr_storage sockaddr;
  189. struct iovec iovec;
  190. int addrlen;
  191. if (instance->totem_config->secauth == 1) {
  192. /*
  193. * Encrypt and digest the message
  194. */
  195. if (crypto_encrypt_and_sign (
  196. instance->crypto_inst,
  197. (const unsigned char *)msg,
  198. msg_len,
  199. buf_out,
  200. &buf_out_len) != 0) {
  201. log_printf(LOGSYS_LEVEL_CRIT, "unable to crypt? now what?");
  202. }
  203. iovec.iov_base = (void *)buf_out;
  204. iovec.iov_len = buf_out_len;
  205. } else {
  206. iovec.iov_base = (void *)msg;
  207. iovec.iov_len = msg_len;
  208. }
  209. /*
  210. * Build unicast message
  211. */
  212. totemip_totemip_to_sockaddr_convert(system_to,
  213. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  214. msg_ucast.msg_name = &sockaddr;
  215. msg_ucast.msg_namelen = addrlen;
  216. msg_ucast.msg_iov = (void *)&iovec;
  217. msg_ucast.msg_iovlen = 1;
  218. #if !defined(COROSYNC_SOLARIS)
  219. msg_ucast.msg_control = 0;
  220. msg_ucast.msg_controllen = 0;
  221. msg_ucast.msg_flags = 0;
  222. #else
  223. msg_ucast.msg_accrights = NULL;
  224. msg_ucast.msg_accrightslen = 0;
  225. #endif
  226. /*
  227. * Transmit unicast message
  228. * An error here is recovered by totemsrp
  229. */
  230. res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
  231. if (res < 0) {
  232. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  233. "sendmsg(ucast) failed (non-critical)");
  234. }
  235. }
  236. static inline void mcast_sendmsg (
  237. struct totemudpu_instance *instance,
  238. const void *msg,
  239. unsigned int msg_len)
  240. {
  241. struct msghdr msg_mcast;
  242. int res = 0;
  243. size_t buf_out_len;
  244. unsigned char buf_out[FRAME_SIZE_MAX];
  245. struct iovec iovec;
  246. struct sockaddr_storage sockaddr;
  247. int addrlen;
  248. struct list_head *list;
  249. struct totemudpu_member *member;
  250. if (instance->totem_config->secauth == 1) {
  251. /*
  252. * Encrypt and digest the message
  253. */
  254. if(crypto_encrypt_and_sign (
  255. instance->crypto_inst,
  256. (const unsigned char *)msg,
  257. msg_len,
  258. buf_out,
  259. &buf_out_len) != 0) {
  260. log_printf(LOGSYS_LEVEL_CRIT, "Unable to crypt? now what?");
  261. }
  262. iovec.iov_base = (void *)buf_out;
  263. iovec.iov_len = buf_out_len;
  264. } else {
  265. iovec.iov_base = (void *)msg;
  266. iovec.iov_len = msg_len;
  267. }
  268. /*
  269. * Build multicast message
  270. */
  271. for (list = instance->member_list.next;
  272. list != &instance->member_list;
  273. list = list->next) {
  274. member = list_entry (list,
  275. struct totemudpu_member,
  276. list);
  277. totemip_totemip_to_sockaddr_convert(&member->member,
  278. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  279. msg_mcast.msg_name = &sockaddr;
  280. msg_mcast.msg_namelen = addrlen;
  281. msg_mcast.msg_iov = (void *)&iovec;
  282. msg_mcast.msg_iovlen = 1;
  283. #if !defined(COROSYNC_SOLARIS)
  284. msg_mcast.msg_control = 0;
  285. msg_mcast.msg_controllen = 0;
  286. msg_mcast.msg_flags = 0;
  287. #else
  288. msg_mcast.msg_accrights = NULL;
  289. msg_mcast.msg_accrightslen = 0;
  290. #endif
  291. /*
  292. * Transmit multicast message
  293. * An error here is recovered by totemsrp
  294. */
  295. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  296. if (res < 0) {
  297. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  298. "sendmsg(mcast) failed (non-critical)");
  299. }
  300. }
  301. }
  302. int totemudpu_finalize (
  303. void *udpu_context)
  304. {
  305. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  306. int res = 0;
  307. if (instance->token_socket > 0) {
  308. close (instance->token_socket);
  309. qb_loop_poll_del (instance->totemudpu_poll_handle,
  310. instance->token_socket);
  311. }
  312. return (res);
  313. }
  314. static int net_deliver_fn (
  315. int fd,
  316. int revents,
  317. void *data)
  318. {
  319. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  320. struct msghdr msg_recv;
  321. struct iovec *iovec;
  322. struct sockaddr_storage system_from;
  323. int bytes_received;
  324. int res = 0;
  325. iovec = &instance->totemudpu_iov_recv;
  326. /*
  327. * Receive datagram
  328. */
  329. msg_recv.msg_name = &system_from;
  330. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  331. msg_recv.msg_iov = iovec;
  332. msg_recv.msg_iovlen = 1;
  333. #if !defined(COROSYNC_SOLARIS)
  334. msg_recv.msg_control = 0;
  335. msg_recv.msg_controllen = 0;
  336. msg_recv.msg_flags = 0;
  337. #else
  338. msg_recv.msg_accrights = NULL;
  339. msg_recv.msg_accrightslen = 0;
  340. #endif
  341. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  342. if (bytes_received == -1) {
  343. return (0);
  344. } else {
  345. instance->stats_recv += bytes_received;
  346. }
  347. if (instance->totem_config->secauth == 1) {
  348. /*
  349. * Authenticate and if authenticated, decrypt datagram
  350. */
  351. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  352. if (res == -1) {
  353. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.");
  354. log_printf (instance->totemudpu_log_level_security,
  355. "Invalid packet data");
  356. iovec->iov_len = FRAME_SIZE_MAX;
  357. return 0;
  358. }
  359. }
  360. iovec->iov_len = bytes_received;
  361. /*
  362. * Handle incoming message
  363. */
  364. instance->totemudpu_deliver_fn (
  365. instance->context,
  366. iovec->iov_base,
  367. iovec->iov_len);
  368. iovec->iov_len = FRAME_SIZE_MAX;
  369. return (0);
  370. }
  371. static int netif_determine (
  372. struct totemudpu_instance *instance,
  373. struct totem_ip_address *bindnet,
  374. struct totem_ip_address *bound_to,
  375. int *interface_up,
  376. int *interface_num)
  377. {
  378. int res;
  379. res = totemip_iface_check (bindnet, bound_to,
  380. interface_up, interface_num,
  381. instance->totem_config->clear_node_high_bit);
  382. return (res);
  383. }
  384. /*
  385. * If the interface is up, the sockets for totem are built. If the interface is down
  386. * this function is requeued in the timer list to retry building the sockets later.
  387. */
  388. static void timer_function_netif_check_timeout (
  389. void *data)
  390. {
  391. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  392. int interface_up;
  393. int interface_num;
  394. struct totem_ip_address *bind_address;
  395. /*
  396. * Build sockets for every interface
  397. */
  398. netif_determine (instance,
  399. &instance->totem_interface->bindnet,
  400. &instance->totem_interface->boundto,
  401. &interface_up, &interface_num);
  402. /*
  403. * If the network interface isn't back up and we are already
  404. * in loopback mode, add timer to check again and return
  405. */
  406. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  407. interface_up == 0) ||
  408. (instance->my_memb_entries == 1 &&
  409. instance->netif_bind_state == BIND_STATE_REGULAR &&
  410. interface_up == 1)) {
  411. qb_loop_timer_add (instance->totemudpu_poll_handle,
  412. QB_LOOP_MED,
  413. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  414. (void *)instance,
  415. timer_function_netif_check_timeout,
  416. &instance->timer_netif_check_timeout);
  417. /*
  418. * Add a timer to check for a downed regular interface
  419. */
  420. return;
  421. }
  422. if (instance->token_socket > 0) {
  423. close (instance->token_socket);
  424. qb_loop_poll_del (instance->totemudpu_poll_handle,
  425. instance->token_socket);
  426. }
  427. if (interface_up == 0) {
  428. /*
  429. * Interface is not up
  430. */
  431. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  432. bind_address = &localhost;
  433. /*
  434. * Add a timer to retry building interfaces and request memb_gather_enter
  435. */
  436. qb_loop_timer_add (instance->totemudpu_poll_handle,
  437. QB_LOOP_MED,
  438. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  439. (void *)instance,
  440. timer_function_netif_check_timeout,
  441. &instance->timer_netif_check_timeout);
  442. } else {
  443. /*
  444. * Interface is up
  445. */
  446. instance->netif_bind_state = BIND_STATE_REGULAR;
  447. bind_address = &instance->totem_interface->bindnet;
  448. }
  449. /*
  450. * Create and bind the multicast and unicast sockets
  451. */
  452. totemudpu_build_sockets (instance,
  453. bind_address,
  454. &instance->totem_interface->boundto);
  455. qb_loop_poll_add (instance->totemudpu_poll_handle,
  456. QB_LOOP_MED,
  457. instance->token_socket,
  458. POLLIN, instance, net_deliver_fn);
  459. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  460. /*
  461. * This reports changes in the interface to the user and totemsrp
  462. */
  463. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  464. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  465. log_printf (instance->totemudpu_log_level_notice,
  466. "The network interface [%s] is now up.",
  467. totemip_print (&instance->totem_interface->boundto));
  468. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  469. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  470. }
  471. /*
  472. * Add a timer to check for interface going down in single membership
  473. */
  474. if (instance->my_memb_entries == 1) {
  475. qb_loop_timer_add (instance->totemudpu_poll_handle,
  476. QB_LOOP_MED,
  477. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  478. (void *)instance,
  479. timer_function_netif_check_timeout,
  480. &instance->timer_netif_check_timeout);
  481. }
  482. } else {
  483. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  484. log_printf (instance->totemudpu_log_level_notice,
  485. "The network interface is down.");
  486. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  487. }
  488. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  489. }
  490. }
  491. /* Set the socket priority to INTERACTIVE to ensure
  492. that our messages don't get queued behind anything else */
  493. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  494. {
  495. #ifdef SO_PRIORITY
  496. int prio = 6; /* TC_PRIO_INTERACTIVE */
  497. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  498. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  499. "Could not set traffic priority");
  500. }
  501. #endif
  502. }
  503. static int totemudpu_build_sockets_ip (
  504. struct totemudpu_instance *instance,
  505. struct totem_ip_address *bindnet_address,
  506. struct totem_ip_address *bound_to,
  507. int interface_num)
  508. {
  509. struct sockaddr_storage sockaddr;
  510. int addrlen;
  511. int res;
  512. unsigned int recvbuf_size;
  513. unsigned int optlen = sizeof (recvbuf_size);
  514. /*
  515. * Setup unicast socket
  516. */
  517. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  518. if (instance->token_socket == -1) {
  519. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  520. "socket() failed");
  521. return (-1);
  522. }
  523. totemip_nosigpipe (instance->token_socket);
  524. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  525. if (res == -1) {
  526. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  527. "Could not set non-blocking operation on token socket");
  528. return (-1);
  529. }
  530. /*
  531. * Bind to unicast socket used for token send/receives
  532. * This has the side effect of binding to the correct interface
  533. */
  534. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  535. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  536. if (res == -1) {
  537. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  538. "bind token socket failed");
  539. return (-1);
  540. }
  541. /*
  542. * the token_socket can receive many messages. Allow a large number
  543. * of receive messages on this socket
  544. */
  545. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  546. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  547. &recvbuf_size, optlen);
  548. if (res == -1) {
  549. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  550. "Could not set recvbuf size");
  551. }
  552. return 0;
  553. }
  554. static int totemudpu_build_sockets (
  555. struct totemudpu_instance *instance,
  556. struct totem_ip_address *bindnet_address,
  557. struct totem_ip_address *bound_to)
  558. {
  559. int interface_num;
  560. int interface_up;
  561. int res;
  562. /*
  563. * Determine the ip address bound to and the interface name
  564. */
  565. res = netif_determine (instance,
  566. bindnet_address,
  567. bound_to,
  568. &interface_up,
  569. &interface_num);
  570. if (res == -1) {
  571. return (-1);
  572. }
  573. totemip_copy(&instance->my_id, bound_to);
  574. res = totemudpu_build_sockets_ip (instance,
  575. bindnet_address, bound_to, interface_num);
  576. /* We only send out of the token socket */
  577. totemudpu_traffic_control_set(instance, instance->token_socket);
  578. return res;
  579. }
  580. /*
  581. * Totem Network interface - also does encryption/decryption
  582. * depends on poll abstraction, POSIX, IPV4
  583. */
  584. /*
  585. * Create an instance
  586. */
  587. int totemudpu_initialize (
  588. qb_loop_t *poll_handle,
  589. void **udpu_context,
  590. struct totem_config *totem_config,
  591. int interface_no,
  592. void *context,
  593. void (*deliver_fn) (
  594. void *context,
  595. const void *msg,
  596. unsigned int msg_len),
  597. void (*iface_change_fn) (
  598. void *context,
  599. const struct totem_ip_address *iface_address),
  600. void (*target_set_completed) (
  601. void *context))
  602. {
  603. struct totemudpu_instance *instance;
  604. instance = malloc (sizeof (struct totemudpu_instance));
  605. if (instance == NULL) {
  606. return (-1);
  607. }
  608. totemudpu_instance_initialize (instance);
  609. instance->totem_config = totem_config;
  610. /*
  611. * Configure logging
  612. */
  613. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  614. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  615. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  616. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  617. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  618. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  619. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  620. /*
  621. * Initialize random number generator for later use to generate salt
  622. */
  623. instance->crypto_inst = crypto_init (totem_config->private_key,
  624. totem_config->private_key_len,
  625. totem_config->crypto_cipher_type,
  626. totem_config->crypto_hash_type,
  627. instance->totemudpu_log_printf,
  628. instance->totemudpu_log_level_security,
  629. instance->totemudpu_log_level_notice,
  630. instance->totemudpu_log_level_error,
  631. instance->totemudpu_subsys_id);
  632. /*
  633. * Initialize local variables for totemudpu
  634. */
  635. instance->totem_interface = &totem_config->interfaces[interface_no];
  636. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  637. instance->totemudpu_poll_handle = poll_handle;
  638. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  639. instance->context = context;
  640. instance->totemudpu_deliver_fn = deliver_fn;
  641. instance->totemudpu_iface_change_fn = iface_change_fn;
  642. instance->totemudpu_target_set_completed = target_set_completed;
  643. totemip_localhost (AF_INET, &localhost);
  644. localhost.nodeid = instance->totem_config->node_id;
  645. /*
  646. * RRP layer isn't ready to receive message because it hasn't
  647. * initialized yet. Add short timer to check the interfaces.
  648. */
  649. qb_loop_timer_add (instance->totemudpu_poll_handle,
  650. QB_LOOP_MED,
  651. 100*QB_TIME_NS_IN_MSEC,
  652. (void *)instance,
  653. timer_function_netif_check_timeout,
  654. &instance->timer_netif_check_timeout);
  655. *udpu_context = instance;
  656. return (0);
  657. }
  658. void *totemudpu_buffer_alloc (void)
  659. {
  660. return malloc (FRAME_SIZE_MAX);
  661. }
  662. void totemudpu_buffer_release (void *ptr)
  663. {
  664. return free (ptr);
  665. }
  666. int totemudpu_processor_count_set (
  667. void *udpu_context,
  668. int processor_count)
  669. {
  670. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  671. int res = 0;
  672. instance->my_memb_entries = processor_count;
  673. qb_loop_timer_del (instance->totemudpu_poll_handle,
  674. instance->timer_netif_check_timeout);
  675. if (processor_count == 1) {
  676. qb_loop_timer_add (instance->totemudpu_poll_handle,
  677. QB_LOOP_MED,
  678. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  679. (void *)instance,
  680. timer_function_netif_check_timeout,
  681. &instance->timer_netif_check_timeout);
  682. }
  683. return (res);
  684. }
  685. int totemudpu_recv_flush (void *udpu_context)
  686. {
  687. int res = 0;
  688. return (res);
  689. }
  690. int totemudpu_send_flush (void *udpu_context)
  691. {
  692. int res = 0;
  693. return (res);
  694. }
  695. int totemudpu_token_send (
  696. void *udpu_context,
  697. const void *msg,
  698. unsigned int msg_len)
  699. {
  700. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  701. int res = 0;
  702. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  703. return (res);
  704. }
  705. int totemudpu_mcast_flush_send (
  706. void *udpu_context,
  707. const void *msg,
  708. unsigned int msg_len)
  709. {
  710. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  711. int res = 0;
  712. mcast_sendmsg (instance, msg, msg_len);
  713. return (res);
  714. }
  715. int totemudpu_mcast_noflush_send (
  716. void *udpu_context,
  717. const void *msg,
  718. unsigned int msg_len)
  719. {
  720. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  721. int res = 0;
  722. mcast_sendmsg (instance, msg, msg_len);
  723. return (res);
  724. }
  725. extern int totemudpu_iface_check (void *udpu_context)
  726. {
  727. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  728. int res = 0;
  729. timer_function_netif_check_timeout (instance);
  730. return (res);
  731. }
  732. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  733. {
  734. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  735. if (totem_config->secauth == 1) {
  736. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_hash_type) +
  737. UDPIP_HEADER_SIZE;
  738. } else {
  739. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  740. }
  741. }
  742. const char *totemudpu_iface_print (void *udpu_context) {
  743. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  744. const char *ret_char;
  745. ret_char = totemip_print (&instance->my_id);
  746. return (ret_char);
  747. }
  748. int totemudpu_iface_get (
  749. void *udpu_context,
  750. struct totem_ip_address *addr)
  751. {
  752. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  753. int res = 0;
  754. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  755. return (res);
  756. }
  757. int totemudpu_token_target_set (
  758. void *udpu_context,
  759. const struct totem_ip_address *token_target)
  760. {
  761. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  762. int res = 0;
  763. memcpy (&instance->token_target, token_target,
  764. sizeof (struct totem_ip_address));
  765. instance->totemudpu_target_set_completed (instance->context);
  766. return (res);
  767. }
  768. extern int totemudpu_recv_mcast_empty (
  769. void *udpu_context)
  770. {
  771. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  772. unsigned int res;
  773. struct sockaddr_storage system_from;
  774. struct msghdr msg_recv;
  775. struct pollfd ufd;
  776. int nfds;
  777. int msg_processed = 0;
  778. /*
  779. * Receive datagram
  780. */
  781. msg_recv.msg_name = &system_from;
  782. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  783. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  784. msg_recv.msg_iovlen = 1;
  785. #if !defined(COROSYNC_SOLARIS)
  786. msg_recv.msg_control = 0;
  787. msg_recv.msg_controllen = 0;
  788. msg_recv.msg_flags = 0;
  789. #else
  790. msg_recv.msg_accrights = NULL;
  791. msg_recv.msg_accrightslen = 0;
  792. #endif
  793. do {
  794. ufd.fd = instance->token_socket;
  795. ufd.events = POLLIN;
  796. nfds = poll (&ufd, 1, 0);
  797. if (nfds == 1 && ufd.revents & POLLIN) {
  798. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  799. if (res != -1) {
  800. msg_processed = 1;
  801. } else {
  802. msg_processed = -1;
  803. }
  804. }
  805. } while (nfds == 1);
  806. return (msg_processed);
  807. }
  808. int totemudpu_member_add (
  809. void *udpu_context,
  810. const struct totem_ip_address *member)
  811. {
  812. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  813. struct totemudpu_member *new_member;
  814. int res;
  815. unsigned int sendbuf_size;
  816. unsigned int optlen = sizeof (sendbuf_size);
  817. new_member = malloc (sizeof (struct totemudpu_member));
  818. if (new_member == NULL) {
  819. return (-1);
  820. }
  821. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  822. totemip_print(member));
  823. list_init (&new_member->list);
  824. list_add_tail (&new_member->list, &instance->member_list);
  825. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  826. new_member->fd = socket (member->family, SOCK_DGRAM, 0);
  827. if (new_member->fd == -1) {
  828. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  829. "Could not create socket for new member");
  830. return (-1);
  831. }
  832. totemip_nosigpipe (new_member->fd);
  833. res = fcntl (new_member->fd, F_SETFL, O_NONBLOCK);
  834. if (res == -1) {
  835. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  836. "Could not set non-blocking operation on token socket");
  837. return (-1);
  838. }
  839. /*
  840. * These sockets are used to send multicast messages, so their buffers
  841. * should be large
  842. */
  843. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  844. res = setsockopt (new_member->fd, SOL_SOCKET, SO_SNDBUF,
  845. &sendbuf_size, optlen);
  846. if (res == -1) {
  847. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  848. "Could not set sendbuf size");
  849. }
  850. return (0);
  851. }
  852. int totemudpu_member_remove (
  853. void *udpu_context,
  854. const struct totem_ip_address *token_target)
  855. {
  856. int found = 0;
  857. struct list_head *list;
  858. struct totemudpu_member *member;
  859. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  860. /*
  861. * Find the member to remove and close its socket
  862. */
  863. for (list = instance->member_list.next;
  864. list != &instance->member_list;
  865. list = list->next) {
  866. member = list_entry (list,
  867. struct totemudpu_member,
  868. list);
  869. if (totemip_compare (token_target, &member->member)==0) {
  870. log_printf(LOGSYS_LEVEL_NOTICE,
  871. "removing UDPU member {%s}",
  872. totemip_print(&member->member));
  873. if (member->fd > 0) {
  874. log_printf(LOGSYS_LEVEL_DEBUG,
  875. "Closing socket to: {%s}",
  876. totemip_print(&member->member));
  877. qb_loop_poll_del (instance->totemudpu_poll_handle,
  878. member->fd);
  879. close (member->fd);
  880. }
  881. found = 1;
  882. break;
  883. }
  884. }
  885. /*
  886. * Delete the member from the list
  887. */
  888. if (found) {
  889. list_del (list);
  890. }
  891. instance = NULL;
  892. return (0);
  893. }