totemudpu.c 28 KB

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