totemudpu.c 33 KB

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