totemudpu.c 29 KB

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