totemudpu.c 26 KB

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