4
0

totemudpu.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. /*
  559. * Setup unicast socket
  560. */
  561. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  562. if (instance->token_socket == -1) {
  563. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  564. "socket() failed");
  565. return (-1);
  566. }
  567. totemip_nosigpipe (instance->token_socket);
  568. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  569. if (res == -1) {
  570. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  571. "Could not set non-blocking operation on token socket");
  572. return (-1);
  573. }
  574. /*
  575. * Bind to unicast socket used for token send/receives
  576. * This has the side effect of binding to the correct interface
  577. */
  578. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  579. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  580. if (res == -1) {
  581. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  582. "bind token socket failed");
  583. return (-1);
  584. }
  585. /*
  586. * the token_socket can receive many messages. Allow a large number
  587. * of receive messages on this socket
  588. */
  589. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  590. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  591. &recvbuf_size, optlen);
  592. if (res == -1) {
  593. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  594. "Could not set recvbuf size");
  595. }
  596. return 0;
  597. }
  598. static int totemudpu_build_sockets (
  599. struct totemudpu_instance *instance,
  600. struct totem_ip_address *bindnet_address,
  601. struct totem_ip_address *bound_to)
  602. {
  603. int interface_num;
  604. int interface_up;
  605. int res;
  606. /*
  607. * Determine the ip address bound to and the interface name
  608. */
  609. res = netif_determine (instance,
  610. bindnet_address,
  611. bound_to,
  612. &interface_up,
  613. &interface_num);
  614. if (res == -1) {
  615. return (-1);
  616. }
  617. totemip_copy(&instance->my_id, bound_to);
  618. res = totemudpu_build_sockets_ip (instance,
  619. bindnet_address, bound_to, interface_num);
  620. /* We only send out of the token socket */
  621. totemudpu_traffic_control_set(instance, instance->token_socket);
  622. /*
  623. * Rebind all members to new ips
  624. */
  625. totemudpu_member_list_rebind_ip(instance);
  626. return res;
  627. }
  628. /*
  629. * Totem Network interface - also does encryption/decryption
  630. * depends on poll abstraction, POSIX, IPV4
  631. */
  632. /*
  633. * Create an instance
  634. */
  635. int totemudpu_initialize (
  636. qb_loop_t *poll_handle,
  637. void **udpu_context,
  638. struct totem_config *totem_config,
  639. totemsrp_stats_t *stats,
  640. int interface_no,
  641. void *context,
  642. void (*deliver_fn) (
  643. void *context,
  644. const void *msg,
  645. unsigned int msg_len),
  646. void (*iface_change_fn) (
  647. void *context,
  648. const struct totem_ip_address *iface_address),
  649. void (*target_set_completed) (
  650. void *context))
  651. {
  652. struct totemudpu_instance *instance;
  653. instance = malloc (sizeof (struct totemudpu_instance));
  654. if (instance == NULL) {
  655. return (-1);
  656. }
  657. totemudpu_instance_initialize (instance);
  658. instance->totem_config = totem_config;
  659. instance->stats = stats;
  660. /*
  661. * Configure logging
  662. */
  663. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  664. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  665. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  666. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  667. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  668. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  669. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  670. /*
  671. * Initialize random number generator for later use to generate salt
  672. */
  673. instance->crypto_inst = crypto_init (totem_config->private_key,
  674. totem_config->private_key_len,
  675. totem_config->crypto_cipher_type,
  676. totem_config->crypto_hash_type,
  677. instance->totemudpu_log_printf,
  678. instance->totemudpu_log_level_security,
  679. instance->totemudpu_log_level_notice,
  680. instance->totemudpu_log_level_error,
  681. instance->totemudpu_subsys_id);
  682. if (instance->crypto_inst == NULL) {
  683. free(instance);
  684. return (-1);
  685. }
  686. /*
  687. * Initialize local variables for totemudpu
  688. */
  689. instance->totem_interface = &totem_config->interfaces[interface_no];
  690. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  691. instance->totemudpu_poll_handle = poll_handle;
  692. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  693. instance->context = context;
  694. instance->totemudpu_deliver_fn = deliver_fn;
  695. instance->totemudpu_iface_change_fn = iface_change_fn;
  696. instance->totemudpu_target_set_completed = target_set_completed;
  697. totemip_localhost (AF_INET, &localhost);
  698. localhost.nodeid = instance->totem_config->node_id;
  699. /*
  700. * RRP layer isn't ready to receive message because it hasn't
  701. * initialized yet. Add short timer to check the interfaces.
  702. */
  703. qb_loop_timer_add (instance->totemudpu_poll_handle,
  704. QB_LOOP_MED,
  705. 100*QB_TIME_NS_IN_MSEC,
  706. (void *)instance,
  707. timer_function_netif_check_timeout,
  708. &instance->timer_netif_check_timeout);
  709. totemudpu_start_merge_detect_timeout(instance);
  710. *udpu_context = instance;
  711. return (0);
  712. }
  713. void *totemudpu_buffer_alloc (void)
  714. {
  715. return malloc (FRAME_SIZE_MAX);
  716. }
  717. void totemudpu_buffer_release (void *ptr)
  718. {
  719. return free (ptr);
  720. }
  721. int totemudpu_processor_count_set (
  722. void *udpu_context,
  723. int processor_count)
  724. {
  725. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  726. int res = 0;
  727. instance->my_memb_entries = processor_count;
  728. qb_loop_timer_del (instance->totemudpu_poll_handle,
  729. instance->timer_netif_check_timeout);
  730. if (processor_count == 1) {
  731. qb_loop_timer_add (instance->totemudpu_poll_handle,
  732. QB_LOOP_MED,
  733. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  734. (void *)instance,
  735. timer_function_netif_check_timeout,
  736. &instance->timer_netif_check_timeout);
  737. }
  738. return (res);
  739. }
  740. int totemudpu_recv_flush (void *udpu_context)
  741. {
  742. int res = 0;
  743. return (res);
  744. }
  745. int totemudpu_send_flush (void *udpu_context)
  746. {
  747. int res = 0;
  748. return (res);
  749. }
  750. int totemudpu_token_send (
  751. void *udpu_context,
  752. const void *msg,
  753. unsigned int msg_len)
  754. {
  755. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  756. int res = 0;
  757. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  758. return (res);
  759. }
  760. int totemudpu_mcast_flush_send (
  761. void *udpu_context,
  762. const void *msg,
  763. unsigned int msg_len)
  764. {
  765. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  766. int res = 0;
  767. mcast_sendmsg (instance, msg, msg_len, 0);
  768. return (res);
  769. }
  770. int totemudpu_mcast_noflush_send (
  771. void *udpu_context,
  772. const void *msg,
  773. unsigned int msg_len)
  774. {
  775. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  776. int res = 0;
  777. mcast_sendmsg (instance, msg, msg_len, 1);
  778. return (res);
  779. }
  780. extern int totemudpu_iface_check (void *udpu_context)
  781. {
  782. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  783. int res = 0;
  784. timer_function_netif_check_timeout (instance);
  785. return (res);
  786. }
  787. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  788. {
  789. assert(totem_config->interface_count > 0);
  790. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  791. totem_config->crypto_hash_type) +
  792. totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  793. }
  794. const char *totemudpu_iface_print (void *udpu_context) {
  795. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  796. const char *ret_char;
  797. ret_char = totemip_print (&instance->my_id);
  798. return (ret_char);
  799. }
  800. int totemudpu_iface_get (
  801. void *udpu_context,
  802. struct totem_ip_address *addr)
  803. {
  804. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  805. int res = 0;
  806. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  807. return (res);
  808. }
  809. int totemudpu_token_target_set (
  810. void *udpu_context,
  811. const struct totem_ip_address *token_target)
  812. {
  813. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  814. int res = 0;
  815. memcpy (&instance->token_target, token_target,
  816. sizeof (struct totem_ip_address));
  817. instance->totemudpu_target_set_completed (instance->context);
  818. return (res);
  819. }
  820. extern int totemudpu_recv_mcast_empty (
  821. void *udpu_context)
  822. {
  823. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  824. unsigned int res;
  825. struct sockaddr_storage system_from;
  826. struct msghdr msg_recv;
  827. struct pollfd ufd;
  828. int nfds;
  829. int msg_processed = 0;
  830. /*
  831. * Receive datagram
  832. */
  833. msg_recv.msg_name = &system_from;
  834. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  835. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  836. msg_recv.msg_iovlen = 1;
  837. #ifdef HAVE_MSGHDR_CONTROL
  838. msg_recv.msg_control = 0;
  839. #endif
  840. #ifdef HAVE_MSGHDR_CONTROLLEN
  841. msg_recv.msg_controllen = 0;
  842. #endif
  843. #ifdef HAVE_MSGHDR_FLAGS
  844. msg_recv.msg_flags = 0;
  845. #endif
  846. #ifdef HAVE_MSGHDR_ACCRIGHTS
  847. msg_recv.msg_accrights = NULL;
  848. #endif
  849. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  850. msg_recv.msg_accrightslen = 0;
  851. #endif
  852. do {
  853. ufd.fd = instance->token_socket;
  854. ufd.events = POLLIN;
  855. nfds = poll (&ufd, 1, 0);
  856. if (nfds == 1 && ufd.revents & POLLIN) {
  857. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  858. if (res != -1) {
  859. msg_processed = 1;
  860. } else {
  861. msg_processed = -1;
  862. }
  863. }
  864. } while (nfds == 1);
  865. return (msg_processed);
  866. }
  867. static int totemudpu_create_sending_socket(
  868. void *udpu_context,
  869. const struct totem_ip_address *member)
  870. {
  871. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  872. int fd;
  873. int res;
  874. unsigned int sendbuf_size;
  875. unsigned int optlen = sizeof (sendbuf_size);
  876. struct sockaddr_storage sockaddr;
  877. int addrlen;
  878. fd = socket (member->family, SOCK_DGRAM, 0);
  879. if (fd == -1) {
  880. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  881. "Could not create socket for new member");
  882. return (-1);
  883. }
  884. totemip_nosigpipe (fd);
  885. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  886. if (res == -1) {
  887. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  888. "Could not set non-blocking operation on token socket");
  889. goto error_close_fd;
  890. }
  891. /*
  892. * These sockets are used to send multicast messages, so their buffers
  893. * should be large
  894. */
  895. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  896. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  897. &sendbuf_size, optlen);
  898. if (res == -1) {
  899. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  900. "Could not set sendbuf size");
  901. /*
  902. * Fail in setting sendbuf size is not fatal -> don't exit
  903. */
  904. }
  905. /*
  906. * Bind to sending interface
  907. */
  908. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  909. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  910. if (res == -1) {
  911. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  912. "bind token socket failed");
  913. goto error_close_fd;
  914. }
  915. return (fd);
  916. error_close_fd:
  917. close(fd);
  918. return (-1);
  919. }
  920. int totemudpu_member_add (
  921. void *udpu_context,
  922. const struct totem_ip_address *member)
  923. {
  924. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  925. struct totemudpu_member *new_member;
  926. new_member = malloc (sizeof (struct totemudpu_member));
  927. if (new_member == NULL) {
  928. return (-1);
  929. }
  930. memset(new_member, 0, sizeof(*new_member));
  931. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  932. totemip_print(member));
  933. list_init (&new_member->list);
  934. list_add_tail (&new_member->list, &instance->member_list);
  935. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  936. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  937. new_member->active = 0;
  938. return (0);
  939. }
  940. int totemudpu_member_remove (
  941. void *udpu_context,
  942. const struct totem_ip_address *token_target)
  943. {
  944. int found = 0;
  945. struct list_head *list;
  946. struct totemudpu_member *member;
  947. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  948. /*
  949. * Find the member to remove and close its socket
  950. */
  951. for (list = instance->member_list.next;
  952. list != &instance->member_list;
  953. list = list->next) {
  954. member = list_entry (list,
  955. struct totemudpu_member,
  956. list);
  957. if (totemip_compare (token_target, &member->member)==0) {
  958. log_printf(LOGSYS_LEVEL_NOTICE,
  959. "removing UDPU member {%s}",
  960. totemip_print(&member->member));
  961. if (member->fd > 0) {
  962. log_printf(LOGSYS_LEVEL_DEBUG,
  963. "Closing socket to: {%s}",
  964. totemip_print(&member->member));
  965. qb_loop_poll_del (instance->totemudpu_poll_handle,
  966. member->fd);
  967. close (member->fd);
  968. }
  969. found = 1;
  970. break;
  971. }
  972. }
  973. /*
  974. * Delete the member from the list
  975. */
  976. if (found) {
  977. list_del (list);
  978. }
  979. instance = NULL;
  980. return (0);
  981. }
  982. int totemudpu_member_list_rebind_ip (
  983. void *udpu_context)
  984. {
  985. struct list_head *list;
  986. struct totemudpu_member *member;
  987. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  988. for (list = instance->member_list.next;
  989. list != &instance->member_list;
  990. list = list->next) {
  991. member = list_entry (list,
  992. struct totemudpu_member,
  993. list);
  994. if (member->fd > 0) {
  995. close (member->fd);
  996. }
  997. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  998. }
  999. return (0);
  1000. }
  1001. int totemudpu_member_set_active (
  1002. void *udpu_context,
  1003. const struct totem_ip_address *member_ip,
  1004. int active)
  1005. {
  1006. struct list_head *list;
  1007. struct totemudpu_member *member;
  1008. int addr_found = 0;
  1009. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1010. /*
  1011. * Find the member to set active flag
  1012. */
  1013. for (list = instance->member_list.next; list != &instance->member_list; list = list->next) {
  1014. member = list_entry (list, struct totemudpu_member, list);
  1015. if (totemip_compare (member_ip, &member->member) == 0) {
  1016. log_printf(LOGSYS_LEVEL_DEBUG,
  1017. "Marking UDPU member %s %s",
  1018. totemip_print(&member->member),
  1019. (active ? "active" : "inactive"));
  1020. member->active = active;
  1021. addr_found = 1;
  1022. break;
  1023. }
  1024. }
  1025. if (!addr_found) {
  1026. log_printf(LOGSYS_LEVEL_DEBUG,
  1027. "Can't find UDPU member %s (should be marked as %s)",
  1028. totemip_print(member_ip),
  1029. (active ? "active" : "inactive"));
  1030. }
  1031. return (0);
  1032. }
  1033. static void timer_function_merge_detect_timeout (
  1034. void *data)
  1035. {
  1036. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  1037. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1038. instance->send_merge_detect_message = 1;
  1039. }
  1040. instance->merge_detect_messages_sent_before_timeout = 0;
  1041. totemudpu_start_merge_detect_timeout(instance);
  1042. }
  1043. static void totemudpu_start_merge_detect_timeout(
  1044. void *udpu_context)
  1045. {
  1046. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1047. qb_loop_timer_add(instance->totemudpu_poll_handle,
  1048. QB_LOOP_MED,
  1049. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1050. (void *)instance,
  1051. timer_function_merge_detect_timeout,
  1052. &instance->timer_merge_detect_timeout);
  1053. }
  1054. static void totemudpu_stop_merge_detect_timeout(
  1055. void *udpu_context)
  1056. {
  1057. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1058. qb_loop_timer_del(instance->totemudpu_poll_handle,
  1059. instance->timer_merge_detect_timeout);
  1060. }