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/qbdefs.h> 62 #include <qb/qbloop.h> 67 #define LOGSYS_UTILS_ONLY 1 80 #define MSG_NOSIGNAL 0 83 #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX) 84 #define NETIF_STATE_REPORT_UP 1 85 #define NETIF_STATE_REPORT_DOWN 2 87 #define BIND_STATE_UNBOUND 0 88 #define BIND_STATE_REGULAR 1 89 #define BIND_STATE_LOOPBACK 2 111 void (*totemudpu_deliver_fn) (
114 unsigned int msg_len);
116 void (*totemudpu_iface_change_fn) (
120 void (*totemudpu_target_set_completed) (
void *context);
137 void (*totemudpu_log_printf) (
140 const char *
function,
150 struct iovec totemudpu_iov_recv;
164 struct timeval stats_tv_start;
191 unsigned int msg_len;
195 static int totemudpu_build_sockets (
200 static int totemudpu_create_sending_socket(
207 static void totemudpu_start_merge_detect_timeout(
210 static void totemudpu_stop_merge_detect_timeout(
233 #define log_printf(level, format, args...) \ 235 instance->totemudpu_log_printf ( \ 236 level, instance->totemudpu_subsys_id, \ 237 __FUNCTION__, __FILE__, __LINE__, \ 238 (const char *)format, ##args); \ 240 #define LOGSYS_PERROR(err_num, level, fmt, args...) \ 242 char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \ 243 const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \ 244 instance->totemudpu_log_printf ( \ 245 level, instance->totemudpu_subsys_id, \ 246 __FUNCTION__, __FILE__, __LINE__, \ 247 fmt ": %s (%d)", ##args, _error_ptr, err_num); \ 252 const char *cipher_type,
253 const char *hash_type)
260 static inline void ucast_sendmsg (
264 unsigned int msg_len)
266 struct msghdr msg_ucast;
270 struct sockaddr_storage sockaddr;
285 (
const unsigned char *)msg,
288 &buf_out_len) != 0) {
293 iovec.iov_base = (
void *)buf_out;
294 iovec.iov_len = buf_out_len;
301 memset(&msg_ucast, 0,
sizeof(msg_ucast));
302 msg_ucast.msg_name = &sockaddr;
303 msg_ucast.msg_namelen = addrlen;
304 msg_ucast.msg_iov = (
void *)&iovec;
305 msg_ucast.msg_iovlen = 1;
306 #ifdef HAVE_MSGHDR_CONTROL 307 msg_ucast.msg_control = 0;
309 #ifdef HAVE_MSGHDR_CONTROLLEN 310 msg_ucast.msg_controllen = 0;
312 #ifdef HAVE_MSGHDR_FLAGS 313 msg_ucast.msg_flags = 0;
315 #ifdef HAVE_MSGHDR_ACCRIGHTS 316 msg_ucast.msg_accrights = NULL;
318 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 319 msg_ucast.msg_accrightslen = 0;
330 "sendmsg(ucast) failed (non-critical)");
334 static inline void mcast_sendmsg (
337 unsigned int msg_len,
340 struct msghdr msg_mcast;
345 struct sockaddr_storage sockaddr;
361 (
const unsigned char *)msg,
364 &buf_out_len) != 0) {
369 iovec.iov_base = (
void *)buf_out;
370 iovec.iov_len = buf_out_len;
372 memset(&msg_mcast, 0,
sizeof(msg_mcast));
393 msg_mcast.msg_name = &sockaddr;
394 msg_mcast.msg_namelen = addrlen;
395 msg_mcast.msg_iov = (
void *)&iovec;
396 msg_mcast.msg_iovlen = 1;
397 #ifdef HAVE_MSGHDR_CONTROL 398 msg_mcast.msg_control = 0;
400 #ifdef HAVE_MSGHDR_CONTROLLEN 401 msg_mcast.msg_controllen = 0;
403 #ifdef HAVE_MSGHDR_FLAGS 404 msg_mcast.msg_flags = 0;
406 #ifdef HAVE_MSGHDR_ACCRIGHTS 407 msg_mcast.msg_accrights = NULL;
409 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 410 msg_mcast.msg_accrightslen = 0;
420 "sendmsg(mcast) failed (non-critical)");
445 totemudpu_stop_merge_detect_timeout(instance);
450 static int net_deliver_fn (
456 struct msghdr msg_recv;
458 struct sockaddr_storage system_from;
461 int truncated_packet;
469 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
470 msg_recv.msg_iov = iovec;
471 msg_recv.msg_iovlen = 1;
472 #ifdef HAVE_MSGHDR_CONTROL 473 msg_recv.msg_control = 0;
475 #ifdef HAVE_MSGHDR_CONTROLLEN 476 msg_recv.msg_controllen = 0;
478 #ifdef HAVE_MSGHDR_FLAGS 479 msg_recv.msg_flags = 0;
481 #ifdef HAVE_MSGHDR_ACCRIGHTS 482 msg_recv.msg_accrights = NULL;
484 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 485 msg_recv.msg_accrightslen = 0;
488 bytes_received = recvmsg (fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
489 if (bytes_received == -1) {
495 truncated_packet = 0;
497 #ifdef HAVE_MSGHDR_FLAGS 498 if (msg_recv.msg_flags & MSG_TRUNC) {
499 truncated_packet = 1;
507 truncated_packet = 1;
511 if (truncated_packet) {
513 "Received too big message. This may be because something bad is happening" 514 "on the network (attack?), or you tried join more nodes than corosync is" 515 "compiled with (%u) or bug in the code (bad estimation of " 528 "Invalid packet data");
532 iovec->iov_len = bytes_received;
546 static int netif_determine (
556 interface_up, interface_num,
568 static void timer_function_netif_check_timeout (
579 netif_determine (instance,
582 &interface_up, &interface_num);
588 interface_up == 0) ||
592 interface_up == 1)) {
598 timer_function_netif_check_timeout,
599 &instance->timer_netif_check_timeout);
613 if (interface_up == 0) {
618 bind_address = &localhost;
627 timer_function_netif_check_timeout,
628 &instance->timer_netif_check_timeout);
639 totemudpu_build_sockets (instance,
646 POLLIN, instance, net_deliver_fn);
656 "The network interface [%s] is now up.",
669 timer_function_netif_check_timeout,
670 &instance->timer_netif_check_timeout);
676 "The network interface is down.");
686 static void totemudpu_traffic_control_set(
struct totemudpu_instance *instance,
int sock)
691 if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio,
sizeof(
int))) {
693 "Could not set traffic priority");
698 static int totemudpu_build_sockets_ip (
704 struct sockaddr_storage sockaddr;
707 unsigned int recvbuf_size;
708 unsigned int optlen =
sizeof (recvbuf_size);
709 unsigned int retries = 0;
722 res = fcntl (instance->
token_socket, F_SETFL, O_NONBLOCK);
725 "Could not set non-blocking operation on token socket");
735 res = bind (instance->
token_socket, (
struct sockaddr *)&sockaddr, addrlen);
740 "bind token socket failed");
760 res = setsockopt (instance->
token_socket, SOL_SOCKET, SO_RCVBUF,
761 &recvbuf_size, optlen);
764 "Could not set recvbuf size");
770 static int totemudpu_build_sockets (
782 res = netif_determine (instance,
794 res = totemudpu_build_sockets_ip (instance,
795 bindnet_address, bound_to, interface_num);
800 "Unable to create sockets, exiting");
805 totemudpu_traffic_control_set(instance, instance->
token_socket);
824 qb_loop_t *poll_handle,
834 unsigned int msg_len),
836 void (*iface_change_fn) (
840 void (*target_set_completed) (
846 if (instance == NULL) {
850 totemudpu_instance_initialize (instance);
908 100*QB_TIME_NS_IN_MSEC,
910 timer_function_netif_check_timeout,
911 &instance->timer_netif_check_timeout);
913 totemudpu_start_merge_detect_timeout(instance);
915 *udpu_context = instance;
939 if (processor_count == 1) {
944 timer_function_netif_check_timeout,
945 &instance->timer_netif_check_timeout);
968 unsigned int msg_len)
973 ucast_sendmsg (instance, &instance->
token_target, msg, msg_len);
980 unsigned int msg_len)
985 mcast_sendmsg (instance, msg, msg_len, 0);
993 unsigned int msg_len)
998 mcast_sendmsg (instance, msg, msg_len, 1);
1008 timer_function_netif_check_timeout (instance);
1025 const char *ret_char;
1064 struct sockaddr_storage system_from;
1065 struct msghdr msg_recv;
1068 int msg_processed = 0;
1074 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
1076 msg_recv.msg_iovlen = 1;
1077 #ifdef HAVE_MSGHDR_CONTROL 1078 msg_recv.msg_control = 0;
1080 #ifdef HAVE_MSGHDR_CONTROLLEN 1081 msg_recv.msg_controllen = 0;
1083 #ifdef HAVE_MSGHDR_FLAGS 1084 msg_recv.msg_flags = 0;
1086 #ifdef HAVE_MSGHDR_ACCRIGHTS 1087 msg_recv.msg_accrights = NULL;
1089 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 1090 msg_recv.msg_accrightslen = 0;
1095 ufd.events = POLLIN;
1096 nfds = poll (&ufd, 1, 0);
1097 if (nfds == 1 && ufd.revents & POLLIN) {
1105 }
while (nfds == 1);
1107 return (msg_processed);
1110 static int totemudpu_create_sending_socket(
1117 unsigned int sendbuf_size;
1118 unsigned int optlen =
sizeof (sendbuf_size);
1119 struct sockaddr_storage sockaddr;
1122 fd = socket (member->
family, SOCK_DGRAM, 0);
1125 "Could not create socket for new member");
1129 res = fcntl (fd, F_SETFL, O_NONBLOCK);
1132 "Could not set non-blocking operation on token socket");
1133 goto error_close_fd;
1141 res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
1142 &sendbuf_size, optlen);
1145 "Could not set sendbuf size");
1155 res = bind (fd, (
struct sockaddr *)&sockaddr, addrlen);
1158 "bind token socket failed");
1159 goto error_close_fd;
1178 if (new_member == NULL) {
1182 memset(new_member, 0,
sizeof(*new_member));
1186 list_init (&new_member->
list);
1189 new_member->
fd = totemudpu_create_sending_socket(udpu_context, member);
1210 list = list->
next) {
1218 "removing UDPU member {%s}",
1221 if (member->
fd > 0) {
1223 "Closing socket to: {%s}",
1255 list = list->
next) {
1261 if (member->
fd > 0) {
1265 member->
fd = totemudpu_create_sending_socket(udpu_context, &member->
member);
1290 "Marking UDPU member %s %s",
1292 (active ?
"active" :
"inactive"));
1303 "Can't find UDPU member %s (should be marked as %s)",
1305 (active ?
"active" :
"inactive"));
1311 static void timer_function_merge_detect_timeout (
1322 totemudpu_start_merge_detect_timeout(instance);
1325 static void totemudpu_start_merge_detect_timeout(
1334 timer_function_merge_detect_timeout,
1335 &instance->timer_merge_detect_timeout);
1339 static void totemudpu_stop_merge_detect_timeout(
unsigned int clear_node_high_bit
#define NETIF_STATE_REPORT_UP
struct totem_config * totem_config
struct totem_ip_address member
int totemip_localhost(int family, struct totem_ip_address *localhost)
unsigned int my_memb_entries
void(* totemudpu_iface_change_fn)(void *context, const struct totem_ip_address *iface_address)
struct totem_interface * interfaces
unsigned int interface_count
size_t crypto_sec_header_size(const char *crypto_cipher_type, const char *crypto_hash_type)
The totem_ip_address struct.
void(*) void udpu_context)
int totemudpu_token_target_set(void *udpu_context, const struct totem_ip_address *token_target)
const char * totemip_print(const struct totem_ip_address *addr)
struct totem_ip_address my_id
size_t crypto_get_current_sec_header_size(const struct crypto_instance *instance)
struct totemudpu_instance * instance
#define NETIF_STATE_REPORT_DOWN
int totemip_compare(const void *a, const void *b)
#define log_printf(level, format, args...)
unsigned char private_key[TOTEM_PRIVATE_KEY_LEN]
int totemudpu_processor_count_set(void *udpu_context, int processor_count)
unsigned char addr[TOTEMIP_ADDRLEN]
int totemudpu_log_level_security
struct crypto_instance * crypto_init(const unsigned char *private_key, unsigned int private_key_len, const char *crypto_cipher_type, const char *crypto_hash_type, void(*log_printf_func)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf, 6, 7))), int log_level_security, int log_level_notice, int log_level_error, int log_subsys_id)
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
unsigned int downcheck_timeout
unsigned int private_key_len
char iov_buffer[FRAME_SIZE_MAX]
qb_loop_timer_handle timer_merge_detect_timeout
int send_merge_detect_message
#define totemip_nosigpipe(s)
int totemudpu_log_level_warning
int totemudpu_log_level_debug
const char * totemudpu_iface_print(void *udpu_context)
struct iovec totemudpu_iov_recv
#define BIND_STATE_REGULAR
int totemip_iface_check(struct totem_ip_address *bindnet, struct totem_ip_address *boundto, int *interface_up, int *interface_num, int mask_high_bit)
int crypto_encrypt_and_sign(struct crypto_instance *instance, const unsigned char *buf_in, const size_t buf_in_len, unsigned char *buf_out, size_t *buf_out_len)
void totemudpu_buffer_release(void *ptr)
void * totemudpu_buffer_alloc(void)
unsigned int merge_detect_messages_sent_before_timeout
qb_loop_t * totemudpu_poll_handle
int totemudpu_mcast_noflush_send(void *udpu_context, const void *msg, unsigned int msg_len)
void(* totemudpu_deliver_fn)(void *context, const void *msg, unsigned int msg_len)
#define BIND_RETRIES_INTERVAL
struct totem_ip_address token_target
int totemudpu_crypto_set(void *udpu_context, const char *cipher_type, const char *hash_type)
#define LOGSYS_LEVEL_DEBUG
struct totem_interface * totem_interface
int totemudpu_token_send(void *udpu_context, const void *msg, unsigned int msg_len)
struct totem_ip_address boundto
size_t totemip_udpip_header_size(int family)
struct list_head member_list
void(* log_printf)(int level, int subsys, const char *function_name, const char *file_name, int file_line, const char *format,...) __attribute__((format(printf
qb_loop_timer_handle timer_netif_check_timeout
void(* totemudpu_target_set_completed)(void *context)
#define BIND_STATE_LOOPBACK
#define MCAST_SOCKET_BUFFER_SIZE
#define PROCESSOR_COUNT_MAX
int crypto_authenticate_and_decrypt(struct crypto_instance *instance, unsigned char *buf, int *buf_len)
int totemudpu_member_remove(void *udpu_context, const struct totem_ip_address *token_target)
int totemudpu_member_set_active(void *udpu_context, const struct totem_ip_address *member_ip, int active)
#define LOGSYS_LEVEL_CRIT
#define list_entry(ptr, type, member)
int totemip_totemip_to_sockaddr_convert(struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen)
struct totem_logging_configuration totem_logging_configuration
#define LOGSYS_LEVEL_NOTICE
int totemudpu_recv_mcast_empty(void *udpu_context)
int totemudpu_initialize(qb_loop_t *poll_handle, void **udpu_context, struct totem_config *totem_config, totemsrp_stats_t *stats, int interface_no, void *context, void(*deliver_fn)(void *context, const void *msg, unsigned int msg_len), void(*iface_change_fn)(void *context, const struct totem_ip_address *iface_address), void(*target_set_completed)(void *context))
Create an instance.
struct srp_addr system_from
char * crypto_cipher_type
int totemudpu_log_level_error
unsigned int merge_timeout
void totemudpu_net_mtu_adjust(void *udpu_context, struct totem_config *totem_config)
struct totem_ip_address bindnet
int totemudpu_mcast_flush_send(void *udpu_context, const void *msg, unsigned int msg_len)
int totemudpu_send_flush(void *udpu_context)
int totemudpu_finalize(void *udpu_context)
struct crypto_instance * crypto_inst
int totemudpu_recv_flush(void *udpu_context)
int totemudpu_member_add(void *udpu_context, const struct totem_ip_address *member)
#define LOGSYS_PERROR(err_num, level, fmt, args...)
int totemudpu_iface_get(void *udpu_context, struct totem_ip_address *addr)
int totemudpu_member_list_rebind_ip(void *udpu_context)
void(* totemudpu_log_printf)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf
int totemudpu_iface_check(void *udpu_context)
int totemudpu_log_level_notice