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. int (*totemudpu_deliver_fn) (
  88. void *context,
  89. const void *msg,
  90. unsigned int msg_len,
  91. const struct sockaddr_storage *system_from);
  92. int (*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 + 1];
  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 + 1; //sizeof (instance->iov_buffer) + 1;
  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, struct totemudpu_member, list);
  268. /*
  269. * Do not send multicast message if message is not "flush", member
  270. * is inactive and timeout for sending merge message didn't expired.
  271. */
  272. if (only_active && !member->active && !instance->send_merge_detect_message)
  273. continue ;
  274. totemip_totemip_to_sockaddr_convert(&member->member,
  275. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  276. msg_mcast.msg_name = &sockaddr;
  277. msg_mcast.msg_namelen = addrlen;
  278. msg_mcast.msg_iov = (void *)&iovec;
  279. msg_mcast.msg_iovlen = 1;
  280. #ifdef HAVE_MSGHDR_CONTROL
  281. msg_mcast.msg_control = 0;
  282. #endif
  283. #ifdef HAVE_MSGHDR_CONTROLLEN
  284. msg_mcast.msg_controllen = 0;
  285. #endif
  286. #ifdef HAVE_MSGHDR_FLAGS
  287. msg_mcast.msg_flags = 0;
  288. #endif
  289. #ifdef HAVE_MSGHDR_ACCRIGHTS
  290. msg_mcast.msg_accrights = NULL;
  291. #endif
  292. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  293. msg_mcast.msg_accrightslen = 0;
  294. #endif
  295. /*
  296. * Transmit multicast message
  297. * An error here is recovered by totemsrp
  298. */
  299. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  300. if (res < 0) {
  301. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  302. "sendmsg(mcast) failed (non-critical)");
  303. }
  304. }
  305. if (!only_active || instance->send_merge_detect_message) {
  306. /*
  307. * Current message was sent to all nodes
  308. */
  309. instance->merge_detect_messages_sent_before_timeout++;
  310. instance->send_merge_detect_message = 0;
  311. }
  312. } else {
  313. /*
  314. * Transmit multicast message to local unix mcast loop
  315. * An error here is recovered by totemsrp
  316. */
  317. msg_mcast.msg_name = NULL;
  318. msg_mcast.msg_namelen = 0;
  319. msg_mcast.msg_iov = (void *)&iovec;
  320. msg_mcast.msg_iovlen = 1;
  321. #ifdef HAVE_MSGHDR_CONTROL
  322. msg_mcast.msg_control = 0;
  323. #endif
  324. #ifdef HAVE_MSGHDR_CONTROLLEN
  325. msg_mcast.msg_controllen = 0;
  326. #endif
  327. #ifdef HAVE_MSGHDR_FLAGS
  328. msg_mcast.msg_flags = 0;
  329. #endif
  330. #ifdef HAVE_MSGHDR_ACCRIGHTS
  331. msg_mcast.msg_accrights = NULL;
  332. #endif
  333. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  334. msg_mcast.msg_accrightslen = 0;
  335. #endif
  336. res = sendmsg (instance->local_loop_sock[1], &msg_mcast,
  337. MSG_NOSIGNAL);
  338. if (res < 0) {
  339. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  340. "sendmsg(local mcast loop) failed (non-critical)");
  341. }
  342. }
  343. }
  344. int totemudpu_finalize (
  345. void *udpu_context)
  346. {
  347. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  348. int res = 0;
  349. if (instance->token_socket > 0) {
  350. qb_loop_poll_del (instance->totemudpu_poll_handle,
  351. instance->token_socket);
  352. close (instance->token_socket);
  353. }
  354. if (instance->local_loop_sock[0] > 0) {
  355. qb_loop_poll_del (instance->totemudpu_poll_handle,
  356. instance->local_loop_sock[0]);
  357. close (instance->local_loop_sock[0]);
  358. close (instance->local_loop_sock[1]);
  359. }
  360. totemudpu_stop_merge_detect_timeout(instance);
  361. return (res);
  362. }
  363. static struct totemudpu_member *find_member_by_sockaddr(
  364. const void *udpu_context,
  365. const struct sockaddr *sa)
  366. {
  367. struct qb_list_head *list;
  368. struct totemudpu_member *member;
  369. struct totemudpu_member *res_member;
  370. const struct totemudpu_instance *instance = (const struct totemudpu_instance *)udpu_context;
  371. res_member = NULL;
  372. qb_list_for_each(list, &(instance->member_list)) {
  373. member = qb_list_entry (list,
  374. struct totemudpu_member,
  375. list);
  376. if (totemip_sa_equal(&member->member, sa)) {
  377. res_member = member;
  378. break ;
  379. }
  380. }
  381. return (res_member);
  382. }
  383. static int net_deliver_fn (
  384. int fd,
  385. int revents,
  386. void *data)
  387. {
  388. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  389. struct msghdr msg_recv;
  390. struct iovec *iovec;
  391. struct sockaddr_storage system_from;
  392. int bytes_received;
  393. iovec = &instance->totemudpu_iov_recv;
  394. /*
  395. * Receive datagram
  396. */
  397. msg_recv.msg_name = &system_from;
  398. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  399. msg_recv.msg_iov = iovec;
  400. msg_recv.msg_iovlen = 1;
  401. #ifdef HAVE_MSGHDR_CONTROL
  402. msg_recv.msg_control = 0;
  403. #endif
  404. #ifdef HAVE_MSGHDR_CONTROLLEN
  405. msg_recv.msg_controllen = 0;
  406. #endif
  407. #ifdef HAVE_MSGHDR_FLAGS
  408. msg_recv.msg_flags = 0;
  409. #endif
  410. #ifdef HAVE_MSGHDR_ACCRIGHTS
  411. msg_recv.msg_accrights = NULL;
  412. #endif
  413. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  414. msg_recv.msg_accrightslen = 0;
  415. #endif
  416. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  417. if (bytes_received == -1) {
  418. return (0);
  419. } else {
  420. instance->stats_recv += bytes_received;
  421. }
  422. if (bytes_received >= UDP_RECEIVE_FRAME_SIZE_MAX + 1) {
  423. /*
  424. * Maximum packet size should be UDP_RECEIVE_FRAME_SIZE_MAX.
  425. * If received packet is UDP_RECEIVE_FRAME_SIZE_MAX + 1 it means packet was truncated
  426. * (iov_buffer size and iov_len are intentionally set to UDP_RECEIVE_FRAME_SIZE_MAX + 1).
  427. */
  428. log_printf (instance->totemudpu_log_level_error,
  429. "Received too big message. This may be because something bad is happening "
  430. "on the network (attack?), or you tried join more nodes than corosync is "
  431. "compiled with (%u) or bug in the code (bad estimation of "
  432. "the UDP_RECEIVE_FRAME_SIZE_MAX). Dropping packet.", PROCESSOR_COUNT_MAX);
  433. return (0);
  434. }
  435. if (instance->totem_config->block_unlisted_ips &&
  436. instance->netif_bind_state == BIND_STATE_REGULAR &&
  437. find_member_by_sockaddr(instance, (const struct sockaddr *)&system_from) == NULL) {
  438. log_printf(instance->totemudpu_log_level_debug, "Packet rejected from %s",
  439. totemip_sa_print((const struct sockaddr *)&system_from));
  440. return (0);
  441. }
  442. iovec->iov_len = bytes_received;
  443. /*
  444. * Handle incoming message
  445. */
  446. instance->totemudpu_deliver_fn (
  447. instance->context,
  448. iovec->iov_base,
  449. iovec->iov_len,
  450. &system_from);
  451. iovec->iov_len = UDP_RECEIVE_FRAME_SIZE_MAX + 1;
  452. return (0);
  453. }
  454. static int netif_determine (
  455. struct totemudpu_instance *instance,
  456. struct totem_ip_address *bindnet,
  457. struct totem_ip_address *bound_to,
  458. int *interface_up,
  459. int *interface_num)
  460. {
  461. int res;
  462. res = totemip_iface_check (bindnet, bound_to,
  463. interface_up, interface_num,
  464. instance->totem_config->clear_node_high_bit);
  465. return (res);
  466. }
  467. /*
  468. * If the interface is up, the sockets for totem are built. If the interface is down
  469. * this function is requeued in the timer list to retry building the sockets later.
  470. */
  471. static void timer_function_netif_check_timeout (
  472. void *data)
  473. {
  474. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  475. int interface_up;
  476. int interface_num;
  477. /*
  478. * Build sockets for every interface
  479. */
  480. netif_determine (instance,
  481. &instance->totem_interface->bindnet,
  482. &instance->totem_interface->boundto,
  483. &interface_up, &interface_num);
  484. /*
  485. * If the network interface isn't back up and we are already
  486. * in loopback mode, add timer to check again and return
  487. */
  488. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  489. interface_up == 0) ||
  490. (instance->my_memb_entries == 1 &&
  491. instance->netif_bind_state == BIND_STATE_REGULAR &&
  492. interface_up == 1)) {
  493. qb_loop_timer_add (instance->totemudpu_poll_handle,
  494. QB_LOOP_MED,
  495. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  496. (void *)instance,
  497. timer_function_netif_check_timeout,
  498. &instance->timer_netif_check_timeout);
  499. /*
  500. * Add a timer to check for a downed regular interface
  501. */
  502. return;
  503. }
  504. if (instance->token_socket > 0) {
  505. qb_loop_poll_del (instance->totemudpu_poll_handle,
  506. instance->token_socket);
  507. close (instance->token_socket);
  508. instance->token_socket = -1;
  509. }
  510. if (interface_up == 0) {
  511. if (instance->netif_bind_state == BIND_STATE_UNBOUND) {
  512. log_printf (instance->totemudpu_log_level_error,
  513. "One of your ip addresses are now bound to localhost. "
  514. "Corosync would not work correctly.");
  515. exit(COROSYNC_DONE_FATAL_ERR);
  516. }
  517. /*
  518. * Interface is not up
  519. */
  520. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  521. /*
  522. * Add a timer to retry building interfaces and request memb_gather_enter
  523. */
  524. qb_loop_timer_add (instance->totemudpu_poll_handle,
  525. QB_LOOP_MED,
  526. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  527. (void *)instance,
  528. timer_function_netif_check_timeout,
  529. &instance->timer_netif_check_timeout);
  530. } else {
  531. /*
  532. * Interface is up
  533. */
  534. instance->netif_bind_state = BIND_STATE_REGULAR;
  535. }
  536. /*
  537. * Create and bind the multicast and unicast sockets
  538. */
  539. totemudpu_build_sockets (instance,
  540. &instance->totem_interface->bindnet,
  541. &instance->totem_interface->boundto);
  542. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  543. qb_loop_poll_add (instance->totemudpu_poll_handle,
  544. QB_LOOP_MED,
  545. instance->token_socket,
  546. POLLIN, instance, net_deliver_fn);
  547. }
  548. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  549. /*
  550. * This reports changes in the interface to the user and totemsrp
  551. */
  552. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  553. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  554. log_printf (instance->totemudpu_log_level_notice,
  555. "The network interface [%s] is now up.",
  556. totemip_print (&instance->totem_interface->boundto));
  557. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  558. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  559. }
  560. /*
  561. * Add a timer to check for interface going down in single membership
  562. */
  563. if (instance->my_memb_entries == 1) {
  564. qb_loop_timer_add (instance->totemudpu_poll_handle,
  565. QB_LOOP_MED,
  566. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  567. (void *)instance,
  568. timer_function_netif_check_timeout,
  569. &instance->timer_netif_check_timeout);
  570. }
  571. } else {
  572. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  573. log_printf (instance->totemudpu_log_level_notice,
  574. "The network interface is down.");
  575. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  576. }
  577. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  578. }
  579. }
  580. /* Set the socket priority to INTERACTIVE to ensure
  581. that our messages don't get queued behind anything else */
  582. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  583. {
  584. #ifdef SO_PRIORITY
  585. int prio = 6; /* TC_PRIO_INTERACTIVE */
  586. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  587. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  588. "Could not set traffic priority");
  589. }
  590. #endif
  591. }
  592. static int totemudpu_build_sockets_ip (
  593. struct totemudpu_instance *instance,
  594. struct totem_ip_address *bindnet_address,
  595. struct totem_ip_address *bound_to,
  596. int interface_num)
  597. {
  598. struct sockaddr_storage sockaddr;
  599. int addrlen;
  600. int res;
  601. unsigned int recvbuf_size;
  602. unsigned int optlen = sizeof (recvbuf_size);
  603. unsigned int retries = 0;
  604. /*
  605. * Setup unicast socket
  606. */
  607. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  608. if (instance->token_socket == -1) {
  609. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  610. "socket() failed");
  611. return (-1);
  612. }
  613. totemip_nosigpipe (instance->token_socket);
  614. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  615. if (res == -1) {
  616. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  617. "Could not set non-blocking operation on token socket");
  618. return (-1);
  619. }
  620. /*
  621. * Bind to unicast socket used for token send/receives
  622. * This has the side effect of binding to the correct interface
  623. */
  624. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  625. while (1) {
  626. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  627. if (res == 0) {
  628. break;
  629. }
  630. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  631. "bind token socket failed");
  632. if (++retries > BIND_MAX_RETRIES) {
  633. break;
  634. }
  635. /*
  636. * Wait for a while
  637. */
  638. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  639. }
  640. if (res == -1) {
  641. return (-1);
  642. }
  643. /*
  644. * the token_socket can receive many messages. Allow a large number
  645. * of receive messages on this socket
  646. */
  647. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  648. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  649. &recvbuf_size, optlen);
  650. if (res == -1) {
  651. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  652. "Could not set recvbuf size");
  653. }
  654. res = set_socket_dscp(instance->token_socket,
  655. instance->totem_config->ip_dscp);
  656. if (res == -1) {
  657. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  658. "Could not set IP_TOS bits");
  659. }
  660. return 0;
  661. }
  662. int totemudpu_nodestatus_get (void *udpu_context, unsigned int nodeid,
  663. struct totem_node_status *node_status)
  664. {
  665. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  666. struct qb_list_head *list;
  667. struct totemudpu_member *member;
  668. qb_list_for_each(list, &(instance->member_list)) {
  669. member = qb_list_entry (list,
  670. struct totemudpu_member,
  671. list);
  672. if (member->member.nodeid == nodeid) {
  673. node_status->nodeid = nodeid;
  674. /* reachable is filled in by totemsrp */
  675. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  676. node_status->link_status[0].enabled = 1;
  677. } else {
  678. node_status->link_status[0].enabled = 0;
  679. }
  680. node_status->link_status[0].connected = node_status->reachable;
  681. node_status->link_status[0].mtu = instance->totem_config->net_mtu;
  682. strncpy(node_status->link_status[0].src_ipaddr, totemip_print(&member->member), KNET_MAX_HOST_LEN-1);
  683. }
  684. }
  685. return (0);
  686. }
  687. int totemudpu_ifaces_get (
  688. void *net_context,
  689. char ***status,
  690. unsigned int *iface_count)
  691. {
  692. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  693. if (status) {
  694. *status = statuses;
  695. }
  696. *iface_count = 1;
  697. return (0);
  698. }
  699. static int totemudpu_build_local_sockets(
  700. struct totemudpu_instance *instance)
  701. {
  702. int i;
  703. unsigned int sendbuf_size;
  704. unsigned int recvbuf_size;
  705. unsigned int optlen = sizeof (sendbuf_size);
  706. int res;
  707. /*
  708. * Create local multicast loop socket
  709. */
  710. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, instance->local_loop_sock) == -1) {
  711. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  712. "socket() failed");
  713. return (-1);
  714. }
  715. for (i = 0; i < 2; i++) {
  716. totemip_nosigpipe (instance->local_loop_sock[i]);
  717. res = fcntl (instance->local_loop_sock[i], F_SETFL, O_NONBLOCK);
  718. if (res == -1) {
  719. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  720. "Could not set non-blocking operation on multicast socket");
  721. return (-1);
  722. }
  723. }
  724. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  725. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  726. res = setsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  727. if (res == -1) {
  728. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  729. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  730. return (-1);
  731. }
  732. res = setsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  733. if (res == -1) {
  734. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  735. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  736. return (-1);
  737. }
  738. res = getsockopt (instance->local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  739. if (res == 0) {
  740. log_printf (instance->totemudpu_log_level_debug,
  741. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  742. }
  743. res = getsockopt (instance->local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  744. if (res == 0) {
  745. log_printf (instance->totemudpu_log_level_debug,
  746. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  747. }
  748. return (0);
  749. }
  750. static int totemudpu_build_sockets (
  751. struct totemudpu_instance *instance,
  752. struct totem_ip_address *bindnet_address,
  753. struct totem_ip_address *bound_to)
  754. {
  755. int interface_num;
  756. int interface_up;
  757. int res;
  758. /*
  759. * Determine the ip address bound to and the interface name
  760. */
  761. res = netif_determine (instance,
  762. bindnet_address,
  763. bound_to,
  764. &interface_up,
  765. &interface_num);
  766. if (res == -1) {
  767. return (-1);
  768. }
  769. totemip_copy(&instance->my_id, bound_to);
  770. res = totemudpu_build_sockets_ip (instance,
  771. bindnet_address, bound_to, interface_num);
  772. if (res == -1) {
  773. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  774. LOGSYS_PERROR (errno, instance->totemudpu_log_level_error,
  775. "Unable to create sockets, exiting");
  776. exit(EXIT_FAILURE);
  777. }
  778. /* We only send out of the token socket */
  779. totemudpu_traffic_control_set(instance, instance->token_socket);
  780. /*
  781. * Rebind all members to new ips
  782. */
  783. totemudpu_member_list_rebind_ip(instance);
  784. return res;
  785. }
  786. /*
  787. * Totem Network interface
  788. * depends on poll abstraction, POSIX, IPV4
  789. */
  790. /*
  791. * Create an instance
  792. */
  793. int totemudpu_initialize (
  794. qb_loop_t *poll_handle,
  795. void **udpu_context,
  796. struct totem_config *totem_config,
  797. totemsrp_stats_t *stats,
  798. void *context,
  799. int (*deliver_fn) (
  800. void *context,
  801. const void *msg,
  802. unsigned int msg_len,
  803. const struct sockaddr_storage *system_from),
  804. int (*iface_change_fn) (
  805. void *context,
  806. const struct totem_ip_address *iface_address,
  807. unsigned int ring_no),
  808. void (*mtu_changed) (
  809. void *context,
  810. int net_mtu),
  811. void (*target_set_completed) (
  812. void *context))
  813. {
  814. struct totemudpu_instance *instance;
  815. instance = malloc (sizeof (struct totemudpu_instance));
  816. if (instance == NULL) {
  817. return (-1);
  818. }
  819. totemudpu_instance_initialize (instance);
  820. instance->totem_config = totem_config;
  821. instance->stats = stats;
  822. /*
  823. * Configure logging
  824. */
  825. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  826. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  827. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  828. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  829. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  830. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  831. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  832. /*
  833. * Initialize local variables for totemudpu
  834. */
  835. instance->totem_interface = &totem_config->interfaces[0];
  836. memset (instance->iov_buffer, 0, UDP_RECEIVE_FRAME_SIZE_MAX + 1);
  837. instance->totemudpu_poll_handle = poll_handle;
  838. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  839. instance->context = context;
  840. instance->totemudpu_deliver_fn = deliver_fn;
  841. instance->totemudpu_iface_change_fn = iface_change_fn;
  842. instance->totemudpu_target_set_completed = target_set_completed;
  843. /*
  844. * Create static local mcast sockets
  845. */
  846. if (totemudpu_build_local_sockets(instance) == -1) {
  847. free(instance);
  848. return (-1);
  849. }
  850. qb_loop_poll_add (
  851. instance->totemudpu_poll_handle,
  852. QB_LOOP_MED,
  853. instance->local_loop_sock[0],
  854. POLLIN, instance, net_deliver_fn);
  855. /*
  856. * RRP layer isn't ready to receive message because it hasn't
  857. * initialized yet. Add short timer to check the interfaces.
  858. */
  859. qb_loop_timer_add (instance->totemudpu_poll_handle,
  860. QB_LOOP_MED,
  861. 100*QB_TIME_NS_IN_MSEC,
  862. (void *)instance,
  863. timer_function_netif_check_timeout,
  864. &instance->timer_netif_check_timeout);
  865. totemudpu_start_merge_detect_timeout((void*)instance);
  866. *udpu_context = instance;
  867. return (0);
  868. }
  869. void *totemudpu_buffer_alloc (void)
  870. {
  871. return malloc (FRAME_SIZE_MAX);
  872. }
  873. void totemudpu_buffer_release (void *ptr)
  874. {
  875. return free (ptr);
  876. }
  877. int totemudpu_processor_count_set (
  878. void *udpu_context,
  879. int processor_count)
  880. {
  881. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  882. int res = 0;
  883. instance->my_memb_entries = processor_count;
  884. qb_loop_timer_del (instance->totemudpu_poll_handle,
  885. instance->timer_netif_check_timeout);
  886. if (processor_count == 1) {
  887. qb_loop_timer_add (instance->totemudpu_poll_handle,
  888. QB_LOOP_MED,
  889. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  890. (void *)instance,
  891. timer_function_netif_check_timeout,
  892. &instance->timer_netif_check_timeout);
  893. }
  894. return (res);
  895. }
  896. int totemudpu_recv_flush (void *udpu_context)
  897. {
  898. int res = 0;
  899. return (res);
  900. }
  901. int totemudpu_send_flush (void *udpu_context)
  902. {
  903. int res = 0;
  904. return (res);
  905. }
  906. int totemudpu_token_send (
  907. void *udpu_context,
  908. const void *msg,
  909. unsigned int msg_len)
  910. {
  911. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  912. int res = 0;
  913. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  914. return (res);
  915. }
  916. int totemudpu_mcast_flush_send (
  917. void *udpu_context,
  918. const void *msg,
  919. unsigned int msg_len)
  920. {
  921. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  922. int res = 0;
  923. mcast_sendmsg (instance, msg, msg_len, 0);
  924. return (res);
  925. }
  926. int totemudpu_mcast_noflush_send (
  927. void *udpu_context,
  928. const void *msg,
  929. unsigned int msg_len)
  930. {
  931. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  932. int res = 0;
  933. mcast_sendmsg (instance, msg, msg_len, 1);
  934. return (res);
  935. }
  936. extern int totemudpu_iface_check (void *udpu_context)
  937. {
  938. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  939. int res = 0;
  940. timer_function_netif_check_timeout (instance);
  941. return (res);
  942. }
  943. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  944. {
  945. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  946. }
  947. int totemudpu_token_target_set (
  948. void *udpu_context,
  949. unsigned int nodeid)
  950. {
  951. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  952. struct qb_list_head *list;
  953. struct totemudpu_member *member;
  954. int res = 0;
  955. qb_list_for_each(list, &(instance->member_list)) {
  956. member = qb_list_entry (list,
  957. struct totemudpu_member,
  958. list);
  959. if (member->member.nodeid == nodeid) {
  960. memcpy (&instance->token_target, &member->member,
  961. sizeof (struct totem_ip_address));
  962. instance->totemudpu_target_set_completed (instance->context);
  963. break;
  964. }
  965. }
  966. return (res);
  967. }
  968. extern int totemudpu_recv_mcast_empty (
  969. void *udpu_context)
  970. {
  971. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  972. unsigned int res;
  973. struct sockaddr_storage system_from;
  974. struct msghdr msg_recv;
  975. struct pollfd ufd;
  976. int nfds, i;
  977. int msg_processed = 0;
  978. int sock;
  979. /*
  980. * Receive datagram
  981. */
  982. msg_recv.msg_name = &system_from;
  983. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  984. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  985. msg_recv.msg_iovlen = 1;
  986. #ifdef HAVE_MSGHDR_CONTROL
  987. msg_recv.msg_control = 0;
  988. #endif
  989. #ifdef HAVE_MSGHDR_CONTROLLEN
  990. msg_recv.msg_controllen = 0;
  991. #endif
  992. #ifdef HAVE_MSGHDR_FLAGS
  993. msg_recv.msg_flags = 0;
  994. #endif
  995. #ifdef HAVE_MSGHDR_ACCRIGHTS
  996. msg_recv.msg_accrights = NULL;
  997. #endif
  998. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  999. msg_recv.msg_accrightslen = 0;
  1000. #endif
  1001. for (i = 0; i < 2; i++) {
  1002. sock = -1;
  1003. if (i == 0) {
  1004. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1005. sock = instance->token_socket;
  1006. } else {
  1007. continue;
  1008. }
  1009. }
  1010. if (i == 1) {
  1011. sock = instance->local_loop_sock[0];
  1012. }
  1013. assert(sock != -1);
  1014. do {
  1015. ufd.fd = sock;
  1016. ufd.events = POLLIN;
  1017. nfds = poll (&ufd, 1, 0);
  1018. if (nfds == 1 && ufd.revents & POLLIN) {
  1019. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1020. if (res != -1) {
  1021. msg_processed = 1;
  1022. } else {
  1023. msg_processed = -1;
  1024. }
  1025. }
  1026. } while (nfds == 1);
  1027. }
  1028. return (msg_processed);
  1029. }
  1030. static int totemudpu_create_sending_socket(
  1031. void *udpu_context,
  1032. const struct totem_ip_address *member)
  1033. {
  1034. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1035. int fd;
  1036. int res;
  1037. unsigned int sendbuf_size;
  1038. unsigned int optlen = sizeof (sendbuf_size);
  1039. struct sockaddr_storage sockaddr;
  1040. int addrlen;
  1041. fd = socket (member->family, SOCK_DGRAM, 0);
  1042. if (fd == -1) {
  1043. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1044. "Could not create socket for new member");
  1045. return (-1);
  1046. }
  1047. totemip_nosigpipe (fd);
  1048. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  1049. if (res == -1) {
  1050. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1051. "Could not set non-blocking operation on token socket");
  1052. goto error_close_fd;
  1053. }
  1054. /*
  1055. * These sockets are used to send multicast messages, so their buffers
  1056. * should be large
  1057. */
  1058. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1059. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  1060. &sendbuf_size, optlen);
  1061. if (res == -1) {
  1062. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1063. "Could not set sendbuf size");
  1064. /*
  1065. * Fail in setting sendbuf size is not fatal -> don't exit
  1066. */
  1067. }
  1068. res = set_socket_dscp(fd, instance->totem_config->ip_dscp);
  1069. if (res == -1) {
  1070. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1071. "Could not set IP_TOS bits");
  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. }