totemudpu.c 28 KB

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