totemudp.c 37 KB

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