totemudpu.c 28 KB

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