totemudpu.c 30 KB

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