4
0

totemudpu.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. instance->netif_bind_state == BIND_STATE_REGULAR &&
  494. find_member_by_sockaddr(instance, (const struct sockaddr *)&system_from) == NULL) {
  495. log_printf(instance->totemudpu_log_level_debug, "Packet rejected from %s",
  496. totemip_sa_print((const struct sockaddr *)&system_from));
  497. return (0);
  498. }
  499. /*
  500. * Authenticate and if authenticated, decrypt datagram
  501. */
  502. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  503. if (res == -1) {
  504. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.");
  505. log_printf (instance->totemudpu_log_level_security,
  506. "Invalid packet data");
  507. iovec->iov_len = FRAME_SIZE_MAX;
  508. return 0;
  509. }
  510. iovec->iov_len = bytes_received;
  511. /*
  512. * Handle incoming message
  513. */
  514. instance->totemudpu_deliver_fn (
  515. instance->context,
  516. iovec->iov_base,
  517. iovec->iov_len);
  518. iovec->iov_len = FRAME_SIZE_MAX;
  519. return (0);
  520. }
  521. static int netif_determine (
  522. struct totemudpu_instance *instance,
  523. struct totem_ip_address *bindnet,
  524. struct totem_ip_address *bound_to,
  525. int *interface_up,
  526. int *interface_num)
  527. {
  528. int res;
  529. res = totemip_iface_check (bindnet, bound_to,
  530. interface_up, interface_num,
  531. instance->totem_config->clear_node_high_bit);
  532. return (res);
  533. }
  534. /*
  535. * If the interface is up, the sockets for totem are built. If the interface is down
  536. * this function is requeued in the timer list to retry building the sockets later.
  537. */
  538. static void timer_function_netif_check_timeout (
  539. void *data)
  540. {
  541. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  542. int interface_up;
  543. int interface_num;
  544. /*
  545. * Build sockets for every interface
  546. */
  547. netif_determine (instance,
  548. &instance->totem_interface->bindnet,
  549. &instance->totem_interface->boundto,
  550. &interface_up, &interface_num);
  551. /*
  552. * If the network interface isn't back up and we are already
  553. * in loopback mode, add timer to check again and return
  554. */
  555. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  556. interface_up == 0) ||
  557. (instance->my_memb_entries == 1 &&
  558. instance->netif_bind_state == BIND_STATE_REGULAR &&
  559. interface_up == 1)) {
  560. qb_loop_timer_add (instance->totemudpu_poll_handle,
  561. QB_LOOP_MED,
  562. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  563. (void *)instance,
  564. timer_function_netif_check_timeout,
  565. &instance->timer_netif_check_timeout);
  566. /*
  567. * Add a timer to check for a downed regular interface
  568. */
  569. return;
  570. }
  571. if (instance->token_socket > 0) {
  572. qb_loop_poll_del (instance->totemudpu_poll_handle,
  573. instance->token_socket);
  574. close (instance->token_socket);
  575. instance->token_socket = -1;
  576. }
  577. if (interface_up == 0) {
  578. if (instance->netif_bind_state == BIND_STATE_UNBOUND) {
  579. log_printf (instance->totemudpu_log_level_error,
  580. "One of your ip addresses are now bound to localhost. "
  581. "Corosync would not work correctly.");
  582. exit(COROSYNC_DONE_FATAL_ERR);
  583. }
  584. /*
  585. * Interface is not up
  586. */
  587. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  588. /*
  589. * Add a timer to retry building interfaces and request memb_gather_enter
  590. */
  591. qb_loop_timer_add (instance->totemudpu_poll_handle,
  592. QB_LOOP_MED,
  593. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  594. (void *)instance,
  595. timer_function_netif_check_timeout,
  596. &instance->timer_netif_check_timeout);
  597. } else {
  598. /*
  599. * Interface is up
  600. */
  601. instance->netif_bind_state = BIND_STATE_REGULAR;
  602. }
  603. /*
  604. * Create and bind the multicast and unicast sockets
  605. */
  606. totemudpu_build_sockets (instance,
  607. &instance->totem_interface->bindnet,
  608. &instance->totem_interface->boundto);
  609. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  610. qb_loop_poll_add (instance->totemudpu_poll_handle,
  611. QB_LOOP_MED,
  612. instance->token_socket,
  613. POLLIN, instance, net_deliver_fn);
  614. }
  615. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  616. /*
  617. * This reports changes in the interface to the user and totemsrp
  618. */
  619. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  620. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  621. log_printf (instance->totemudpu_log_level_notice,
  622. "The network interface [%s] is now up.",
  623. totemip_print (&instance->totem_interface->boundto));
  624. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  625. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  626. }
  627. /*
  628. * Add a timer to check for interface going down in single membership
  629. */
  630. if (instance->my_memb_entries == 1) {
  631. qb_loop_timer_add (instance->totemudpu_poll_handle,
  632. QB_LOOP_MED,
  633. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  634. (void *)instance,
  635. timer_function_netif_check_timeout,
  636. &instance->timer_netif_check_timeout);
  637. }
  638. } else {
  639. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  640. log_printf (instance->totemudpu_log_level_notice,
  641. "The network interface is down.");
  642. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  643. }
  644. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  645. }
  646. }
  647. /* Set the socket priority to INTERACTIVE to ensure
  648. that our messages don't get queued behind anything else */
  649. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  650. {
  651. #ifdef SO_PRIORITY
  652. int prio = 6; /* TC_PRIO_INTERACTIVE */
  653. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  654. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  655. "Could not set traffic priority");
  656. }
  657. #endif
  658. }
  659. static int totemudpu_build_sockets_ip (
  660. struct totemudpu_instance *instance,
  661. struct totem_ip_address *bindnet_address,
  662. struct totem_ip_address *bound_to,
  663. int interface_num)
  664. {
  665. struct sockaddr_storage sockaddr;
  666. int addrlen;
  667. int res;
  668. unsigned int recvbuf_size;
  669. unsigned int optlen = sizeof (recvbuf_size);
  670. unsigned int retries = 0;
  671. /*
  672. * Setup unicast socket
  673. */
  674. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  675. if (instance->token_socket == -1) {
  676. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  677. "socket() failed");
  678. return (-1);
  679. }
  680. totemip_nosigpipe (instance->token_socket);
  681. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  682. if (res == -1) {
  683. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  684. "Could not set non-blocking operation on token socket");
  685. return (-1);
  686. }
  687. /*
  688. * Bind to unicast socket used for token send/receives
  689. * This has the side effect of binding to the correct interface
  690. */
  691. totemip_totemip_to_sockaddr_convert_with_scopeid(bound_to, instance->totem_interface->ip_port,
  692. &sockaddr, &addrlen, 1);
  693. while (1) {
  694. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  695. if (res == 0) {
  696. break;
  697. }
  698. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  699. "bind token socket failed");
  700. if (++retries > BIND_MAX_RETRIES) {
  701. break;
  702. }
  703. /*
  704. * Wait for a while
  705. */
  706. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  707. }
  708. if (res == -1) {
  709. return (-1);
  710. }
  711. /*
  712. * the token_socket can receive many messages. Allow a large number
  713. * of receive messages on this socket
  714. */
  715. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  716. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  717. &recvbuf_size, optlen);
  718. if (res == -1) {
  719. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  720. "Could not set recvbuf size");
  721. }
  722. return 0;
  723. }
  724. static int totemudpu_build_local_sockets(
  725. struct totemudpu_instance *instance)
  726. {
  727. int i;
  728. unsigned int sendbuf_size;
  729. unsigned int recvbuf_size;
  730. unsigned int optlen = sizeof (sendbuf_size);
  731. int res;
  732. /*
  733. * Create local multicast loop socket
  734. */
  735. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, instance->local_loop_sock) == -1) {
  736. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  737. "socket() failed");
  738. return (-1);
  739. }
  740. for (i = 0; i < 2; i++) {
  741. totemip_nosigpipe (instance->local_loop_sock[i]);
  742. res = fcntl (instance->local_loop_sock[i], F_SETFL, O_NONBLOCK);
  743. if (res == -1) {
  744. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  745. "Could not set non-blocking operation on multicast socket");
  746. return (-1);
  747. }
  748. }
  749. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  750. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  751. res = setsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  752. if (res == -1) {
  753. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  754. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  755. return (-1);
  756. }
  757. res = setsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  758. if (res == -1) {
  759. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  760. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  761. return (-1);
  762. }
  763. res = getsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  764. if (res == 0) {
  765. log_printf (instance->totemudpu_log_level_debug,
  766. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  767. }
  768. res = getsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  769. if (res == 0) {
  770. log_printf (instance->totemudpu_log_level_debug,
  771. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  772. }
  773. return (0);
  774. }
  775. static int totemudpu_build_sockets (
  776. struct totemudpu_instance *instance,
  777. struct totem_ip_address *bindnet_address,
  778. struct totem_ip_address *bound_to)
  779. {
  780. int interface_num;
  781. int interface_up;
  782. int res;
  783. /*
  784. * Determine the ip address bound to and the interface name
  785. */
  786. res = netif_determine (instance,
  787. bindnet_address,
  788. bound_to,
  789. &interface_up,
  790. &interface_num);
  791. if (res == -1) {
  792. return (-1);
  793. }
  794. totemip_copy(&instance->my_id, bound_to);
  795. res = totemudpu_build_sockets_ip (instance,
  796. bindnet_address, bound_to, interface_num);
  797. if (res == -1) {
  798. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  799. LOGSYS_PERROR (errno, instance->totemudpu_log_level_error,
  800. "Unable to create sockets, exiting");
  801. exit(EXIT_FAILURE);
  802. }
  803. /* We only send out of the token socket */
  804. totemudpu_traffic_control_set(instance, instance->token_socket);
  805. /*
  806. * Rebind all members to new ips
  807. */
  808. totemudpu_member_list_rebind_ip(instance);
  809. return res;
  810. }
  811. /*
  812. * Totem Network interface - also does encryption/decryption
  813. * depends on poll abstraction, POSIX, IPV4
  814. */
  815. /*
  816. * Create an instance
  817. */
  818. int totemudpu_initialize (
  819. qb_loop_t *poll_handle,
  820. void **udpu_context,
  821. struct totem_config *totem_config,
  822. totemsrp_stats_t *stats,
  823. int interface_no,
  824. void *context,
  825. void (*deliver_fn) (
  826. void *context,
  827. const void *msg,
  828. unsigned int msg_len),
  829. void (*iface_change_fn) (
  830. void *context,
  831. const struct totem_ip_address *iface_address),
  832. void (*target_set_completed) (
  833. void *context))
  834. {
  835. struct totemudpu_instance *instance;
  836. instance = malloc (sizeof (struct totemudpu_instance));
  837. if (instance == NULL) {
  838. return (-1);
  839. }
  840. totemudpu_instance_initialize (instance);
  841. instance->totem_config = totem_config;
  842. instance->stats = stats;
  843. /*
  844. * Configure logging
  845. */
  846. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  847. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  848. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  849. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  850. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  851. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  852. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  853. /*
  854. * Initialize random number generator for later use to generate salt
  855. */
  856. instance->crypto_inst = crypto_init (totem_config->private_key,
  857. totem_config->private_key_len,
  858. totem_config->crypto_cipher_type,
  859. totem_config->crypto_hash_type,
  860. instance->totemudpu_log_printf,
  861. instance->totemudpu_log_level_security,
  862. instance->totemudpu_log_level_notice,
  863. instance->totemudpu_log_level_error,
  864. instance->totemudpu_subsys_id);
  865. if (instance->crypto_inst == NULL) {
  866. free(instance);
  867. return (-1);
  868. }
  869. /*
  870. * Initialize local variables for totemudpu
  871. */
  872. instance->totem_interface = &totem_config->interfaces[interface_no];
  873. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  874. instance->totemudpu_poll_handle = poll_handle;
  875. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  876. instance->context = context;
  877. instance->totemudpu_deliver_fn = deliver_fn;
  878. instance->totemudpu_iface_change_fn = iface_change_fn;
  879. instance->totemudpu_target_set_completed = target_set_completed;
  880. /*
  881. * Create static local mcast sockets
  882. */
  883. if (totemudpu_build_local_sockets(instance) == -1) {
  884. free(instance);
  885. return (-1);
  886. }
  887. qb_loop_poll_add (
  888. instance->totemudpu_poll_handle,
  889. QB_LOOP_MED,
  890. instance->local_loop_sock[0],
  891. POLLIN, instance, net_deliver_fn);
  892. /*
  893. * RRP layer isn't ready to receive message because it hasn't
  894. * initialized yet. Add short timer to check the interfaces.
  895. */
  896. qb_loop_timer_add (instance->totemudpu_poll_handle,
  897. QB_LOOP_MED,
  898. 100*QB_TIME_NS_IN_MSEC,
  899. (void *)instance,
  900. timer_function_netif_check_timeout,
  901. &instance->timer_netif_check_timeout);
  902. totemudpu_start_merge_detect_timeout((void*)instance);
  903. *udpu_context = instance;
  904. return (0);
  905. }
  906. void *totemudpu_buffer_alloc (void)
  907. {
  908. return malloc (FRAME_SIZE_MAX);
  909. }
  910. void totemudpu_buffer_release (void *ptr)
  911. {
  912. return free (ptr);
  913. }
  914. int totemudpu_processor_count_set (
  915. void *udpu_context,
  916. int processor_count)
  917. {
  918. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  919. int res = 0;
  920. instance->my_memb_entries = processor_count;
  921. qb_loop_timer_del (instance->totemudpu_poll_handle,
  922. instance->timer_netif_check_timeout);
  923. if (processor_count == 1) {
  924. qb_loop_timer_add (instance->totemudpu_poll_handle,
  925. QB_LOOP_MED,
  926. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  927. (void *)instance,
  928. timer_function_netif_check_timeout,
  929. &instance->timer_netif_check_timeout);
  930. }
  931. return (res);
  932. }
  933. int totemudpu_recv_flush (void *udpu_context)
  934. {
  935. int res = 0;
  936. return (res);
  937. }
  938. int totemudpu_send_flush (void *udpu_context)
  939. {
  940. int res = 0;
  941. return (res);
  942. }
  943. int totemudpu_token_send (
  944. void *udpu_context,
  945. const void *msg,
  946. unsigned int msg_len)
  947. {
  948. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  949. int res = 0;
  950. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  951. return (res);
  952. }
  953. int totemudpu_mcast_flush_send (
  954. void *udpu_context,
  955. const void *msg,
  956. unsigned int msg_len)
  957. {
  958. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  959. int res = 0;
  960. mcast_sendmsg (instance, msg, msg_len, 0);
  961. return (res);
  962. }
  963. int totemudpu_mcast_noflush_send (
  964. void *udpu_context,
  965. const void *msg,
  966. unsigned int msg_len)
  967. {
  968. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  969. int res = 0;
  970. mcast_sendmsg (instance, msg, msg_len, 1);
  971. return (res);
  972. }
  973. extern int totemudpu_iface_check (void *udpu_context)
  974. {
  975. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  976. int res = 0;
  977. timer_function_netif_check_timeout (instance);
  978. return (res);
  979. }
  980. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  981. {
  982. assert(totem_config->interface_count > 0);
  983. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  984. totem_config->crypto_hash_type) +
  985. totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  986. }
  987. const char *totemudpu_iface_print (void *udpu_context) {
  988. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  989. const char *ret_char;
  990. ret_char = totemip_print (&instance->my_id);
  991. return (ret_char);
  992. }
  993. int totemudpu_iface_get (
  994. void *udpu_context,
  995. struct totem_ip_address *addr)
  996. {
  997. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  998. int res = 0;
  999. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1000. return (res);
  1001. }
  1002. int totemudpu_token_target_set (
  1003. void *udpu_context,
  1004. const struct totem_ip_address *token_target)
  1005. {
  1006. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1007. int res = 0;
  1008. memcpy (&instance->token_target, token_target,
  1009. sizeof (struct totem_ip_address));
  1010. instance->totemudpu_target_set_completed (instance->context);
  1011. return (res);
  1012. }
  1013. extern int totemudpu_recv_mcast_empty (
  1014. void *udpu_context)
  1015. {
  1016. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1017. unsigned int res;
  1018. struct sockaddr_storage system_from;
  1019. struct msghdr msg_recv;
  1020. struct pollfd ufd;
  1021. int nfds, i;
  1022. int msg_processed = 0;
  1023. int sock;
  1024. /*
  1025. * Receive datagram
  1026. */
  1027. msg_recv.msg_name = &system_from;
  1028. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1029. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  1030. msg_recv.msg_iovlen = 1;
  1031. #ifdef HAVE_MSGHDR_CONTROL
  1032. msg_recv.msg_control = 0;
  1033. #endif
  1034. #ifdef HAVE_MSGHDR_CONTROLLEN
  1035. msg_recv.msg_controllen = 0;
  1036. #endif
  1037. #ifdef HAVE_MSGHDR_FLAGS
  1038. msg_recv.msg_flags = 0;
  1039. #endif
  1040. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1041. msg_recv.msg_accrights = NULL;
  1042. #endif
  1043. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1044. msg_recv.msg_accrightslen = 0;
  1045. #endif
  1046. for (i = 0; i < 2; i++) {
  1047. sock = -1;
  1048. if (i == 0) {
  1049. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1050. sock = instance->token_socket;
  1051. } else {
  1052. continue;
  1053. }
  1054. }
  1055. if (i == 1) {
  1056. sock = instance->local_loop_sock[0];
  1057. }
  1058. assert(sock != -1);
  1059. do {
  1060. ufd.fd = sock;
  1061. ufd.events = POLLIN;
  1062. nfds = poll (&ufd, 1, 0);
  1063. if (nfds == 1 && ufd.revents & POLLIN) {
  1064. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1065. if (res != -1) {
  1066. msg_processed = 1;
  1067. } else {
  1068. msg_processed = -1;
  1069. }
  1070. }
  1071. } while (nfds == 1);
  1072. }
  1073. return (msg_processed);
  1074. }
  1075. static int totemudpu_create_sending_socket(
  1076. void *udpu_context,
  1077. const struct totem_ip_address *member)
  1078. {
  1079. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1080. int fd;
  1081. int res;
  1082. unsigned int sendbuf_size;
  1083. unsigned int optlen = sizeof (sendbuf_size);
  1084. struct sockaddr_storage sockaddr;
  1085. int addrlen;
  1086. fd = socket (member->family, SOCK_DGRAM, 0);
  1087. if (fd == -1) {
  1088. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1089. "Could not create socket for new member");
  1090. return (-1);
  1091. }
  1092. totemip_nosigpipe (fd);
  1093. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  1094. if (res == -1) {
  1095. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1096. "Could not set non-blocking operation on token socket");
  1097. goto error_close_fd;
  1098. }
  1099. /*
  1100. * These sockets are used to send multicast messages, so their buffers
  1101. * should be large
  1102. */
  1103. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1104. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  1105. &sendbuf_size, optlen);
  1106. if (res == -1) {
  1107. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1108. "Could not set sendbuf size");
  1109. /*
  1110. * Fail in setting sendbuf size is not fatal -> don't exit
  1111. */
  1112. }
  1113. /*
  1114. * Bind to sending interface
  1115. */
  1116. totemip_totemip_to_sockaddr_convert_with_scopeid(&instance->my_id, 0, &sockaddr, &addrlen, 1);
  1117. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  1118. if (res == -1) {
  1119. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1120. "bind token socket failed");
  1121. goto error_close_fd;
  1122. }
  1123. return (fd);
  1124. error_close_fd:
  1125. close(fd);
  1126. return (-1);
  1127. }
  1128. int totemudpu_member_add (
  1129. void *udpu_context,
  1130. const struct totem_ip_address *member)
  1131. {
  1132. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1133. struct totemudpu_member *new_member;
  1134. new_member = malloc (sizeof (struct totemudpu_member));
  1135. if (new_member == NULL) {
  1136. return (-1);
  1137. }
  1138. memset(new_member, 0, sizeof(*new_member));
  1139. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  1140. totemip_print(member));
  1141. list_init (&new_member->list);
  1142. list_add_tail (&new_member->list, &instance->member_list);
  1143. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1144. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  1145. new_member->active = 0;
  1146. return (0);
  1147. }
  1148. int totemudpu_member_remove (
  1149. void *udpu_context,
  1150. const struct totem_ip_address *token_target)
  1151. {
  1152. int found = 0;
  1153. struct list_head *list;
  1154. struct totemudpu_member *member;
  1155. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1156. /*
  1157. * Find the member to remove and close its socket
  1158. */
  1159. for (list = instance->member_list.next;
  1160. list != &instance->member_list;
  1161. list = list->next) {
  1162. member = list_entry (list,
  1163. struct totemudpu_member,
  1164. list);
  1165. if (totemip_compare (token_target, &member->member)==0) {
  1166. log_printf(LOGSYS_LEVEL_NOTICE,
  1167. "removing UDPU member {%s}",
  1168. totemip_print(&member->member));
  1169. if (member->fd > 0) {
  1170. log_printf(LOGSYS_LEVEL_DEBUG,
  1171. "Closing socket to: {%s}",
  1172. totemip_print(&member->member));
  1173. qb_loop_poll_del (instance->totemudpu_poll_handle,
  1174. member->fd);
  1175. close (member->fd);
  1176. }
  1177. found = 1;
  1178. break;
  1179. }
  1180. }
  1181. /*
  1182. * Delete the member from the list
  1183. */
  1184. if (found) {
  1185. list_del (list);
  1186. }
  1187. instance = NULL;
  1188. return (0);
  1189. }
  1190. int totemudpu_member_list_rebind_ip (
  1191. void *udpu_context)
  1192. {
  1193. struct list_head *list;
  1194. struct totemudpu_member *member;
  1195. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1196. for (list = instance->member_list.next;
  1197. list != &instance->member_list;
  1198. list = list->next) {
  1199. member = list_entry (list,
  1200. struct totemudpu_member,
  1201. list);
  1202. if (member->fd > 0) {
  1203. close (member->fd);
  1204. }
  1205. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  1206. }
  1207. return (0);
  1208. }
  1209. int totemudpu_member_set_active (
  1210. void *udpu_context,
  1211. const struct totem_ip_address *member_ip,
  1212. int active)
  1213. {
  1214. struct list_head *list;
  1215. struct totemudpu_member *member;
  1216. int addr_found = 0;
  1217. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1218. /*
  1219. * Find the member to set active flag
  1220. */
  1221. for (list = instance->member_list.next; list != &instance->member_list; list = list->next) {
  1222. member = list_entry (list, struct totemudpu_member, list);
  1223. if (totemip_compare (member_ip, &member->member) == 0) {
  1224. log_printf(LOGSYS_LEVEL_DEBUG,
  1225. "Marking UDPU member %s %s",
  1226. totemip_print(&member->member),
  1227. (active ? "active" : "inactive"));
  1228. member->active = active;
  1229. addr_found = 1;
  1230. break;
  1231. }
  1232. }
  1233. if (!addr_found) {
  1234. log_printf(LOGSYS_LEVEL_DEBUG,
  1235. "Can't find UDPU member %s (should be marked as %s)",
  1236. totemip_print(member_ip),
  1237. (active ? "active" : "inactive"));
  1238. }
  1239. return (0);
  1240. }
  1241. static void timer_function_merge_detect_timeout (
  1242. void *data)
  1243. {
  1244. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  1245. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1246. instance->send_merge_detect_message = 1;
  1247. }
  1248. instance->merge_detect_messages_sent_before_timeout = 0;
  1249. totemudpu_start_merge_detect_timeout(instance);
  1250. }
  1251. static void totemudpu_start_merge_detect_timeout(
  1252. void *udpu_context)
  1253. {
  1254. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1255. qb_loop_timer_add(instance->totemudpu_poll_handle,
  1256. QB_LOOP_MED,
  1257. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1258. (void *)instance,
  1259. timer_function_merge_detect_timeout,
  1260. &instance->timer_merge_detect_timeout);
  1261. }
  1262. static void totemudpu_stop_merge_detect_timeout(
  1263. void *udpu_context)
  1264. {
  1265. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1266. qb_loop_timer_del(instance->totemudpu_poll_handle,
  1267. instance->timer_merge_detect_timeout);
  1268. }