totemudpu.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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/qblist.h>
  58. #include <qb/qbdefs.h>
  59. #include <qb/qbloop.h>
  60. #include <corosync/sq.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 <nss.h>
  67. #include <pk11pub.h>
  68. #include <pkcs11.h>
  69. #include <prerror.h>
  70. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. #define UDPU_FRAME_SIZE_MAX 10000
  74. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * UDPU_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 qb_list_head list;
  82. struct totem_ip_address member;
  83. int fd;
  84. int active;
  85. };
  86. struct totemudpu_instance {
  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. unsigned int ring_no);
  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[UDPU_FRAME_SIZE_MAX];
  120. struct iovec totemudpu_iov_recv;
  121. struct qb_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 = UDPU_FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  165. /*
  166. * There is always atleast 1 processor
  167. */
  168. instance->my_memb_entries = 1;
  169. qb_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. struct sockaddr_storage sockaddr;
  203. struct iovec iovec;
  204. int addrlen;
  205. iovec.iov_base = (void *)msg;
  206. iovec.iov_len = msg_len;
  207. /*
  208. * Build unicast message
  209. */
  210. totemip_totemip_to_sockaddr_convert(system_to,
  211. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  212. memset(&msg_ucast, 0, sizeof(msg_ucast));
  213. msg_ucast.msg_name = &sockaddr;
  214. msg_ucast.msg_namelen = addrlen;
  215. msg_ucast.msg_iov = (void *)&iovec;
  216. msg_ucast.msg_iovlen = 1;
  217. #ifdef HAVE_MSGHDR_CONTROL
  218. msg_ucast.msg_control = 0;
  219. #endif
  220. #ifdef HAVE_MSGHDR_CONTROLLEN
  221. msg_ucast.msg_controllen = 0;
  222. #endif
  223. #ifdef HAVE_MSGHDR_FLAGS
  224. msg_ucast.msg_flags = 0;
  225. #endif
  226. #ifdef HAVE_MSGHDR_ACCRIGHTS
  227. msg_ucast.msg_accrights = NULL;
  228. #endif
  229. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  230. msg_ucast.msg_accrightslen = 0;
  231. #endif
  232. /*
  233. * Transmit unicast message
  234. * An error here is recovered by totemsrp
  235. */
  236. res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
  237. if (res < 0) {
  238. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  239. "sendmsg(ucast) failed (non-critical)");
  240. }
  241. }
  242. static inline void mcast_sendmsg (
  243. struct totemudpu_instance *instance,
  244. const void *msg,
  245. unsigned int msg_len,
  246. int only_active)
  247. {
  248. struct msghdr msg_mcast;
  249. int res = 0;
  250. struct iovec iovec;
  251. struct sockaddr_storage sockaddr;
  252. int addrlen;
  253. struct qb_list_head *list;
  254. struct totemudpu_member *member;
  255. iovec.iov_base = (void *)msg;
  256. iovec.iov_len = msg_len;
  257. memset(&msg_mcast, 0, sizeof(msg_mcast));
  258. /*
  259. * Build multicast message
  260. */
  261. qb_list_for_each(list, &(instance->member_list)) {
  262. member = qb_list_entry (list,
  263. struct totemudpu_member,
  264. list);
  265. /*
  266. * Do not send multicast message if message is not "flush", member
  267. * is inactive and timeout for sending merge message didn't expired.
  268. */
  269. if (only_active && !member->active && !instance->send_merge_detect_message)
  270. continue ;
  271. totemip_totemip_to_sockaddr_convert(&member->member,
  272. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  273. msg_mcast.msg_name = &sockaddr;
  274. msg_mcast.msg_namelen = addrlen;
  275. msg_mcast.msg_iov = (void *)&iovec;
  276. msg_mcast.msg_iovlen = 1;
  277. #ifdef HAVE_MSGHDR_CONTROL
  278. msg_mcast.msg_control = 0;
  279. #endif
  280. #ifdef HAVE_MSGHDR_CONTROLLEN
  281. msg_mcast.msg_controllen = 0;
  282. #endif
  283. #ifdef HAVE_MSGHDR_FLAGS
  284. msg_mcast.msg_flags = 0;
  285. #endif
  286. #ifdef HAVE_MSGHDR_ACCRIGHTS
  287. msg_mcast.msg_accrights = NULL;
  288. #endif
  289. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  290. msg_mcast.msg_accrightslen = 0;
  291. #endif
  292. /*
  293. * Transmit multicast message
  294. * An error here is recovered by totemsrp
  295. */
  296. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  297. if (res < 0) {
  298. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  299. "sendmsg(mcast) failed (non-critical)");
  300. }
  301. }
  302. if (!only_active || instance->send_merge_detect_message) {
  303. /*
  304. * Current message was sent to all nodes
  305. */
  306. instance->merge_detect_messages_sent_before_timeout++;
  307. instance->send_merge_detect_message = 0;
  308. }
  309. }
  310. int totemudpu_finalize (
  311. void *udpu_context)
  312. {
  313. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  314. int res = 0;
  315. if (instance->token_socket > 0) {
  316. qb_loop_poll_del (instance->totemudpu_poll_handle,
  317. instance->token_socket);
  318. close (instance->token_socket);
  319. }
  320. totemudpu_stop_merge_detect_timeout(instance);
  321. return (res);
  322. }
  323. static int net_deliver_fn (
  324. int fd,
  325. int revents,
  326. void *data)
  327. {
  328. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  329. struct msghdr msg_recv;
  330. struct iovec *iovec;
  331. struct sockaddr_storage system_from;
  332. int bytes_received;
  333. iovec = &instance->totemudpu_iov_recv;
  334. /*
  335. * Receive datagram
  336. */
  337. msg_recv.msg_name = &system_from;
  338. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  339. msg_recv.msg_iov = iovec;
  340. msg_recv.msg_iovlen = 1;
  341. #ifdef HAVE_MSGHDR_CONTROL
  342. msg_recv.msg_control = 0;
  343. #endif
  344. #ifdef HAVE_MSGHDR_CONTROLLEN
  345. msg_recv.msg_controllen = 0;
  346. #endif
  347. #ifdef HAVE_MSGHDR_FLAGS
  348. msg_recv.msg_flags = 0;
  349. #endif
  350. #ifdef HAVE_MSGHDR_ACCRIGHTS
  351. msg_recv.msg_accrights = NULL;
  352. #endif
  353. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  354. msg_recv.msg_accrightslen = 0;
  355. #endif
  356. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  357. if (bytes_received == -1) {
  358. return (0);
  359. } else {
  360. instance->stats_recv += bytes_received;
  361. }
  362. iovec->iov_len = bytes_received;
  363. /*
  364. * Handle incoming message
  365. */
  366. instance->totemudpu_deliver_fn (
  367. instance->context,
  368. iovec->iov_base,
  369. iovec->iov_len);
  370. iovec->iov_len = UDPU_FRAME_SIZE_MAX;
  371. return (0);
  372. }
  373. static int netif_determine (
  374. struct totemudpu_instance *instance,
  375. struct totem_ip_address *bindnet,
  376. struct totem_ip_address *bound_to,
  377. int *interface_up,
  378. int *interface_num)
  379. {
  380. int res;
  381. res = totemip_iface_check (bindnet, bound_to,
  382. interface_up, interface_num,
  383. instance->totem_config->clear_node_high_bit);
  384. return (res);
  385. }
  386. /*
  387. * If the interface is up, the sockets for totem are built. If the interface is down
  388. * this function is requeued in the timer list to retry building the sockets later.
  389. */
  390. static void timer_function_netif_check_timeout (
  391. void *data)
  392. {
  393. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  394. int interface_up;
  395. int interface_num;
  396. struct totem_ip_address *bind_address;
  397. /*
  398. * Build sockets for every interface
  399. */
  400. netif_determine (instance,
  401. &instance->totem_interface->bindnet,
  402. &instance->totem_interface->boundto,
  403. &interface_up, &interface_num);
  404. /*
  405. * If the network interface isn't back up and we are already
  406. * in loopback mode, add timer to check again and return
  407. */
  408. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  409. interface_up == 0) ||
  410. (instance->my_memb_entries == 1 &&
  411. instance->netif_bind_state == BIND_STATE_REGULAR &&
  412. interface_up == 1)) {
  413. qb_loop_timer_add (instance->totemudpu_poll_handle,
  414. QB_LOOP_MED,
  415. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  416. (void *)instance,
  417. timer_function_netif_check_timeout,
  418. &instance->timer_netif_check_timeout);
  419. /*
  420. * Add a timer to check for a downed regular interface
  421. */
  422. return;
  423. }
  424. if (instance->token_socket > 0) {
  425. qb_loop_poll_del (instance->totemudpu_poll_handle,
  426. instance->token_socket);
  427. close (instance->token_socket);
  428. }
  429. if (interface_up == 0) {
  430. /*
  431. * Interface is not up
  432. */
  433. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  434. bind_address = &localhost;
  435. /*
  436. * Add a timer to retry building interfaces and request memb_gather_enter
  437. */
  438. qb_loop_timer_add (instance->totemudpu_poll_handle,
  439. QB_LOOP_MED,
  440. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  441. (void *)instance,
  442. timer_function_netif_check_timeout,
  443. &instance->timer_netif_check_timeout);
  444. } else {
  445. /*
  446. * Interface is up
  447. */
  448. instance->netif_bind_state = BIND_STATE_REGULAR;
  449. bind_address = &instance->totem_interface->bindnet;
  450. }
  451. /*
  452. * Create and bind the multicast and unicast sockets
  453. */
  454. totemudpu_build_sockets (instance,
  455. bind_address,
  456. &instance->totem_interface->boundto);
  457. qb_loop_poll_add (instance->totemudpu_poll_handle,
  458. QB_LOOP_MED,
  459. instance->token_socket,
  460. POLLIN, instance, net_deliver_fn);
  461. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  462. /*
  463. * This reports changes in the interface to the user and totemsrp
  464. */
  465. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  466. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  467. log_printf (instance->totemudpu_log_level_notice,
  468. "The network interface [%s] is now up.",
  469. totemip_print (&instance->totem_interface->boundto));
  470. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  471. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  472. }
  473. /*
  474. * Add a timer to check for interface going down in single membership
  475. */
  476. if (instance->my_memb_entries == 1) {
  477. qb_loop_timer_add (instance->totemudpu_poll_handle,
  478. QB_LOOP_MED,
  479. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  480. (void *)instance,
  481. timer_function_netif_check_timeout,
  482. &instance->timer_netif_check_timeout);
  483. }
  484. } else {
  485. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  486. log_printf (instance->totemudpu_log_level_notice,
  487. "The network interface is down.");
  488. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id, 0);
  489. }
  490. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  491. }
  492. }
  493. /* Set the socket priority to INTERACTIVE to ensure
  494. that our messages don't get queued behind anything else */
  495. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  496. {
  497. #ifdef SO_PRIORITY
  498. int prio = 6; /* TC_PRIO_INTERACTIVE */
  499. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  500. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  501. "Could not set traffic priority");
  502. }
  503. #endif
  504. }
  505. static int totemudpu_build_sockets_ip (
  506. struct totemudpu_instance *instance,
  507. struct totem_ip_address *bindnet_address,
  508. struct totem_ip_address *bound_to,
  509. int interface_num)
  510. {
  511. struct sockaddr_storage sockaddr;
  512. int addrlen;
  513. int res;
  514. unsigned int recvbuf_size;
  515. unsigned int optlen = sizeof (recvbuf_size);
  516. unsigned int retries = 0;
  517. /*
  518. * Setup unicast socket
  519. */
  520. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  521. if (instance->token_socket == -1) {
  522. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  523. "socket() failed");
  524. return (-1);
  525. }
  526. totemip_nosigpipe (instance->token_socket);
  527. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  528. if (res == -1) {
  529. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  530. "Could not set non-blocking operation on token socket");
  531. return (-1);
  532. }
  533. /*
  534. * Bind to unicast socket used for token send/receives
  535. * This has the side effect of binding to the correct interface
  536. */
  537. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  538. while (1) {
  539. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  540. if (res == 0) {
  541. break;
  542. }
  543. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  544. "bind token socket failed");
  545. if (++retries > BIND_MAX_RETRIES) {
  546. break;
  547. }
  548. /*
  549. * Wait for a while
  550. */
  551. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  552. }
  553. if (res == -1) {
  554. return (-1);
  555. }
  556. /*
  557. * the token_socket can receive many messages. Allow a large number
  558. * of receive messages on this socket
  559. */
  560. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  561. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  562. &recvbuf_size, optlen);
  563. if (res == -1) {
  564. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  565. "Could not set recvbuf size");
  566. }
  567. return 0;
  568. }
  569. int totemudpu_ifaces_get (
  570. void *net_context,
  571. char ***status,
  572. unsigned int *iface_count)
  573. {
  574. static char *statuses[INTERFACE_MAX] = {(char*)"OK"};
  575. if (status) {
  576. *status = statuses;
  577. }
  578. *iface_count = 1;
  579. return (0);
  580. }
  581. static int totemudpu_build_sockets (
  582. struct totemudpu_instance *instance,
  583. struct totem_ip_address *bindnet_address,
  584. struct totem_ip_address *bound_to)
  585. {
  586. int interface_num;
  587. int interface_up;
  588. int res;
  589. /*
  590. * Determine the ip address bound to and the interface name
  591. */
  592. res = netif_determine (instance,
  593. bindnet_address,
  594. bound_to,
  595. &interface_up,
  596. &interface_num);
  597. if (res == -1) {
  598. return (-1);
  599. }
  600. totemip_copy(&instance->my_id, bound_to);
  601. res = totemudpu_build_sockets_ip (instance,
  602. bindnet_address, bound_to, interface_num);
  603. if (res == -1) {
  604. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  605. LOGSYS_PERROR (errno, instance->totemudpu_log_level_error,
  606. "Unable to create sockets, exiting");
  607. exit(EXIT_FAILURE);
  608. }
  609. /* We only send out of the token socket */
  610. totemudpu_traffic_control_set(instance, instance->token_socket);
  611. /*
  612. * Rebind all members to new ips
  613. */
  614. totemudpu_member_list_rebind_ip(instance);
  615. return res;
  616. }
  617. /*
  618. * Totem Network interface
  619. * depends on poll abstraction, POSIX, IPV4
  620. */
  621. /*
  622. * Create an instance
  623. */
  624. int totemudpu_initialize (
  625. qb_loop_t *poll_handle,
  626. void **udpu_context,
  627. struct totem_config *totem_config,
  628. totemsrp_stats_t *stats,
  629. void *context,
  630. void (*deliver_fn) (
  631. void *context,
  632. const void *msg,
  633. unsigned int msg_len),
  634. void (*iface_change_fn) (
  635. void *context,
  636. const struct totem_ip_address *iface_address,
  637. unsigned int ring_no),
  638. void (*mtu_changed) (
  639. void *context,
  640. int net_mtu),
  641. void (*target_set_completed) (
  642. void *context))
  643. {
  644. struct totemudpu_instance *instance;
  645. instance = malloc (sizeof (struct totemudpu_instance));
  646. if (instance == NULL) {
  647. return (-1);
  648. }
  649. totemudpu_instance_initialize (instance);
  650. instance->totem_config = totem_config;
  651. instance->stats = stats;
  652. /*
  653. * Configure logging
  654. */
  655. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  656. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  657. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  658. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  659. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  660. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  661. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  662. /*
  663. * Initialize local variables for totemudpu
  664. */
  665. instance->totem_interface = &totem_config->interfaces[0];
  666. memset (instance->iov_buffer, 0, UDPU_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. totemudpu_start_merge_detect_timeout(instance);
  686. *udpu_context = instance;
  687. return (0);
  688. }
  689. void *totemudpu_buffer_alloc (void)
  690. {
  691. return malloc (UDPU_FRAME_SIZE_MAX);
  692. }
  693. void totemudpu_buffer_release (void *ptr)
  694. {
  695. return free (ptr);
  696. }
  697. int totemudpu_processor_count_set (
  698. void *udpu_context,
  699. int processor_count)
  700. {
  701. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  702. int res = 0;
  703. instance->my_memb_entries = processor_count;
  704. qb_loop_timer_del (instance->totemudpu_poll_handle,
  705. instance->timer_netif_check_timeout);
  706. if (processor_count == 1) {
  707. qb_loop_timer_add (instance->totemudpu_poll_handle,
  708. QB_LOOP_MED,
  709. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  710. (void *)instance,
  711. timer_function_netif_check_timeout,
  712. &instance->timer_netif_check_timeout);
  713. }
  714. return (res);
  715. }
  716. int totemudpu_recv_flush (void *udpu_context)
  717. {
  718. int res = 0;
  719. return (res);
  720. }
  721. int totemudpu_send_flush (void *udpu_context)
  722. {
  723. int res = 0;
  724. return (res);
  725. }
  726. int totemudpu_token_send (
  727. void *udpu_context,
  728. const void *msg,
  729. unsigned int msg_len)
  730. {
  731. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  732. int res = 0;
  733. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  734. return (res);
  735. }
  736. int totemudpu_mcast_flush_send (
  737. void *udpu_context,
  738. const void *msg,
  739. unsigned int msg_len)
  740. {
  741. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  742. int res = 0;
  743. mcast_sendmsg (instance, msg, msg_len, 0);
  744. return (res);
  745. }
  746. int totemudpu_mcast_noflush_send (
  747. void *udpu_context,
  748. const void *msg,
  749. unsigned int msg_len)
  750. {
  751. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  752. int res = 0;
  753. mcast_sendmsg (instance, msg, msg_len, 1);
  754. return (res);
  755. }
  756. extern int totemudpu_iface_check (void *udpu_context)
  757. {
  758. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  759. int res = 0;
  760. timer_function_netif_check_timeout (instance);
  761. return (res);
  762. }
  763. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  764. {
  765. totem_config->net_mtu -= totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  766. }
  767. int totemudpu_token_target_set (
  768. void *udpu_context,
  769. const struct totem_ip_address *token_target)
  770. {
  771. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  772. int res = 0;
  773. memcpy (&instance->token_target, token_target,
  774. sizeof (struct totem_ip_address));
  775. instance->totemudpu_target_set_completed (instance->context);
  776. return (res);
  777. }
  778. extern int totemudpu_recv_mcast_empty (
  779. void *udpu_context)
  780. {
  781. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  782. unsigned int res;
  783. struct sockaddr_storage system_from;
  784. struct msghdr msg_recv;
  785. struct pollfd ufd;
  786. int nfds;
  787. int msg_processed = 0;
  788. /*
  789. * Receive datagram
  790. */
  791. msg_recv.msg_name = &system_from;
  792. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  793. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  794. msg_recv.msg_iovlen = 1;
  795. #ifdef HAVE_MSGHDR_CONTROL
  796. msg_recv.msg_control = 0;
  797. #endif
  798. #ifdef HAVE_MSGHDR_CONTROLLEN
  799. msg_recv.msg_controllen = 0;
  800. #endif
  801. #ifdef HAVE_MSGHDR_FLAGS
  802. msg_recv.msg_flags = 0;
  803. #endif
  804. #ifdef HAVE_MSGHDR_ACCRIGHTS
  805. msg_recv.msg_accrights = NULL;
  806. #endif
  807. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  808. msg_recv.msg_accrightslen = 0;
  809. #endif
  810. do {
  811. ufd.fd = instance->token_socket;
  812. ufd.events = POLLIN;
  813. nfds = poll (&ufd, 1, 0);
  814. if (nfds == 1 && ufd.revents & POLLIN) {
  815. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  816. if (res != -1) {
  817. msg_processed = 1;
  818. } else {
  819. msg_processed = -1;
  820. }
  821. }
  822. } while (nfds == 1);
  823. return (msg_processed);
  824. }
  825. static int totemudpu_create_sending_socket(
  826. void *udpu_context,
  827. const struct totem_ip_address *member)
  828. {
  829. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  830. int fd;
  831. int res;
  832. unsigned int sendbuf_size;
  833. unsigned int optlen = sizeof (sendbuf_size);
  834. struct sockaddr_storage sockaddr;
  835. int addrlen;
  836. fd = socket (member->family, SOCK_DGRAM, 0);
  837. if (fd == -1) {
  838. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  839. "Could not create socket for new member");
  840. return (-1);
  841. }
  842. totemip_nosigpipe (fd);
  843. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  844. if (res == -1) {
  845. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  846. "Could not set non-blocking operation on token socket");
  847. goto error_close_fd;
  848. }
  849. /*
  850. * These sockets are used to send multicast messages, so their buffers
  851. * should be large
  852. */
  853. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  854. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  855. &sendbuf_size, optlen);
  856. if (res == -1) {
  857. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  858. "Could not set sendbuf size");
  859. /*
  860. * Fail in setting sendbuf size is not fatal -> don't exit
  861. */
  862. }
  863. /*
  864. * Bind to sending interface
  865. */
  866. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  867. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  868. if (res == -1) {
  869. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  870. "bind token socket failed");
  871. goto error_close_fd;
  872. }
  873. return (fd);
  874. error_close_fd:
  875. close(fd);
  876. return (-1);
  877. }
  878. int totemudpu_iface_set (void *net_context,
  879. const struct totem_ip_address *local_addr,
  880. unsigned short ip_port,
  881. unsigned int iface_no)
  882. {
  883. /* Not supported */
  884. return (-1);
  885. }
  886. int totemudpu_member_add (
  887. void *udpu_context,
  888. const struct totem_ip_address *local,
  889. const struct totem_ip_address *member,
  890. int ring_no)
  891. {
  892. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  893. struct totemudpu_member *new_member;
  894. new_member = malloc (sizeof (struct totemudpu_member));
  895. if (new_member == NULL) {
  896. return (-1);
  897. }
  898. memset(new_member, 0, sizeof(*new_member));
  899. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  900. totemip_print(member));
  901. qb_list_init (&new_member->list);
  902. qb_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. new_member->active = 1;
  906. return (0);
  907. }
  908. int totemudpu_member_remove (
  909. void *udpu_context,
  910. const struct totem_ip_address *token_target,
  911. int ring_no)
  912. {
  913. int found = 0;
  914. struct qb_list_head *list;
  915. struct totemudpu_member *member;
  916. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  917. /*
  918. * Find the member to remove and close its socket
  919. */
  920. qb_list_for_each(list, &(instance->member_list)) {
  921. member = qb_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. qb_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 qb_list_head *list;
  953. struct totemudpu_member *member;
  954. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  955. qb_list_for_each(list, &(instance->member_list)) {
  956. member = qb_list_entry (list,
  957. struct totemudpu_member,
  958. list);
  959. if (member->fd > 0) {
  960. close (member->fd);
  961. }
  962. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  963. }
  964. return (0);
  965. }
  966. static void timer_function_merge_detect_timeout (
  967. void *data)
  968. {
  969. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  970. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  971. instance->send_merge_detect_message = 1;
  972. }
  973. instance->merge_detect_messages_sent_before_timeout = 0;
  974. totemudpu_start_merge_detect_timeout(instance);
  975. }
  976. static void totemudpu_start_merge_detect_timeout(
  977. void *udpu_context)
  978. {
  979. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  980. qb_loop_timer_add(instance->totemudpu_poll_handle,
  981. QB_LOOP_MED,
  982. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  983. (void *)instance,
  984. timer_function_merge_detect_timeout,
  985. &instance->timer_merge_detect_timeout);
  986. }
  987. static void totemudpu_stop_merge_detect_timeout(
  988. void *udpu_context)
  989. {
  990. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  991. qb_loop_timer_del(instance->totemudpu_poll_handle,
  992. instance->timer_merge_detect_timeout);
  993. }