totemudpu.c 38 KB

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