totemudp.c 36 KB

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