40 #include <sys/types.h>
42 #include <sys/socket.h>
45 #include <sys/ioctl.h>
46 #include <sys/param.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
61 #include <qb/qblist.h>
62 #include <qb/qbdefs.h>
63 #include <qb/qbloop.h>
67 #define LOGSYS_UTILS_ONLY 1
74 #define MSG_NOSIGNAL 0
77 #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * UDP_RECEIVE_FRAME_SIZE_MAX)
78 #define NETIF_STATE_REPORT_UP 1
79 #define NETIF_STATE_REPORT_DOWN 2
81 #define BIND_STATE_UNBOUND 0
82 #define BIND_STATE_REGULAR 1
83 #define BIND_STATE_LOOPBACK 2
86 struct qb_list_head
list;
106 unsigned int msg_len,
112 unsigned int ring_no);
134 const char *
function,
191 static int totemudpu_build_sockets (
196 static int totemudpu_create_sending_socket(
203 static void totemudpu_start_merge_detect_timeout(
206 static void totemudpu_stop_merge_detect_timeout(
227 #define log_printf(level, format, args...) \
229 instance->totemudpu_log_printf ( \
230 level, instance->totemudpu_subsys_id, \
231 __FUNCTION__, __FILE__, __LINE__, \
232 (const char *)format, ##args); \
234 #define LOGSYS_PERROR(err_num, level, fmt, args...) \
236 char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
237 const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
238 instance->totemudpu_log_printf ( \
239 level, instance->totemudpu_subsys_id, \
240 __FUNCTION__, __FILE__, __LINE__, \
241 fmt ": %s (%d)", ##args, _error_ptr, err_num); \
246 const char *cipher_type,
247 const char *hash_type)
254 static inline void ucast_sendmsg (
258 unsigned int msg_len)
260 struct msghdr msg_ucast;
262 struct sockaddr_storage sockaddr;
267 iovec.iov_base = (
void *)msg;
268 iovec.iov_len = msg_len;
275 memset(&msg_ucast, 0,
sizeof(msg_ucast));
276 msg_ucast.msg_name = &sockaddr;
277 msg_ucast.msg_namelen = addrlen;
278 msg_ucast.msg_iov = (
void *)&iovec;
279 msg_ucast.msg_iovlen = 1;
280 #ifdef HAVE_MSGHDR_CONTROL
281 msg_ucast.msg_control = 0;
283 #ifdef HAVE_MSGHDR_CONTROLLEN
284 msg_ucast.msg_controllen = 0;
286 #ifdef HAVE_MSGHDR_FLAGS
287 msg_ucast.msg_flags = 0;
289 #ifdef HAVE_MSGHDR_ACCRIGHTS
290 msg_ucast.msg_accrights = NULL;
292 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
293 msg_ucast.msg_accrightslen = 0;
300 msg_ucast.msg_name = NULL;
301 msg_ucast.msg_namelen = 0;
312 "sendmsg(ucast) failed (non-critical)");
316 static inline void mcast_sendmsg (
319 unsigned int msg_len,
322 struct msghdr msg_mcast;
325 struct sockaddr_storage sockaddr;
327 struct qb_list_head *list;
330 iovec.iov_base = (
void *)msg;
331 iovec.iov_len = msg_len;
333 memset(&msg_mcast, 0,
sizeof(msg_mcast));
351 msg_mcast.msg_name = &sockaddr;
352 msg_mcast.msg_namelen = addrlen;
353 msg_mcast.msg_iov = (
void *)&iovec;
354 msg_mcast.msg_iovlen = 1;
355 #ifdef HAVE_MSGHDR_CONTROL
356 msg_mcast.msg_control = 0;
358 #ifdef HAVE_MSGHDR_CONTROLLEN
359 msg_mcast.msg_controllen = 0;
361 #ifdef HAVE_MSGHDR_FLAGS
362 msg_mcast.msg_flags = 0;
364 #ifdef HAVE_MSGHDR_ACCRIGHTS
365 msg_mcast.msg_accrights = NULL;
367 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
368 msg_mcast.msg_accrightslen = 0;
378 "sendmsg(mcast) failed (non-critical)");
394 msg_mcast.msg_name = NULL;
395 msg_mcast.msg_namelen = 0;
396 msg_mcast.msg_iov = (
void *)&iovec;
397 msg_mcast.msg_iovlen = 1;
398 #ifdef HAVE_MSGHDR_CONTROL
399 msg_mcast.msg_control = 0;
401 #ifdef HAVE_MSGHDR_CONTROLLEN
402 msg_mcast.msg_controllen = 0;
404 #ifdef HAVE_MSGHDR_FLAGS
405 msg_mcast.msg_flags = 0;
407 #ifdef HAVE_MSGHDR_ACCRIGHTS
408 msg_mcast.msg_accrights = NULL;
410 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
411 msg_mcast.msg_accrightslen = 0;
418 "sendmsg(local mcast loop) failed (non-critical)");
442 totemudpu_stop_merge_detect_timeout(instance);
448 const void *udpu_context,
449 const struct sockaddr *sa)
451 struct qb_list_head *list;
459 member = qb_list_entry (list,
473 static int net_deliver_fn (
479 struct msghdr msg_recv;
483 int truncated_packet;
491 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
492 msg_recv.msg_iov = iovec;
493 msg_recv.msg_iovlen = 1;
494 #ifdef HAVE_MSGHDR_CONTROL
495 msg_recv.msg_control = 0;
497 #ifdef HAVE_MSGHDR_CONTROLLEN
498 msg_recv.msg_controllen = 0;
500 #ifdef HAVE_MSGHDR_FLAGS
501 msg_recv.msg_flags = 0;
503 #ifdef HAVE_MSGHDR_ACCRIGHTS
504 msg_recv.msg_accrights = NULL;
506 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
507 msg_recv.msg_accrightslen = 0;
510 bytes_received = recvmsg (fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
511 if (bytes_received == -1) {
517 truncated_packet = 0;
519 #ifdef HAVE_MSGHDR_FLAGS
520 if (msg_recv.msg_flags & MSG_TRUNC) {
521 truncated_packet = 1;
529 truncated_packet = 1;
533 if (truncated_packet) {
535 "Received too big message. This may be because something bad is happening"
536 "on the network (attack?), or you tried join more nodes than corosync is"
537 "compiled with (%u) or bug in the code (bad estimation of "
543 find_member_by_sockaddr(instance, (
const struct sockaddr *)&
system_from) == NULL) {
550 iovec->iov_len = bytes_received;
565 static int netif_determine (
575 interface_up, interface_num,
587 static void timer_function_netif_check_timeout (
597 netif_determine (instance,
600 &interface_up, &interface_num);
606 interface_up == 0) ||
610 interface_up == 1)) {
616 timer_function_netif_check_timeout,
617 &instance->timer_netif_check_timeout);
632 if (interface_up == 0) {
635 "One of your ip addresses are now bound to localhost. "
636 "Corosync would not work correctly.");
652 timer_function_netif_check_timeout,
653 &instance->timer_netif_check_timeout);
663 totemudpu_build_sockets (instance,
671 POLLIN, instance, net_deliver_fn);
682 "The network interface [%s] is now up.",
695 timer_function_netif_check_timeout,
696 &instance->timer_netif_check_timeout);
702 "The network interface is down.");
712 static void totemudpu_traffic_control_set(
struct totemudpu_instance *instance,
int sock)
717 if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio,
sizeof(
int))) {
719 "Could not set traffic priority");
724 static int totemudpu_build_sockets_ip (
730 struct sockaddr_storage sockaddr;
733 unsigned int recvbuf_size;
734 unsigned int optlen =
sizeof (recvbuf_size);
735 unsigned int retries = 0;
748 res = fcntl (instance->
token_socket, F_SETFL, O_NONBLOCK);
751 "Could not set non-blocking operation on token socket");
761 res = bind (instance->
token_socket, (
struct sockaddr *)&sockaddr, addrlen);
766 "bind token socket failed");
786 res = setsockopt (instance->
token_socket, SOL_SOCKET, SO_RCVBUF,
787 &recvbuf_size, optlen);
790 "Could not set recvbuf size");
799 unsigned int *iface_count)
812 static int totemudpu_build_local_sockets(
816 unsigned int sendbuf_size;
817 unsigned int recvbuf_size;
818 unsigned int optlen =
sizeof (sendbuf_size);
824 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, instance->
local_loop_sock) == -1) {
830 for (i = 0; i < 2; i++) {
835 "Could not set non-blocking operation on multicast socket");
843 res = setsockopt (instance->
local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
846 "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
849 res = setsockopt (instance->
local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
852 "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
856 res = getsockopt (instance->
local_loop_sock[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
859 "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
862 res = getsockopt (instance->
local_loop_sock[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
865 "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
871 static int totemudpu_build_sockets (
883 res = netif_determine (instance,
895 res = totemudpu_build_sockets_ip (instance,
896 bindnet_address, bound_to, interface_num);
901 "Unable to create sockets, exiting");
906 totemudpu_traffic_control_set(instance, instance->
token_socket);
925 qb_loop_t *poll_handle,
934 unsigned int msg_len,
937 void (*iface_change_fn) (
940 unsigned int ring_no),
942 void (*mtu_changed) (
946 void (*target_set_completed) (
952 if (instance == NULL) {
956 totemudpu_instance_initialize (instance);
992 if (totemudpu_build_local_sockets(instance) == -1) {
1001 POLLIN, instance, net_deliver_fn);
1009 100*QB_TIME_NS_IN_MSEC,
1011 timer_function_netif_check_timeout,
1014 totemudpu_start_merge_detect_timeout((
void*)instance);
1032 int processor_count)
1040 if (processor_count == 1) {
1045 timer_function_netif_check_timeout,
1046 &instance->timer_netif_check_timeout);
1069 unsigned int msg_len)
1074 ucast_sendmsg (instance, &instance->
token_target, msg, msg_len);
1081 unsigned int msg_len)
1086 mcast_sendmsg (instance, msg, msg_len, 0);
1094 unsigned int msg_len)
1099 mcast_sendmsg (instance, msg, msg_len, 1);
1109 timer_function_netif_check_timeout (instance);
1126 struct qb_list_head *list;
1152 struct msghdr msg_recv;
1155 int msg_processed = 0;
1162 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
1164 msg_recv.msg_iovlen = 1;
1165 #ifdef HAVE_MSGHDR_CONTROL
1166 msg_recv.msg_control = 0;
1168 #ifdef HAVE_MSGHDR_CONTROLLEN
1169 msg_recv.msg_controllen = 0;
1171 #ifdef HAVE_MSGHDR_FLAGS
1172 msg_recv.msg_flags = 0;
1174 #ifdef HAVE_MSGHDR_ACCRIGHTS
1175 msg_recv.msg_accrights = NULL;
1177 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
1178 msg_recv.msg_accrightslen = 0;
1181 for (i = 0; i < 2; i++) {
1197 ufd.events = POLLIN;
1198 nfds = poll (&ufd, 1, 0);
1199 if (nfds == 1 && ufd.revents & POLLIN) {
1200 res = recvmsg (sock, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
1207 }
while (nfds == 1);
1210 return (msg_processed);
1213 static int totemudpu_create_sending_socket(
1220 unsigned int sendbuf_size;
1221 unsigned int optlen =
sizeof (sendbuf_size);
1222 struct sockaddr_storage sockaddr;
1225 fd = socket (member->
family, SOCK_DGRAM, 0);
1228 "Could not create socket for new member");
1232 res = fcntl (fd, F_SETFL, O_NONBLOCK);
1235 "Could not set non-blocking operation on token socket");
1236 goto error_close_fd;
1244 res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
1245 &sendbuf_size, optlen);
1248 "Could not set sendbuf size");
1258 res = bind (fd, (
struct sockaddr *)&sockaddr, addrlen);
1261 "bind token socket failed");
1262 goto error_close_fd;
1274 unsigned short ip_port,
1275 unsigned int iface_no)
1292 if (new_member == NULL) {
1296 memset(new_member, 0,
sizeof(*new_member));
1300 qb_list_init (&new_member->
list);
1303 new_member->
fd = totemudpu_create_sending_socket(udpu_context,
member);
1315 struct qb_list_head *list;
1323 qb_list_for_each(list, &(instance->
member_list)) {
1324 member = qb_list_entry (list,
1330 "removing UDPU member {%s}",
1333 if (member->
fd > 0) {
1335 "Closing socket to: {%s}",
1360 struct qb_list_head *list;
1365 qb_list_for_each(list, &(instance->
member_list)) {
1366 member = qb_list_entry (list,
1370 if (member->
fd > 0) {
1381 static void timer_function_merge_detect_timeout (
1392 totemudpu_start_merge_detect_timeout(instance);
1395 static void totemudpu_start_merge_detect_timeout(
1404 timer_function_merge_detect_timeout,
1405 &instance->timer_merge_detect_timeout);
1409 static void totemudpu_stop_merge_detect_timeout(
#define PROCESSOR_COUNT_MAX
@ COROSYNC_DONE_FATAL_ERR
#define LOGSYS_LEVEL_NOTICE
#define LOGSYS_LEVEL_DEBUG
struct totem_logging_configuration totem_logging_configuration
unsigned int downcheck_timeout
struct totem_interface * interfaces
unsigned int clear_node_high_bit
unsigned int merge_timeout
unsigned int block_unlisted_ips
struct totem_ip_address boundto
struct totem_ip_address bindnet
The totem_ip_address struct.
void(* log_printf)(int level, int subsys, const char *function_name, const char *file_name, int file_line, const char *format,...) __attribute__((format(printf
struct totem_config * totem_config
qb_loop_t * totemudpu_poll_handle
void(* totemudpu_log_printf)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf
void(* totemudpu_deliver_fn)(void *context, const void *msg, unsigned int msg_len, const struct sockaddr_storage *system_from)
struct totem_ip_address my_id
int totemudpu_log_level_notice
qb_loop_timer_handle timer_netif_check_timeout
qb_loop_timer_handle timer_merge_detect_timeout
unsigned int my_memb_entries
int totemudpu_log_level_security
struct totem_ip_address token_target
unsigned int merge_detect_messages_sent_before_timeout
int send_merge_detect_message
struct timeval stats_tv_start
int totemudpu_log_level_warning
char iov_buffer[UDP_RECEIVE_FRAME_SIZE_MAX]
struct iovec totemudpu_iov_recv
struct totem_interface * totem_interface
int totemudpu_log_level_debug
int totemudpu_log_level_error
void(* totemudpu_target_set_completed)(void *context)
struct qb_list_head member_list
void(*) void udpu_context)
void(* totemudpu_iface_change_fn)(void *context, const struct totem_ip_address *iface_address, unsigned int ring_no)
struct totem_ip_address member
struct totemudpu_instance * instance
#define BIND_RETRIES_INTERVAL
#define UDP_RECEIVE_FRAME_SIZE_MAX
int totemip_sa_equal(const struct totem_ip_address *totem_ip, const struct sockaddr *sa)
int totemip_iface_check(struct totem_ip_address *bindnet, struct totem_ip_address *boundto, int *interface_up, int *interface_num, int mask_high_bit)
#define totemip_nosigpipe(s)
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
size_t totemip_udpip_header_size(int family)
int totemip_compare(const void *a, const void *b)
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
const char * totemip_sa_print(const struct sockaddr *sa)
const char * totemip_print(const struct totem_ip_address *addr)
struct srp_addr system_from
void totemudpu_buffer_release(void *ptr)
int totemudpu_ifaces_get(void *net_context, char ***status, unsigned int *iface_count)
#define NETIF_STATE_REPORT_UP
#define log_printf(level, format, args...)
int totemudpu_send_flush(void *udpu_context)
int totemudpu_token_target_set(void *udpu_context, unsigned int nodeid)
#define BIND_STATE_LOOPBACK
#define NETIF_STATE_REPORT_DOWN
int totemudpu_member_list_rebind_ip(void *udpu_context)
int totemudpu_recv_mcast_empty(void *udpu_context)
int totemudpu_processor_count_set(void *udpu_context, int processor_count)
int totemudpu_token_send(void *udpu_context, const void *msg, unsigned int msg_len)
int totemudpu_iface_set(void *net_context, const struct totem_ip_address *local_addr, unsigned short ip_port, unsigned int iface_no)
int totemudpu_initialize(qb_loop_t *poll_handle, void **udpu_context, struct totem_config *totem_config, totemsrp_stats_t *stats, void *context, void(*deliver_fn)(void *context, const void *msg, unsigned int msg_len, const struct sockaddr_storage *system_from), void(*iface_change_fn)(void *context, const struct totem_ip_address *iface_address, unsigned int ring_no), void(*mtu_changed)(void *context, int net_mtu), void(*target_set_completed)(void *context))
Create an instance.
int totemudpu_crypto_set(void *udpu_context, const char *cipher_type, const char *hash_type)
#define MCAST_SOCKET_BUFFER_SIZE
int totemudpu_recv_flush(void *udpu_context)
int totemudpu_mcast_noflush_send(void *udpu_context, const void *msg, unsigned int msg_len)
int totemudpu_member_remove(void *udpu_context, const struct totem_ip_address *token_target, int ring_no)
int totemudpu_finalize(void *udpu_context)
int totemudpu_mcast_flush_send(void *udpu_context, const void *msg, unsigned int msg_len)
int totemudpu_iface_check(void *udpu_context)
void * totemudpu_buffer_alloc(void)
int totemudpu_reconfigure(void *udpu_context, struct totem_config *totem_config)
#define LOGSYS_PERROR(err_num, level, fmt, args...)
#define BIND_STATE_UNBOUND
#define BIND_STATE_REGULAR
void totemudpu_net_mtu_adjust(void *udpu_context, struct totem_config *totem_config)
int totemudpu_member_add(void *udpu_context, const struct totem_ip_address *local, const struct totem_ip_address *member, int ring_no)