totemudp.c 36 KB

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