totemudpu.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 int totemudpu_create_sending_socket(
  144. void *udpu_context,
  145. const struct totem_ip_address *member);
  146. int totemudpu_member_list_rebind_ip (
  147. void *udpu_context);
  148. static struct totem_ip_address localhost;
  149. static void totemudpu_instance_initialize (struct totemudpu_instance *instance)
  150. {
  151. memset (instance, 0, sizeof (struct totemudpu_instance));
  152. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  153. instance->totemudpu_iov_recv.iov_base = instance->iov_buffer;
  154. instance->totemudpu_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  155. /*
  156. * There is always atleast 1 processor
  157. */
  158. instance->my_memb_entries = 1;
  159. list_init (&instance->member_list);
  160. }
  161. #define log_printf(level, format, args...) \
  162. do { \
  163. instance->totemudpu_log_printf ( \
  164. level, instance->totemudpu_subsys_id, \
  165. __FUNCTION__, __FILE__, __LINE__, \
  166. (const char *)format, ##args); \
  167. } while (0);
  168. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  169. do { \
  170. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  171. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  172. instance->totemudpu_log_printf ( \
  173. level, instance->totemudpu_subsys_id, \
  174. __FUNCTION__, __FILE__, __LINE__, \
  175. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  176. } while(0)
  177. int totemudpu_crypto_set (
  178. void *udpu_context,
  179. const char *cipher_type,
  180. const char *hash_type)
  181. {
  182. return (0);
  183. }
  184. static inline void ucast_sendmsg (
  185. struct totemudpu_instance *instance,
  186. struct totem_ip_address *system_to,
  187. const void *msg,
  188. unsigned int msg_len)
  189. {
  190. struct msghdr msg_ucast;
  191. int res = 0;
  192. size_t buf_out_len;
  193. unsigned char buf_out[FRAME_SIZE_MAX];
  194. struct sockaddr_storage sockaddr;
  195. struct iovec iovec;
  196. int addrlen;
  197. /*
  198. * Encrypt and digest the message
  199. */
  200. if (crypto_encrypt_and_sign (
  201. instance->crypto_inst,
  202. (const unsigned char *)msg,
  203. msg_len,
  204. buf_out,
  205. &buf_out_len) != 0) {
  206. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  207. return;
  208. }
  209. iovec.iov_base = (void *)buf_out;
  210. iovec.iov_len = buf_out_len;
  211. /*
  212. * Build unicast message
  213. */
  214. totemip_totemip_to_sockaddr_convert(system_to,
  215. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  216. msg_ucast.msg_name = &sockaddr;
  217. msg_ucast.msg_namelen = addrlen;
  218. msg_ucast.msg_iov = (void *)&iovec;
  219. msg_ucast.msg_iovlen = 1;
  220. #if !defined(COROSYNC_SOLARIS)
  221. msg_ucast.msg_control = 0;
  222. msg_ucast.msg_controllen = 0;
  223. msg_ucast.msg_flags = 0;
  224. #else
  225. msg_ucast.msg_accrights = NULL;
  226. msg_ucast.msg_accrightslen = 0;
  227. #endif
  228. /*
  229. * Transmit unicast message
  230. * An error here is recovered by totemsrp
  231. */
  232. res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
  233. if (res < 0) {
  234. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  235. "sendmsg(ucast) failed (non-critical)");
  236. }
  237. }
  238. static inline void mcast_sendmsg (
  239. struct totemudpu_instance *instance,
  240. const void *msg,
  241. unsigned int msg_len)
  242. {
  243. struct msghdr msg_mcast;
  244. int res = 0;
  245. size_t buf_out_len;
  246. unsigned char buf_out[FRAME_SIZE_MAX];
  247. struct iovec iovec;
  248. struct sockaddr_storage sockaddr;
  249. int addrlen;
  250. struct list_head *list;
  251. struct totemudpu_member *member;
  252. /*
  253. * Encrypt and digest the message
  254. */
  255. if (crypto_encrypt_and_sign (
  256. instance->crypto_inst,
  257. (const unsigned char *)msg,
  258. msg_len,
  259. buf_out,
  260. &buf_out_len) != 0) {
  261. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  262. return;
  263. }
  264. iovec.iov_base = (void *)buf_out;
  265. iovec.iov_len = buf_out_len;
  266. /*
  267. * Build multicast message
  268. */
  269. for (list = instance->member_list.next;
  270. list != &instance->member_list;
  271. list = list->next) {
  272. member = list_entry (list,
  273. struct totemudpu_member,
  274. list);
  275. totemip_totemip_to_sockaddr_convert(&member->member,
  276. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  277. msg_mcast.msg_name = &sockaddr;
  278. msg_mcast.msg_namelen = addrlen;
  279. msg_mcast.msg_iov = (void *)&iovec;
  280. msg_mcast.msg_iovlen = 1;
  281. #if !defined(COROSYNC_SOLARIS)
  282. msg_mcast.msg_control = 0;
  283. msg_mcast.msg_controllen = 0;
  284. msg_mcast.msg_flags = 0;
  285. #else
  286. msg_mcast.msg_accrights = NULL;
  287. msg_mcast.msg_accrightslen = 0;
  288. #endif
  289. /*
  290. * Transmit multicast message
  291. * An error here is recovered by totemsrp
  292. */
  293. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  294. if (res < 0) {
  295. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  296. "sendmsg(mcast) failed (non-critical)");
  297. }
  298. }
  299. }
  300. int totemudpu_finalize (
  301. void *udpu_context)
  302. {
  303. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  304. int res = 0;
  305. if (instance->token_socket > 0) {
  306. close (instance->token_socket);
  307. qb_loop_poll_del (instance->totemudpu_poll_handle,
  308. instance->token_socket);
  309. }
  310. return (res);
  311. }
  312. static int net_deliver_fn (
  313. int fd,
  314. int revents,
  315. void *data)
  316. {
  317. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  318. struct msghdr msg_recv;
  319. struct iovec *iovec;
  320. struct sockaddr_storage system_from;
  321. int bytes_received;
  322. int res = 0;
  323. iovec = &instance->totemudpu_iov_recv;
  324. /*
  325. * Receive datagram
  326. */
  327. msg_recv.msg_name = &system_from;
  328. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  329. msg_recv.msg_iov = iovec;
  330. msg_recv.msg_iovlen = 1;
  331. #if !defined(COROSYNC_SOLARIS)
  332. msg_recv.msg_control = 0;
  333. msg_recv.msg_controllen = 0;
  334. msg_recv.msg_flags = 0;
  335. #else
  336. msg_recv.msg_accrights = NULL;
  337. msg_recv.msg_accrightslen = 0;
  338. #endif
  339. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  340. if (bytes_received == -1) {
  341. return (0);
  342. } else {
  343. instance->stats_recv += bytes_received;
  344. }
  345. /*
  346. * Authenticate and if authenticated, decrypt datagram
  347. */
  348. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  349. if (res == -1) {
  350. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.");
  351. log_printf (instance->totemudpu_log_level_security,
  352. "Invalid packet data");
  353. iovec->iov_len = FRAME_SIZE_MAX;
  354. return 0;
  355. }
  356. iovec->iov_len = bytes_received;
  357. /*
  358. * Handle incoming message
  359. */
  360. instance->totemudpu_deliver_fn (
  361. instance->context,
  362. iovec->iov_base,
  363. iovec->iov_len);
  364. iovec->iov_len = FRAME_SIZE_MAX;
  365. return (0);
  366. }
  367. static int netif_determine (
  368. struct totemudpu_instance *instance,
  369. struct totem_ip_address *bindnet,
  370. struct totem_ip_address *bound_to,
  371. int *interface_up,
  372. int *interface_num)
  373. {
  374. int res;
  375. res = totemip_iface_check (bindnet, bound_to,
  376. interface_up, interface_num,
  377. instance->totem_config->clear_node_high_bit);
  378. return (res);
  379. }
  380. /*
  381. * If the interface is up, the sockets for totem are built. If the interface is down
  382. * this function is requeued in the timer list to retry building the sockets later.
  383. */
  384. static void timer_function_netif_check_timeout (
  385. void *data)
  386. {
  387. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  388. int interface_up;
  389. int interface_num;
  390. struct totem_ip_address *bind_address;
  391. /*
  392. * Build sockets for every interface
  393. */
  394. netif_determine (instance,
  395. &instance->totem_interface->bindnet,
  396. &instance->totem_interface->boundto,
  397. &interface_up, &interface_num);
  398. /*
  399. * If the network interface isn't back up and we are already
  400. * in loopback mode, add timer to check again and return
  401. */
  402. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  403. interface_up == 0) ||
  404. (instance->my_memb_entries == 1 &&
  405. instance->netif_bind_state == BIND_STATE_REGULAR &&
  406. interface_up == 1)) {
  407. qb_loop_timer_add (instance->totemudpu_poll_handle,
  408. QB_LOOP_MED,
  409. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  410. (void *)instance,
  411. timer_function_netif_check_timeout,
  412. &instance->timer_netif_check_timeout);
  413. /*
  414. * Add a timer to check for a downed regular interface
  415. */
  416. return;
  417. }
  418. if (instance->token_socket > 0) {
  419. close (instance->token_socket);
  420. qb_loop_poll_del (instance->totemudpu_poll_handle,
  421. instance->token_socket);
  422. }
  423. if (interface_up == 0) {
  424. /*
  425. * Interface is not up
  426. */
  427. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  428. bind_address = &localhost;
  429. /*
  430. * Add a timer to retry building interfaces and request memb_gather_enter
  431. */
  432. qb_loop_timer_add (instance->totemudpu_poll_handle,
  433. QB_LOOP_MED,
  434. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  435. (void *)instance,
  436. timer_function_netif_check_timeout,
  437. &instance->timer_netif_check_timeout);
  438. } else {
  439. /*
  440. * Interface is up
  441. */
  442. instance->netif_bind_state = BIND_STATE_REGULAR;
  443. bind_address = &instance->totem_interface->bindnet;
  444. }
  445. /*
  446. * Create and bind the multicast and unicast sockets
  447. */
  448. totemudpu_build_sockets (instance,
  449. bind_address,
  450. &instance->totem_interface->boundto);
  451. qb_loop_poll_add (instance->totemudpu_poll_handle,
  452. QB_LOOP_MED,
  453. instance->token_socket,
  454. POLLIN, instance, net_deliver_fn);
  455. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  456. /*
  457. * This reports changes in the interface to the user and totemsrp
  458. */
  459. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  460. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  461. log_printf (instance->totemudpu_log_level_notice,
  462. "The network interface [%s] is now up.",
  463. totemip_print (&instance->totem_interface->boundto));
  464. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  465. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  466. }
  467. /*
  468. * Add a timer to check for interface going down in single membership
  469. */
  470. if (instance->my_memb_entries == 1) {
  471. qb_loop_timer_add (instance->totemudpu_poll_handle,
  472. QB_LOOP_MED,
  473. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  474. (void *)instance,
  475. timer_function_netif_check_timeout,
  476. &instance->timer_netif_check_timeout);
  477. }
  478. } else {
  479. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  480. log_printf (instance->totemudpu_log_level_notice,
  481. "The network interface is down.");
  482. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  483. }
  484. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  485. }
  486. }
  487. /* Set the socket priority to INTERACTIVE to ensure
  488. that our messages don't get queued behind anything else */
  489. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  490. {
  491. #ifdef SO_PRIORITY
  492. int prio = 6; /* TC_PRIO_INTERACTIVE */
  493. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  494. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  495. "Could not set traffic priority");
  496. }
  497. #endif
  498. }
  499. static int totemudpu_build_sockets_ip (
  500. struct totemudpu_instance *instance,
  501. struct totem_ip_address *bindnet_address,
  502. struct totem_ip_address *bound_to,
  503. int interface_num)
  504. {
  505. struct sockaddr_storage sockaddr;
  506. int addrlen;
  507. int res;
  508. unsigned int recvbuf_size;
  509. unsigned int optlen = sizeof (recvbuf_size);
  510. /*
  511. * Setup unicast socket
  512. */
  513. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  514. if (instance->token_socket == -1) {
  515. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  516. "socket() failed");
  517. return (-1);
  518. }
  519. totemip_nosigpipe (instance->token_socket);
  520. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  521. if (res == -1) {
  522. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  523. "Could not set non-blocking operation on token socket");
  524. return (-1);
  525. }
  526. /*
  527. * Bind to unicast socket used for token send/receives
  528. * This has the side effect of binding to the correct interface
  529. */
  530. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  531. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  532. if (res == -1) {
  533. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  534. "bind token socket failed");
  535. return (-1);
  536. }
  537. /*
  538. * the token_socket can receive many messages. Allow a large number
  539. * of receive messages on this socket
  540. */
  541. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  542. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  543. &recvbuf_size, optlen);
  544. if (res == -1) {
  545. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  546. "Could not set recvbuf size");
  547. }
  548. return 0;
  549. }
  550. static int totemudpu_build_sockets (
  551. struct totemudpu_instance *instance,
  552. struct totem_ip_address *bindnet_address,
  553. struct totem_ip_address *bound_to)
  554. {
  555. int interface_num;
  556. int interface_up;
  557. int res;
  558. /*
  559. * Determine the ip address bound to and the interface name
  560. */
  561. res = netif_determine (instance,
  562. bindnet_address,
  563. bound_to,
  564. &interface_up,
  565. &interface_num);
  566. if (res == -1) {
  567. return (-1);
  568. }
  569. totemip_copy(&instance->my_id, bound_to);
  570. res = totemudpu_build_sockets_ip (instance,
  571. bindnet_address, bound_to, interface_num);
  572. /* We only send out of the token socket */
  573. totemudpu_traffic_control_set(instance, instance->token_socket);
  574. /*
  575. * Rebind all members to new ips
  576. */
  577. totemudpu_member_list_rebind_ip(instance);
  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. if (instance->crypto_inst == NULL) {
  633. return (-1);
  634. }
  635. /*
  636. * Initialize local variables for totemudpu
  637. */
  638. instance->totem_interface = &totem_config->interfaces[interface_no];
  639. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  640. instance->totemudpu_poll_handle = poll_handle;
  641. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  642. instance->context = context;
  643. instance->totemudpu_deliver_fn = deliver_fn;
  644. instance->totemudpu_iface_change_fn = iface_change_fn;
  645. instance->totemudpu_target_set_completed = target_set_completed;
  646. totemip_localhost (AF_INET, &localhost);
  647. localhost.nodeid = instance->totem_config->node_id;
  648. /*
  649. * RRP layer isn't ready to receive message because it hasn't
  650. * initialized yet. Add short timer to check the interfaces.
  651. */
  652. qb_loop_timer_add (instance->totemudpu_poll_handle,
  653. QB_LOOP_MED,
  654. 100*QB_TIME_NS_IN_MSEC,
  655. (void *)instance,
  656. timer_function_netif_check_timeout,
  657. &instance->timer_netif_check_timeout);
  658. *udpu_context = instance;
  659. return (0);
  660. }
  661. void *totemudpu_buffer_alloc (void)
  662. {
  663. return malloc (FRAME_SIZE_MAX);
  664. }
  665. void totemudpu_buffer_release (void *ptr)
  666. {
  667. return free (ptr);
  668. }
  669. int totemudpu_processor_count_set (
  670. void *udpu_context,
  671. int processor_count)
  672. {
  673. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  674. int res = 0;
  675. instance->my_memb_entries = processor_count;
  676. qb_loop_timer_del (instance->totemudpu_poll_handle,
  677. instance->timer_netif_check_timeout);
  678. if (processor_count == 1) {
  679. qb_loop_timer_add (instance->totemudpu_poll_handle,
  680. QB_LOOP_MED,
  681. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  682. (void *)instance,
  683. timer_function_netif_check_timeout,
  684. &instance->timer_netif_check_timeout);
  685. }
  686. return (res);
  687. }
  688. int totemudpu_recv_flush (void *udpu_context)
  689. {
  690. int res = 0;
  691. return (res);
  692. }
  693. int totemudpu_send_flush (void *udpu_context)
  694. {
  695. int res = 0;
  696. return (res);
  697. }
  698. int totemudpu_token_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. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  706. return (res);
  707. }
  708. int totemudpu_mcast_flush_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. int totemudpu_mcast_noflush_send (
  719. void *udpu_context,
  720. const void *msg,
  721. unsigned int msg_len)
  722. {
  723. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  724. int res = 0;
  725. mcast_sendmsg (instance, msg, msg_len);
  726. return (res);
  727. }
  728. extern int totemudpu_iface_check (void *udpu_context)
  729. {
  730. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  731. int res = 0;
  732. timer_function_netif_check_timeout (instance);
  733. return (res);
  734. }
  735. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  736. {
  737. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  738. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  739. totem_config->crypto_hash_type) +
  740. UDPIP_HEADER_SIZE;
  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. static int totemudpu_create_sending_socket(
  809. void *udpu_context,
  810. const struct totem_ip_address *member)
  811. {
  812. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  813. int fd;
  814. int res;
  815. unsigned int sendbuf_size;
  816. unsigned int optlen = sizeof (sendbuf_size);
  817. struct sockaddr_storage sockaddr;
  818. int addrlen;
  819. fd = socket (member->family, SOCK_DGRAM, 0);
  820. if (fd == -1) {
  821. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  822. "Could not create socket for new member");
  823. return (-1);
  824. }
  825. totemip_nosigpipe (fd);
  826. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  827. if (res == -1) {
  828. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  829. "Could not set non-blocking operation on token socket");
  830. return (-1);
  831. }
  832. /*
  833. * These sockets are used to send multicast messages, so their buffers
  834. * should be large
  835. */
  836. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  837. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  838. &sendbuf_size, optlen);
  839. if (res == -1) {
  840. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  841. "Could not set sendbuf size");
  842. }
  843. /*
  844. * Bind to sending interface
  845. */
  846. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  847. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  848. if (res == -1) {
  849. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  850. "bind token socket failed");
  851. return (-1);
  852. }
  853. return (fd);
  854. }
  855. int totemudpu_member_add (
  856. void *udpu_context,
  857. const struct totem_ip_address *member)
  858. {
  859. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  860. struct totemudpu_member *new_member;
  861. new_member = malloc (sizeof (struct totemudpu_member));
  862. if (new_member == NULL) {
  863. return (-1);
  864. }
  865. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  866. totemip_print(member));
  867. list_init (&new_member->list);
  868. list_add_tail (&new_member->list, &instance->member_list);
  869. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  870. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  871. return (0);
  872. }
  873. int totemudpu_member_remove (
  874. void *udpu_context,
  875. const struct totem_ip_address *token_target)
  876. {
  877. int found = 0;
  878. struct list_head *list;
  879. struct totemudpu_member *member;
  880. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  881. /*
  882. * Find the member to remove and close its socket
  883. */
  884. for (list = instance->member_list.next;
  885. list != &instance->member_list;
  886. list = list->next) {
  887. member = list_entry (list,
  888. struct totemudpu_member,
  889. list);
  890. if (totemip_compare (token_target, &member->member)==0) {
  891. log_printf(LOGSYS_LEVEL_NOTICE,
  892. "removing UDPU member {%s}",
  893. totemip_print(&member->member));
  894. if (member->fd > 0) {
  895. log_printf(LOGSYS_LEVEL_DEBUG,
  896. "Closing socket to: {%s}",
  897. totemip_print(&member->member));
  898. qb_loop_poll_del (instance->totemudpu_poll_handle,
  899. member->fd);
  900. close (member->fd);
  901. }
  902. found = 1;
  903. break;
  904. }
  905. }
  906. /*
  907. * Delete the member from the list
  908. */
  909. if (found) {
  910. list_del (list);
  911. }
  912. instance = NULL;
  913. return (0);
  914. }
  915. int totemudpu_member_list_rebind_ip (
  916. void *udpu_context)
  917. {
  918. struct list_head *list;
  919. struct totemudpu_member *member;
  920. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  921. for (list = instance->member_list.next;
  922. list != &instance->member_list;
  923. list = list->next) {
  924. member = list_entry (list,
  925. struct totemudpu_member,
  926. list);
  927. if (member->fd > 0) {
  928. close (member->fd);
  929. }
  930. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  931. }
  932. return (0);
  933. }