4
0

totemudp.c 39 KB

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