4
0

totemudp.c 39 KB

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