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
114 unsigned int msg_len);
140 const char *
function,
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;
279 (
const unsigned char *)msg,
282 &buf_out_len) != 0) {
287 iovec.iov_base = (
void *)buf_out;
288 iovec.iov_len = buf_out_len;
295 memset(&msg_ucast, 0,
sizeof(msg_ucast));
296 msg_ucast.msg_name = &sockaddr;
297 msg_ucast.msg_namelen = addrlen;
298 msg_ucast.msg_iov = (
void *)&iovec;
299 msg_ucast.msg_iovlen = 1;
300 #ifdef HAVE_MSGHDR_CONTROL
301 msg_ucast.msg_control = 0;
303 #ifdef HAVE_MSGHDR_CONTROLLEN
304 msg_ucast.msg_controllen = 0;
306 #ifdef HAVE_MSGHDR_FLAGS
307 msg_ucast.msg_flags = 0;
309 #ifdef HAVE_MSGHDR_ACCRIGHTS
310 msg_ucast.msg_accrights = NULL;
312 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
313 msg_ucast.msg_accrightslen = 0;
324 "sendmsg(ucast) failed (non-critical)");
328 static inline void mcast_sendmsg (
331 unsigned int msg_len,
334 struct msghdr msg_mcast;
339 struct sockaddr_storage sockaddr;
349 (
const unsigned char *)msg,
352 &buf_out_len) != 0) {
357 iovec.iov_base = (
void *)buf_out;
358 iovec.iov_len = buf_out_len;
360 memset(&msg_mcast, 0,
sizeof(msg_mcast));
381 msg_mcast.msg_name = &sockaddr;
382 msg_mcast.msg_namelen = addrlen;
383 msg_mcast.msg_iov = (
void *)&iovec;
384 msg_mcast.msg_iovlen = 1;
385 #ifdef HAVE_MSGHDR_CONTROL
386 msg_mcast.msg_control = 0;
388 #ifdef HAVE_MSGHDR_CONTROLLEN
389 msg_mcast.msg_controllen = 0;
391 #ifdef HAVE_MSGHDR_FLAGS
392 msg_mcast.msg_flags = 0;
394 #ifdef HAVE_MSGHDR_ACCRIGHTS
395 msg_mcast.msg_accrights = NULL;
397 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
398 msg_mcast.msg_accrightslen = 0;
408 "sendmsg(mcast) failed (non-critical)");
433 totemudpu_stop_merge_detect_timeout(instance);
438 static int net_deliver_fn (
444 struct msghdr msg_recv;
456 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
457 msg_recv.msg_iov = iovec;
458 msg_recv.msg_iovlen = 1;
459 #ifdef HAVE_MSGHDR_CONTROL
460 msg_recv.msg_control = 0;
462 #ifdef HAVE_MSGHDR_CONTROLLEN
463 msg_recv.msg_controllen = 0;
465 #ifdef HAVE_MSGHDR_FLAGS
466 msg_recv.msg_flags = 0;
468 #ifdef HAVE_MSGHDR_ACCRIGHTS
469 msg_recv.msg_accrights = NULL;
471 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
472 msg_recv.msg_accrightslen = 0;
475 bytes_received = recvmsg (fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
476 if (bytes_received == -1) {
490 "Invalid packet data");
494 iovec->iov_len = bytes_received;
508 static int netif_determine (
518 interface_up, interface_num,
530 static void timer_function_netif_check_timeout (
541 netif_determine (instance,
544 &interface_up, &interface_num);
550 interface_up == 0) ||
554 interface_up == 1)) {
560 timer_function_netif_check_timeout,
575 if (interface_up == 0) {
580 bind_address = &localhost;
589 timer_function_netif_check_timeout,
601 totemudpu_build_sockets (instance,
608 POLLIN, instance, net_deliver_fn);
618 "The network interface [%s] is now up.",
631 timer_function_netif_check_timeout,
638 "The network interface is down.");
648 static void totemudpu_traffic_control_set(
struct totemudpu_instance *instance,
int sock)
653 if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio,
sizeof(
int))) {
655 "Could not set traffic priority");
660 static int totemudpu_build_sockets_ip (
666 struct sockaddr_storage sockaddr;
669 unsigned int recvbuf_size;
670 unsigned int optlen =
sizeof (recvbuf_size);
683 res = fcntl (instance->
token_socket, F_SETFL, O_NONBLOCK);
686 "Could not set non-blocking operation on token socket");
695 res = bind (instance->
token_socket, (
struct sockaddr *)&sockaddr, addrlen);
698 "bind token socket failed");
707 res = setsockopt (instance->
token_socket, SOL_SOCKET, SO_RCVBUF,
708 &recvbuf_size, optlen);
711 "Could not set recvbuf size");
717 static int totemudpu_build_sockets (
729 res = netif_determine (instance,
741 res = totemudpu_build_sockets_ip (instance,
742 bindnet_address, bound_to, interface_num);
745 totemudpu_traffic_control_set(instance, instance->
token_socket);
764 qb_loop_t *poll_handle,
774 unsigned int msg_len),
776 void (*iface_change_fn) (
780 void (*target_set_completed) (
786 if (instance == NULL) {
790 totemudpu_instance_initialize (instance);
848 100*QB_TIME_NS_IN_MSEC,
850 timer_function_netif_check_timeout,
853 totemudpu_start_merge_detect_timeout(instance);
855 *udpu_context = instance;
879 if (processor_count == 1) {
884 timer_function_netif_check_timeout,
908 unsigned int msg_len)
913 ucast_sendmsg (instance, &instance->
token_target, msg, msg_len);
920 unsigned int msg_len)
925 mcast_sendmsg (instance, msg, msg_len, 0);
933 unsigned int msg_len)
938 mcast_sendmsg (instance, msg, msg_len, 1);
948 timer_function_netif_check_timeout (instance);
965 const char *ret_char;
1004 struct sockaddr_storage system_from;
1005 struct msghdr msg_recv;
1008 int msg_processed = 0;
1014 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
1016 msg_recv.msg_iovlen = 1;
1017 #ifdef HAVE_MSGHDR_CONTROL
1018 msg_recv.msg_control = 0;
1020 #ifdef HAVE_MSGHDR_CONTROLLEN
1021 msg_recv.msg_controllen = 0;
1023 #ifdef HAVE_MSGHDR_FLAGS
1024 msg_recv.msg_flags = 0;
1026 #ifdef HAVE_MSGHDR_ACCRIGHTS
1027 msg_recv.msg_accrights = NULL;
1029 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
1030 msg_recv.msg_accrightslen = 0;
1035 ufd.events = POLLIN;
1036 nfds = poll (&ufd, 1, 0);
1037 if (nfds == 1 && ufd.revents & POLLIN) {
1045 }
while (nfds == 1);
1047 return (msg_processed);
1050 static int totemudpu_create_sending_socket(
1057 unsigned int sendbuf_size;
1058 unsigned int optlen =
sizeof (sendbuf_size);
1059 struct sockaddr_storage sockaddr;
1062 fd = socket (member->
family, SOCK_DGRAM, 0);
1065 "Could not create socket for new member");
1069 res = fcntl (fd, F_SETFL, O_NONBLOCK);
1072 "Could not set non-blocking operation on token socket");
1073 goto error_close_fd;
1081 res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
1082 &sendbuf_size, optlen);
1085 "Could not set sendbuf size");
1095 res = bind (fd, (
struct sockaddr *)&sockaddr, addrlen);
1098 "bind token socket failed");
1099 goto error_close_fd;
1118 if (new_member == NULL) {
1122 memset(new_member, 0,
sizeof(*new_member));
1126 list_init (&new_member->
list);
1129 new_member->
fd = totemudpu_create_sending_socket(udpu_context, member);
1150 list = list->
next) {
1158 "removing UDPU member {%s}",
1161 if (member->
fd > 0) {
1163 "Closing socket to: {%s}",
1195 list = list->
next) {
1201 if (member->
fd > 0) {
1205 member->
fd = totemudpu_create_sending_socket(udpu_context, &member->
member);
1230 "Marking UDPU member %s %s",
1232 (active ?
"active" :
"inactive"));
1243 "Can't find UDPU member %s (should be marked as %s)",
1245 (active ?
"active" :
"inactive"));
1251 static void timer_function_merge_detect_timeout (
1262 totemudpu_start_merge_detect_timeout(instance);
1265 static void totemudpu_start_merge_detect_timeout(
1274 timer_function_merge_detect_timeout,
1279 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)
void(* totemudpu_iface_change_fn)(void *context, const struct totem_ip_address *iface_address)
unsigned int my_memb_entries
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.
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
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(*) void udpu_context)
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_log_printf)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf
struct totem_ip_address token_target
int totemudpu_crypto_set(void *udpu_context, const char *cipher_type, const char *hash_type)
void(* log_printf)(int level, int subsys, const char *function_name, const char *file_name, int file_line, const char *format,...) __attribute__((format(printf
#define LOGSYS_LEVEL_DEBUG
void(* totemudpu_target_set_completed)(void *context)
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
struct timeval stats_tv_start
qb_loop_timer_handle timer_netif_check_timeout
#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)
void(* totemudpu_deliver_fn)(void *context, const void *msg, unsigned int msg_len)
#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)
int totemudpu_iface_check(void *udpu_context)
int totemudpu_log_level_notice