totemudp.c 40 KB

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