totemudpu.c 30 KB

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