totemudpu.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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 + 1];
  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 + 1; //sizeof (instance->iov_buffer) + 1;
  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. iovec = &instance->totemudpu_iov_recv;
  396. /*
  397. * Receive datagram
  398. */
  399. msg_recv.msg_name = &system_from;
  400. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  401. msg_recv.msg_iov = iovec;
  402. msg_recv.msg_iovlen = 1;
  403. #ifdef HAVE_MSGHDR_CONTROL
  404. msg_recv.msg_control = 0;
  405. #endif
  406. #ifdef HAVE_MSGHDR_CONTROLLEN
  407. msg_recv.msg_controllen = 0;
  408. #endif
  409. #ifdef HAVE_MSGHDR_FLAGS
  410. msg_recv.msg_flags = 0;
  411. #endif
  412. #ifdef HAVE_MSGHDR_ACCRIGHTS
  413. msg_recv.msg_accrights = NULL;
  414. #endif
  415. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  416. msg_recv.msg_accrightslen = 0;
  417. #endif
  418. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  419. if (bytes_received == -1) {
  420. return (0);
  421. } else {
  422. instance->stats_recv += bytes_received;
  423. }
  424. if (bytes_received >= UDP_RECEIVE_FRAME_SIZE_MAX + 1) {
  425. /*
  426. * Maximum packet size should be UDP_RECEIVE_FRAME_SIZE_MAX.
  427. * If received packet is UDP_RECEIVE_FRAME_SIZE_MAX + 1 it means packet was truncated
  428. * (iov_buffer size and iov_len are intentionally set to UDP_RECEIVE_FRAME_SIZE_MAX + 1).
  429. */
  430. log_printf (instance->totemudpu_log_level_error,
  431. "Received too big message. This may be because something bad is happening "
  432. "on the network (attack?), or you tried join more nodes than corosync is "
  433. "compiled with (%u) or bug in the code (bad estimation of "
  434. "the UDP_RECEIVE_FRAME_SIZE_MAX). Dropping packet.", PROCESSOR_COUNT_MAX);
  435. return (0);
  436. }
  437. if (instance->totem_config->block_unlisted_ips &&
  438. instance->netif_bind_state == BIND_STATE_REGULAR &&
  439. find_member_by_sockaddr(instance, (const struct sockaddr *)&system_from) == NULL) {
  440. log_printf(instance->totemudpu_log_level_debug, "Packet rejected from %s",
  441. totemip_sa_print((const struct sockaddr *)&system_from));
  442. return (0);
  443. }
  444. iovec->iov_len = bytes_received;
  445. /*
  446. * Handle incoming message
  447. */
  448. instance->totemudpu_deliver_fn (
  449. instance->context,
  450. iovec->iov_base,
  451. iovec->iov_len,
  452. &system_from);
  453. iovec->iov_len = UDP_RECEIVE_FRAME_SIZE_MAX + 1;
  454. return (0);
  455. }
  456. static int netif_determine (
  457. struct totemudpu_instance *instance,
  458. struct totem_ip_address *bindnet,
  459. struct totem_ip_address *bound_to,
  460. int *interface_up,
  461. int *interface_num)
  462. {
  463. int res;
  464. res = totemip_iface_check (bindnet, bound_to,
  465. interface_up, interface_num,
  466. instance->totem_config->clear_node_high_bit);
  467. return (res);
  468. }
  469. /*
  470. * If the interface is up, the sockets for totem are built. If the interface is down
  471. * this function is requeued in the timer list to retry building the sockets later.
  472. */
  473. static void timer_function_netif_check_timeout (
  474. void *data)
  475. {
  476. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  477. int interface_up;
  478. int interface_num;
  479. /*
  480. * Build sockets for every interface
  481. */
  482. netif_determine (instance,
  483. &instance->totem_interface->bindnet,
  484. &instance->totem_interface->boundto,
  485. &interface_up, &interface_num);
  486. /*
  487. * If the network interface isn't back up and we are already
  488. * in loopback mode, add timer to check again and return
  489. */
  490. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  491. interface_up == 0) ||
  492. (instance->my_memb_entries == 1 &&
  493. instance->netif_bind_state == BIND_STATE_REGULAR &&
  494. interface_up == 1)) {
  495. qb_loop_timer_add (instance->totemudpu_poll_handle,
  496. QB_LOOP_MED,
  497. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  498. (void *)instance,
  499. timer_function_netif_check_timeout,
  500. &instance->timer_netif_check_timeout);
  501. /*
  502. * Add a timer to check for a downed regular interface
  503. */
  504. return;
  505. }
  506. if (instance->token_socket > 0) {
  507. qb_loop_poll_del (instance->totemudpu_poll_handle,
  508. instance->token_socket);
  509. close (instance->token_socket);
  510. instance->token_socket = -1;
  511. }
  512. if (interface_up == 0) {
  513. if (instance->netif_bind_state == BIND_STATE_UNBOUND) {
  514. log_printf (instance->totemudpu_log_level_error,
  515. "One of your ip addresses are now bound to localhost. "
  516. "Corosync would not work correctly.");
  517. exit(COROSYNC_DONE_FATAL_ERR);
  518. }
  519. /*
  520. * Interface is not up
  521. */
  522. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  523. /*
  524. * Add a timer to retry building interfaces and request memb_gather_enter
  525. */
  526. qb_loop_timer_add (instance->totemudpu_poll_handle,
  527. QB_LOOP_MED,
  528. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  529. (void *)instance,
  530. timer_function_netif_check_timeout,
  531. &instance->timer_netif_check_timeout);
  532. } else {
  533. /*
  534. * Interface is up
  535. */
  536. instance->netif_bind_state = BIND_STATE_REGULAR;
  537. }
  538. /*
  539. * Create and bind the multicast and unicast sockets
  540. */
  541. totemudpu_build_sockets (instance,
  542. &instance->totem_interface->bindnet,
  543. &instance->totem_interface->boundto);
  544. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  545. qb_loop_poll_add (instance->totemudpu_poll_handle,
  546. QB_LOOP_MED,
  547. instance->token_socket,
  548. POLLIN, instance, net_deliver_fn);
  549. }
  550. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  551. /*
  552. * This reports changes in the interface to the user and totemsrp
  553. */
  554. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  555. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  556. log_printf (instance->totemudpu_log_level_notice,
  557. "The network interface [%s] is now up.",
  558. totemip_print (&instance->totem_interface->boundto));
  559. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  560. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  561. }
  562. /*
  563. * Add a timer to check for interface going down in single membership
  564. */
  565. if (instance->my_memb_entries == 1) {
  566. qb_loop_timer_add (instance->totemudpu_poll_handle,
  567. QB_LOOP_MED,
  568. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  569. (void *)instance,
  570. timer_function_netif_check_timeout,
  571. &instance->timer_netif_check_timeout);
  572. }
  573. } else {
  574. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  575. log_printf (instance->totemudpu_log_level_notice,
  576. "The network interface is down.");
  577. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  578. }
  579. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  580. }
  581. }
  582. /* Set the socket priority to INTERACTIVE to ensure
  583. that our messages don't get queued behind anything else */
  584. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  585. {
  586. #ifdef SO_PRIORITY
  587. int prio = 6; /* TC_PRIO_INTERACTIVE */
  588. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  589. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  590. "Could not set traffic priority");
  591. }
  592. #endif
  593. }
  594. static int totemudpu_build_sockets_ip (
  595. struct totemudpu_instance *instance,
  596. struct totem_ip_address *bindnet_address,
  597. struct totem_ip_address *bound_to,
  598. int interface_num)
  599. {
  600. struct sockaddr_storage sockaddr;
  601. int addrlen;
  602. int res;
  603. unsigned int recvbuf_size;
  604. unsigned int optlen = sizeof (recvbuf_size);
  605. unsigned int retries = 0;
  606. /*
  607. * Setup unicast socket
  608. */
  609. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  610. if (instance->token_socket == -1) {
  611. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  612. "socket() failed");
  613. return (-1);
  614. }
  615. totemip_nosigpipe (instance->token_socket);
  616. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  617. if (res == -1) {
  618. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  619. "Could not set non-blocking operation on token socket");
  620. return (-1);
  621. }
  622. /*
  623. * Bind to unicast socket used for token send/receives
  624. * This has the side effect of binding to the correct interface
  625. */
  626. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  627. while (1) {
  628. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  629. if (res == 0) {
  630. break;
  631. }
  632. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  633. "bind token socket failed");
  634. if (++retries > BIND_MAX_RETRIES) {
  635. break;
  636. }
  637. /*
  638. * Wait for a while
  639. */
  640. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  641. }
  642. if (res == -1) {
  643. return (-1);
  644. }
  645. /*
  646. * the token_socket can receive many messages. Allow a large number
  647. * of receive messages on this socket
  648. */
  649. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  650. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  651. &recvbuf_size, optlen);
  652. if (res == -1) {
  653. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  654. "Could not set recvbuf size");
  655. }
  656. res = set_socket_dscp(instance->token_socket,
  657. instance->totem_config->ip_dscp);
  658. if (res == -1) {
  659. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  660. "Could not set IP_TOS bits");
  661. }
  662. return 0;
  663. }
  664. int totemudpu_nodestatus_get (void *udpu_context, unsigned int nodeid,
  665. struct totem_node_status *node_status)
  666. {
  667. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  668. struct qb_list_head *list;
  669. struct totemudpu_member *member;
  670. qb_list_for_each(list, &(instance->member_list)) {
  671. member = qb_list_entry (list,
  672. struct totemudpu_member,
  673. list);
  674. if (member->member.nodeid == nodeid) {
  675. node_status->nodeid = nodeid;
  676. /* reachable is filled in by totemsrp */
  677. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  678. node_status->link_status[0].enabled = 1;
  679. } else {
  680. node_status->link_status[0].enabled = 0;
  681. }
  682. node_status->link_status[0].connected = node_status->reachable;
  683. node_status->link_status[0].mtu = instance->totem_config->net_mtu;
  684. strncpy(node_status->link_status[0].src_ipaddr, totemip_print(&member->member), KNET_MAX_HOST_LEN-1);
  685. }
  686. }
  687. return (0);
  688. }
  689. int totemudpu_ifaces_get (
  690. void *net_context,
  691. char ***status,
  692. unsigned int *iface_count)
  693. {
  694. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  695. if (status) {
  696. *status = statuses;
  697. }
  698. *iface_count = 1;
  699. return (0);
  700. }
  701. static int totemudpu_build_local_sockets(
  702. struct totemudpu_instance *instance)
  703. {
  704. int i;
  705. unsigned int sendbuf_size;
  706. unsigned int recvbuf_size;
  707. unsigned int optlen = sizeof (sendbuf_size);
  708. int res;
  709. /*
  710. * Create local multicast loop socket
  711. */
  712. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, instance->local_loop_sock) == -1) {
  713. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  714. "socket() failed");
  715. return (-1);
  716. }
  717. for (i = 0; i < 2; i++) {
  718. totemip_nosigpipe (instance->local_loop_sock[i]);
  719. res = fcntl (instance->local_loop_sock[i], F_SETFL, O_NONBLOCK);
  720. if (res == -1) {
  721. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  722. "Could not set non-blocking operation on multicast socket");
  723. return (-1);
  724. }
  725. }
  726. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  727. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  728. res = setsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  729. if (res == -1) {
  730. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  731. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  732. return (-1);
  733. }
  734. res = setsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  735. if (res == -1) {
  736. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  737. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  738. return (-1);
  739. }
  740. res = getsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  741. if (res == 0) {
  742. log_printf (instance->totemudpu_log_level_debug,
  743. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  744. }
  745. res = getsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  746. if (res == 0) {
  747. log_printf (instance->totemudpu_log_level_debug,
  748. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  749. }
  750. return (0);
  751. }
  752. static int totemudpu_build_sockets (
  753. struct totemudpu_instance *instance,
  754. struct totem_ip_address *bindnet_address,
  755. struct totem_ip_address *bound_to)
  756. {
  757. int interface_num;
  758. int interface_up;
  759. int res;
  760. /*
  761. * Determine the ip address bound to and the interface name
  762. */
  763. res = netif_determine (instance,
  764. bindnet_address,
  765. bound_to,
  766. &interface_up,
  767. &interface_num);
  768. if (res == -1) {
  769. return (-1);
  770. }
  771. totemip_copy(&instance->my_id, bound_to);
  772. res = totemudpu_build_sockets_ip (instance,
  773. bindnet_address, bound_to, interface_num);
  774. if (res == -1) {
  775. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  776. LOGSYS_PERROR (errno, instance->totemudpu_log_level_error,
  777. "Unable to create sockets, exiting");
  778. exit(EXIT_FAILURE);
  779. }
  780. /* We only send out of the token socket */
  781. totemudpu_traffic_control_set(instance, instance->token_socket);
  782. /*
  783. * Rebind all members to new ips
  784. */
  785. totemudpu_member_list_rebind_ip(instance);
  786. return res;
  787. }
  788. /*
  789. * Totem Network interface
  790. * depends on poll abstraction, POSIX, IPV4
  791. */
  792. /*
  793. * Create an instance
  794. */
  795. int totemudpu_initialize (
  796. qb_loop_t *poll_handle,
  797. void **udpu_context,
  798. struct totem_config *totem_config,
  799. totemsrp_stats_t *stats,
  800. void *context,
  801. int (*deliver_fn) (
  802. void *context,
  803. const void *msg,
  804. unsigned int msg_len,
  805. const struct sockaddr_storage *system_from),
  806. int (*iface_change_fn) (
  807. void *context,
  808. const struct totem_ip_address *iface_address,
  809. unsigned int ring_no),
  810. void (*mtu_changed) (
  811. void *context,
  812. int net_mtu),
  813. void (*target_set_completed) (
  814. void *context))
  815. {
  816. struct totemudpu_instance *instance;
  817. instance = malloc (sizeof (struct totemudpu_instance));
  818. if (instance == NULL) {
  819. return (-1);
  820. }
  821. totemudpu_instance_initialize (instance);
  822. instance->totem_config = totem_config;
  823. instance->stats = stats;
  824. /*
  825. * Configure logging
  826. */
  827. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  828. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  829. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  830. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  831. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  832. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  833. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  834. /*
  835. * Initialize local variables for totemudpu
  836. */
  837. instance->totem_interface = &totem_config->interfaces[0];
  838. memset (instance->iov_buffer, 0, UDP_RECEIVE_FRAME_SIZE_MAX + 1);
  839. instance->totemudpu_poll_handle = poll_handle;
  840. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  841. instance->context = context;
  842. instance->totemudpu_deliver_fn = deliver_fn;
  843. instance->totemudpu_iface_change_fn = iface_change_fn;
  844. instance->totemudpu_target_set_completed = target_set_completed;
  845. /*
  846. * Create static local mcast sockets
  847. */
  848. if (totemudpu_build_local_sockets(instance) == -1) {
  849. free(instance);
  850. return (-1);
  851. }
  852. qb_loop_poll_add (
  853. instance->totemudpu_poll_handle,
  854. QB_LOOP_MED,
  855. instance->local_loop_sock[0],
  856. POLLIN, instance, net_deliver_fn);
  857. /*
  858. * RRP layer isn't ready to receive message because it hasn't
  859. * initialized yet. Add short timer to check the interfaces.
  860. */
  861. qb_loop_timer_add (instance->totemudpu_poll_handle,
  862. QB_LOOP_MED,
  863. 100*QB_TIME_NS_IN_MSEC,
  864. (void *)instance,
  865. timer_function_netif_check_timeout,
  866. &instance->timer_netif_check_timeout);
  867. totemudpu_start_merge_detect_timeout((void*)instance);
  868. *udpu_context = instance;
  869. return (0);
  870. }
  871. void *totemudpu_buffer_alloc (void)
  872. {
  873. return malloc (FRAME_SIZE_MAX);
  874. }
  875. void totemudpu_buffer_release (void *ptr)
  876. {
  877. return free (ptr);
  878. }
  879. int totemudpu_processor_count_set (
  880. void *udpu_context,
  881. int processor_count)
  882. {
  883. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  884. int res = 0;
  885. instance->my_memb_entries = processor_count;
  886. qb_loop_timer_del (instance->totemudpu_poll_handle,
  887. instance->timer_netif_check_timeout);
  888. if (processor_count == 1) {
  889. qb_loop_timer_add (instance->totemudpu_poll_handle,
  890. QB_LOOP_MED,
  891. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  892. (void *)instance,
  893. timer_function_netif_check_timeout,
  894. &instance->timer_netif_check_timeout);
  895. }
  896. return (res);
  897. }
  898. int totemudpu_recv_flush (void *udpu_context)
  899. {
  900. int res = 0;
  901. return (res);
  902. }
  903. int totemudpu_send_flush (void *udpu_context)
  904. {
  905. int res = 0;
  906. return (res);
  907. }
  908. int totemudpu_token_send (
  909. void *udpu_context,
  910. const void *msg,
  911. unsigned int msg_len)
  912. {
  913. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  914. int res = 0;
  915. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  916. return (res);
  917. }
  918. int totemudpu_mcast_flush_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. mcast_sendmsg (instance, msg, msg_len, 0);
  926. return (res);
  927. }
  928. int totemudpu_mcast_noflush_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, 1);
  936. return (res);
  937. }
  938. extern int totemudpu_iface_check (void *udpu_context)
  939. {
  940. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  941. int res = 0;
  942. timer_function_netif_check_timeout (instance);
  943. return (res);
  944. }
  945. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  946. {
  947. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  948. }
  949. int totemudpu_token_target_set (
  950. void *udpu_context,
  951. unsigned int nodeid)
  952. {
  953. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  954. struct qb_list_head *list;
  955. struct totemudpu_member *member;
  956. int res = 0;
  957. qb_list_for_each(list, &(instance->member_list)) {
  958. member = qb_list_entry (list,
  959. struct totemudpu_member,
  960. list);
  961. if (member->member.nodeid == nodeid) {
  962. memcpy (&instance->token_target, &member->member,
  963. sizeof (struct totem_ip_address));
  964. instance->totemudpu_target_set_completed (instance->context);
  965. break;
  966. }
  967. }
  968. return (res);
  969. }
  970. extern int totemudpu_recv_mcast_empty (
  971. void *udpu_context)
  972. {
  973. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  974. unsigned int res;
  975. struct sockaddr_storage system_from;
  976. struct msghdr msg_recv;
  977. struct pollfd ufd;
  978. int nfds, i;
  979. int msg_processed = 0;
  980. int sock;
  981. /*
  982. * Receive datagram
  983. */
  984. msg_recv.msg_name = &system_from;
  985. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  986. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  987. msg_recv.msg_iovlen = 1;
  988. #ifdef HAVE_MSGHDR_CONTROL
  989. msg_recv.msg_control = 0;
  990. #endif
  991. #ifdef HAVE_MSGHDR_CONTROLLEN
  992. msg_recv.msg_controllen = 0;
  993. #endif
  994. #ifdef HAVE_MSGHDR_FLAGS
  995. msg_recv.msg_flags = 0;
  996. #endif
  997. #ifdef HAVE_MSGHDR_ACCRIGHTS
  998. msg_recv.msg_accrights = NULL;
  999. #endif
  1000. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1001. msg_recv.msg_accrightslen = 0;
  1002. #endif
  1003. for (i = 0; i < 2; i++) {
  1004. sock = -1;
  1005. if (i == 0) {
  1006. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1007. sock = instance->token_socket;
  1008. } else {
  1009. continue;
  1010. }
  1011. }
  1012. if (i == 1) {
  1013. sock = instance->local_loop_sock[0];
  1014. }
  1015. assert(sock != -1);
  1016. do {
  1017. ufd.fd = sock;
  1018. ufd.events = POLLIN;
  1019. nfds = poll (&ufd, 1, 0);
  1020. if (nfds == 1 && ufd.revents & POLLIN) {
  1021. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1022. if (res != -1) {
  1023. msg_processed = 1;
  1024. } else {
  1025. msg_processed = -1;
  1026. }
  1027. }
  1028. } while (nfds == 1);
  1029. }
  1030. return (msg_processed);
  1031. }
  1032. static int totemudpu_create_sending_socket(
  1033. void *udpu_context,
  1034. const struct totem_ip_address *member)
  1035. {
  1036. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1037. int fd;
  1038. int res;
  1039. unsigned int sendbuf_size;
  1040. unsigned int optlen = sizeof (sendbuf_size);
  1041. struct sockaddr_storage sockaddr;
  1042. int addrlen;
  1043. fd = socket (member->family, SOCK_DGRAM, 0);
  1044. if (fd == -1) {
  1045. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1046. "Could not create socket for new member");
  1047. return (-1);
  1048. }
  1049. totemip_nosigpipe (fd);
  1050. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  1051. if (res == -1) {
  1052. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1053. "Could not set non-blocking operation on token socket");
  1054. goto error_close_fd;
  1055. }
  1056. /*
  1057. * These sockets are used to send multicast messages, so their buffers
  1058. * should be large
  1059. */
  1060. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1061. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  1062. &sendbuf_size, optlen);
  1063. if (res == -1) {
  1064. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1065. "Could not set sendbuf size");
  1066. /*
  1067. * Fail in setting sendbuf size is not fatal -> don't exit
  1068. */
  1069. }
  1070. res = set_socket_dscp(fd, instance->totem_config->ip_dscp);
  1071. if (res == -1) {
  1072. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1073. "Could not set IP_TOS bits");
  1074. }
  1075. /*
  1076. * Bind to sending interface
  1077. */
  1078. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  1079. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  1080. if (res == -1) {
  1081. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1082. "bind token socket failed");
  1083. goto error_close_fd;
  1084. }
  1085. return (fd);
  1086. error_close_fd:
  1087. close(fd);
  1088. return (-1);
  1089. }
  1090. int totemudpu_iface_set (void *net_context,
  1091. const struct totem_ip_address *local_addr,
  1092. unsigned short ip_port,
  1093. unsigned int iface_no)
  1094. {
  1095. /* Not supported */
  1096. return (-1);
  1097. }
  1098. int totemudpu_member_add (
  1099. void *udpu_context,
  1100. const struct totem_ip_address *local,
  1101. const struct totem_ip_address *member,
  1102. int ring_no)
  1103. {
  1104. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1105. struct totemudpu_member *new_member;
  1106. new_member = malloc (sizeof (struct totemudpu_member));
  1107. if (new_member == NULL) {
  1108. return (-1);
  1109. }
  1110. memset(new_member, 0, sizeof(*new_member));
  1111. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  1112. totemip_print(member));
  1113. qb_list_init (&new_member->list);
  1114. qb_list_add_tail (&new_member->list, &instance->member_list);
  1115. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1116. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  1117. new_member->active = 1;
  1118. return (0);
  1119. }
  1120. int totemudpu_member_remove (
  1121. void *udpu_context,
  1122. const struct totem_ip_address *token_target,
  1123. int ring_no)
  1124. {
  1125. int found = 0;
  1126. struct qb_list_head *list;
  1127. struct totemudpu_member *member;
  1128. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1129. /*
  1130. * Find the member to remove and close its socket
  1131. */
  1132. qb_list_for_each(list, &(instance->member_list)) {
  1133. member = qb_list_entry (list,
  1134. struct totemudpu_member,
  1135. list);
  1136. if (totemip_compare (token_target, &member->member)==0) {
  1137. log_printf(LOGSYS_LEVEL_NOTICE,
  1138. "removing UDPU member {%s}",
  1139. totemip_print(&member->member));
  1140. if (member->fd > 0) {
  1141. log_printf(LOGSYS_LEVEL_DEBUG,
  1142. "Closing socket to: {%s}",
  1143. totemip_print(&member->member));
  1144. qb_loop_poll_del (instance->totemudpu_poll_handle,
  1145. member->fd);
  1146. close (member->fd);
  1147. }
  1148. found = 1;
  1149. break;
  1150. }
  1151. }
  1152. /*
  1153. * Delete the member from the list
  1154. */
  1155. if (found) {
  1156. qb_list_del (list);
  1157. }
  1158. instance = NULL;
  1159. return (0);
  1160. }
  1161. int totemudpu_member_list_rebind_ip (
  1162. void *udpu_context)
  1163. {
  1164. struct qb_list_head *list;
  1165. struct totemudpu_member *member;
  1166. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1167. qb_list_for_each(list, &(instance->member_list)) {
  1168. member = qb_list_entry (list,
  1169. struct totemudpu_member,
  1170. list);
  1171. if (member->fd > 0) {
  1172. close (member->fd);
  1173. }
  1174. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  1175. }
  1176. return (0);
  1177. }
  1178. static void timer_function_merge_detect_timeout (
  1179. void *data)
  1180. {
  1181. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  1182. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1183. instance->send_merge_detect_message = 1;
  1184. }
  1185. instance->merge_detect_messages_sent_before_timeout = 0;
  1186. totemudpu_start_merge_detect_timeout(instance);
  1187. }
  1188. static void totemudpu_start_merge_detect_timeout(
  1189. void *udpu_context)
  1190. {
  1191. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1192. qb_loop_timer_add(instance->totemudpu_poll_handle,
  1193. QB_LOOP_MED,
  1194. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1195. (void *)instance,
  1196. timer_function_merge_detect_timeout,
  1197. &instance->timer_merge_detect_timeout);
  1198. }
  1199. static void totemudpu_stop_merge_detect_timeout(
  1200. void *udpu_context)
  1201. {
  1202. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1203. qb_loop_timer_del(instance->totemudpu_poll_handle,
  1204. instance->timer_merge_detect_timeout);
  1205. }
  1206. int totemudpu_reconfigure (
  1207. void *udpu_context,
  1208. struct totem_config *totem_config)
  1209. {
  1210. /* Not supported */
  1211. return (-1);
  1212. }