totemudpu.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. void (*totemudpu_deliver_fn) (
  88. void *context,
  89. const void *msg,
  90. unsigned int msg_len,
  91. const struct sockaddr_storage *system_from);
  92. void (*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. return 0;
  667. }
  668. int totemudpu_nodestatus_get (void *udpu_context, unsigned int nodeid,
  669. struct totem_node_status *node_status)
  670. {
  671. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  672. struct qb_list_head *list;
  673. struct totemudpu_member *member;
  674. qb_list_for_each(list, &(instance->member_list)) {
  675. member = qb_list_entry (list,
  676. struct totemudpu_member,
  677. list);
  678. if (member->member.nodeid == nodeid) {
  679. node_status->nodeid = nodeid;
  680. /* reachable is filled in by totemsrp */
  681. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  682. node_status->link_status[0].enabled = 1;
  683. } else {
  684. node_status->link_status[0].enabled = 0;
  685. }
  686. node_status->link_status[0].connected = node_status->reachable;
  687. node_status->link_status[0].mtu = instance->totem_config->net_mtu;
  688. strncpy(node_status->link_status[0].src_ipaddr, totemip_print(&member->member), KNET_MAX_HOST_LEN-1);
  689. }
  690. }
  691. return (0);
  692. }
  693. int totemudpu_ifaces_get (
  694. void *net_context,
  695. char ***status,
  696. unsigned int *iface_count)
  697. {
  698. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  699. if (status) {
  700. *status = statuses;
  701. }
  702. *iface_count = 1;
  703. return (0);
  704. }
  705. static int totemudpu_build_local_sockets(
  706. struct totemudpu_instance *instance)
  707. {
  708. int i;
  709. unsigned int sendbuf_size;
  710. unsigned int recvbuf_size;
  711. unsigned int optlen = sizeof (sendbuf_size);
  712. int res;
  713. /*
  714. * Create local multicast loop socket
  715. */
  716. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, instance->local_loop_sock) == -1) {
  717. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  718. "socket() failed");
  719. return (-1);
  720. }
  721. for (i = 0; i < 2; i++) {
  722. totemip_nosigpipe (instance->local_loop_sock[i]);
  723. res = fcntl (instance->local_loop_sock[i], F_SETFL, O_NONBLOCK);
  724. if (res == -1) {
  725. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  726. "Could not set non-blocking operation on multicast socket");
  727. return (-1);
  728. }
  729. }
  730. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  731. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  732. res = setsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  733. if (res == -1) {
  734. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  735. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  736. return (-1);
  737. }
  738. res = setsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  739. if (res == -1) {
  740. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  741. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  742. return (-1);
  743. }
  744. res = getsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  745. if (res == 0) {
  746. log_printf (instance->totemudpu_log_level_debug,
  747. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  748. }
  749. res = getsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  750. if (res == 0) {
  751. log_printf (instance->totemudpu_log_level_debug,
  752. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  753. }
  754. return (0);
  755. }
  756. static int totemudpu_build_sockets (
  757. struct totemudpu_instance *instance,
  758. struct totem_ip_address *bindnet_address,
  759. struct totem_ip_address *bound_to)
  760. {
  761. int interface_num;
  762. int interface_up;
  763. int res;
  764. /*
  765. * Determine the ip address bound to and the interface name
  766. */
  767. res = netif_determine (instance,
  768. bindnet_address,
  769. bound_to,
  770. &interface_up,
  771. &interface_num);
  772. if (res == -1) {
  773. return (-1);
  774. }
  775. totemip_copy(&instance->my_id, bound_to);
  776. res = totemudpu_build_sockets_ip (instance,
  777. bindnet_address, bound_to, interface_num);
  778. if (res == -1) {
  779. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  780. LOGSYS_PERROR (errno, instance->totemudpu_log_level_error,
  781. "Unable to create sockets, exiting");
  782. exit(EXIT_FAILURE);
  783. }
  784. /* We only send out of the token socket */
  785. totemudpu_traffic_control_set(instance, instance->token_socket);
  786. /*
  787. * Rebind all members to new ips
  788. */
  789. totemudpu_member_list_rebind_ip(instance);
  790. return res;
  791. }
  792. /*
  793. * Totem Network interface
  794. * depends on poll abstraction, POSIX, IPV4
  795. */
  796. /*
  797. * Create an instance
  798. */
  799. int totemudpu_initialize (
  800. qb_loop_t *poll_handle,
  801. void **udpu_context,
  802. struct totem_config *totem_config,
  803. totemsrp_stats_t *stats,
  804. void *context,
  805. void (*deliver_fn) (
  806. void *context,
  807. const void *msg,
  808. unsigned int msg_len,
  809. const struct sockaddr_storage *system_from),
  810. void (*iface_change_fn) (
  811. void *context,
  812. const struct totem_ip_address *iface_address,
  813. unsigned int ring_no),
  814. void (*mtu_changed) (
  815. void *context,
  816. int net_mtu),
  817. void (*target_set_completed) (
  818. void *context))
  819. {
  820. struct totemudpu_instance *instance;
  821. instance = malloc (sizeof (struct totemudpu_instance));
  822. if (instance == NULL) {
  823. return (-1);
  824. }
  825. totemudpu_instance_initialize (instance);
  826. instance->totem_config = totem_config;
  827. instance->stats = stats;
  828. /*
  829. * Configure logging
  830. */
  831. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  832. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  833. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  834. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  835. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  836. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  837. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  838. /*
  839. * Initialize local variables for totemudpu
  840. */
  841. instance->totem_interface = &totem_config->interfaces[0];
  842. memset (instance->iov_buffer, 0, UDP_RECEIVE_FRAME_SIZE_MAX);
  843. instance->totemudpu_poll_handle = poll_handle;
  844. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  845. instance->context = context;
  846. instance->totemudpu_deliver_fn = deliver_fn;
  847. instance->totemudpu_iface_change_fn = iface_change_fn;
  848. instance->totemudpu_target_set_completed = target_set_completed;
  849. /*
  850. * Create static local mcast sockets
  851. */
  852. if (totemudpu_build_local_sockets(instance) == -1) {
  853. free(instance);
  854. return (-1);
  855. }
  856. qb_loop_poll_add (
  857. instance->totemudpu_poll_handle,
  858. QB_LOOP_MED,
  859. instance->local_loop_sock[0],
  860. POLLIN, instance, net_deliver_fn);
  861. /*
  862. * RRP layer isn't ready to receive message because it hasn't
  863. * initialized yet. Add short timer to check the interfaces.
  864. */
  865. qb_loop_timer_add (instance->totemudpu_poll_handle,
  866. QB_LOOP_MED,
  867. 100*QB_TIME_NS_IN_MSEC,
  868. (void *)instance,
  869. timer_function_netif_check_timeout,
  870. &instance->timer_netif_check_timeout);
  871. totemudpu_start_merge_detect_timeout((void*)instance);
  872. *udpu_context = instance;
  873. return (0);
  874. }
  875. void *totemudpu_buffer_alloc (void)
  876. {
  877. return malloc (FRAME_SIZE_MAX);
  878. }
  879. void totemudpu_buffer_release (void *ptr)
  880. {
  881. return free (ptr);
  882. }
  883. int totemudpu_processor_count_set (
  884. void *udpu_context,
  885. int processor_count)
  886. {
  887. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  888. int res = 0;
  889. instance->my_memb_entries = processor_count;
  890. qb_loop_timer_del (instance->totemudpu_poll_handle,
  891. instance->timer_netif_check_timeout);
  892. if (processor_count == 1) {
  893. qb_loop_timer_add (instance->totemudpu_poll_handle,
  894. QB_LOOP_MED,
  895. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  896. (void *)instance,
  897. timer_function_netif_check_timeout,
  898. &instance->timer_netif_check_timeout);
  899. }
  900. return (res);
  901. }
  902. int totemudpu_recv_flush (void *udpu_context)
  903. {
  904. int res = 0;
  905. return (res);
  906. }
  907. int totemudpu_send_flush (void *udpu_context)
  908. {
  909. int res = 0;
  910. return (res);
  911. }
  912. int totemudpu_token_send (
  913. void *udpu_context,
  914. const void *msg,
  915. unsigned int msg_len)
  916. {
  917. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  918. int res = 0;
  919. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  920. return (res);
  921. }
  922. int totemudpu_mcast_flush_send (
  923. void *udpu_context,
  924. const void *msg,
  925. unsigned int msg_len)
  926. {
  927. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  928. int res = 0;
  929. mcast_sendmsg (instance, msg, msg_len, 0);
  930. return (res);
  931. }
  932. int totemudpu_mcast_noflush_send (
  933. void *udpu_context,
  934. const void *msg,
  935. unsigned int msg_len)
  936. {
  937. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  938. int res = 0;
  939. mcast_sendmsg (instance, msg, msg_len, 1);
  940. return (res);
  941. }
  942. extern int totemudpu_iface_check (void *udpu_context)
  943. {
  944. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  945. int res = 0;
  946. timer_function_netif_check_timeout (instance);
  947. return (res);
  948. }
  949. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  950. {
  951. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  952. }
  953. int totemudpu_token_target_set (
  954. void *udpu_context,
  955. unsigned int nodeid)
  956. {
  957. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  958. struct qb_list_head *list;
  959. struct totemudpu_member *member;
  960. int res = 0;
  961. qb_list_for_each(list, &(instance->member_list)) {
  962. member = qb_list_entry (list,
  963. struct totemudpu_member,
  964. list);
  965. if (member->member.nodeid == nodeid) {
  966. memcpy (&instance->token_target, &member->member,
  967. sizeof (struct totem_ip_address));
  968. instance->totemudpu_target_set_completed (instance->context);
  969. break;
  970. }
  971. }
  972. return (res);
  973. }
  974. extern int totemudpu_recv_mcast_empty (
  975. void *udpu_context)
  976. {
  977. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  978. unsigned int res;
  979. struct sockaddr_storage system_from;
  980. struct msghdr msg_recv;
  981. struct pollfd ufd;
  982. int nfds, i;
  983. int msg_processed = 0;
  984. int sock;
  985. /*
  986. * Receive datagram
  987. */
  988. msg_recv.msg_name = &system_from;
  989. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  990. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  991. msg_recv.msg_iovlen = 1;
  992. #ifdef HAVE_MSGHDR_CONTROL
  993. msg_recv.msg_control = 0;
  994. #endif
  995. #ifdef HAVE_MSGHDR_CONTROLLEN
  996. msg_recv.msg_controllen = 0;
  997. #endif
  998. #ifdef HAVE_MSGHDR_FLAGS
  999. msg_recv.msg_flags = 0;
  1000. #endif
  1001. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1002. msg_recv.msg_accrights = NULL;
  1003. #endif
  1004. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1005. msg_recv.msg_accrightslen = 0;
  1006. #endif
  1007. for (i = 0; i < 2; i++) {
  1008. sock = -1;
  1009. if (i == 0) {
  1010. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1011. sock = instance->token_socket;
  1012. } else {
  1013. continue;
  1014. }
  1015. }
  1016. if (i == 1) {
  1017. sock = instance->local_loop_sock[0];
  1018. }
  1019. assert(sock != -1);
  1020. do {
  1021. ufd.fd = sock;
  1022. ufd.events = POLLIN;
  1023. nfds = poll (&ufd, 1, 0);
  1024. if (nfds == 1 && ufd.revents & POLLIN) {
  1025. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1026. if (res != -1) {
  1027. msg_processed = 1;
  1028. } else {
  1029. msg_processed = -1;
  1030. }
  1031. }
  1032. } while (nfds == 1);
  1033. }
  1034. return (msg_processed);
  1035. }
  1036. static int totemudpu_create_sending_socket(
  1037. void *udpu_context,
  1038. const struct totem_ip_address *member)
  1039. {
  1040. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1041. int fd;
  1042. int res;
  1043. unsigned int sendbuf_size;
  1044. unsigned int optlen = sizeof (sendbuf_size);
  1045. struct sockaddr_storage sockaddr;
  1046. int addrlen;
  1047. fd = socket (member->family, SOCK_DGRAM, 0);
  1048. if (fd == -1) {
  1049. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1050. "Could not create socket for new member");
  1051. return (-1);
  1052. }
  1053. totemip_nosigpipe (fd);
  1054. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  1055. if (res == -1) {
  1056. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1057. "Could not set non-blocking operation on token socket");
  1058. goto error_close_fd;
  1059. }
  1060. /*
  1061. * These sockets are used to send multicast messages, so their buffers
  1062. * should be large
  1063. */
  1064. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1065. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  1066. &sendbuf_size, optlen);
  1067. if (res == -1) {
  1068. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1069. "Could not set sendbuf size");
  1070. /*
  1071. * Fail in setting sendbuf size is not fatal -> don't exit
  1072. */
  1073. }
  1074. /*
  1075. * Bind to sending interface
  1076. */
  1077. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  1078. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  1079. if (res == -1) {
  1080. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1081. "bind token socket failed");
  1082. goto error_close_fd;
  1083. }
  1084. return (fd);
  1085. error_close_fd:
  1086. close(fd);
  1087. return (-1);
  1088. }
  1089. int totemudpu_iface_set (void *net_context,
  1090. const struct totem_ip_address *local_addr,
  1091. unsigned short ip_port,
  1092. unsigned int iface_no)
  1093. {
  1094. /* Not supported */
  1095. return (-1);
  1096. }
  1097. int totemudpu_member_add (
  1098. void *udpu_context,
  1099. const struct totem_ip_address *local,
  1100. const struct totem_ip_address *member,
  1101. int ring_no)
  1102. {
  1103. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1104. struct totemudpu_member *new_member;
  1105. new_member = malloc (sizeof (struct totemudpu_member));
  1106. if (new_member == NULL) {
  1107. return (-1);
  1108. }
  1109. memset(new_member, 0, sizeof(*new_member));
  1110. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  1111. totemip_print(member));
  1112. qb_list_init (&new_member->list);
  1113. qb_list_add_tail (&new_member->list, &instance->member_list);
  1114. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1115. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  1116. new_member->active = 1;
  1117. return (0);
  1118. }
  1119. int totemudpu_member_remove (
  1120. void *udpu_context,
  1121. const struct totem_ip_address *token_target,
  1122. int ring_no)
  1123. {
  1124. int found = 0;
  1125. struct qb_list_head *list;
  1126. struct totemudpu_member *member;
  1127. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1128. /*
  1129. * Find the member to remove and close its socket
  1130. */
  1131. qb_list_for_each(list, &(instance->member_list)) {
  1132. member = qb_list_entry (list,
  1133. struct totemudpu_member,
  1134. list);
  1135. if (totemip_compare (token_target, &member->member)==0) {
  1136. log_printf(LOGSYS_LEVEL_NOTICE,
  1137. "removing UDPU member {%s}",
  1138. totemip_print(&member->member));
  1139. if (member->fd > 0) {
  1140. log_printf(LOGSYS_LEVEL_DEBUG,
  1141. "Closing socket to: {%s}",
  1142. totemip_print(&member->member));
  1143. qb_loop_poll_del (instance->totemudpu_poll_handle,
  1144. member->fd);
  1145. close (member->fd);
  1146. }
  1147. found = 1;
  1148. break;
  1149. }
  1150. }
  1151. /*
  1152. * Delete the member from the list
  1153. */
  1154. if (found) {
  1155. qb_list_del (list);
  1156. }
  1157. instance = NULL;
  1158. return (0);
  1159. }
  1160. int totemudpu_member_list_rebind_ip (
  1161. void *udpu_context)
  1162. {
  1163. struct qb_list_head *list;
  1164. struct totemudpu_member *member;
  1165. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1166. qb_list_for_each(list, &(instance->member_list)) {
  1167. member = qb_list_entry (list,
  1168. struct totemudpu_member,
  1169. list);
  1170. if (member->fd > 0) {
  1171. close (member->fd);
  1172. }
  1173. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  1174. }
  1175. return (0);
  1176. }
  1177. static void timer_function_merge_detect_timeout (
  1178. void *data)
  1179. {
  1180. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  1181. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1182. instance->send_merge_detect_message = 1;
  1183. }
  1184. instance->merge_detect_messages_sent_before_timeout = 0;
  1185. totemudpu_start_merge_detect_timeout(instance);
  1186. }
  1187. static void totemudpu_start_merge_detect_timeout(
  1188. void *udpu_context)
  1189. {
  1190. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1191. qb_loop_timer_add(instance->totemudpu_poll_handle,
  1192. QB_LOOP_MED,
  1193. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1194. (void *)instance,
  1195. timer_function_merge_detect_timeout,
  1196. &instance->timer_merge_detect_timeout);
  1197. }
  1198. static void totemudpu_stop_merge_detect_timeout(
  1199. void *udpu_context)
  1200. {
  1201. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1202. qb_loop_timer_del(instance->totemudpu_poll_handle,
  1203. instance->timer_merge_detect_timeout);
  1204. }
  1205. int totemudpu_reconfigure (
  1206. void *udpu_context,
  1207. struct totem_config *totem_config)
  1208. {
  1209. /* Not supported */
  1210. return (-1);
  1211. }