totemudpu.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. /*
  192. * Encrypt and digest the message
  193. */
  194. if (crypto_encrypt_and_sign (
  195. instance->crypto_inst,
  196. (const unsigned char *)msg,
  197. msg_len,
  198. buf_out,
  199. &buf_out_len) != 0) {
  200. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  201. return;
  202. }
  203. iovec.iov_base = (void *)buf_out;
  204. iovec.iov_len = buf_out_len;
  205. /*
  206. * Build unicast message
  207. */
  208. totemip_totemip_to_sockaddr_convert(system_to,
  209. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  210. msg_ucast.msg_name = &sockaddr;
  211. msg_ucast.msg_namelen = addrlen;
  212. msg_ucast.msg_iov = (void *)&iovec;
  213. msg_ucast.msg_iovlen = 1;
  214. #if !defined(COROSYNC_SOLARIS)
  215. msg_ucast.msg_control = 0;
  216. msg_ucast.msg_controllen = 0;
  217. msg_ucast.msg_flags = 0;
  218. #else
  219. msg_ucast.msg_accrights = NULL;
  220. msg_ucast.msg_accrightslen = 0;
  221. #endif
  222. /*
  223. * Transmit unicast message
  224. * An error here is recovered by totemsrp
  225. */
  226. res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
  227. if (res < 0) {
  228. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  229. "sendmsg(ucast) failed (non-critical)");
  230. }
  231. }
  232. static inline void mcast_sendmsg (
  233. struct totemudpu_instance *instance,
  234. const void *msg,
  235. unsigned int msg_len)
  236. {
  237. struct msghdr msg_mcast;
  238. int res = 0;
  239. size_t buf_out_len;
  240. unsigned char buf_out[FRAME_SIZE_MAX];
  241. struct iovec iovec;
  242. struct sockaddr_storage sockaddr;
  243. int addrlen;
  244. struct list_head *list;
  245. struct totemudpu_member *member;
  246. /*
  247. * Encrypt and digest the message
  248. */
  249. if (crypto_encrypt_and_sign (
  250. instance->crypto_inst,
  251. (const unsigned char *)msg,
  252. msg_len,
  253. buf_out,
  254. &buf_out_len) != 0) {
  255. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  256. return;
  257. }
  258. iovec.iov_base = (void *)buf_out;
  259. iovec.iov_len = buf_out_len;
  260. /*
  261. * Build multicast message
  262. */
  263. for (list = instance->member_list.next;
  264. list != &instance->member_list;
  265. list = list->next) {
  266. member = list_entry (list,
  267. struct totemudpu_member,
  268. list);
  269. totemip_totemip_to_sockaddr_convert(&member->member,
  270. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  271. msg_mcast.msg_name = &sockaddr;
  272. msg_mcast.msg_namelen = addrlen;
  273. msg_mcast.msg_iov = (void *)&iovec;
  274. msg_mcast.msg_iovlen = 1;
  275. #if !defined(COROSYNC_SOLARIS)
  276. msg_mcast.msg_control = 0;
  277. msg_mcast.msg_controllen = 0;
  278. msg_mcast.msg_flags = 0;
  279. #else
  280. msg_mcast.msg_accrights = NULL;
  281. msg_mcast.msg_accrightslen = 0;
  282. #endif
  283. /*
  284. * Transmit multicast message
  285. * An error here is recovered by totemsrp
  286. */
  287. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  288. if (res < 0) {
  289. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  290. "sendmsg(mcast) failed (non-critical)");
  291. }
  292. }
  293. }
  294. int totemudpu_finalize (
  295. void *udpu_context)
  296. {
  297. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  298. int res = 0;
  299. if (instance->token_socket > 0) {
  300. close (instance->token_socket);
  301. qb_loop_poll_del (instance->totemudpu_poll_handle,
  302. instance->token_socket);
  303. }
  304. return (res);
  305. }
  306. static int net_deliver_fn (
  307. int fd,
  308. int revents,
  309. void *data)
  310. {
  311. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  312. struct msghdr msg_recv;
  313. struct iovec *iovec;
  314. struct sockaddr_storage system_from;
  315. int bytes_received;
  316. int res = 0;
  317. iovec = &instance->totemudpu_iov_recv;
  318. /*
  319. * Receive datagram
  320. */
  321. msg_recv.msg_name = &system_from;
  322. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  323. msg_recv.msg_iov = iovec;
  324. msg_recv.msg_iovlen = 1;
  325. #if !defined(COROSYNC_SOLARIS)
  326. msg_recv.msg_control = 0;
  327. msg_recv.msg_controllen = 0;
  328. msg_recv.msg_flags = 0;
  329. #else
  330. msg_recv.msg_accrights = NULL;
  331. msg_recv.msg_accrightslen = 0;
  332. #endif
  333. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  334. if (bytes_received == -1) {
  335. return (0);
  336. } else {
  337. instance->stats_recv += bytes_received;
  338. }
  339. /*
  340. * Authenticate and if authenticated, decrypt datagram
  341. */
  342. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  343. if (res == -1) {
  344. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.");
  345. log_printf (instance->totemudpu_log_level_security,
  346. "Invalid packet data");
  347. iovec->iov_len = FRAME_SIZE_MAX;
  348. return 0;
  349. }
  350. iovec->iov_len = bytes_received;
  351. /*
  352. * Handle incoming message
  353. */
  354. instance->totemudpu_deliver_fn (
  355. instance->context,
  356. iovec->iov_base,
  357. iovec->iov_len);
  358. iovec->iov_len = FRAME_SIZE_MAX;
  359. return (0);
  360. }
  361. static int netif_determine (
  362. struct totemudpu_instance *instance,
  363. struct totem_ip_address *bindnet,
  364. struct totem_ip_address *bound_to,
  365. int *interface_up,
  366. int *interface_num)
  367. {
  368. int res;
  369. res = totemip_iface_check (bindnet, bound_to,
  370. interface_up, interface_num,
  371. instance->totem_config->clear_node_high_bit);
  372. return (res);
  373. }
  374. /*
  375. * If the interface is up, the sockets for totem are built. If the interface is down
  376. * this function is requeued in the timer list to retry building the sockets later.
  377. */
  378. static void timer_function_netif_check_timeout (
  379. void *data)
  380. {
  381. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  382. int interface_up;
  383. int interface_num;
  384. struct totem_ip_address *bind_address;
  385. /*
  386. * Build sockets for every interface
  387. */
  388. netif_determine (instance,
  389. &instance->totem_interface->bindnet,
  390. &instance->totem_interface->boundto,
  391. &interface_up, &interface_num);
  392. /*
  393. * If the network interface isn't back up and we are already
  394. * in loopback mode, add timer to check again and return
  395. */
  396. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  397. interface_up == 0) ||
  398. (instance->my_memb_entries == 1 &&
  399. instance->netif_bind_state == BIND_STATE_REGULAR &&
  400. interface_up == 1)) {
  401. qb_loop_timer_add (instance->totemudpu_poll_handle,
  402. QB_LOOP_MED,
  403. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  404. (void *)instance,
  405. timer_function_netif_check_timeout,
  406. &instance->timer_netif_check_timeout);
  407. /*
  408. * Add a timer to check for a downed regular interface
  409. */
  410. return;
  411. }
  412. if (instance->token_socket > 0) {
  413. close (instance->token_socket);
  414. qb_loop_poll_del (instance->totemudpu_poll_handle,
  415. instance->token_socket);
  416. }
  417. if (interface_up == 0) {
  418. /*
  419. * Interface is not up
  420. */
  421. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  422. bind_address = &localhost;
  423. /*
  424. * Add a timer to retry building interfaces and request memb_gather_enter
  425. */
  426. qb_loop_timer_add (instance->totemudpu_poll_handle,
  427. QB_LOOP_MED,
  428. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  429. (void *)instance,
  430. timer_function_netif_check_timeout,
  431. &instance->timer_netif_check_timeout);
  432. } else {
  433. /*
  434. * Interface is up
  435. */
  436. instance->netif_bind_state = BIND_STATE_REGULAR;
  437. bind_address = &instance->totem_interface->bindnet;
  438. }
  439. /*
  440. * Create and bind the multicast and unicast sockets
  441. */
  442. totemudpu_build_sockets (instance,
  443. bind_address,
  444. &instance->totem_interface->boundto);
  445. qb_loop_poll_add (instance->totemudpu_poll_handle,
  446. QB_LOOP_MED,
  447. instance->token_socket,
  448. POLLIN, instance, net_deliver_fn);
  449. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  450. /*
  451. * This reports changes in the interface to the user and totemsrp
  452. */
  453. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  454. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  455. log_printf (instance->totemudpu_log_level_notice,
  456. "The network interface [%s] is now up.",
  457. totemip_print (&instance->totem_interface->boundto));
  458. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  459. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  460. }
  461. /*
  462. * Add a timer to check for interface going down in single membership
  463. */
  464. if (instance->my_memb_entries == 1) {
  465. qb_loop_timer_add (instance->totemudpu_poll_handle,
  466. QB_LOOP_MED,
  467. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  468. (void *)instance,
  469. timer_function_netif_check_timeout,
  470. &instance->timer_netif_check_timeout);
  471. }
  472. } else {
  473. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  474. log_printf (instance->totemudpu_log_level_notice,
  475. "The network interface is down.");
  476. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  477. }
  478. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  479. }
  480. }
  481. /* Set the socket priority to INTERACTIVE to ensure
  482. that our messages don't get queued behind anything else */
  483. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  484. {
  485. #ifdef SO_PRIORITY
  486. int prio = 6; /* TC_PRIO_INTERACTIVE */
  487. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  488. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  489. "Could not set traffic priority");
  490. }
  491. #endif
  492. }
  493. static int totemudpu_build_sockets_ip (
  494. struct totemudpu_instance *instance,
  495. struct totem_ip_address *bindnet_address,
  496. struct totem_ip_address *bound_to,
  497. int interface_num)
  498. {
  499. struct sockaddr_storage sockaddr;
  500. int addrlen;
  501. int res;
  502. unsigned int recvbuf_size;
  503. unsigned int optlen = sizeof (recvbuf_size);
  504. /*
  505. * Setup unicast socket
  506. */
  507. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  508. if (instance->token_socket == -1) {
  509. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  510. "socket() failed");
  511. return (-1);
  512. }
  513. totemip_nosigpipe (instance->token_socket);
  514. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  515. if (res == -1) {
  516. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  517. "Could not set non-blocking operation on token socket");
  518. return (-1);
  519. }
  520. /*
  521. * Bind to unicast socket used for token send/receives
  522. * This has the side effect of binding to the correct interface
  523. */
  524. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  525. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  526. if (res == -1) {
  527. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  528. "bind token socket failed");
  529. return (-1);
  530. }
  531. /*
  532. * the token_socket can receive many messages. Allow a large number
  533. * of receive messages on this socket
  534. */
  535. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  536. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  537. &recvbuf_size, optlen);
  538. if (res == -1) {
  539. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  540. "Could not set recvbuf size");
  541. }
  542. return 0;
  543. }
  544. static int totemudpu_build_sockets (
  545. struct totemudpu_instance *instance,
  546. struct totem_ip_address *bindnet_address,
  547. struct totem_ip_address *bound_to)
  548. {
  549. int interface_num;
  550. int interface_up;
  551. int res;
  552. /*
  553. * Determine the ip address bound to and the interface name
  554. */
  555. res = netif_determine (instance,
  556. bindnet_address,
  557. bound_to,
  558. &interface_up,
  559. &interface_num);
  560. if (res == -1) {
  561. return (-1);
  562. }
  563. totemip_copy(&instance->my_id, bound_to);
  564. res = totemudpu_build_sockets_ip (instance,
  565. bindnet_address, bound_to, interface_num);
  566. /* We only send out of the token socket */
  567. totemudpu_traffic_control_set(instance, instance->token_socket);
  568. return res;
  569. }
  570. /*
  571. * Totem Network interface - also does encryption/decryption
  572. * depends on poll abstraction, POSIX, IPV4
  573. */
  574. /*
  575. * Create an instance
  576. */
  577. int totemudpu_initialize (
  578. qb_loop_t *poll_handle,
  579. void **udpu_context,
  580. struct totem_config *totem_config,
  581. int interface_no,
  582. void *context,
  583. void (*deliver_fn) (
  584. void *context,
  585. const void *msg,
  586. unsigned int msg_len),
  587. void (*iface_change_fn) (
  588. void *context,
  589. const struct totem_ip_address *iface_address),
  590. void (*target_set_completed) (
  591. void *context))
  592. {
  593. struct totemudpu_instance *instance;
  594. instance = malloc (sizeof (struct totemudpu_instance));
  595. if (instance == NULL) {
  596. return (-1);
  597. }
  598. totemudpu_instance_initialize (instance);
  599. instance->totem_config = totem_config;
  600. /*
  601. * Configure logging
  602. */
  603. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  604. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  605. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  606. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  607. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  608. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  609. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  610. /*
  611. * Initialize random number generator for later use to generate salt
  612. */
  613. instance->crypto_inst = crypto_init (totem_config->private_key,
  614. totem_config->private_key_len,
  615. totem_config->crypto_cipher_type,
  616. totem_config->crypto_hash_type,
  617. instance->totemudpu_log_printf,
  618. instance->totemudpu_log_level_security,
  619. instance->totemudpu_log_level_notice,
  620. instance->totemudpu_log_level_error,
  621. instance->totemudpu_subsys_id);
  622. if (instance->crypto_inst == NULL) {
  623. return (-1);
  624. }
  625. /*
  626. * Initialize local variables for totemudpu
  627. */
  628. instance->totem_interface = &totem_config->interfaces[interface_no];
  629. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  630. instance->totemudpu_poll_handle = poll_handle;
  631. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  632. instance->context = context;
  633. instance->totemudpu_deliver_fn = deliver_fn;
  634. instance->totemudpu_iface_change_fn = iface_change_fn;
  635. instance->totemudpu_target_set_completed = target_set_completed;
  636. totemip_localhost (AF_INET, &localhost);
  637. localhost.nodeid = instance->totem_config->node_id;
  638. /*
  639. * RRP layer isn't ready to receive message because it hasn't
  640. * initialized yet. Add short timer to check the interfaces.
  641. */
  642. qb_loop_timer_add (instance->totemudpu_poll_handle,
  643. QB_LOOP_MED,
  644. 100*QB_TIME_NS_IN_MSEC,
  645. (void *)instance,
  646. timer_function_netif_check_timeout,
  647. &instance->timer_netif_check_timeout);
  648. *udpu_context = instance;
  649. return (0);
  650. }
  651. void *totemudpu_buffer_alloc (void)
  652. {
  653. return malloc (FRAME_SIZE_MAX);
  654. }
  655. void totemudpu_buffer_release (void *ptr)
  656. {
  657. return free (ptr);
  658. }
  659. int totemudpu_processor_count_set (
  660. void *udpu_context,
  661. int processor_count)
  662. {
  663. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  664. int res = 0;
  665. instance->my_memb_entries = processor_count;
  666. qb_loop_timer_del (instance->totemudpu_poll_handle,
  667. instance->timer_netif_check_timeout);
  668. if (processor_count == 1) {
  669. qb_loop_timer_add (instance->totemudpu_poll_handle,
  670. QB_LOOP_MED,
  671. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  672. (void *)instance,
  673. timer_function_netif_check_timeout,
  674. &instance->timer_netif_check_timeout);
  675. }
  676. return (res);
  677. }
  678. int totemudpu_recv_flush (void *udpu_context)
  679. {
  680. int res = 0;
  681. return (res);
  682. }
  683. int totemudpu_send_flush (void *udpu_context)
  684. {
  685. int res = 0;
  686. return (res);
  687. }
  688. int totemudpu_token_send (
  689. void *udpu_context,
  690. const void *msg,
  691. unsigned int msg_len)
  692. {
  693. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  694. int res = 0;
  695. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  696. return (res);
  697. }
  698. int totemudpu_mcast_flush_send (
  699. void *udpu_context,
  700. const void *msg,
  701. unsigned int msg_len)
  702. {
  703. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  704. int res = 0;
  705. mcast_sendmsg (instance, msg, msg_len);
  706. return (res);
  707. }
  708. int totemudpu_mcast_noflush_send (
  709. void *udpu_context,
  710. const void *msg,
  711. unsigned int msg_len)
  712. {
  713. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  714. int res = 0;
  715. mcast_sendmsg (instance, msg, msg_len);
  716. return (res);
  717. }
  718. extern int totemudpu_iface_check (void *udpu_context)
  719. {
  720. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  721. int res = 0;
  722. timer_function_netif_check_timeout (instance);
  723. return (res);
  724. }
  725. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  726. {
  727. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  728. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  729. totem_config->crypto_hash_type) +
  730. UDPIP_HEADER_SIZE;
  731. }
  732. const char *totemudpu_iface_print (void *udpu_context) {
  733. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  734. const char *ret_char;
  735. ret_char = totemip_print (&instance->my_id);
  736. return (ret_char);
  737. }
  738. int totemudpu_iface_get (
  739. void *udpu_context,
  740. struct totem_ip_address *addr)
  741. {
  742. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  743. int res = 0;
  744. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  745. return (res);
  746. }
  747. int totemudpu_token_target_set (
  748. void *udpu_context,
  749. const struct totem_ip_address *token_target)
  750. {
  751. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  752. int res = 0;
  753. memcpy (&instance->token_target, token_target,
  754. sizeof (struct totem_ip_address));
  755. instance->totemudpu_target_set_completed (instance->context);
  756. return (res);
  757. }
  758. extern int totemudpu_recv_mcast_empty (
  759. void *udpu_context)
  760. {
  761. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  762. unsigned int res;
  763. struct sockaddr_storage system_from;
  764. struct msghdr msg_recv;
  765. struct pollfd ufd;
  766. int nfds;
  767. int msg_processed = 0;
  768. /*
  769. * Receive datagram
  770. */
  771. msg_recv.msg_name = &system_from;
  772. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  773. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  774. msg_recv.msg_iovlen = 1;
  775. #if !defined(COROSYNC_SOLARIS)
  776. msg_recv.msg_control = 0;
  777. msg_recv.msg_controllen = 0;
  778. msg_recv.msg_flags = 0;
  779. #else
  780. msg_recv.msg_accrights = NULL;
  781. msg_recv.msg_accrightslen = 0;
  782. #endif
  783. do {
  784. ufd.fd = instance->token_socket;
  785. ufd.events = POLLIN;
  786. nfds = poll (&ufd, 1, 0);
  787. if (nfds == 1 && ufd.revents & POLLIN) {
  788. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  789. if (res != -1) {
  790. msg_processed = 1;
  791. } else {
  792. msg_processed = -1;
  793. }
  794. }
  795. } while (nfds == 1);
  796. return (msg_processed);
  797. }
  798. int totemudpu_member_add (
  799. void *udpu_context,
  800. const struct totem_ip_address *member)
  801. {
  802. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  803. struct totemudpu_member *new_member;
  804. int res;
  805. unsigned int sendbuf_size;
  806. unsigned int optlen = sizeof (sendbuf_size);
  807. new_member = malloc (sizeof (struct totemudpu_member));
  808. if (new_member == NULL) {
  809. return (-1);
  810. }
  811. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  812. totemip_print(member));
  813. list_init (&new_member->list);
  814. list_add_tail (&new_member->list, &instance->member_list);
  815. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  816. new_member->fd = socket (member->family, SOCK_DGRAM, 0);
  817. if (new_member->fd == -1) {
  818. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  819. "Could not create socket for new member");
  820. return (-1);
  821. }
  822. totemip_nosigpipe (new_member->fd);
  823. res = fcntl (new_member->fd, F_SETFL, O_NONBLOCK);
  824. if (res == -1) {
  825. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  826. "Could not set non-blocking operation on token socket");
  827. return (-1);
  828. }
  829. /*
  830. * These sockets are used to send multicast messages, so their buffers
  831. * should be large
  832. */
  833. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  834. res = setsockopt (new_member->fd, SOL_SOCKET, SO_SNDBUF,
  835. &sendbuf_size, optlen);
  836. if (res == -1) {
  837. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  838. "Could not set sendbuf size");
  839. }
  840. return (0);
  841. }
  842. int totemudpu_member_remove (
  843. void *udpu_context,
  844. const struct totem_ip_address *token_target)
  845. {
  846. int found = 0;
  847. struct list_head *list;
  848. struct totemudpu_member *member;
  849. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  850. /*
  851. * Find the member to remove and close its socket
  852. */
  853. for (list = instance->member_list.next;
  854. list != &instance->member_list;
  855. list = list->next) {
  856. member = list_entry (list,
  857. struct totemudpu_member,
  858. list);
  859. if (totemip_compare (token_target, &member->member)==0) {
  860. log_printf(LOGSYS_LEVEL_NOTICE,
  861. "removing UDPU member {%s}",
  862. totemip_print(&member->member));
  863. if (member->fd > 0) {
  864. log_printf(LOGSYS_LEVEL_DEBUG,
  865. "Closing socket to: {%s}",
  866. totemip_print(&member->member));
  867. qb_loop_poll_del (instance->totemudpu_poll_handle,
  868. member->fd);
  869. close (member->fd);
  870. }
  871. found = 1;
  872. break;
  873. }
  874. }
  875. /*
  876. * Delete the member from the list
  877. */
  878. if (found) {
  879. list_del (list);
  880. }
  881. instance = NULL;
  882. return (0);
  883. }