totemudpu.c 38 KB

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