totemudp.c 37 KB

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