totemudpu.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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/qbdefs.h>
  58. #include <qb/qbloop.h>
  59. #include <corosync/sq.h>
  60. #include <corosync/list.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 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 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. 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 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. for (list = instance->member_list.next;
  261. list != &instance->member_list;
  262. list = list->next) {
  263. member = list_entry (list,
  264. struct totemudpu_member,
  265. list);
  266. /*
  267. * Do not send multicast message if message is not "flush", member
  268. * is inactive and timeout for sending merge message didn't expired.
  269. */
  270. if (only_active && !member->active && !instance->send_merge_detect_message)
  271. continue ;
  272. totemip_totemip_to_sockaddr_convert(&member->member,
  273. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  274. msg_mcast.msg_name = &sockaddr;
  275. msg_mcast.msg_namelen = addrlen;
  276. msg_mcast.msg_iov = (void *)&iovec;
  277. msg_mcast.msg_iovlen = 1;
  278. #ifdef HAVE_MSGHDR_CONTROL
  279. msg_mcast.msg_control = 0;
  280. #endif
  281. #ifdef HAVE_MSGHDR_CONTROLLEN
  282. msg_mcast.msg_controllen = 0;
  283. #endif
  284. #ifdef HAVE_MSGHDR_FLAGS
  285. msg_mcast.msg_flags = 0;
  286. #endif
  287. #ifdef HAVE_MSGHDR_ACCRIGHTS
  288. msg_mcast.msg_accrights = NULL;
  289. #endif
  290. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  291. msg_mcast.msg_accrightslen = 0;
  292. #endif
  293. /*
  294. * Transmit multicast message
  295. * An error here is recovered by totemsrp
  296. */
  297. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  298. if (res < 0) {
  299. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  300. "sendmsg(mcast) failed (non-critical)");
  301. }
  302. }
  303. if (!only_active || instance->send_merge_detect_message) {
  304. /*
  305. * Current message was sent to all nodes
  306. */
  307. instance->merge_detect_messages_sent_before_timeout++;
  308. instance->send_merge_detect_message = 0;
  309. }
  310. }
  311. int totemudpu_finalize (
  312. void *udpu_context)
  313. {
  314. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  315. int res = 0;
  316. if (instance->token_socket > 0) {
  317. qb_loop_poll_del (instance->totemudpu_poll_handle,
  318. instance->token_socket);
  319. close (instance->token_socket);
  320. }
  321. totemudpu_stop_merge_detect_timeout(instance);
  322. return (res);
  323. }
  324. static int net_deliver_fn (
  325. int fd,
  326. int revents,
  327. void *data)
  328. {
  329. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  330. struct msghdr msg_recv;
  331. struct iovec *iovec;
  332. struct sockaddr_storage system_from;
  333. int bytes_received;
  334. iovec = &instance->totemudpu_iov_recv;
  335. /*
  336. * Receive datagram
  337. */
  338. msg_recv.msg_name = &system_from;
  339. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  340. msg_recv.msg_iov = iovec;
  341. msg_recv.msg_iovlen = 1;
  342. #ifdef HAVE_MSGHDR_CONTROL
  343. msg_recv.msg_control = 0;
  344. #endif
  345. #ifdef HAVE_MSGHDR_CONTROLLEN
  346. msg_recv.msg_controllen = 0;
  347. #endif
  348. #ifdef HAVE_MSGHDR_FLAGS
  349. msg_recv.msg_flags = 0;
  350. #endif
  351. #ifdef HAVE_MSGHDR_ACCRIGHTS
  352. msg_recv.msg_accrights = NULL;
  353. #endif
  354. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  355. msg_recv.msg_accrightslen = 0;
  356. #endif
  357. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  358. if (bytes_received == -1) {
  359. return (0);
  360. } else {
  361. instance->stats_recv += bytes_received;
  362. }
  363. iovec->iov_len = bytes_received;
  364. /*
  365. * Handle incoming message
  366. */
  367. instance->totemudpu_deliver_fn (
  368. instance->context,
  369. iovec->iov_base,
  370. iovec->iov_len);
  371. iovec->iov_len = FRAME_SIZE_MAX;
  372. return (0);
  373. }
  374. static int netif_determine (
  375. struct totemudpu_instance *instance,
  376. struct totem_ip_address *bindnet,
  377. struct totem_ip_address *bound_to,
  378. int *interface_up,
  379. int *interface_num)
  380. {
  381. int res;
  382. res = totemip_iface_check (bindnet, bound_to,
  383. interface_up, interface_num,
  384. instance->totem_config->clear_node_high_bit);
  385. return (res);
  386. }
  387. /*
  388. * If the interface is up, the sockets for totem are built. If the interface is down
  389. * this function is requeued in the timer list to retry building the sockets later.
  390. */
  391. static void timer_function_netif_check_timeout (
  392. void *data)
  393. {
  394. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  395. int interface_up;
  396. int interface_num;
  397. struct totem_ip_address *bind_address;
  398. /*
  399. * Build sockets for every interface
  400. */
  401. netif_determine (instance,
  402. &instance->totem_interface->bindnet,
  403. &instance->totem_interface->boundto,
  404. &interface_up, &interface_num);
  405. /*
  406. * If the network interface isn't back up and we are already
  407. * in loopback mode, add timer to check again and return
  408. */
  409. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  410. interface_up == 0) ||
  411. (instance->my_memb_entries == 1 &&
  412. instance->netif_bind_state == BIND_STATE_REGULAR &&
  413. interface_up == 1)) {
  414. qb_loop_timer_add (instance->totemudpu_poll_handle,
  415. QB_LOOP_MED,
  416. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  417. (void *)instance,
  418. timer_function_netif_check_timeout,
  419. &instance->timer_netif_check_timeout);
  420. /*
  421. * Add a timer to check for a downed regular interface
  422. */
  423. return;
  424. }
  425. if (instance->token_socket > 0) {
  426. qb_loop_poll_del (instance->totemudpu_poll_handle,
  427. instance->token_socket);
  428. close (instance->token_socket);
  429. }
  430. if (interface_up == 0) {
  431. /*
  432. * Interface is not up
  433. */
  434. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  435. bind_address = &localhost;
  436. /*
  437. * Add a timer to retry building interfaces and request memb_gather_enter
  438. */
  439. qb_loop_timer_add (instance->totemudpu_poll_handle,
  440. QB_LOOP_MED,
  441. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  442. (void *)instance,
  443. timer_function_netif_check_timeout,
  444. &instance->timer_netif_check_timeout);
  445. } else {
  446. /*
  447. * Interface is up
  448. */
  449. instance->netif_bind_state = BIND_STATE_REGULAR;
  450. bind_address = &instance->totem_interface->bindnet;
  451. }
  452. /*
  453. * Create and bind the multicast and unicast sockets
  454. */
  455. totemudpu_build_sockets (instance,
  456. bind_address,
  457. &instance->totem_interface->boundto);
  458. qb_loop_poll_add (instance->totemudpu_poll_handle,
  459. QB_LOOP_MED,
  460. instance->token_socket,
  461. POLLIN, instance, net_deliver_fn);
  462. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  463. /*
  464. * This reports changes in the interface to the user and totemsrp
  465. */
  466. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  467. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  468. log_printf (instance->totemudpu_log_level_notice,
  469. "The network interface [%s] is now up.",
  470. totemip_print (&instance->totem_interface->boundto));
  471. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  472. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  473. }
  474. /*
  475. * Add a timer to check for interface going down in single membership
  476. */
  477. if (instance->my_memb_entries == 1) {
  478. qb_loop_timer_add (instance->totemudpu_poll_handle,
  479. QB_LOOP_MED,
  480. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  481. (void *)instance,
  482. timer_function_netif_check_timeout,
  483. &instance->timer_netif_check_timeout);
  484. }
  485. } else {
  486. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  487. log_printf (instance->totemudpu_log_level_notice,
  488. "The network interface is down.");
  489. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  490. }
  491. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  492. }
  493. }
  494. /* Set the socket priority to INTERACTIVE to ensure
  495. that our messages don't get queued behind anything else */
  496. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  497. {
  498. #ifdef SO_PRIORITY
  499. int prio = 6; /* TC_PRIO_INTERACTIVE */
  500. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  501. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  502. "Could not set traffic priority");
  503. }
  504. #endif
  505. }
  506. static int totemudpu_build_sockets_ip (
  507. struct totemudpu_instance *instance,
  508. struct totem_ip_address *bindnet_address,
  509. struct totem_ip_address *bound_to,
  510. int interface_num)
  511. {
  512. struct sockaddr_storage sockaddr;
  513. int addrlen;
  514. int res;
  515. unsigned int recvbuf_size;
  516. unsigned int optlen = sizeof (recvbuf_size);
  517. /*
  518. * Setup unicast socket
  519. */
  520. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  521. if (instance->token_socket == -1) {
  522. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  523. "socket() failed");
  524. return (-1);
  525. }
  526. totemip_nosigpipe (instance->token_socket);
  527. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  528. if (res == -1) {
  529. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  530. "Could not set non-blocking operation on token socket");
  531. return (-1);
  532. }
  533. /*
  534. * Bind to unicast socket used for token send/receives
  535. * This has the side effect of binding to the correct interface
  536. */
  537. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  538. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  539. if (res == -1) {
  540. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  541. "bind token socket failed");
  542. return (-1);
  543. }
  544. /*
  545. * the token_socket can receive many messages. Allow a large number
  546. * of receive messages on this socket
  547. */
  548. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  549. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  550. &recvbuf_size, optlen);
  551. if (res == -1) {
  552. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  553. "Could not set recvbuf size");
  554. }
  555. return 0;
  556. }
  557. int totemudpu_ifaces_get (
  558. void *net_context,
  559. char ***status,
  560. unsigned int *iface_count)
  561. {
  562. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  563. if (status) {
  564. *status = statuses;
  565. }
  566. *iface_count = 1;
  567. return (0);
  568. }
  569. static int totemudpu_build_sockets (
  570. struct totemudpu_instance *instance,
  571. struct totem_ip_address *bindnet_address,
  572. struct totem_ip_address *bound_to)
  573. {
  574. int interface_num;
  575. int interface_up;
  576. int res;
  577. /*
  578. * Determine the ip address bound to and the interface name
  579. */
  580. res = netif_determine (instance,
  581. bindnet_address,
  582. bound_to,
  583. &interface_up,
  584. &interface_num);
  585. if (res == -1) {
  586. return (-1);
  587. }
  588. totemip_copy(&instance->my_id, bound_to);
  589. res = totemudpu_build_sockets_ip (instance,
  590. bindnet_address, bound_to, interface_num);
  591. /* We only send out of the token socket */
  592. totemudpu_traffic_control_set(instance, instance->token_socket);
  593. /*
  594. * Rebind all members to new ips
  595. */
  596. totemudpu_member_list_rebind_ip(instance);
  597. return res;
  598. }
  599. /*
  600. * Totem Network interface
  601. * depends on poll abstraction, POSIX, IPV4
  602. */
  603. /*
  604. * Create an instance
  605. */
  606. int totemudpu_initialize (
  607. qb_loop_t *poll_handle,
  608. void **udpu_context,
  609. struct totem_config *totem_config,
  610. totemsrp_stats_t *stats,
  611. void *context,
  612. void (*deliver_fn) (
  613. void *context,
  614. const void *msg,
  615. unsigned int msg_len),
  616. void (*iface_change_fn) (
  617. void *context,
  618. const struct totem_ip_address *iface_address,
  619. unsigned int ring_no),
  620. void (*mtu_changed) (
  621. void *context,
  622. int net_mtu),
  623. void (*target_set_completed) (
  624. void *context))
  625. {
  626. struct totemudpu_instance *instance;
  627. instance = malloc (sizeof (struct totemudpu_instance));
  628. if (instance == NULL) {
  629. return (-1);
  630. }
  631. totemudpu_instance_initialize (instance);
  632. instance->totem_config = totem_config;
  633. instance->stats = stats;
  634. /*
  635. * Configure logging
  636. */
  637. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  638. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  639. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  640. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  641. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  642. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  643. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  644. /*
  645. * Initialize local variables for totemudpu
  646. */
  647. instance->totem_interface = &totem_config->interfaces[0];
  648. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  649. instance->totemudpu_poll_handle = poll_handle;
  650. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  651. instance->context = context;
  652. instance->totemudpu_deliver_fn = deliver_fn;
  653. instance->totemudpu_iface_change_fn = iface_change_fn;
  654. instance->totemudpu_target_set_completed = target_set_completed;
  655. totemip_localhost (AF_INET, &localhost);
  656. localhost.nodeid = instance->totem_config->node_id;
  657. /*
  658. * RRP layer isn't ready to receive message because it hasn't
  659. * initialized yet. Add short timer to check the interfaces.
  660. */
  661. qb_loop_timer_add (instance->totemudpu_poll_handle,
  662. QB_LOOP_MED,
  663. 100*QB_TIME_NS_IN_MSEC,
  664. (void *)instance,
  665. timer_function_netif_check_timeout,
  666. &instance->timer_netif_check_timeout);
  667. totemudpu_start_merge_detect_timeout(instance);
  668. *udpu_context = instance;
  669. return (0);
  670. }
  671. void *totemudpu_buffer_alloc (void)
  672. {
  673. return malloc (FRAME_SIZE_MAX);
  674. }
  675. void totemudpu_buffer_release (void *ptr)
  676. {
  677. return free (ptr);
  678. }
  679. int totemudpu_processor_count_set (
  680. void *udpu_context,
  681. int processor_count)
  682. {
  683. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  684. int res = 0;
  685. instance->my_memb_entries = processor_count;
  686. qb_loop_timer_del (instance->totemudpu_poll_handle,
  687. instance->timer_netif_check_timeout);
  688. if (processor_count == 1) {
  689. qb_loop_timer_add (instance->totemudpu_poll_handle,
  690. QB_LOOP_MED,
  691. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  692. (void *)instance,
  693. timer_function_netif_check_timeout,
  694. &instance->timer_netif_check_timeout);
  695. }
  696. return (res);
  697. }
  698. int totemudpu_recv_flush (void *udpu_context)
  699. {
  700. int res = 0;
  701. return (res);
  702. }
  703. int totemudpu_send_flush (void *udpu_context)
  704. {
  705. int res = 0;
  706. return (res);
  707. }
  708. int totemudpu_token_send (
  709. void *udpu_context,
  710. const void *msg,
  711. unsigned int msg_len)
  712. {
  713. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  714. int res = 0;
  715. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  716. return (res);
  717. }
  718. int totemudpu_mcast_flush_send (
  719. void *udpu_context,
  720. const void *msg,
  721. unsigned int msg_len)
  722. {
  723. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  724. int res = 0;
  725. mcast_sendmsg (instance, msg, msg_len, 0);
  726. return (res);
  727. }
  728. int totemudpu_mcast_noflush_send (
  729. void *udpu_context,
  730. const void *msg,
  731. unsigned int msg_len)
  732. {
  733. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  734. int res = 0;
  735. mcast_sendmsg (instance, msg, msg_len, 1);
  736. return (res);
  737. }
  738. extern int totemudpu_iface_check (void *udpu_context)
  739. {
  740. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  741. int res = 0;
  742. timer_function_netif_check_timeout (instance);
  743. return (res);
  744. }
  745. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  746. {
  747. assert(totem_config->interface_count > 0);
  748. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  749. }
  750. int totemudpu_token_target_set (
  751. void *udpu_context,
  752. const struct totem_ip_address *token_target)
  753. {
  754. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  755. int res = 0;
  756. memcpy (&instance->token_target, token_target,
  757. sizeof (struct totem_ip_address));
  758. instance->totemudpu_target_set_completed (instance->context);
  759. return (res);
  760. }
  761. extern int totemudpu_recv_mcast_empty (
  762. void *udpu_context)
  763. {
  764. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  765. unsigned int res;
  766. struct sockaddr_storage system_from;
  767. struct msghdr msg_recv;
  768. struct pollfd ufd;
  769. int nfds;
  770. int msg_processed = 0;
  771. /*
  772. * Receive datagram
  773. */
  774. msg_recv.msg_name = &system_from;
  775. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  776. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  777. msg_recv.msg_iovlen = 1;
  778. #ifdef HAVE_MSGHDR_CONTROL
  779. msg_recv.msg_control = 0;
  780. #endif
  781. #ifdef HAVE_MSGHDR_CONTROLLEN
  782. msg_recv.msg_controllen = 0;
  783. #endif
  784. #ifdef HAVE_MSGHDR_FLAGS
  785. msg_recv.msg_flags = 0;
  786. #endif
  787. #ifdef HAVE_MSGHDR_ACCRIGHTS
  788. msg_recv.msg_accrights = NULL;
  789. #endif
  790. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  791. msg_recv.msg_accrightslen = 0;
  792. #endif
  793. do {
  794. ufd.fd = instance->token_socket;
  795. ufd.events = POLLIN;
  796. nfds = poll (&ufd, 1, 0);
  797. if (nfds == 1 && ufd.revents & POLLIN) {
  798. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  799. if (res != -1) {
  800. msg_processed = 1;
  801. } else {
  802. msg_processed = -1;
  803. }
  804. }
  805. } while (nfds == 1);
  806. return (msg_processed);
  807. }
  808. static int totemudpu_create_sending_socket(
  809. void *udpu_context,
  810. const struct totem_ip_address *member)
  811. {
  812. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  813. int fd;
  814. int res;
  815. unsigned int sendbuf_size;
  816. unsigned int optlen = sizeof (sendbuf_size);
  817. struct sockaddr_storage sockaddr;
  818. int addrlen;
  819. fd = socket (member->family, SOCK_DGRAM, 0);
  820. if (fd == -1) {
  821. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  822. "Could not create socket for new member");
  823. return (-1);
  824. }
  825. totemip_nosigpipe (fd);
  826. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  827. if (res == -1) {
  828. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  829. "Could not set non-blocking operation on token socket");
  830. goto error_close_fd;
  831. }
  832. /*
  833. * These sockets are used to send multicast messages, so their buffers
  834. * should be large
  835. */
  836. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  837. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  838. &sendbuf_size, optlen);
  839. if (res == -1) {
  840. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  841. "Could not set sendbuf size");
  842. /*
  843. * Fail in setting sendbuf size is not fatal -> don't exit
  844. */
  845. }
  846. /*
  847. * Bind to sending interface
  848. */
  849. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  850. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  851. if (res == -1) {
  852. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  853. "bind token socket failed");
  854. goto error_close_fd;
  855. }
  856. return (fd);
  857. error_close_fd:
  858. close(fd);
  859. return (-1);
  860. }
  861. int totemudpu_member_add (
  862. void *udpu_context,
  863. const struct totem_ip_address *local,
  864. const struct totem_ip_address *member,
  865. int ring_no)
  866. {
  867. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  868. struct totemudpu_member *new_member;
  869. new_member = malloc (sizeof (struct totemudpu_member));
  870. if (new_member == NULL) {
  871. return (-1);
  872. }
  873. memset(new_member, 0, sizeof(*new_member));
  874. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  875. totemip_print(member));
  876. list_init (&new_member->list);
  877. list_add_tail (&new_member->list, &instance->member_list);
  878. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  879. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  880. new_member->active = 1;
  881. return (0);
  882. }
  883. int totemudpu_member_remove (
  884. void *udpu_context,
  885. const struct totem_ip_address *token_target,
  886. int ring_no)
  887. {
  888. int found = 0;
  889. struct list_head *list;
  890. struct totemudpu_member *member;
  891. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  892. /*
  893. * Find the member to remove and close its socket
  894. */
  895. for (list = instance->member_list.next;
  896. list != &instance->member_list;
  897. list = list->next) {
  898. member = list_entry (list,
  899. struct totemudpu_member,
  900. list);
  901. if (totemip_compare (token_target, &member->member)==0) {
  902. log_printf(LOGSYS_LEVEL_NOTICE,
  903. "removing UDPU member {%s}",
  904. totemip_print(&member->member));
  905. if (member->fd > 0) {
  906. log_printf(LOGSYS_LEVEL_DEBUG,
  907. "Closing socket to: {%s}",
  908. totemip_print(&member->member));
  909. qb_loop_poll_del (instance->totemudpu_poll_handle,
  910. member->fd);
  911. close (member->fd);
  912. }
  913. found = 1;
  914. break;
  915. }
  916. }
  917. /*
  918. * Delete the member from the list
  919. */
  920. if (found) {
  921. list_del (list);
  922. }
  923. instance = NULL;
  924. return (0);
  925. }
  926. int totemudpu_member_list_rebind_ip (
  927. void *udpu_context)
  928. {
  929. struct list_head *list;
  930. struct totemudpu_member *member;
  931. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  932. for (list = instance->member_list.next;
  933. list != &instance->member_list;
  934. list = list->next) {
  935. member = list_entry (list,
  936. struct totemudpu_member,
  937. list);
  938. if (member->fd > 0) {
  939. close (member->fd);
  940. }
  941. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  942. }
  943. return (0);
  944. }
  945. static void timer_function_merge_detect_timeout (
  946. void *data)
  947. {
  948. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  949. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  950. instance->send_merge_detect_message = 1;
  951. }
  952. instance->merge_detect_messages_sent_before_timeout = 0;
  953. totemudpu_start_merge_detect_timeout(instance);
  954. }
  955. static void totemudpu_start_merge_detect_timeout(
  956. void *udpu_context)
  957. {
  958. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  959. qb_loop_timer_add(instance->totemudpu_poll_handle,
  960. QB_LOOP_MED,
  961. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  962. (void *)instance,
  963. timer_function_merge_detect_timeout,
  964. &instance->timer_merge_detect_timeout);
  965. }
  966. static void totemudpu_stop_merge_detect_timeout(
  967. void *udpu_context)
  968. {
  969. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  970. qb_loop_timer_del(instance->totemudpu_poll_handle,
  971. instance->timer_merge_detect_timeout);
  972. }