totemudpu.c 30 KB

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