totemudpu.c 33 KB

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