4
0

totemudp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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 <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <sys/uio.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/list.h>
  61. #include <qb/qbdefs.h>
  62. #include <qb/qbloop.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/logsys.h>
  65. #include "totemudp.h"
  66. #include "util.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. #define MESSAGE_TYPE_MEMB_JOIN 3
  81. struct totemudp_socket {
  82. int mcast_recv;
  83. int mcast_send;
  84. int token;
  85. /*
  86. * Socket used for local multicast delivery. We don't rely on multicast
  87. * loop and rather this UNIX DGRAM socket is used. Socket is created by
  88. * socketpair call and they are used in same way as pipe (so [0] is read
  89. * end and [1] is write end)
  90. */
  91. int local_mcast_loop[2];
  92. };
  93. struct totemudp_instance {
  94. qb_loop_t *totemudp_poll_handle;
  95. struct totem_interface *totem_interface;
  96. int netif_state_report;
  97. int netif_bind_state;
  98. void *context;
  99. void (*totemudp_deliver_fn) (
  100. void *context,
  101. const void *msg,
  102. unsigned int msg_len);
  103. void (*totemudp_iface_change_fn) (
  104. void *context,
  105. const struct totem_ip_address *iface_address,
  106. unsigned int ring_no);
  107. void (*totemudp_target_set_completed) (void *context);
  108. /*
  109. * Function and data used to log messages
  110. */
  111. int totemudp_log_level_security;
  112. int totemudp_log_level_error;
  113. int totemudp_log_level_warning;
  114. int totemudp_log_level_notice;
  115. int totemudp_log_level_debug;
  116. int totemudp_subsys_id;
  117. void (*totemudp_log_printf) (
  118. int level,
  119. int subsys,
  120. const char *function,
  121. const char *file,
  122. int line,
  123. const char *format,
  124. ...)__attribute__((format(printf, 6, 7)));
  125. void *udp_context;
  126. char iov_buffer[FRAME_SIZE_MAX];
  127. char iov_buffer_flush[FRAME_SIZE_MAX];
  128. struct iovec totemudp_iov_recv;
  129. struct iovec totemudp_iov_recv_flush;
  130. struct totemudp_socket totemudp_sockets;
  131. struct totem_ip_address mcast_address;
  132. int stats_sent;
  133. int stats_recv;
  134. int stats_delv;
  135. int stats_remcasts;
  136. int stats_orf_token;
  137. struct timeval stats_tv_start;
  138. struct totem_ip_address my_id;
  139. int firstrun;
  140. qb_loop_timer_handle timer_netif_check_timeout;
  141. unsigned int my_memb_entries;
  142. int flushing;
  143. struct totem_config *totem_config;
  144. totemsrp_stats_t *stats;
  145. struct totem_ip_address token_target;
  146. };
  147. struct work_item {
  148. const void *msg;
  149. unsigned int msg_len;
  150. struct totemudp_instance *instance;
  151. };
  152. static int totemudp_build_sockets (
  153. struct totemudp_instance *instance,
  154. struct totem_ip_address *bindnet_address,
  155. struct totem_ip_address *mcastaddress,
  156. struct totemudp_socket *sockets,
  157. struct totem_ip_address *bound_to);
  158. static struct totem_ip_address localhost;
  159. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  160. {
  161. memset (instance, 0, sizeof (struct totemudp_instance));
  162. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  163. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  164. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  165. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  166. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  167. /*
  168. * There is always atleast 1 processor
  169. */
  170. instance->my_memb_entries = 1;
  171. }
  172. #define log_printf(level, format, args...) \
  173. do { \
  174. instance->totemudp_log_printf ( \
  175. level, instance->totemudp_subsys_id, \
  176. __FUNCTION__, __FILE__, __LINE__, \
  177. (const char *)format, ##args); \
  178. } while (0);
  179. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  180. do { \
  181. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  182. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  183. instance->totemudp_log_printf ( \
  184. level, instance->totemudp_subsys_id, \
  185. __FUNCTION__, __FILE__, __LINE__, \
  186. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  187. } while(0)
  188. int totemudp_crypto_set (
  189. void *udp_context,
  190. const char *cipher_type,
  191. const char *hash_type)
  192. {
  193. return (0);
  194. }
  195. static inline void ucast_sendmsg (
  196. struct totemudp_instance *instance,
  197. struct totem_ip_address *system_to,
  198. const void *msg,
  199. unsigned int msg_len)
  200. {
  201. struct msghdr msg_ucast;
  202. int res = 0;
  203. struct sockaddr_storage sockaddr;
  204. struct iovec iovec;
  205. int addrlen;
  206. iovec.iov_base = (void*)msg;
  207. iovec.iov_len = msg_len;
  208. /*
  209. * Build unicast message
  210. */
  211. memset(&msg_ucast, 0, sizeof(msg_ucast));
  212. totemip_totemip_to_sockaddr_convert(system_to,
  213. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  214. msg_ucast.msg_name = &sockaddr;
  215. msg_ucast.msg_namelen = addrlen;
  216. msg_ucast.msg_iov = (void *)&iovec;
  217. msg_ucast.msg_iovlen = 1;
  218. #ifdef HAVE_MSGHDR_CONTROL
  219. msg_ucast.msg_control = 0;
  220. #endif
  221. #ifdef HAVE_MSGHDR_CONTROLLEN
  222. msg_ucast.msg_controllen = 0;
  223. #endif
  224. #ifdef HAVE_MSGHDR_FLAGS
  225. msg_ucast.msg_flags = 0;
  226. #endif
  227. #ifdef HAVE_MSGHDR_ACCRIGHTS
  228. msg_ucast.msg_accrights = NULL;
  229. #endif
  230. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  231. msg_ucast.msg_accrightslen = 0;
  232. #endif
  233. /*
  234. * Transmit unicast message
  235. * An error here is recovered by totemsrp
  236. */
  237. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  238. MSG_NOSIGNAL);
  239. if (res < 0) {
  240. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  241. "sendmsg(ucast) failed (non-critical)");
  242. }
  243. }
  244. static inline void mcast_sendmsg (
  245. struct totemudp_instance *instance,
  246. const void *msg,
  247. unsigned int msg_len)
  248. {
  249. struct msghdr msg_mcast;
  250. int res = 0;
  251. struct iovec iovec;
  252. struct sockaddr_storage sockaddr;
  253. int addrlen;
  254. iovec.iov_base = (void *)msg;
  255. iovec.iov_len = msg_len;
  256. /*
  257. * Build multicast message
  258. */
  259. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  260. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  261. memset(&msg_mcast, 0, sizeof(msg_mcast));
  262. msg_mcast.msg_name = &sockaddr;
  263. msg_mcast.msg_namelen = addrlen;
  264. msg_mcast.msg_iov = (void *)&iovec;
  265. msg_mcast.msg_iovlen = 1;
  266. #ifdef HAVE_MSGHDR_CONTROL
  267. msg_mcast.msg_control = 0;
  268. #endif
  269. #ifdef HAVE_MSGHDR_CONTROLLEN
  270. msg_mcast.msg_controllen = 0;
  271. #endif
  272. #ifdef HAVE_MSGHDR_FLAGS
  273. msg_mcast.msg_flags = 0;
  274. #endif
  275. #ifdef HAVE_MSGHDR_ACCRIGHTS
  276. msg_mcast.msg_accrights = NULL;
  277. #endif
  278. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  279. msg_mcast.msg_accrightslen = 0;
  280. #endif
  281. /*
  282. * Transmit multicast message
  283. * An error here is recovered by totemsrp
  284. */
  285. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  286. MSG_NOSIGNAL);
  287. if (res < 0) {
  288. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  289. "sendmsg(mcast) failed (non-critical)");
  290. instance->stats->continuous_sendmsg_failures++;
  291. } else {
  292. instance->stats->continuous_sendmsg_failures = 0;
  293. }
  294. /*
  295. * Transmit multicast message to local unix mcast loop
  296. * An error here is recovered by totemsrp
  297. */
  298. msg_mcast.msg_name = NULL;
  299. msg_mcast.msg_namelen = 0;
  300. res = sendmsg (instance->totemudp_sockets.local_mcast_loop[1], &msg_mcast,
  301. MSG_NOSIGNAL);
  302. if (res < 0) {
  303. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  304. "sendmsg(local mcast loop) failed (non-critical)");
  305. }
  306. }
  307. int totemudp_finalize (
  308. void *udp_context)
  309. {
  310. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  311. int res = 0;
  312. if (instance->totemudp_sockets.mcast_recv > 0) {
  313. qb_loop_poll_del (instance->totemudp_poll_handle,
  314. instance->totemudp_sockets.mcast_recv);
  315. close (instance->totemudp_sockets.mcast_recv);
  316. }
  317. if (instance->totemudp_sockets.mcast_send > 0) {
  318. close (instance->totemudp_sockets.mcast_send);
  319. }
  320. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  321. qb_loop_poll_del (instance->totemudp_poll_handle,
  322. instance->totemudp_sockets.local_mcast_loop[0]);
  323. close (instance->totemudp_sockets.local_mcast_loop[0]);
  324. close (instance->totemudp_sockets.local_mcast_loop[1]);
  325. }
  326. if (instance->totemudp_sockets.token > 0) {
  327. qb_loop_poll_del (instance->totemudp_poll_handle,
  328. instance->totemudp_sockets.token);
  329. close (instance->totemudp_sockets.token);
  330. }
  331. return (res);
  332. }
  333. /*
  334. * Only designed to work with a message with one iov
  335. */
  336. static int net_deliver_fn (
  337. int fd,
  338. int revents,
  339. void *data)
  340. {
  341. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  342. struct msghdr msg_recv;
  343. struct iovec *iovec;
  344. struct sockaddr_storage system_from;
  345. int bytes_received;
  346. char *message_type;
  347. if (instance->flushing == 1) {
  348. iovec = &instance->totemudp_iov_recv_flush;
  349. } else {
  350. iovec = &instance->totemudp_iov_recv;
  351. }
  352. /*
  353. * Receive datagram
  354. */
  355. msg_recv.msg_name = &system_from;
  356. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  357. msg_recv.msg_iov = iovec;
  358. msg_recv.msg_iovlen = 1;
  359. #ifdef HAVE_MSGHDR_CONTROL
  360. msg_recv.msg_control = 0;
  361. #endif
  362. #ifdef HAVE_MSGHDR_CONTROLLEN
  363. msg_recv.msg_controllen = 0;
  364. #endif
  365. #ifdef HAVE_MSGHDR_FLAGS
  366. msg_recv.msg_flags = 0;
  367. #endif
  368. #ifdef HAVE_MSGHDR_ACCRIGHTS
  369. msg_recv.msg_accrights = NULL;
  370. #endif
  371. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  372. msg_recv.msg_accrightslen = 0;
  373. #endif
  374. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  375. if (bytes_received == -1) {
  376. return (0);
  377. } else {
  378. instance->stats_recv += bytes_received;
  379. }
  380. iovec->iov_len = bytes_received;
  381. /*
  382. * Drop all non-mcast messages (more specifically join
  383. * messages should be dropped)
  384. */
  385. message_type = (char *)iovec->iov_base;
  386. if (instance->flushing == 1 && *message_type == MESSAGE_TYPE_MEMB_JOIN) {
  387. log_printf(instance->totemudp_log_level_warning, "JOIN or LEAVE message was thrown away during flush operation.");
  388. iovec->iov_len = FRAME_SIZE_MAX;
  389. return (0);
  390. }
  391. /*
  392. * Handle incoming message
  393. */
  394. instance->totemudp_deliver_fn (
  395. instance->context,
  396. iovec->iov_base,
  397. iovec->iov_len);
  398. iovec->iov_len = FRAME_SIZE_MAX;
  399. return (0);
  400. }
  401. static int netif_determine (
  402. struct totemudp_instance *instance,
  403. struct totem_ip_address *bindnet,
  404. struct totem_ip_address *bound_to,
  405. int *interface_up,
  406. int *interface_num)
  407. {
  408. int res;
  409. res = totemip_iface_check (bindnet, bound_to,
  410. interface_up, interface_num,
  411. instance->totem_config->clear_node_high_bit);
  412. return (res);
  413. }
  414. /*
  415. * If the interface is up, the sockets for totem are built. If the interface is down
  416. * this function is requeued in the timer list to retry building the sockets later.
  417. */
  418. static void timer_function_netif_check_timeout (
  419. void *data)
  420. {
  421. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  422. int interface_up;
  423. int interface_num;
  424. struct totem_ip_address *bind_address;
  425. /*
  426. * Build sockets for every interface
  427. */
  428. netif_determine (instance,
  429. &instance->totem_interface->bindnet,
  430. &instance->totem_interface->boundto,
  431. &interface_up, &interface_num);
  432. /*
  433. * If the network interface isn't back up and we are already
  434. * in loopback mode, add timer to check again and return
  435. */
  436. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  437. interface_up == 0) ||
  438. (instance->my_memb_entries == 1 &&
  439. instance->netif_bind_state == BIND_STATE_REGULAR &&
  440. interface_up == 1)) {
  441. qb_loop_timer_add (instance->totemudp_poll_handle,
  442. QB_LOOP_MED,
  443. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  444. (void *)instance,
  445. timer_function_netif_check_timeout,
  446. &instance->timer_netif_check_timeout);
  447. /*
  448. * Add a timer to check for a downed regular interface
  449. */
  450. return;
  451. }
  452. if (instance->totemudp_sockets.mcast_recv > 0) {
  453. qb_loop_poll_del (instance->totemudp_poll_handle,
  454. instance->totemudp_sockets.mcast_recv);
  455. close (instance->totemudp_sockets.mcast_recv);
  456. }
  457. if (instance->totemudp_sockets.mcast_send > 0) {
  458. close (instance->totemudp_sockets.mcast_send);
  459. }
  460. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  461. qb_loop_poll_del (instance->totemudp_poll_handle,
  462. instance->totemudp_sockets.local_mcast_loop[0]);
  463. close (instance->totemudp_sockets.local_mcast_loop[0]);
  464. close (instance->totemudp_sockets.local_mcast_loop[1]);
  465. }
  466. if (instance->totemudp_sockets.token > 0) {
  467. qb_loop_poll_del (instance->totemudp_poll_handle,
  468. instance->totemudp_sockets.token);
  469. close (instance->totemudp_sockets.token);
  470. }
  471. if (interface_up == 0) {
  472. /*
  473. * Interface is not up
  474. */
  475. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  476. bind_address = &localhost;
  477. /*
  478. * Add a timer to retry building interfaces and request memb_gather_enter
  479. */
  480. qb_loop_timer_add (instance->totemudp_poll_handle,
  481. QB_LOOP_MED,
  482. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  483. (void *)instance,
  484. timer_function_netif_check_timeout,
  485. &instance->timer_netif_check_timeout);
  486. } else {
  487. /*
  488. * Interface is up
  489. */
  490. instance->netif_bind_state = BIND_STATE_REGULAR;
  491. bind_address = &instance->totem_interface->bindnet;
  492. }
  493. /*
  494. * Create and bind the multicast and unicast sockets
  495. */
  496. (void)totemudp_build_sockets (instance,
  497. &instance->mcast_address,
  498. bind_address,
  499. &instance->totemudp_sockets,
  500. &instance->totem_interface->boundto);
  501. qb_loop_poll_add (
  502. instance->totemudp_poll_handle,
  503. QB_LOOP_MED,
  504. instance->totemudp_sockets.mcast_recv,
  505. POLLIN, instance, net_deliver_fn);
  506. qb_loop_poll_add (
  507. instance->totemudp_poll_handle,
  508. QB_LOOP_MED,
  509. instance->totemudp_sockets.local_mcast_loop[0],
  510. POLLIN, instance, net_deliver_fn);
  511. qb_loop_poll_add (
  512. instance->totemudp_poll_handle,
  513. QB_LOOP_MED,
  514. instance->totemudp_sockets.token,
  515. POLLIN, instance, net_deliver_fn);
  516. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  517. /*
  518. * This reports changes in the interface to the user and totemsrp
  519. */
  520. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  521. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  522. log_printf (instance->totemudp_log_level_notice,
  523. "The network interface [%s] is now up.",
  524. totemip_print (&instance->totem_interface->boundto));
  525. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  526. instance->totemudp_iface_change_fn (instance->context, &instance->my_id, 0);
  527. }
  528. /*
  529. * Add a timer to check for interface going down in single membership
  530. */
  531. if (instance->my_memb_entries == 1) {
  532. qb_loop_timer_add (instance->totemudp_poll_handle,
  533. QB_LOOP_MED,
  534. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  535. (void *)instance,
  536. timer_function_netif_check_timeout,
  537. &instance->timer_netif_check_timeout);
  538. }
  539. } else {
  540. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  541. log_printf (instance->totemudp_log_level_notice,
  542. "The network interface is down.");
  543. instance->totemudp_iface_change_fn (instance->context, &instance->my_id, 0);
  544. }
  545. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  546. }
  547. }
  548. /* Set the socket priority to INTERACTIVE to ensure
  549. that our messages don't get queued behind anything else */
  550. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  551. {
  552. #ifdef SO_PRIORITY
  553. int prio = 6; /* TC_PRIO_INTERACTIVE */
  554. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  555. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  556. }
  557. #endif
  558. }
  559. static int totemudp_build_sockets_ip (
  560. struct totemudp_instance *instance,
  561. struct totem_ip_address *mcast_address,
  562. struct totem_ip_address *bindnet_address,
  563. struct totemudp_socket *sockets,
  564. struct totem_ip_address *bound_to,
  565. int interface_num)
  566. {
  567. struct sockaddr_storage sockaddr;
  568. struct ipv6_mreq mreq6;
  569. struct ip_mreq mreq;
  570. struct sockaddr_storage mcast_ss, boundto_ss;
  571. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  572. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  573. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  574. unsigned int sendbuf_size;
  575. unsigned int recvbuf_size;
  576. unsigned int optlen = sizeof (sendbuf_size);
  577. int addrlen;
  578. int res;
  579. int flag;
  580. uint8_t sflag;
  581. int i;
  582. /*
  583. * Create multicast recv socket
  584. */
  585. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  586. if (sockets->mcast_recv == -1) {
  587. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  588. "socket() failed");
  589. return (-1);
  590. }
  591. totemip_nosigpipe (sockets->mcast_recv);
  592. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  593. if (res == -1) {
  594. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  595. "Could not set non-blocking operation on multicast socket");
  596. return (-1);
  597. }
  598. /*
  599. * Force reuse
  600. */
  601. flag = 1;
  602. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  603. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  604. "setsockopt(SO_REUSEADDR) failed");
  605. return (-1);
  606. }
  607. /*
  608. * Create local multicast loop socket
  609. */
  610. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
  611. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  612. "socket() failed");
  613. return (-1);
  614. }
  615. for (i = 0; i < 2; i++) {
  616. totemip_nosigpipe (sockets->local_mcast_loop[i]);
  617. res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
  618. if (res == -1) {
  619. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  620. "Could not set non-blocking operation on multicast socket");
  621. return (-1);
  622. }
  623. }
  624. /*
  625. * Setup mcast send socket
  626. */
  627. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  628. if (sockets->mcast_send == -1) {
  629. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  630. "socket() failed");
  631. return (-1);
  632. }
  633. totemip_nosigpipe (sockets->mcast_send);
  634. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  635. if (res == -1) {
  636. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  637. "Could not set non-blocking operation on multicast socket");
  638. return (-1);
  639. }
  640. /*
  641. * Force reuse
  642. */
  643. flag = 1;
  644. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  645. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  646. "setsockopt(SO_REUSEADDR) failed");
  647. return (-1);
  648. }
  649. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  650. &sockaddr, &addrlen);
  651. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  652. if (res == -1) {
  653. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  654. "Unable to bind the socket to send multicast packets");
  655. return (-1);
  656. }
  657. /*
  658. * Setup unicast socket
  659. */
  660. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  661. if (sockets->token == -1) {
  662. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  663. "socket() failed");
  664. return (-1);
  665. }
  666. totemip_nosigpipe (sockets->token);
  667. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  668. if (res == -1) {
  669. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  670. "Could not set non-blocking operation on token socket");
  671. return (-1);
  672. }
  673. /*
  674. * Force reuse
  675. */
  676. flag = 1;
  677. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  678. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  679. "setsockopt(SO_REUSEADDR) failed");
  680. return (-1);
  681. }
  682. /*
  683. * Bind to unicast socket used for token send/receives
  684. * This has the side effect of binding to the correct interface
  685. */
  686. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  687. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  688. if (res == -1) {
  689. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  690. "Unable to bind UDP unicast socket");
  691. return (-1);
  692. }
  693. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  694. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  695. /*
  696. * Set buffer sizes to avoid overruns
  697. */
  698. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  699. if (res == -1) {
  700. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  701. "Unable to set SO_RCVBUF size on UDP mcast socket");
  702. return (-1);
  703. }
  704. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  705. if (res == -1) {
  706. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  707. "Unable to set SO_SNDBUF size on UDP mcast socket");
  708. return (-1);
  709. }
  710. res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  711. if (res == -1) {
  712. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  713. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  714. return (-1);
  715. }
  716. res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  717. if (res == -1) {
  718. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  719. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  720. return (-1);
  721. }
  722. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  723. if (res == 0) {
  724. log_printf (instance->totemudp_log_level_debug,
  725. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  726. }
  727. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  728. if (res == 0) {
  729. log_printf (instance->totemudp_log_level_debug,
  730. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  731. }
  732. res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  733. if (res == 0) {
  734. log_printf (instance->totemudp_log_level_debug,
  735. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  736. }
  737. res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  738. if (res == 0) {
  739. log_printf (instance->totemudp_log_level_debug,
  740. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  741. }
  742. /*
  743. * Join group membership on socket
  744. */
  745. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  746. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  747. if (instance->totem_config->broadcast_use == 1) {
  748. unsigned int broadcast = 1;
  749. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  750. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  751. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  752. "setting broadcast option failed");
  753. return (-1);
  754. }
  755. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  756. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  757. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  758. "setting broadcast option failed");
  759. return (-1);
  760. }
  761. } else {
  762. switch (bindnet_address->family) {
  763. case AF_INET:
  764. memset(&mreq, 0, sizeof(mreq));
  765. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  766. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  767. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  768. &mreq, sizeof (mreq));
  769. if (res == -1) {
  770. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  771. "join ipv4 multicast group failed");
  772. return (-1);
  773. }
  774. break;
  775. case AF_INET6:
  776. memset(&mreq6, 0, sizeof(mreq6));
  777. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  778. mreq6.ipv6mr_interface = interface_num;
  779. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  780. &mreq6, sizeof (mreq6));
  781. if (res == -1) {
  782. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  783. "join ipv6 multicast group failed");
  784. return (-1);
  785. }
  786. break;
  787. }
  788. }
  789. /*
  790. * Turn off multicast loopback
  791. */
  792. flag = 0;
  793. switch ( bindnet_address->family ) {
  794. case AF_INET:
  795. sflag = 0;
  796. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  797. &sflag, sizeof (sflag));
  798. break;
  799. case AF_INET6:
  800. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  801. &flag, sizeof (flag));
  802. }
  803. if (res == -1) {
  804. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  805. "Unable to turn off multicast loopback");
  806. return (-1);
  807. }
  808. /*
  809. * Set multicast packets TTL
  810. */
  811. flag = instance->totem_interface->ttl;
  812. if (bindnet_address->family == AF_INET6) {
  813. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  814. &flag, sizeof (flag));
  815. if (res == -1) {
  816. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  817. "set mcast v6 TTL failed");
  818. return (-1);
  819. }
  820. } else {
  821. sflag = flag;
  822. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  823. &sflag, sizeof(sflag));
  824. if (res == -1) {
  825. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  826. "set mcast v4 TTL failed");
  827. return (-1);
  828. }
  829. }
  830. /*
  831. * Bind to a specific interface for multicast send and receive
  832. */
  833. switch ( bindnet_address->family ) {
  834. case AF_INET:
  835. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  836. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  837. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  838. "cannot select interface for multicast packets (send)");
  839. return (-1);
  840. }
  841. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  842. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  843. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  844. "cannot select interface for multicast packets (recv)");
  845. return (-1);
  846. }
  847. break;
  848. case AF_INET6:
  849. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  850. &interface_num, sizeof (interface_num)) < 0) {
  851. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  852. "cannot select interface for multicast packets (send v6)");
  853. return (-1);
  854. }
  855. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  856. &interface_num, sizeof (interface_num)) < 0) {
  857. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  858. "cannot select interface for multicast packets (recv v6)");
  859. return (-1);
  860. }
  861. break;
  862. }
  863. /*
  864. * Bind to multicast socket used for multicast receives
  865. * This needs to happen after all of the multicast setsockopt() calls
  866. * as the kernel seems to only put them into effect (for IPV6) when bind()
  867. * is called.
  868. */
  869. totemip_totemip_to_sockaddr_convert(mcast_address,
  870. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  871. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  872. if (res == -1) {
  873. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  874. "Unable to bind the socket to receive multicast packets");
  875. return (-1);
  876. }
  877. return 0;
  878. }
  879. static int totemudp_build_sockets (
  880. struct totemudp_instance *instance,
  881. struct totem_ip_address *mcast_address,
  882. struct totem_ip_address *bindnet_address,
  883. struct totemudp_socket *sockets,
  884. struct totem_ip_address *bound_to)
  885. {
  886. int interface_num;
  887. int interface_up;
  888. int res;
  889. /*
  890. * Determine the ip address bound to and the interface name
  891. */
  892. res = netif_determine (instance,
  893. bindnet_address,
  894. bound_to,
  895. &interface_up,
  896. &interface_num);
  897. if (res == -1) {
  898. return (-1);
  899. }
  900. totemip_copy(&instance->my_id, bound_to);
  901. res = totemudp_build_sockets_ip (instance, mcast_address,
  902. bindnet_address, sockets, bound_to, interface_num);
  903. /* We only send out of the token socket */
  904. totemudp_traffic_control_set(instance, sockets->token);
  905. return res;
  906. }
  907. /*
  908. * Totem Network interface
  909. * depends on poll abstraction, POSIX, IPV4
  910. */
  911. /*
  912. * Create an instance
  913. */
  914. int totemudp_initialize (
  915. qb_loop_t *poll_handle,
  916. void **udp_context,
  917. struct totem_config *totem_config,
  918. totemsrp_stats_t *stats,
  919. void *context,
  920. void (*deliver_fn) (
  921. void *context,
  922. const void *msg,
  923. unsigned int msg_len),
  924. void (*iface_change_fn) (
  925. void *context,
  926. const struct totem_ip_address *iface_address,
  927. unsigned int ring_no),
  928. void (*mtu_changed) (
  929. void *context,
  930. int net_mtu),
  931. void (*target_set_completed) (
  932. void *context))
  933. {
  934. struct totemudp_instance *instance;
  935. instance = malloc (sizeof (struct totemudp_instance));
  936. if (instance == NULL) {
  937. return (-1);
  938. }
  939. totemudp_instance_initialize (instance);
  940. instance->totem_config = totem_config;
  941. instance->stats = stats;
  942. /*
  943. * Configure logging
  944. */
  945. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  946. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  947. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  948. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  949. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  950. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  951. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  952. /*
  953. * Initialize local variables for totemudp
  954. */
  955. instance->totem_interface = &totem_config->interfaces[0];
  956. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  957. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  958. instance->totemudp_poll_handle = poll_handle;
  959. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  960. instance->context = context;
  961. instance->totemudp_deliver_fn = deliver_fn;
  962. instance->totemudp_iface_change_fn = iface_change_fn;
  963. instance->totemudp_target_set_completed = target_set_completed;
  964. totemip_localhost (instance->mcast_address.family, &localhost);
  965. localhost.nodeid = instance->totem_config->node_id;
  966. /*
  967. * RRP layer isn't ready to receive message because it hasn't
  968. * initialized yet. Add short timer to check the interfaces.
  969. */
  970. qb_loop_timer_add (instance->totemudp_poll_handle,
  971. QB_LOOP_MED,
  972. 100*QB_TIME_NS_IN_MSEC,
  973. (void *)instance,
  974. timer_function_netif_check_timeout,
  975. &instance->timer_netif_check_timeout);
  976. *udp_context = instance;
  977. return (0);
  978. }
  979. void *totemudp_buffer_alloc (void)
  980. {
  981. return malloc (FRAME_SIZE_MAX);
  982. }
  983. void totemudp_buffer_release (void *ptr)
  984. {
  985. return free (ptr);
  986. }
  987. int totemudp_processor_count_set (
  988. void *udp_context,
  989. int processor_count)
  990. {
  991. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  992. int res = 0;
  993. instance->my_memb_entries = processor_count;
  994. qb_loop_timer_del (instance->totemudp_poll_handle,
  995. instance->timer_netif_check_timeout);
  996. if (processor_count == 1) {
  997. qb_loop_timer_add (instance->totemudp_poll_handle,
  998. QB_LOOP_MED,
  999. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1000. (void *)instance,
  1001. timer_function_netif_check_timeout,
  1002. &instance->timer_netif_check_timeout);
  1003. }
  1004. return (res);
  1005. }
  1006. int totemudp_recv_flush (void *udp_context)
  1007. {
  1008. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1009. struct pollfd ufd;
  1010. int nfds;
  1011. int res = 0;
  1012. int i;
  1013. int sock;
  1014. instance->flushing = 1;
  1015. for (i = 0; i < 2; i++) {
  1016. sock = -1;
  1017. if (i == 0) {
  1018. sock = instance->totemudp_sockets.mcast_recv;
  1019. }
  1020. if (i == 1) {
  1021. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1022. }
  1023. assert(sock != -1);
  1024. do {
  1025. ufd.fd = sock;
  1026. ufd.events = POLLIN;
  1027. nfds = poll (&ufd, 1, 0);
  1028. if (nfds == 1 && ufd.revents & POLLIN) {
  1029. net_deliver_fn (sock, ufd.revents, instance);
  1030. }
  1031. } while (nfds == 1);
  1032. }
  1033. instance->flushing = 0;
  1034. return (res);
  1035. }
  1036. int totemudp_send_flush (void *udp_context)
  1037. {
  1038. return 0;
  1039. }
  1040. int totemudp_token_send (
  1041. void *udp_context,
  1042. const void *msg,
  1043. unsigned int msg_len)
  1044. {
  1045. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1046. int res = 0;
  1047. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1048. return (res);
  1049. }
  1050. int totemudp_mcast_flush_send (
  1051. void *udp_context,
  1052. const void *msg,
  1053. unsigned int msg_len)
  1054. {
  1055. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1056. int res = 0;
  1057. mcast_sendmsg (instance, msg, msg_len);
  1058. return (res);
  1059. }
  1060. int totemudp_mcast_noflush_send (
  1061. void *udp_context,
  1062. const void *msg,
  1063. unsigned int msg_len)
  1064. {
  1065. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1066. int res = 0;
  1067. mcast_sendmsg (instance, msg, msg_len);
  1068. return (res);
  1069. }
  1070. extern int totemudp_iface_check (void *udp_context)
  1071. {
  1072. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1073. int res = 0;
  1074. timer_function_netif_check_timeout (instance);
  1075. return (res);
  1076. }
  1077. int totemudp_ifaces_get (
  1078. void *net_context,
  1079. char ***status,
  1080. unsigned int *iface_count)
  1081. {
  1082. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  1083. if (status) {
  1084. *status = statuses;
  1085. }
  1086. *iface_count = 1;
  1087. return (0);
  1088. }
  1089. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1090. {
  1091. assert(totem_config->interface_count > 0);
  1092. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  1093. }
  1094. int totemudp_token_target_set (
  1095. void *udp_context,
  1096. const struct totem_ip_address *token_target)
  1097. {
  1098. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1099. int res = 0;
  1100. memcpy (&instance->token_target, token_target,
  1101. sizeof (struct totem_ip_address));
  1102. instance->totemudp_target_set_completed (instance->context);
  1103. return (res);
  1104. }
  1105. extern int totemudp_recv_mcast_empty (
  1106. void *udp_context)
  1107. {
  1108. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1109. unsigned int res;
  1110. struct sockaddr_storage system_from;
  1111. struct msghdr msg_recv;
  1112. struct pollfd ufd;
  1113. int nfds;
  1114. int msg_processed = 0;
  1115. int i;
  1116. int sock;
  1117. /*
  1118. * Receive datagram
  1119. */
  1120. msg_recv.msg_name = &system_from;
  1121. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1122. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1123. msg_recv.msg_iovlen = 1;
  1124. #ifdef HAVE_MSGHDR_CONTROL
  1125. msg_recv.msg_control = 0;
  1126. #endif
  1127. #ifdef HAVE_MSGHDR_CONTROLLEN
  1128. msg_recv.msg_controllen = 0;
  1129. #endif
  1130. #ifdef HAVE_MSGHDR_FLAGS
  1131. msg_recv.msg_flags = 0;
  1132. #endif
  1133. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1134. msg_recv.msg_accrights = NULL;
  1135. #endif
  1136. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1137. msg_recv.msg_accrightslen = 0;
  1138. #endif
  1139. for (i = 0; i < 2; i++) {
  1140. sock = -1;
  1141. if (i == 0) {
  1142. sock = instance->totemudp_sockets.mcast_recv;
  1143. }
  1144. if (i == 1) {
  1145. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1146. }
  1147. assert(sock != -1);
  1148. do {
  1149. ufd.fd = sock;
  1150. ufd.events = POLLIN;
  1151. nfds = poll (&ufd, 1, 0);
  1152. if (nfds == 1 && ufd.revents & POLLIN) {
  1153. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1154. if (res != -1) {
  1155. msg_processed = 1;
  1156. } else {
  1157. msg_processed = -1;
  1158. }
  1159. }
  1160. } while (nfds == 1);
  1161. }
  1162. return (msg_processed);
  1163. }