4
0

totemudpu.c 36 KB

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