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;
468 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
469 msg_recv.msg_iov = iovec;
470 msg_recv.msg_iovlen = 1;
471 #ifdef HAVE_MSGHDR_CONTROL 472 msg_recv.msg_control = 0;
474 #ifdef HAVE_MSGHDR_CONTROLLEN 475 msg_recv.msg_controllen = 0;
477 #ifdef HAVE_MSGHDR_FLAGS 478 msg_recv.msg_flags = 0;
480 #ifdef HAVE_MSGHDR_ACCRIGHTS 481 msg_recv.msg_accrights = NULL;
483 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 484 msg_recv.msg_accrightslen = 0;
487 bytes_received = recvmsg (fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
488 if (bytes_received == -1) {
502 "Invalid packet data");
506 iovec->iov_len = bytes_received;
520 static int netif_determine (
530 interface_up, interface_num,
542 static void timer_function_netif_check_timeout (
553 netif_determine (instance,
556 &interface_up, &interface_num);
562 interface_up == 0) ||
566 interface_up == 1)) {
572 timer_function_netif_check_timeout,
587 if (interface_up == 0) {
592 bind_address = &localhost;
601 timer_function_netif_check_timeout,
613 totemudpu_build_sockets (instance,
620 POLLIN, instance, net_deliver_fn);
630 "The network interface [%s] is now up.",
643 timer_function_netif_check_timeout,
650 "The network interface is down.");
660 static void totemudpu_traffic_control_set(
struct totemudpu_instance *instance,
int sock)
665 if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio,
sizeof(
int))) {
667 "Could not set traffic priority");
672 static int totemudpu_build_sockets_ip (
678 struct sockaddr_storage sockaddr;
681 unsigned int recvbuf_size;
682 unsigned int optlen =
sizeof (recvbuf_size);
695 res = fcntl (instance->
token_socket, F_SETFL, O_NONBLOCK);
698 "Could not set non-blocking operation on token socket");
707 res = bind (instance->
token_socket, (
struct sockaddr *)&sockaddr, addrlen);
710 "bind token socket failed");
719 res = setsockopt (instance->
token_socket, SOL_SOCKET, SO_RCVBUF,
720 &recvbuf_size, optlen);
723 "Could not set recvbuf size");
729 static int totemudpu_build_sockets (
741 res = netif_determine (instance,
753 res = totemudpu_build_sockets_ip (instance,
754 bindnet_address, bound_to, interface_num);
757 totemudpu_traffic_control_set(instance, instance->
token_socket);
776 qb_loop_t *poll_handle,
786 unsigned int msg_len),
788 void (*iface_change_fn) (
792 void (*target_set_completed) (
798 if (instance == NULL) {
802 totemudpu_instance_initialize (instance);
860 100*QB_TIME_NS_IN_MSEC,
862 timer_function_netif_check_timeout,
865 totemudpu_start_merge_detect_timeout(instance);
867 *udpu_context = instance;
891 if (processor_count == 1) {
896 timer_function_netif_check_timeout,
920 unsigned int msg_len)
925 ucast_sendmsg (instance, &instance->
token_target, msg, msg_len);
932 unsigned int msg_len)
937 mcast_sendmsg (instance, msg, msg_len, 0);
945 unsigned int msg_len)
950 mcast_sendmsg (instance, msg, msg_len, 1);
960 timer_function_netif_check_timeout (instance);
977 const char *ret_char;
1016 struct sockaddr_storage system_from;
1017 struct msghdr msg_recv;
1020 int msg_processed = 0;
1026 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
1028 msg_recv.msg_iovlen = 1;
1029 #ifdef HAVE_MSGHDR_CONTROL 1030 msg_recv.msg_control = 0;
1032 #ifdef HAVE_MSGHDR_CONTROLLEN 1033 msg_recv.msg_controllen = 0;
1035 #ifdef HAVE_MSGHDR_FLAGS 1036 msg_recv.msg_flags = 0;
1038 #ifdef HAVE_MSGHDR_ACCRIGHTS 1039 msg_recv.msg_accrights = NULL;
1041 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN 1042 msg_recv.msg_accrightslen = 0;
1047 ufd.events = POLLIN;
1048 nfds = poll (&ufd, 1, 0);
1049 if (nfds == 1 && ufd.revents & POLLIN) {
1057 }
while (nfds == 1);
1059 return (msg_processed);
1062 static int totemudpu_create_sending_socket(
1069 unsigned int sendbuf_size;
1070 unsigned int optlen =
sizeof (sendbuf_size);
1071 struct sockaddr_storage sockaddr;
1074 fd = socket (member->
family, SOCK_DGRAM, 0);
1077 "Could not create socket for new member");
1081 res = fcntl (fd, F_SETFL, O_NONBLOCK);
1084 "Could not set non-blocking operation on token socket");
1085 goto error_close_fd;
1093 res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
1094 &sendbuf_size, optlen);
1097 "Could not set sendbuf size");
1107 res = bind (fd, (
struct sockaddr *)&sockaddr, addrlen);
1110 "bind token socket failed");
1111 goto error_close_fd;
1130 if (new_member == NULL) {
1134 memset(new_member, 0,
sizeof(*new_member));
1138 list_init (&new_member->
list);
1141 new_member->
fd = totemudpu_create_sending_socket(udpu_context, member);
1162 list = list->
next) {
1170 "removing UDPU member {%s}",
1173 if (member->
fd > 0) {
1175 "Closing socket to: {%s}",
1207 list = list->
next) {
1213 if (member->
fd > 0) {
1217 member->
fd = totemudpu_create_sending_socket(udpu_context, &member->
member);
1242 "Marking UDPU member %s %s",
1244 (active ?
"active" :
"inactive"));
1255 "Can't find UDPU member %s (should be marked as %s)",
1257 (active ?
"active" :
"inactive"));
1263 static void timer_function_merge_detect_timeout (
1274 totemudpu_start_merge_detect_timeout(instance);
1277 static void totemudpu_start_merge_detect_timeout(
1286 timer_function_merge_detect_timeout,
1291 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)
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)
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
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