totemudpu.c 32 KB

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