totemudpu.c 30 KB

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