totemudpu.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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 <sys/uio.h>
  56. #include <limits.h>
  57. #include <qb/qblist.h>
  58. #include <qb/qbdefs.h>
  59. #include <qb/qbloop.h>
  60. #include <corosync/sq.h>
  61. #include <corosync/swab.h>
  62. #define LOGSYS_UTILS_ONLY 1
  63. #include <corosync/logsys.h>
  64. #include "totemudpu.h"
  65. #include "util.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 qb_list_head list;
  81. struct totem_ip_address member;
  82. int fd;
  83. int active;
  84. };
  85. struct totemudpu_instance {
  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. unsigned int ring_no);
  99. void (*totemudpu_target_set_completed) (void *context);
  100. /*
  101. * Function and data used to log messages
  102. */
  103. int totemudpu_log_level_security;
  104. int totemudpu_log_level_error;
  105. int totemudpu_log_level_warning;
  106. int totemudpu_log_level_notice;
  107. int totemudpu_log_level_debug;
  108. int totemudpu_subsys_id;
  109. void (*totemudpu_log_printf) (
  110. int level,
  111. int subsys,
  112. const char *function,
  113. const char *file,
  114. int line,
  115. const char *format,
  116. ...)__attribute__((format(printf, 6, 7)));
  117. void *udpu_context;
  118. char iov_buffer[FRAME_SIZE_MAX];
  119. struct iovec totemudpu_iov_recv;
  120. struct qb_list_head member_list;
  121. int stats_sent;
  122. int stats_recv;
  123. int stats_delv;
  124. int stats_remcasts;
  125. int stats_orf_token;
  126. struct timeval stats_tv_start;
  127. struct totem_ip_address my_id;
  128. int firstrun;
  129. qb_loop_timer_handle timer_netif_check_timeout;
  130. unsigned int my_memb_entries;
  131. struct totem_config *totem_config;
  132. totemsrp_stats_t *stats;
  133. struct totem_ip_address token_target;
  134. int token_socket;
  135. qb_loop_timer_handle timer_merge_detect_timeout;
  136. int send_merge_detect_message;
  137. unsigned int merge_detect_messages_sent_before_timeout;
  138. };
  139. struct work_item {
  140. const void *msg;
  141. unsigned int msg_len;
  142. struct totemudpu_instance *instance;
  143. };
  144. static int totemudpu_build_sockets (
  145. struct totemudpu_instance *instance,
  146. struct totem_ip_address *bindnet_address,
  147. struct totem_ip_address *bound_to);
  148. static int totemudpu_create_sending_socket(
  149. void *udpu_context,
  150. const struct totem_ip_address *member);
  151. int totemudpu_member_list_rebind_ip (
  152. void *udpu_context);
  153. static void totemudpu_start_merge_detect_timeout(
  154. void *udpu_context);
  155. static void totemudpu_stop_merge_detect_timeout(
  156. void *udpu_context);
  157. static struct totem_ip_address localhost;
  158. static void totemudpu_instance_initialize (struct totemudpu_instance *instance)
  159. {
  160. memset (instance, 0, sizeof (struct totemudpu_instance));
  161. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  162. instance->totemudpu_iov_recv.iov_base = instance->iov_buffer;
  163. instance->totemudpu_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  164. /*
  165. * There is always atleast 1 processor
  166. */
  167. instance->my_memb_entries = 1;
  168. qb_list_init (&instance->member_list);
  169. }
  170. #define log_printf(level, format, args...) \
  171. do { \
  172. instance->totemudpu_log_printf ( \
  173. level, instance->totemudpu_subsys_id, \
  174. __FUNCTION__, __FILE__, __LINE__, \
  175. (const char *)format, ##args); \
  176. } while (0);
  177. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  178. do { \
  179. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  180. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  181. instance->totemudpu_log_printf ( \
  182. level, instance->totemudpu_subsys_id, \
  183. __FUNCTION__, __FILE__, __LINE__, \
  184. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  185. } while(0)
  186. int totemudpu_crypto_set (
  187. void *udpu_context,
  188. const char *cipher_type,
  189. const char *hash_type)
  190. {
  191. return (0);
  192. }
  193. static inline void ucast_sendmsg (
  194. struct totemudpu_instance *instance,
  195. struct totem_ip_address *system_to,
  196. const void *msg,
  197. unsigned int msg_len)
  198. {
  199. struct msghdr msg_ucast;
  200. int res = 0;
  201. struct sockaddr_storage sockaddr;
  202. struct iovec iovec;
  203. int addrlen;
  204. iovec.iov_base = (void *)msg;
  205. iovec.iov_len = msg_len;
  206. /*
  207. * Build unicast message
  208. */
  209. totemip_totemip_to_sockaddr_convert(system_to,
  210. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  211. memset(&msg_ucast, 0, sizeof(msg_ucast));
  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. #ifdef HAVE_MSGHDR_CONTROL
  217. msg_ucast.msg_control = 0;
  218. #endif
  219. #ifdef HAVE_MSGHDR_CONTROLLEN
  220. msg_ucast.msg_controllen = 0;
  221. #endif
  222. #ifdef HAVE_MSGHDR_FLAGS
  223. msg_ucast.msg_flags = 0;
  224. #endif
  225. #ifdef HAVE_MSGHDR_ACCRIGHTS
  226. msg_ucast.msg_accrights = NULL;
  227. #endif
  228. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  229. msg_ucast.msg_accrightslen = 0;
  230. #endif
  231. /*
  232. * Transmit unicast message
  233. * An error here is recovered by totemsrp
  234. */
  235. res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
  236. if (res < 0) {
  237. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  238. "sendmsg(ucast) failed (non-critical)");
  239. }
  240. }
  241. static inline void mcast_sendmsg (
  242. struct totemudpu_instance *instance,
  243. const void *msg,
  244. unsigned int msg_len,
  245. int only_active)
  246. {
  247. struct msghdr msg_mcast;
  248. int res = 0;
  249. struct iovec iovec;
  250. struct sockaddr_storage sockaddr;
  251. int addrlen;
  252. struct qb_list_head *list;
  253. struct totemudpu_member *member;
  254. iovec.iov_base = (void *)msg;
  255. iovec.iov_len = msg_len;
  256. memset(&msg_mcast, 0, sizeof(msg_mcast));
  257. /*
  258. * Build multicast message
  259. */
  260. qb_list_for_each(list, &(instance->member_list)) {
  261. member = qb_list_entry (list,
  262. struct totemudpu_member,
  263. list);
  264. /*
  265. * Do not send multicast message if message is not "flush", member
  266. * is inactive and timeout for sending merge message didn't expired.
  267. */
  268. if (only_active && !member->active && !instance->send_merge_detect_message)
  269. continue ;
  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. #ifdef HAVE_MSGHDR_CONTROL
  277. msg_mcast.msg_control = 0;
  278. #endif
  279. #ifdef HAVE_MSGHDR_CONTROLLEN
  280. msg_mcast.msg_controllen = 0;
  281. #endif
  282. #ifdef HAVE_MSGHDR_FLAGS
  283. msg_mcast.msg_flags = 0;
  284. #endif
  285. #ifdef HAVE_MSGHDR_ACCRIGHTS
  286. msg_mcast.msg_accrights = NULL;
  287. #endif
  288. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  289. msg_mcast.msg_accrightslen = 0;
  290. #endif
  291. /*
  292. * Transmit multicast message
  293. * An error here is recovered by totemsrp
  294. */
  295. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  296. if (res < 0) {
  297. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  298. "sendmsg(mcast) failed (non-critical)");
  299. }
  300. }
  301. if (!only_active || instance->send_merge_detect_message) {
  302. /*
  303. * Current message was sent to all nodes
  304. */
  305. instance->merge_detect_messages_sent_before_timeout++;
  306. instance->send_merge_detect_message = 0;
  307. }
  308. }
  309. int totemudpu_finalize (
  310. void *udpu_context)
  311. {
  312. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  313. int res = 0;
  314. if (instance->token_socket > 0) {
  315. qb_loop_poll_del (instance->totemudpu_poll_handle,
  316. instance->token_socket);
  317. close (instance->token_socket);
  318. }
  319. totemudpu_stop_merge_detect_timeout(instance);
  320. return (res);
  321. }
  322. static int net_deliver_fn (
  323. int fd,
  324. int revents,
  325. void *data)
  326. {
  327. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  328. struct msghdr msg_recv;
  329. struct iovec *iovec;
  330. struct sockaddr_storage system_from;
  331. int bytes_received;
  332. iovec = &instance->totemudpu_iov_recv;
  333. /*
  334. * Receive datagram
  335. */
  336. msg_recv.msg_name = &system_from;
  337. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  338. msg_recv.msg_iov = iovec;
  339. msg_recv.msg_iovlen = 1;
  340. #ifdef HAVE_MSGHDR_CONTROL
  341. msg_recv.msg_control = 0;
  342. #endif
  343. #ifdef HAVE_MSGHDR_CONTROLLEN
  344. msg_recv.msg_controllen = 0;
  345. #endif
  346. #ifdef HAVE_MSGHDR_FLAGS
  347. msg_recv.msg_flags = 0;
  348. #endif
  349. #ifdef HAVE_MSGHDR_ACCRIGHTS
  350. msg_recv.msg_accrights = NULL;
  351. #endif
  352. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  353. msg_recv.msg_accrightslen = 0;
  354. #endif
  355. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  356. if (bytes_received == -1) {
  357. return (0);
  358. } else {
  359. instance->stats_recv += bytes_received;
  360. }
  361. iovec->iov_len = bytes_received;
  362. /*
  363. * Handle incoming message
  364. */
  365. instance->totemudpu_deliver_fn (
  366. instance->context,
  367. iovec->iov_base,
  368. iovec->iov_len);
  369. iovec->iov_len = FRAME_SIZE_MAX;
  370. return (0);
  371. }
  372. static int netif_determine (
  373. struct totemudpu_instance *instance,
  374. struct totem_ip_address *bindnet,
  375. struct totem_ip_address *bound_to,
  376. int *interface_up,
  377. int *interface_num)
  378. {
  379. int res;
  380. res = totemip_iface_check (bindnet, bound_to,
  381. interface_up, interface_num,
  382. instance->totem_config->clear_node_high_bit);
  383. return (res);
  384. }
  385. /*
  386. * If the interface is up, the sockets for totem are built. If the interface is down
  387. * this function is requeued in the timer list to retry building the sockets later.
  388. */
  389. static void timer_function_netif_check_timeout (
  390. void *data)
  391. {
  392. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  393. int interface_up;
  394. int interface_num;
  395. struct totem_ip_address *bind_address;
  396. /*
  397. * Build sockets for every interface
  398. */
  399. netif_determine (instance,
  400. &instance->totem_interface->bindnet,
  401. &instance->totem_interface->boundto,
  402. &interface_up, &interface_num);
  403. /*
  404. * If the network interface isn't back up and we are already
  405. * in loopback mode, add timer to check again and return
  406. */
  407. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  408. interface_up == 0) ||
  409. (instance->my_memb_entries == 1 &&
  410. instance->netif_bind_state == BIND_STATE_REGULAR &&
  411. interface_up == 1)) {
  412. qb_loop_timer_add (instance->totemudpu_poll_handle,
  413. QB_LOOP_MED,
  414. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  415. (void *)instance,
  416. timer_function_netif_check_timeout,
  417. &instance->timer_netif_check_timeout);
  418. /*
  419. * Add a timer to check for a downed regular interface
  420. */
  421. return;
  422. }
  423. if (instance->token_socket > 0) {
  424. qb_loop_poll_del (instance->totemudpu_poll_handle,
  425. instance->token_socket);
  426. close (instance->token_socket);
  427. }
  428. if (interface_up == 0) {
  429. /*
  430. * Interface is not up
  431. */
  432. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  433. bind_address = &localhost;
  434. /*
  435. * Add a timer to retry building interfaces and request memb_gather_enter
  436. */
  437. qb_loop_timer_add (instance->totemudpu_poll_handle,
  438. QB_LOOP_MED,
  439. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  440. (void *)instance,
  441. timer_function_netif_check_timeout,
  442. &instance->timer_netif_check_timeout);
  443. } else {
  444. /*
  445. * Interface is up
  446. */
  447. instance->netif_bind_state = BIND_STATE_REGULAR;
  448. bind_address = &instance->totem_interface->bindnet;
  449. }
  450. /*
  451. * Create and bind the multicast and unicast sockets
  452. */
  453. totemudpu_build_sockets (instance,
  454. bind_address,
  455. &instance->totem_interface->boundto);
  456. qb_loop_poll_add (instance->totemudpu_poll_handle,
  457. QB_LOOP_MED,
  458. instance->token_socket,
  459. POLLIN, instance, net_deliver_fn);
  460. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  461. /*
  462. * This reports changes in the interface to the user and totemsrp
  463. */
  464. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  465. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  466. log_printf (instance->totemudpu_log_level_notice,
  467. "The network interface [%s] is now up.",
  468. totemip_print (&instance->totem_interface->boundto));
  469. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  470. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  471. }
  472. /*
  473. * Add a timer to check for interface going down in single membership
  474. */
  475. if (instance->my_memb_entries == 1) {
  476. qb_loop_timer_add (instance->totemudpu_poll_handle,
  477. QB_LOOP_MED,
  478. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  479. (void *)instance,
  480. timer_function_netif_check_timeout,
  481. &instance->timer_netif_check_timeout);
  482. }
  483. } else {
  484. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  485. log_printf (instance->totemudpu_log_level_notice,
  486. "The network interface is down.");
  487. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  488. }
  489. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  490. }
  491. }
  492. /* Set the socket priority to INTERACTIVE to ensure
  493. that our messages don't get queued behind anything else */
  494. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  495. {
  496. #ifdef SO_PRIORITY
  497. int prio = 6; /* TC_PRIO_INTERACTIVE */
  498. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  499. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  500. "Could not set traffic priority");
  501. }
  502. #endif
  503. }
  504. static int totemudpu_build_sockets_ip (
  505. struct totemudpu_instance *instance,
  506. struct totem_ip_address *bindnet_address,
  507. struct totem_ip_address *bound_to,
  508. int interface_num)
  509. {
  510. struct sockaddr_storage sockaddr;
  511. int addrlen;
  512. int res;
  513. unsigned int recvbuf_size;
  514. unsigned int optlen = sizeof (recvbuf_size);
  515. /*
  516. * Setup unicast socket
  517. */
  518. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  519. if (instance->token_socket == -1) {
  520. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  521. "socket() failed");
  522. return (-1);
  523. }
  524. totemip_nosigpipe (instance->token_socket);
  525. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  526. if (res == -1) {
  527. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  528. "Could not set non-blocking operation on token socket");
  529. return (-1);
  530. }
  531. /*
  532. * Bind to unicast socket used for token send/receives
  533. * This has the side effect of binding to the correct interface
  534. */
  535. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  536. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  537. if (res == -1) {
  538. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  539. "bind token socket failed");
  540. return (-1);
  541. }
  542. /*
  543. * the token_socket can receive many messages. Allow a large number
  544. * of receive messages on this socket
  545. */
  546. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  547. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  548. &recvbuf_size, optlen);
  549. if (res == -1) {
  550. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  551. "Could not set recvbuf size");
  552. }
  553. return 0;
  554. }
  555. int totemudpu_ifaces_get (
  556. void *net_context,
  557. char ***status,
  558. unsigned int *iface_count)
  559. {
  560. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  561. if (status) {
  562. *status = statuses;
  563. }
  564. *iface_count = 1;
  565. return (0);
  566. }
  567. static int totemudpu_build_sockets (
  568. struct totemudpu_instance *instance,
  569. struct totem_ip_address *bindnet_address,
  570. struct totem_ip_address *bound_to)
  571. {
  572. int interface_num;
  573. int interface_up;
  574. int res;
  575. /*
  576. * Determine the ip address bound to and the interface name
  577. */
  578. res = netif_determine (instance,
  579. bindnet_address,
  580. bound_to,
  581. &interface_up,
  582. &interface_num);
  583. if (res == -1) {
  584. return (-1);
  585. }
  586. totemip_copy(&instance->my_id, bound_to);
  587. res = totemudpu_build_sockets_ip (instance,
  588. bindnet_address, bound_to, interface_num);
  589. /* We only send out of the token socket */
  590. totemudpu_traffic_control_set(instance, instance->token_socket);
  591. /*
  592. * Rebind all members to new ips
  593. */
  594. totemudpu_member_list_rebind_ip(instance);
  595. return res;
  596. }
  597. /*
  598. * Totem Network interface
  599. * depends on poll abstraction, POSIX, IPV4
  600. */
  601. /*
  602. * Create an instance
  603. */
  604. int totemudpu_initialize (
  605. qb_loop_t *poll_handle,
  606. void **udpu_context,
  607. struct totem_config *totem_config,
  608. totemsrp_stats_t *stats,
  609. void *context,
  610. void (*deliver_fn) (
  611. void *context,
  612. const void *msg,
  613. unsigned int msg_len),
  614. void (*iface_change_fn) (
  615. void *context,
  616. const struct totem_ip_address *iface_address,
  617. unsigned int ring_no),
  618. void (*mtu_changed) (
  619. void *context,
  620. int net_mtu),
  621. void (*target_set_completed) (
  622. void *context))
  623. {
  624. struct totemudpu_instance *instance;
  625. instance = malloc (sizeof (struct totemudpu_instance));
  626. if (instance == NULL) {
  627. return (-1);
  628. }
  629. totemudpu_instance_initialize (instance);
  630. instance->totem_config = totem_config;
  631. instance->stats = stats;
  632. /*
  633. * Configure logging
  634. */
  635. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  636. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  637. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  638. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  639. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  640. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  641. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  642. /*
  643. * Initialize local variables for totemudpu
  644. */
  645. instance->totem_interface = &totem_config->interfaces[0];
  646. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  647. instance->totemudpu_poll_handle = poll_handle;
  648. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  649. instance->context = context;
  650. instance->totemudpu_deliver_fn = deliver_fn;
  651. instance->totemudpu_iface_change_fn = iface_change_fn;
  652. instance->totemudpu_target_set_completed = target_set_completed;
  653. totemip_localhost (AF_INET, &localhost);
  654. localhost.nodeid = instance->totem_config->node_id;
  655. /*
  656. * RRP layer isn't ready to receive message because it hasn't
  657. * initialized yet. Add short timer to check the interfaces.
  658. */
  659. qb_loop_timer_add (instance->totemudpu_poll_handle,
  660. QB_LOOP_MED,
  661. 100*QB_TIME_NS_IN_MSEC,
  662. (void *)instance,
  663. timer_function_netif_check_timeout,
  664. &instance->timer_netif_check_timeout);
  665. totemudpu_start_merge_detect_timeout(instance);
  666. *udpu_context = instance;
  667. return (0);
  668. }
  669. void *totemudpu_buffer_alloc (void)
  670. {
  671. return malloc (FRAME_SIZE_MAX);
  672. }
  673. void totemudpu_buffer_release (void *ptr)
  674. {
  675. return free (ptr);
  676. }
  677. int totemudpu_processor_count_set (
  678. void *udpu_context,
  679. int processor_count)
  680. {
  681. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  682. int res = 0;
  683. instance->my_memb_entries = processor_count;
  684. qb_loop_timer_del (instance->totemudpu_poll_handle,
  685. instance->timer_netif_check_timeout);
  686. if (processor_count == 1) {
  687. qb_loop_timer_add (instance->totemudpu_poll_handle,
  688. QB_LOOP_MED,
  689. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  690. (void *)instance,
  691. timer_function_netif_check_timeout,
  692. &instance->timer_netif_check_timeout);
  693. }
  694. return (res);
  695. }
  696. int totemudpu_recv_flush (void *udpu_context)
  697. {
  698. int res = 0;
  699. return (res);
  700. }
  701. int totemudpu_send_flush (void *udpu_context)
  702. {
  703. int res = 0;
  704. return (res);
  705. }
  706. int totemudpu_token_send (
  707. void *udpu_context,
  708. const void *msg,
  709. unsigned int msg_len)
  710. {
  711. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  712. int res = 0;
  713. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  714. return (res);
  715. }
  716. int totemudpu_mcast_flush_send (
  717. void *udpu_context,
  718. const void *msg,
  719. unsigned int msg_len)
  720. {
  721. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  722. int res = 0;
  723. mcast_sendmsg (instance, msg, msg_len, 0);
  724. return (res);
  725. }
  726. int totemudpu_mcast_noflush_send (
  727. void *udpu_context,
  728. const void *msg,
  729. unsigned int msg_len)
  730. {
  731. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  732. int res = 0;
  733. mcast_sendmsg (instance, msg, msg_len, 1);
  734. return (res);
  735. }
  736. extern int totemudpu_iface_check (void *udpu_context)
  737. {
  738. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  739. int res = 0;
  740. timer_function_netif_check_timeout (instance);
  741. return (res);
  742. }
  743. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  744. {
  745. assert(totem_config->interface_count > 0);
  746. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  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. #ifdef HAVE_MSGHDR_CONTROL
  777. msg_recv.msg_control = 0;
  778. #endif
  779. #ifdef HAVE_MSGHDR_CONTROLLEN
  780. msg_recv.msg_controllen = 0;
  781. #endif
  782. #ifdef HAVE_MSGHDR_FLAGS
  783. msg_recv.msg_flags = 0;
  784. #endif
  785. #ifdef HAVE_MSGHDR_ACCRIGHTS
  786. msg_recv.msg_accrights = NULL;
  787. #endif
  788. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  789. msg_recv.msg_accrightslen = 0;
  790. #endif
  791. do {
  792. ufd.fd = instance->token_socket;
  793. ufd.events = POLLIN;
  794. nfds = poll (&ufd, 1, 0);
  795. if (nfds == 1 && ufd.revents & POLLIN) {
  796. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  797. if (res != -1) {
  798. msg_processed = 1;
  799. } else {
  800. msg_processed = -1;
  801. }
  802. }
  803. } while (nfds == 1);
  804. return (msg_processed);
  805. }
  806. static int totemudpu_create_sending_socket(
  807. void *udpu_context,
  808. const struct totem_ip_address *member)
  809. {
  810. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  811. int fd;
  812. int res;
  813. unsigned int sendbuf_size;
  814. unsigned int optlen = sizeof (sendbuf_size);
  815. struct sockaddr_storage sockaddr;
  816. int addrlen;
  817. fd = socket (member->family, SOCK_DGRAM, 0);
  818. if (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 (fd);
  824. res = fcntl (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. goto error_close_fd;
  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 (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. * Fail in setting sendbuf size is not fatal -> don't exit
  842. */
  843. }
  844. /*
  845. * Bind to sending interface
  846. */
  847. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  848. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  849. if (res == -1) {
  850. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  851. "bind token socket failed");
  852. goto error_close_fd;
  853. }
  854. return (fd);
  855. error_close_fd:
  856. close(fd);
  857. return (-1);
  858. }
  859. int totemudpu_member_add (
  860. void *udpu_context,
  861. const struct totem_ip_address *local,
  862. const struct totem_ip_address *member,
  863. int ring_no)
  864. {
  865. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  866. struct totemudpu_member *new_member;
  867. new_member = malloc (sizeof (struct totemudpu_member));
  868. if (new_member == NULL) {
  869. return (-1);
  870. }
  871. memset(new_member, 0, sizeof(*new_member));
  872. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  873. totemip_print(member));
  874. qb_list_init (&new_member->list);
  875. qb_list_add_tail (&new_member->list, &instance->member_list);
  876. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  877. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  878. new_member->active = 1;
  879. return (0);
  880. }
  881. int totemudpu_member_remove (
  882. void *udpu_context,
  883. const struct totem_ip_address *token_target,
  884. int ring_no)
  885. {
  886. int found = 0;
  887. struct qb_list_head *list;
  888. struct totemudpu_member *member;
  889. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  890. /*
  891. * Find the member to remove and close its socket
  892. */
  893. qb_list_for_each(list, &(instance->member_list)) {
  894. member = qb_list_entry (list,
  895. struct totemudpu_member,
  896. list);
  897. if (totemip_compare (token_target, &member->member)==0) {
  898. log_printf(LOGSYS_LEVEL_NOTICE,
  899. "removing UDPU member {%s}",
  900. totemip_print(&member->member));
  901. if (member->fd > 0) {
  902. log_printf(LOGSYS_LEVEL_DEBUG,
  903. "Closing socket to: {%s}",
  904. totemip_print(&member->member));
  905. qb_loop_poll_del (instance->totemudpu_poll_handle,
  906. member->fd);
  907. close (member->fd);
  908. }
  909. found = 1;
  910. break;
  911. }
  912. }
  913. /*
  914. * Delete the member from the list
  915. */
  916. if (found) {
  917. qb_list_del (list);
  918. }
  919. instance = NULL;
  920. return (0);
  921. }
  922. int totemudpu_member_list_rebind_ip (
  923. void *udpu_context)
  924. {
  925. struct qb_list_head *list;
  926. struct totemudpu_member *member;
  927. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  928. qb_list_for_each(list, &(instance->member_list)) {
  929. member = qb_list_entry (list,
  930. struct totemudpu_member,
  931. list);
  932. if (member->fd > 0) {
  933. close (member->fd);
  934. }
  935. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  936. }
  937. return (0);
  938. }
  939. static void timer_function_merge_detect_timeout (
  940. void *data)
  941. {
  942. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  943. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  944. instance->send_merge_detect_message = 1;
  945. }
  946. instance->merge_detect_messages_sent_before_timeout = 0;
  947. totemudpu_start_merge_detect_timeout(instance);
  948. }
  949. static void totemudpu_start_merge_detect_timeout(
  950. void *udpu_context)
  951. {
  952. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  953. qb_loop_timer_add(instance->totemudpu_poll_handle,
  954. QB_LOOP_MED,
  955. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  956. (void *)instance,
  957. timer_function_merge_detect_timeout,
  958. &instance->timer_merge_detect_timeout);
  959. }
  960. static void totemudpu_stop_merge_detect_timeout(
  961. void *udpu_context)
  962. {
  963. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  964. qb_loop_timer_del(instance->totemudpu_poll_handle,
  965. instance->timer_merge_detect_timeout);
  966. }