totemudpu.c 28 KB

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