totemudpu.c 26 KB

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