totemudp.c 39 KB

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