totemudpu.c 30 KB

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