totemudpu.c 28 KB

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