totemudpu.c 29 KB

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