totemudpu.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2018 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. #ifndef MSG_NOSIGNAL
  67. #define MSG_NOSIGNAL 0
  68. #endif
  69. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * UDP_RECEIVE_FRAME_SIZE_MAX)
  70. #define NETIF_STATE_REPORT_UP 1
  71. #define NETIF_STATE_REPORT_DOWN 2
  72. #define BIND_STATE_UNBOUND 0
  73. #define BIND_STATE_REGULAR 1
  74. #define BIND_STATE_LOOPBACK 2
  75. struct totemudpu_member {
  76. struct qb_list_head list;
  77. struct totem_ip_address member;
  78. int fd;
  79. int active;
  80. };
  81. struct totemudpu_instance {
  82. qb_loop_t *totemudpu_poll_handle;
  83. struct totem_interface *totem_interface;
  84. int netif_state_report;
  85. int netif_bind_state;
  86. void *context;
  87. int (*totemudpu_deliver_fn) (
  88. void *context,
  89. const void *msg,
  90. unsigned int msg_len,
  91. const struct sockaddr_storage *system_from);
  92. int (*totemudpu_iface_change_fn) (
  93. void *context,
  94. const struct totem_ip_address *iface_address,
  95. unsigned int ring_no);
  96. void (*totemudpu_target_set_completed) (void *context);
  97. /*
  98. * Function and data used to log messages
  99. */
  100. int totemudpu_log_level_security;
  101. int totemudpu_log_level_error;
  102. int totemudpu_log_level_warning;
  103. int totemudpu_log_level_notice;
  104. int totemudpu_log_level_debug;
  105. int totemudpu_subsys_id;
  106. void (*totemudpu_log_printf) (
  107. int level,
  108. int subsys,
  109. const char *function,
  110. const char *file,
  111. int line,
  112. const char *format,
  113. ...)__attribute__((format(printf, 6, 7)));
  114. void *udpu_context;
  115. char iov_buffer[UDP_RECEIVE_FRAME_SIZE_MAX];
  116. struct iovec totemudpu_iov_recv;
  117. struct qb_list_head member_list;
  118. int stats_sent;
  119. int stats_recv;
  120. int stats_delv;
  121. int stats_remcasts;
  122. int stats_orf_token;
  123. struct timeval stats_tv_start;
  124. struct totem_ip_address my_id;
  125. int firstrun;
  126. qb_loop_timer_handle timer_netif_check_timeout;
  127. unsigned int my_memb_entries;
  128. struct totem_config *totem_config;
  129. totemsrp_stats_t *stats;
  130. struct totem_ip_address token_target;
  131. int token_socket;
  132. int local_loop_sock[2];
  133. qb_loop_timer_handle timer_merge_detect_timeout;
  134. int send_merge_detect_message;
  135. unsigned int merge_detect_messages_sent_before_timeout;
  136. };
  137. struct work_item {
  138. const void *msg;
  139. unsigned int msg_len;
  140. struct totemudpu_instance *instance;
  141. };
  142. static int totemudpu_build_sockets (
  143. struct totemudpu_instance *instance,
  144. struct totem_ip_address *bindnet_address,
  145. struct totem_ip_address *bound_to);
  146. static int totemudpu_create_sending_socket(
  147. void *udpu_context,
  148. const struct totem_ip_address *member);
  149. int totemudpu_member_list_rebind_ip (
  150. void *udpu_context);
  151. static void totemudpu_start_merge_detect_timeout(
  152. void *udpu_context);
  153. static void totemudpu_stop_merge_detect_timeout(
  154. void *udpu_context);
  155. static void totemudpu_instance_initialize (struct totemudpu_instance *instance)
  156. {
  157. memset (instance, 0, sizeof (struct totemudpu_instance));
  158. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  159. instance->totemudpu_iov_recv.iov_base = instance->iov_buffer;
  160. instance->totemudpu_iov_recv.iov_len = UDP_RECEIVE_FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  161. /*
  162. * There is always atleast 1 processor
  163. */
  164. instance->my_memb_entries = 1;
  165. qb_list_init (&instance->member_list);
  166. }
  167. #define log_printf(level, format, args...) \
  168. do { \
  169. instance->totemudpu_log_printf ( \
  170. level, instance->totemudpu_subsys_id, \
  171. __FUNCTION__, __FILE__, __LINE__, \
  172. (const char *)format, ##args); \
  173. } while (0);
  174. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  175. do { \
  176. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  177. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  178. instance->totemudpu_log_printf ( \
  179. level, instance->totemudpu_subsys_id, \
  180. __FUNCTION__, __FILE__, __LINE__, \
  181. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  182. } while(0)
  183. int totemudpu_crypto_set (
  184. void *udpu_context,
  185. const char *cipher_type,
  186. const char *hash_type)
  187. {
  188. return (0);
  189. }
  190. static inline void ucast_sendmsg (
  191. struct totemudpu_instance *instance,
  192. struct totem_ip_address *system_to,
  193. const void *msg,
  194. unsigned int msg_len)
  195. {
  196. struct msghdr msg_ucast;
  197. int res = 0;
  198. struct sockaddr_storage sockaddr;
  199. struct iovec iovec;
  200. int addrlen;
  201. int send_sock;
  202. iovec.iov_base = (void *)msg;
  203. iovec.iov_len = msg_len;
  204. /*
  205. * Build unicast message
  206. */
  207. totemip_totemip_to_sockaddr_convert(system_to,
  208. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  209. memset(&msg_ucast, 0, sizeof(msg_ucast));
  210. msg_ucast.msg_name = &sockaddr;
  211. msg_ucast.msg_namelen = addrlen;
  212. msg_ucast.msg_iov = (void *)&iovec;
  213. msg_ucast.msg_iovlen = 1;
  214. #ifdef HAVE_MSGHDR_CONTROL
  215. msg_ucast.msg_control = 0;
  216. #endif
  217. #ifdef HAVE_MSGHDR_CONTROLLEN
  218. msg_ucast.msg_controllen = 0;
  219. #endif
  220. #ifdef HAVE_MSGHDR_FLAGS
  221. msg_ucast.msg_flags = 0;
  222. #endif
  223. #ifdef HAVE_MSGHDR_ACCRIGHTS
  224. msg_ucast.msg_accrights = NULL;
  225. #endif
  226. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  227. msg_ucast.msg_accrightslen = 0;
  228. #endif
  229. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  230. send_sock = instance->token_socket;
  231. } else {
  232. send_sock = instance->local_loop_sock[1];
  233. msg_ucast.msg_name = NULL;
  234. msg_ucast.msg_namelen = 0;
  235. }
  236. /*
  237. * Transmit unicast message
  238. * An error here is recovered by totemsrp
  239. */
  240. res = sendmsg (send_sock, &msg_ucast, MSG_NOSIGNAL);
  241. if (res < 0) {
  242. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  243. "sendmsg(ucast) failed (non-critical)");
  244. }
  245. }
  246. static inline void mcast_sendmsg (
  247. struct totemudpu_instance *instance,
  248. const void *msg,
  249. unsigned int msg_len,
  250. int only_active)
  251. {
  252. struct msghdr msg_mcast;
  253. int res = 0;
  254. struct iovec iovec;
  255. struct sockaddr_storage sockaddr;
  256. int addrlen;
  257. struct qb_list_head *list;
  258. struct totemudpu_member *member;
  259. iovec.iov_base = (void *)msg;
  260. iovec.iov_len = msg_len;
  261. memset(&msg_mcast, 0, sizeof(msg_mcast));
  262. /*
  263. * Build multicast message
  264. */
  265. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  266. qb_list_for_each(list, &(instance->member_list)) {
  267. member = qb_list_entry (list,
  268. struct totemudpu_member,
  269. list);
  270. /*
  271. * Do not send multicast message if message is not "flush", member
  272. * is inactive and timeout for sending merge message didn't expired.
  273. */
  274. if (only_active && !member->active && !instance->send_merge_detect_message)
  275. continue ;
  276. totemip_totemip_to_sockaddr_convert(&member->member,
  277. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  278. msg_mcast.msg_name = &sockaddr;
  279. msg_mcast.msg_namelen = addrlen;
  280. msg_mcast.msg_iov = (void *)&iovec;
  281. msg_mcast.msg_iovlen = 1;
  282. #ifdef HAVE_MSGHDR_CONTROL
  283. msg_mcast.msg_control = 0;
  284. #endif
  285. #ifdef HAVE_MSGHDR_CONTROLLEN
  286. msg_mcast.msg_controllen = 0;
  287. #endif
  288. #ifdef HAVE_MSGHDR_FLAGS
  289. msg_mcast.msg_flags = 0;
  290. #endif
  291. #ifdef HAVE_MSGHDR_ACCRIGHTS
  292. msg_mcast.msg_accrights = NULL;
  293. #endif
  294. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  295. msg_mcast.msg_accrightslen = 0;
  296. #endif
  297. /*
  298. * Transmit multicast message
  299. * An error here is recovered by totemsrp
  300. */
  301. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  302. if (res < 0) {
  303. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  304. "sendmsg(mcast) failed (non-critical)");
  305. }
  306. }
  307. if (!only_active || instance->send_merge_detect_message) {
  308. /*
  309. * Current message was sent to all nodes
  310. */
  311. instance->merge_detect_messages_sent_before_timeout++;
  312. instance->send_merge_detect_message = 0;
  313. }
  314. } else {
  315. /*
  316. * Transmit multicast message to local unix mcast loop
  317. * An error here is recovered by totemsrp
  318. */
  319. msg_mcast.msg_name = NULL;
  320. msg_mcast.msg_namelen = 0;
  321. msg_mcast.msg_iov = (void *)&iovec;
  322. msg_mcast.msg_iovlen = 1;
  323. #ifdef HAVE_MSGHDR_CONTROL
  324. msg_mcast.msg_control = 0;
  325. #endif
  326. #ifdef HAVE_MSGHDR_CONTROLLEN
  327. msg_mcast.msg_controllen = 0;
  328. #endif
  329. #ifdef HAVE_MSGHDR_FLAGS
  330. msg_mcast.msg_flags = 0;
  331. #endif
  332. #ifdef HAVE_MSGHDR_ACCRIGHTS
  333. msg_mcast.msg_accrights = NULL;
  334. #endif
  335. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  336. msg_mcast.msg_accrightslen = 0;
  337. #endif
  338. res = sendmsg (instance->local_loop_sock[1], &msg_mcast,
  339. MSG_NOSIGNAL);
  340. if (res < 0) {
  341. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  342. "sendmsg(local mcast loop) failed (non-critical)");
  343. }
  344. }
  345. }
  346. int totemudpu_finalize (
  347. void *udpu_context)
  348. {
  349. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  350. int res = 0;
  351. if (instance->token_socket > 0) {
  352. qb_loop_poll_del (instance->totemudpu_poll_handle,
  353. instance->token_socket);
  354. close (instance->token_socket);
  355. }
  356. if (instance->local_loop_sock[0] > 0) {
  357. qb_loop_poll_del (instance->totemudpu_poll_handle,
  358. instance->local_loop_sock[0]);
  359. close (instance->local_loop_sock[0]);
  360. close (instance->local_loop_sock[1]);
  361. }
  362. totemudpu_stop_merge_detect_timeout(instance);
  363. return (res);
  364. }
  365. static struct totemudpu_member *find_member_by_sockaddr(
  366. const void *udpu_context,
  367. const struct sockaddr *sa)
  368. {
  369. struct qb_list_head *list;
  370. struct totemudpu_member *member;
  371. struct totemudpu_member *res_member;
  372. const struct totemudpu_instance *instance = (const struct totemudpu_instance *)udpu_context;
  373. res_member = NULL;
  374. qb_list_for_each(list, &(instance->member_list)) {
  375. member = qb_list_entry (list,
  376. struct totemudpu_member,
  377. list);
  378. if (totemip_sa_equal(&member->member, sa)) {
  379. res_member = member;
  380. break ;
  381. }
  382. }
  383. return (res_member);
  384. }
  385. static int net_deliver_fn (
  386. int fd,
  387. int revents,
  388. void *data)
  389. {
  390. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  391. struct msghdr msg_recv;
  392. struct iovec *iovec;
  393. struct sockaddr_storage system_from;
  394. int bytes_received;
  395. int truncated_packet;
  396. iovec = &instance->totemudpu_iov_recv;
  397. /*
  398. * Receive datagram
  399. */
  400. msg_recv.msg_name = &system_from;
  401. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  402. msg_recv.msg_iov = iovec;
  403. msg_recv.msg_iovlen = 1;
  404. #ifdef HAVE_MSGHDR_CONTROL
  405. msg_recv.msg_control = 0;
  406. #endif
  407. #ifdef HAVE_MSGHDR_CONTROLLEN
  408. msg_recv.msg_controllen = 0;
  409. #endif
  410. #ifdef HAVE_MSGHDR_FLAGS
  411. msg_recv.msg_flags = 0;
  412. #endif
  413. #ifdef HAVE_MSGHDR_ACCRIGHTS
  414. msg_recv.msg_accrights = NULL;
  415. #endif
  416. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  417. msg_recv.msg_accrightslen = 0;
  418. #endif
  419. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  420. if (bytes_received == -1) {
  421. return (0);
  422. } else {
  423. instance->stats_recv += bytes_received;
  424. }
  425. truncated_packet = 0;
  426. #ifdef HAVE_MSGHDR_FLAGS
  427. if (msg_recv.msg_flags & MSG_TRUNC) {
  428. truncated_packet = 1;
  429. }
  430. #else
  431. /*
  432. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  433. * if bytes_received == UDP_RECEIVE_FRAME_SIZE_MAX then packet is truncated
  434. */
  435. if (bytes_received == UDP_RECEIVE_FRAME_SIZE_MAX) {
  436. truncated_packet = 1;
  437. }
  438. #endif
  439. if (truncated_packet) {
  440. log_printf (instance->totemudpu_log_level_error,
  441. "Received too big message. This may be because something bad is happening"
  442. "on the network (attack?), or you tried join more nodes than corosync is"
  443. "compiled with (%u) or bug in the code (bad estimation of "
  444. "the UDP_RECEIVE_FRAME_SIZE_MAX). Dropping packet.", PROCESSOR_COUNT_MAX);
  445. return (0);
  446. }
  447. if (instance->totem_config->block_unlisted_ips &&
  448. instance->netif_bind_state == BIND_STATE_REGULAR &&
  449. find_member_by_sockaddr(instance, (const struct sockaddr *)&system_from) == NULL) {
  450. log_printf(instance->totemudpu_log_level_debug, "Packet rejected from %s",
  451. totemip_sa_print((const struct sockaddr *)&system_from));
  452. return (0);
  453. }
  454. iovec->iov_len = bytes_received;
  455. /*
  456. * Handle incoming message
  457. */
  458. instance->totemudpu_deliver_fn (
  459. instance->context,
  460. iovec->iov_base,
  461. iovec->iov_len,
  462. &system_from);
  463. iovec->iov_len = UDP_RECEIVE_FRAME_SIZE_MAX;
  464. return (0);
  465. }
  466. static int netif_determine (
  467. struct totemudpu_instance *instance,
  468. struct totem_ip_address *bindnet,
  469. struct totem_ip_address *bound_to,
  470. int *interface_up,
  471. int *interface_num)
  472. {
  473. int res;
  474. res = totemip_iface_check (bindnet, bound_to,
  475. interface_up, interface_num,
  476. instance->totem_config->clear_node_high_bit);
  477. return (res);
  478. }
  479. /*
  480. * If the interface is up, the sockets for totem are built. If the interface is down
  481. * this function is requeued in the timer list to retry building the sockets later.
  482. */
  483. static void timer_function_netif_check_timeout (
  484. void *data)
  485. {
  486. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  487. int interface_up;
  488. int interface_num;
  489. /*
  490. * Build sockets for every interface
  491. */
  492. netif_determine (instance,
  493. &instance->totem_interface->bindnet,
  494. &instance->totem_interface->boundto,
  495. &interface_up, &interface_num);
  496. /*
  497. * If the network interface isn't back up and we are already
  498. * in loopback mode, add timer to check again and return
  499. */
  500. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  501. interface_up == 0) ||
  502. (instance->my_memb_entries == 1 &&
  503. instance->netif_bind_state == BIND_STATE_REGULAR &&
  504. interface_up == 1)) {
  505. qb_loop_timer_add (instance->totemudpu_poll_handle,
  506. QB_LOOP_MED,
  507. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  508. (void *)instance,
  509. timer_function_netif_check_timeout,
  510. &instance->timer_netif_check_timeout);
  511. /*
  512. * Add a timer to check for a downed regular interface
  513. */
  514. return;
  515. }
  516. if (instance->token_socket > 0) {
  517. qb_loop_poll_del (instance->totemudpu_poll_handle,
  518. instance->token_socket);
  519. close (instance->token_socket);
  520. instance->token_socket = -1;
  521. }
  522. if (interface_up == 0) {
  523. if (instance->netif_bind_state == BIND_STATE_UNBOUND) {
  524. log_printf (instance->totemudpu_log_level_error,
  525. "One of your ip addresses are now bound to localhost. "
  526. "Corosync would not work correctly.");
  527. exit(COROSYNC_DONE_FATAL_ERR);
  528. }
  529. /*
  530. * Interface is not up
  531. */
  532. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  533. /*
  534. * Add a timer to retry building interfaces and request memb_gather_enter
  535. */
  536. qb_loop_timer_add (instance->totemudpu_poll_handle,
  537. QB_LOOP_MED,
  538. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  539. (void *)instance,
  540. timer_function_netif_check_timeout,
  541. &instance->timer_netif_check_timeout);
  542. } else {
  543. /*
  544. * Interface is up
  545. */
  546. instance->netif_bind_state = BIND_STATE_REGULAR;
  547. }
  548. /*
  549. * Create and bind the multicast and unicast sockets
  550. */
  551. totemudpu_build_sockets (instance,
  552. &instance->totem_interface->bindnet,
  553. &instance->totem_interface->boundto);
  554. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  555. qb_loop_poll_add (instance->totemudpu_poll_handle,
  556. QB_LOOP_MED,
  557. instance->token_socket,
  558. POLLIN, instance, net_deliver_fn);
  559. }
  560. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  561. /*
  562. * This reports changes in the interface to the user and totemsrp
  563. */
  564. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  565. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  566. log_printf (instance->totemudpu_log_level_notice,
  567. "The network interface [%s] is now up.",
  568. totemip_print (&instance->totem_interface->boundto));
  569. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  570. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  571. }
  572. /*
  573. * Add a timer to check for interface going down in single membership
  574. */
  575. if (instance->my_memb_entries == 1) {
  576. qb_loop_timer_add (instance->totemudpu_poll_handle,
  577. QB_LOOP_MED,
  578. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  579. (void *)instance,
  580. timer_function_netif_check_timeout,
  581. &instance->timer_netif_check_timeout);
  582. }
  583. } else {
  584. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  585. log_printf (instance->totemudpu_log_level_notice,
  586. "The network interface is down.");
  587. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  588. }
  589. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  590. }
  591. }
  592. /* Set the socket priority to INTERACTIVE to ensure
  593. that our messages don't get queued behind anything else */
  594. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  595. {
  596. #ifdef SO_PRIORITY
  597. int prio = 6; /* TC_PRIO_INTERACTIVE */
  598. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  599. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  600. "Could not set traffic priority");
  601. }
  602. #endif
  603. }
  604. static int totemudpu_build_sockets_ip (
  605. struct totemudpu_instance *instance,
  606. struct totem_ip_address *bindnet_address,
  607. struct totem_ip_address *bound_to,
  608. int interface_num)
  609. {
  610. struct sockaddr_storage sockaddr;
  611. int addrlen;
  612. int res;
  613. unsigned int recvbuf_size;
  614. unsigned int optlen = sizeof (recvbuf_size);
  615. unsigned int retries = 0;
  616. /*
  617. * Setup unicast socket
  618. */
  619. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  620. if (instance->token_socket == -1) {
  621. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  622. "socket() failed");
  623. return (-1);
  624. }
  625. totemip_nosigpipe (instance->token_socket);
  626. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  627. if (res == -1) {
  628. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  629. "Could not set non-blocking operation on token socket");
  630. return (-1);
  631. }
  632. /*
  633. * Bind to unicast socket used for token send/receives
  634. * This has the side effect of binding to the correct interface
  635. */
  636. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  637. while (1) {
  638. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  639. if (res == 0) {
  640. break;
  641. }
  642. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  643. "bind token socket failed");
  644. if (++retries > BIND_MAX_RETRIES) {
  645. break;
  646. }
  647. /*
  648. * Wait for a while
  649. */
  650. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  651. }
  652. if (res == -1) {
  653. return (-1);
  654. }
  655. /*
  656. * the token_socket can receive many messages. Allow a large number
  657. * of receive messages on this socket
  658. */
  659. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  660. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  661. &recvbuf_size, optlen);
  662. if (res == -1) {
  663. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  664. "Could not set recvbuf size");
  665. }
  666. res = set_socket_dscp(instance->token_socket,
  667. instance->totem_config->ip_dscp);
  668. if (res == -1) {
  669. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  670. "Could not set IP_TOS bits");
  671. }
  672. return 0;
  673. }
  674. int totemudpu_nodestatus_get (void *udpu_context, unsigned int nodeid,
  675. struct totem_node_status *node_status)
  676. {
  677. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  678. struct qb_list_head *list;
  679. struct totemudpu_member *member;
  680. qb_list_for_each(list, &(instance->member_list)) {
  681. member = qb_list_entry (list,
  682. struct totemudpu_member,
  683. list);
  684. if (member->member.nodeid == nodeid) {
  685. node_status->nodeid = nodeid;
  686. /* reachable is filled in by totemsrp */
  687. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  688. node_status->link_status[0].enabled = 1;
  689. } else {
  690. node_status->link_status[0].enabled = 0;
  691. }
  692. node_status->link_status[0].connected = node_status->reachable;
  693. node_status->link_status[0].mtu = instance->totem_config->net_mtu;
  694. strncpy(node_status->link_status[0].src_ipaddr, totemip_print(&member->member), KNET_MAX_HOST_LEN-1);
  695. }
  696. }
  697. return (0);
  698. }
  699. int totemudpu_ifaces_get (
  700. void *net_context,
  701. char ***status,
  702. unsigned int *iface_count)
  703. {
  704. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  705. if (status) {
  706. *status = statuses;
  707. }
  708. *iface_count = 1;
  709. return (0);
  710. }
  711. static int totemudpu_build_local_sockets(
  712. struct totemudpu_instance *instance)
  713. {
  714. int i;
  715. unsigned int sendbuf_size;
  716. unsigned int recvbuf_size;
  717. unsigned int optlen = sizeof (sendbuf_size);
  718. int res;
  719. /*
  720. * Create local multicast loop socket
  721. */
  722. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, instance->local_loop_sock) == -1) {
  723. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  724. "socket() failed");
  725. return (-1);
  726. }
  727. for (i = 0; i < 2; i++) {
  728. totemip_nosigpipe (instance->local_loop_sock[i]);
  729. res = fcntl (instance->local_loop_sock[i], F_SETFL, O_NONBLOCK);
  730. if (res == -1) {
  731. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  732. "Could not set non-blocking operation on multicast socket");
  733. return (-1);
  734. }
  735. }
  736. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  737. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  738. res = setsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  739. if (res == -1) {
  740. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  741. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  742. return (-1);
  743. }
  744. res = setsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  745. if (res == -1) {
  746. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  747. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  748. return (-1);
  749. }
  750. res = getsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  751. if (res == 0) {
  752. log_printf (instance->totemudpu_log_level_debug,
  753. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  754. }
  755. res = getsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  756. if (res == 0) {
  757. log_printf (instance->totemudpu_log_level_debug,
  758. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  759. }
  760. return (0);
  761. }
  762. static int totemudpu_build_sockets (
  763. struct totemudpu_instance *instance,
  764. struct totem_ip_address *bindnet_address,
  765. struct totem_ip_address *bound_to)
  766. {
  767. int interface_num;
  768. int interface_up;
  769. int res;
  770. /*
  771. * Determine the ip address bound to and the interface name
  772. */
  773. res = netif_determine (instance,
  774. bindnet_address,
  775. bound_to,
  776. &interface_up,
  777. &interface_num);
  778. if (res == -1) {
  779. return (-1);
  780. }
  781. totemip_copy(&instance->my_id, bound_to);
  782. res = totemudpu_build_sockets_ip (instance,
  783. bindnet_address, bound_to, interface_num);
  784. if (res == -1) {
  785. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  786. LOGSYS_PERROR (errno, instance->totemudpu_log_level_error,
  787. "Unable to create sockets, exiting");
  788. exit(EXIT_FAILURE);
  789. }
  790. /* We only send out of the token socket */
  791. totemudpu_traffic_control_set(instance, instance->token_socket);
  792. /*
  793. * Rebind all members to new ips
  794. */
  795. totemudpu_member_list_rebind_ip(instance);
  796. return res;
  797. }
  798. /*
  799. * Totem Network interface
  800. * depends on poll abstraction, POSIX, IPV4
  801. */
  802. /*
  803. * Create an instance
  804. */
  805. int totemudpu_initialize (
  806. qb_loop_t *poll_handle,
  807. void **udpu_context,
  808. struct totem_config *totem_config,
  809. totemsrp_stats_t *stats,
  810. void *context,
  811. int (*deliver_fn) (
  812. void *context,
  813. const void *msg,
  814. unsigned int msg_len,
  815. const struct sockaddr_storage *system_from),
  816. int (*iface_change_fn) (
  817. void *context,
  818. const struct totem_ip_address *iface_address,
  819. unsigned int ring_no),
  820. void (*mtu_changed) (
  821. void *context,
  822. int net_mtu),
  823. void (*target_set_completed) (
  824. void *context))
  825. {
  826. struct totemudpu_instance *instance;
  827. instance = malloc (sizeof (struct totemudpu_instance));
  828. if (instance == NULL) {
  829. return (-1);
  830. }
  831. totemudpu_instance_initialize (instance);
  832. instance->totem_config = totem_config;
  833. instance->stats = stats;
  834. /*
  835. * Configure logging
  836. */
  837. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  838. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  839. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  840. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  841. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  842. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  843. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  844. /*
  845. * Initialize local variables for totemudpu
  846. */
  847. instance->totem_interface = &totem_config->interfaces[0];
  848. memset (instance->iov_buffer, 0, UDP_RECEIVE_FRAME_SIZE_MAX);
  849. instance->totemudpu_poll_handle = poll_handle;
  850. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  851. instance->context = context;
  852. instance->totemudpu_deliver_fn = deliver_fn;
  853. instance->totemudpu_iface_change_fn = iface_change_fn;
  854. instance->totemudpu_target_set_completed = target_set_completed;
  855. /*
  856. * Create static local mcast sockets
  857. */
  858. if (totemudpu_build_local_sockets(instance) == -1) {
  859. free(instance);
  860. return (-1);
  861. }
  862. qb_loop_poll_add (
  863. instance->totemudpu_poll_handle,
  864. QB_LOOP_MED,
  865. instance->local_loop_sock[0],
  866. POLLIN, instance, net_deliver_fn);
  867. /*
  868. * RRP layer isn't ready to receive message because it hasn't
  869. * initialized yet. Add short timer to check the interfaces.
  870. */
  871. qb_loop_timer_add (instance->totemudpu_poll_handle,
  872. QB_LOOP_MED,
  873. 100*QB_TIME_NS_IN_MSEC,
  874. (void *)instance,
  875. timer_function_netif_check_timeout,
  876. &instance->timer_netif_check_timeout);
  877. totemudpu_start_merge_detect_timeout((void*)instance);
  878. *udpu_context = instance;
  879. return (0);
  880. }
  881. void *totemudpu_buffer_alloc (void)
  882. {
  883. return malloc (FRAME_SIZE_MAX);
  884. }
  885. void totemudpu_buffer_release (void *ptr)
  886. {
  887. return free (ptr);
  888. }
  889. int totemudpu_processor_count_set (
  890. void *udpu_context,
  891. int processor_count)
  892. {
  893. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  894. int res = 0;
  895. instance->my_memb_entries = processor_count;
  896. qb_loop_timer_del (instance->totemudpu_poll_handle,
  897. instance->timer_netif_check_timeout);
  898. if (processor_count == 1) {
  899. qb_loop_timer_add (instance->totemudpu_poll_handle,
  900. QB_LOOP_MED,
  901. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  902. (void *)instance,
  903. timer_function_netif_check_timeout,
  904. &instance->timer_netif_check_timeout);
  905. }
  906. return (res);
  907. }
  908. int totemudpu_recv_flush (void *udpu_context)
  909. {
  910. int res = 0;
  911. return (res);
  912. }
  913. int totemudpu_send_flush (void *udpu_context)
  914. {
  915. int res = 0;
  916. return (res);
  917. }
  918. int totemudpu_token_send (
  919. void *udpu_context,
  920. const void *msg,
  921. unsigned int msg_len)
  922. {
  923. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  924. int res = 0;
  925. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  926. return (res);
  927. }
  928. int totemudpu_mcast_flush_send (
  929. void *udpu_context,
  930. const void *msg,
  931. unsigned int msg_len)
  932. {
  933. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  934. int res = 0;
  935. mcast_sendmsg (instance, msg, msg_len, 0);
  936. return (res);
  937. }
  938. int totemudpu_mcast_noflush_send (
  939. void *udpu_context,
  940. const void *msg,
  941. unsigned int msg_len)
  942. {
  943. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  944. int res = 0;
  945. mcast_sendmsg (instance, msg, msg_len, 1);
  946. return (res);
  947. }
  948. extern int totemudpu_iface_check (void *udpu_context)
  949. {
  950. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  951. int res = 0;
  952. timer_function_netif_check_timeout (instance);
  953. return (res);
  954. }
  955. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  956. {
  957. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  958. }
  959. int totemudpu_token_target_set (
  960. void *udpu_context,
  961. unsigned int nodeid)
  962. {
  963. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  964. struct qb_list_head *list;
  965. struct totemudpu_member *member;
  966. int res = 0;
  967. qb_list_for_each(list, &(instance->member_list)) {
  968. member = qb_list_entry (list,
  969. struct totemudpu_member,
  970. list);
  971. if (member->member.nodeid == nodeid) {
  972. memcpy (&instance->token_target, &member->member,
  973. sizeof (struct totem_ip_address));
  974. instance->totemudpu_target_set_completed (instance->context);
  975. break;
  976. }
  977. }
  978. return (res);
  979. }
  980. extern int totemudpu_recv_mcast_empty (
  981. void *udpu_context)
  982. {
  983. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  984. unsigned int res;
  985. struct sockaddr_storage system_from;
  986. struct msghdr msg_recv;
  987. struct pollfd ufd;
  988. int nfds, i;
  989. int msg_processed = 0;
  990. int sock;
  991. /*
  992. * Receive datagram
  993. */
  994. msg_recv.msg_name = &system_from;
  995. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  996. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  997. msg_recv.msg_iovlen = 1;
  998. #ifdef HAVE_MSGHDR_CONTROL
  999. msg_recv.msg_control = 0;
  1000. #endif
  1001. #ifdef HAVE_MSGHDR_CONTROLLEN
  1002. msg_recv.msg_controllen = 0;
  1003. #endif
  1004. #ifdef HAVE_MSGHDR_FLAGS
  1005. msg_recv.msg_flags = 0;
  1006. #endif
  1007. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1008. msg_recv.msg_accrights = NULL;
  1009. #endif
  1010. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1011. msg_recv.msg_accrightslen = 0;
  1012. #endif
  1013. for (i = 0; i < 2; i++) {
  1014. sock = -1;
  1015. if (i == 0) {
  1016. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1017. sock = instance->token_socket;
  1018. } else {
  1019. continue;
  1020. }
  1021. }
  1022. if (i == 1) {
  1023. sock = instance->local_loop_sock[0];
  1024. }
  1025. assert(sock != -1);
  1026. do {
  1027. ufd.fd = sock;
  1028. ufd.events = POLLIN;
  1029. nfds = poll (&ufd, 1, 0);
  1030. if (nfds == 1 && ufd.revents & POLLIN) {
  1031. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1032. if (res != -1) {
  1033. msg_processed = 1;
  1034. } else {
  1035. msg_processed = -1;
  1036. }
  1037. }
  1038. } while (nfds == 1);
  1039. }
  1040. return (msg_processed);
  1041. }
  1042. static int totemudpu_create_sending_socket(
  1043. void *udpu_context,
  1044. const struct totem_ip_address *member)
  1045. {
  1046. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1047. int fd;
  1048. int res;
  1049. unsigned int sendbuf_size;
  1050. unsigned int optlen = sizeof (sendbuf_size);
  1051. struct sockaddr_storage sockaddr;
  1052. int addrlen;
  1053. fd = socket (member->family, SOCK_DGRAM, 0);
  1054. if (fd == -1) {
  1055. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1056. "Could not create socket for new member");
  1057. return (-1);
  1058. }
  1059. totemip_nosigpipe (fd);
  1060. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  1061. if (res == -1) {
  1062. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1063. "Could not set non-blocking operation on token socket");
  1064. goto error_close_fd;
  1065. }
  1066. /*
  1067. * These sockets are used to send multicast messages, so their buffers
  1068. * should be large
  1069. */
  1070. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1071. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  1072. &sendbuf_size, optlen);
  1073. if (res == -1) {
  1074. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1075. "Could not set sendbuf size");
  1076. /*
  1077. * Fail in setting sendbuf size is not fatal -> don't exit
  1078. */
  1079. }
  1080. res = set_socket_dscp(fd, instance->totem_config->ip_dscp);
  1081. if (res == -1) {
  1082. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1083. "Could not set IP_TOS bits");
  1084. }
  1085. /*
  1086. * Bind to sending interface
  1087. */
  1088. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  1089. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  1090. if (res == -1) {
  1091. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1092. "bind token socket failed");
  1093. goto error_close_fd;
  1094. }
  1095. return (fd);
  1096. error_close_fd:
  1097. close(fd);
  1098. return (-1);
  1099. }
  1100. int totemudpu_iface_set (void *net_context,
  1101. const struct totem_ip_address *local_addr,
  1102. unsigned short ip_port,
  1103. unsigned int iface_no)
  1104. {
  1105. /* Not supported */
  1106. return (-1);
  1107. }
  1108. int totemudpu_member_add (
  1109. void *udpu_context,
  1110. const struct totem_ip_address *local,
  1111. const struct totem_ip_address *member,
  1112. int ring_no)
  1113. {
  1114. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1115. struct totemudpu_member *new_member;
  1116. new_member = malloc (sizeof (struct totemudpu_member));
  1117. if (new_member == NULL) {
  1118. return (-1);
  1119. }
  1120. memset(new_member, 0, sizeof(*new_member));
  1121. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  1122. totemip_print(member));
  1123. qb_list_init (&new_member->list);
  1124. qb_list_add_tail (&new_member->list, &instance->member_list);
  1125. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1126. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  1127. new_member->active = 1;
  1128. return (0);
  1129. }
  1130. int totemudpu_member_remove (
  1131. void *udpu_context,
  1132. const struct totem_ip_address *token_target,
  1133. int ring_no)
  1134. {
  1135. int found = 0;
  1136. struct qb_list_head *list;
  1137. struct totemudpu_member *member;
  1138. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1139. /*
  1140. * Find the member to remove and close its socket
  1141. */
  1142. qb_list_for_each(list, &(instance->member_list)) {
  1143. member = qb_list_entry (list,
  1144. struct totemudpu_member,
  1145. list);
  1146. if (totemip_compare (token_target, &member->member)==0) {
  1147. log_printf(LOGSYS_LEVEL_NOTICE,
  1148. "removing UDPU member {%s}",
  1149. totemip_print(&member->member));
  1150. if (member->fd > 0) {
  1151. log_printf(LOGSYS_LEVEL_DEBUG,
  1152. "Closing socket to: {%s}",
  1153. totemip_print(&member->member));
  1154. qb_loop_poll_del (instance->totemudpu_poll_handle,
  1155. member->fd);
  1156. close (member->fd);
  1157. }
  1158. found = 1;
  1159. break;
  1160. }
  1161. }
  1162. /*
  1163. * Delete the member from the list
  1164. */
  1165. if (found) {
  1166. qb_list_del (list);
  1167. }
  1168. instance = NULL;
  1169. return (0);
  1170. }
  1171. int totemudpu_member_list_rebind_ip (
  1172. void *udpu_context)
  1173. {
  1174. struct qb_list_head *list;
  1175. struct totemudpu_member *member;
  1176. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1177. qb_list_for_each(list, &(instance->member_list)) {
  1178. member = qb_list_entry (list,
  1179. struct totemudpu_member,
  1180. list);
  1181. if (member->fd > 0) {
  1182. close (member->fd);
  1183. }
  1184. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  1185. }
  1186. return (0);
  1187. }
  1188. static void timer_function_merge_detect_timeout (
  1189. void *data)
  1190. {
  1191. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  1192. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1193. instance->send_merge_detect_message = 1;
  1194. }
  1195. instance->merge_detect_messages_sent_before_timeout = 0;
  1196. totemudpu_start_merge_detect_timeout(instance);
  1197. }
  1198. static void totemudpu_start_merge_detect_timeout(
  1199. void *udpu_context)
  1200. {
  1201. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1202. qb_loop_timer_add(instance->totemudpu_poll_handle,
  1203. QB_LOOP_MED,
  1204. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1205. (void *)instance,
  1206. timer_function_merge_detect_timeout,
  1207. &instance->timer_merge_detect_timeout);
  1208. }
  1209. static void totemudpu_stop_merge_detect_timeout(
  1210. void *udpu_context)
  1211. {
  1212. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1213. qb_loop_timer_del(instance->totemudpu_poll_handle,
  1214. instance->timer_merge_detect_timeout);
  1215. }
  1216. int totemudpu_reconfigure (
  1217. void *udpu_context,
  1218. struct totem_config *totem_config)
  1219. {
  1220. /* Not supported */
  1221. return (-1);
  1222. }