totemudpu.c 28 KB

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