totemudpu.c 37 KB

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