43#include <sys/socket.h>
48#include <netinet/in.h>
66#define LOGSYS_UTILS_ONLY 1
76#define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
77#define NETIF_STATE_REPORT_UP 1
78#define NETIF_STATE_REPORT_DOWN 2
80#define BIND_STATE_UNBOUND 0
81#define BIND_STATE_REGULAR 1
82#define BIND_STATE_LOOPBACK 2
116 unsigned int msg_len,
122 unsigned int ring_no);
144 const char *function,
201static int totemudp_build_sockets (
231#define log_printf(level, format, args...) \
233 instance->totemudp_log_printf ( \
234 level, instance->totemudp_subsys_id, \
235 __FUNCTION__, __FILE__, __LINE__, \
236 (const char *)format, ##args); \
239#define LOGSYS_PERROR(err_num, level, fmt, args...) \
241 char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
242 const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
243 instance->totemudp_log_printf ( \
244 level, instance->totemudp_subsys_id, \
245 __FUNCTION__, __FILE__, __LINE__, \
246 fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
251 const char *cipher_type,
252 const char *hash_type)
259static inline void ucast_sendmsg (
263 unsigned int msg_len)
265 struct msghdr msg_ucast;
267 struct sockaddr_storage sockaddr;
271 iovec.iov_base = (
void*)msg;
272 iovec.iov_len = msg_len;
277 memset(&msg_ucast, 0,
sizeof(msg_ucast));
280 msg_ucast.msg_name = &sockaddr;
281 msg_ucast.msg_namelen = addrlen;
282 msg_ucast.msg_iov = (
void *)&iovec;
283 msg_ucast.msg_iovlen = 1;
293 "sendmsg(ucast) failed (non-critical)");
297static inline void mcast_sendmsg (
300 unsigned int msg_len)
302 struct msghdr msg_mcast;
305 struct sockaddr_storage sockaddr;
308 iovec.iov_base = (
void *)msg;
309 iovec.iov_len = msg_len;
316 memset(&msg_mcast, 0,
sizeof(msg_mcast));
317 msg_mcast.msg_name = &sockaddr;
318 msg_mcast.msg_namelen = addrlen;
319 msg_mcast.msg_iov = (
void *)&iovec;
320 msg_mcast.msg_iovlen = 1;
330 "sendmsg(mcast) failed (non-critical)");
340 msg_mcast.msg_name = NULL;
341 msg_mcast.msg_namelen = 0;
347 "sendmsg(local mcast loop) failed (non-critical)");
385static int net_deliver_fn (
391 struct msghdr msg_recv;
405 memset(&msg_recv, 0,
sizeof(msg_recv));
407 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
408 msg_recv.msg_iov = iovec;
409 msg_recv.msg_iovlen = 1;
411 bytes_received = recvmsg (fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
412 if (bytes_received == -1) {
425 "Received too big message. This may be because something bad is happening "
426 "on the network (attack?), or you tried join more nodes than corosync is "
427 "compiled with (%u) or bug in the code (bad estimation of "
432 iovec->iov_len = bytes_received;
447static int netif_determine (
457 interface_up, interface_num,
469static void timer_function_netif_check_timeout (
480 netif_determine (instance,
483 &interface_up, &interface_num);
489 interface_up == 0) ||
493 interface_up == 1)) {
499 timer_function_netif_check_timeout,
500 &instance->timer_netif_check_timeout);
528 if (interface_up == 0) {
533 bind_address = &localhost;
542 timer_function_netif_check_timeout,
543 &instance->timer_netif_check_timeout);
554 (void)totemudp_build_sockets (instance,
564 POLLIN, instance, net_deliver_fn);
570 POLLIN, instance, net_deliver_fn);
576 POLLIN, instance, net_deliver_fn);
586 "The network interface [%s] is now up.",
599 timer_function_netif_check_timeout,
600 &instance->timer_netif_check_timeout);
606 "The network interface is down.");
616static void totemudp_traffic_control_set(
struct totemudp_instance *instance,
int sock)
621 if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio,
sizeof(
int))) {
627static int totemudp_build_sockets_ip (
635 struct sockaddr_storage sockaddr;
636 struct ipv6_mreq mreq6;
638 struct sockaddr_storage mcast_ss, boundto_ss;
639 struct sockaddr_in6 *mcast_sin6 = (
struct sockaddr_in6 *)&mcast_ss;
640 struct sockaddr_in *mcast_sin = (
struct sockaddr_in *)&mcast_ss;
641 struct sockaddr_in *boundto_sin = (
struct sockaddr_in *)&boundto_ss;
642 unsigned int sendbuf_size;
643 unsigned int recvbuf_size;
644 unsigned int optlen =
sizeof (sendbuf_size);
645 unsigned int retries;
663 res = fcntl (sockets->
mcast_recv, F_SETFL, O_NONBLOCK);
666 "Could not set non-blocking operation on multicast socket");
674 if ( setsockopt(sockets->
mcast_recv, SOL_SOCKET, SO_REUSEADDR, (
char *)&flag, sizeof (flag)) < 0) {
676 "setsockopt(SO_REUSEADDR) failed");
689 for (i = 0; i < 2; i++) {
694 "Could not set non-blocking operation on multicast socket");
712 res = fcntl (sockets->
mcast_send, F_SETFL, O_NONBLOCK);
715 "Could not set non-blocking operation on multicast socket");
723 if ( setsockopt(sockets->
mcast_send, SOL_SOCKET, SO_REUSEADDR, (
char *)&flag, sizeof (flag)) < 0) {
725 "setsockopt(SO_REUSEADDR) failed");
733 "Could not set IP_TOS bits");
738 &sockaddr, &addrlen);
742 res = bind (sockets->
mcast_send, (
struct sockaddr *)&sockaddr, addrlen);
747 "Unable to bind the socket to send multicast packets");
764 sockets->
token = socket (bindnet_address->
family, SOCK_DGRAM, 0);
765 if (sockets->
token == -1) {
772 res = fcntl (sockets->
token, F_SETFL, O_NONBLOCK);
775 "Could not set non-blocking operation on token socket");
783 if ( setsockopt(sockets->
token, SOL_SOCKET, SO_REUSEADDR, (
char *)&flag, sizeof (flag)) < 0) {
785 "setsockopt(SO_REUSEADDR) failed");
792 "Could not set IP_TOS bits");
804 res = bind (sockets->
token, (
struct sockaddr *)&sockaddr, addrlen);
809 "Unable to bind UDP unicast socket");
828 res = setsockopt (sockets->
mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
831 "Unable to set SO_RCVBUF size on UDP mcast socket");
834 res = setsockopt (sockets->
mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
837 "Unable to set SO_SNDBUF size on UDP mcast socket");
840 res = setsockopt (sockets->
local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
843 "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
846 res = setsockopt (sockets->
local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
849 "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
853 res = getsockopt (sockets->
mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
856 "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
859 res = getsockopt (sockets->
mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
862 "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
865 res = getsockopt (sockets->
local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
868 "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
871 res = getsockopt (sockets->
local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
874 "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
885 unsigned int broadcast = 1;
887 if ((setsockopt(sockets->
mcast_recv, SOL_SOCKET,
888 SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
890 "setting broadcast option failed");
893 if ((setsockopt(sockets->
mcast_send, SOL_SOCKET,
894 SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
896 "setting broadcast option failed");
900 switch (bindnet_address->
family) {
902 memset(&mreq, 0,
sizeof(mreq));
903 mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
904 mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
905 res = setsockopt (sockets->
mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
906 &mreq, sizeof (mreq));
909 "join ipv4 multicast group failed");
914 memset(&mreq6, 0,
sizeof(mreq6));
915 memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr,
sizeof(
struct in6_addr));
916 mreq6.ipv6mr_interface = interface_num;
918 res = setsockopt (sockets->
mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
919 &mreq6, sizeof (mreq6));
922 "join ipv6 multicast group failed");
934 switch ( bindnet_address->
family ) {
937 res = setsockopt (sockets->
mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
938 &sflag, sizeof (sflag));
941 res = setsockopt (sockets->
mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
942 &flag, sizeof (flag));
946 "Unable to turn off multicast loopback");
954 if (bindnet_address->
family == AF_INET6) {
955 res = setsockopt (sockets->
mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
956 &flag, sizeof (flag));
959 "set mcast v6 TTL failed");
964 res = setsockopt(sockets->
mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
965 &sflag,
sizeof(sflag));
968 "set mcast v4 TTL failed");
976 switch ( bindnet_address->
family ) {
978 if (setsockopt (sockets->
mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
979 &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
981 "cannot select interface for multicast packets (send)");
984 if (setsockopt (sockets->
mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
985 &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
987 "cannot select interface for multicast packets (recv)");
992 if (setsockopt (sockets->
mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
993 &interface_num, sizeof (interface_num)) < 0) {
995 "cannot select interface for multicast packets (send v6)");
998 if (setsockopt (sockets->
mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
999 &interface_num, sizeof (interface_num)) < 0) {
1001 "cannot select interface for multicast packets (recv v6)");
1018 res = bind (sockets->
mcast_recv, (
struct sockaddr *)&sockaddr, addrlen);
1023 "Unable to bind the socket to receive multicast packets");
1040static int totemudp_build_sockets (
1054 res = netif_determine (instance,
1066 res = totemudp_build_sockets_ip (instance, mcast_address,
1067 bindnet_address, sockets, bound_to, interface_num);
1072 "Unable to create sockets, exiting");
1077 totemudp_traffic_control_set(instance, sockets->
token);
1090 qb_loop_t *poll_handle,
1100 unsigned int msg_len,
1103 int (*iface_change_fn) (
1106 unsigned int ring_no),
1108 void (*mtu_changed) (
1112 void (*target_set_completed) (
1118 if (instance == NULL) {
1122 totemudp_instance_initialize (instance);
1166 100*QB_TIME_NS_IN_MSEC,
1168 timer_function_netif_check_timeout,
1187 int processor_count)
1195 if (processor_count == 1) {
1200 timer_function_netif_check_timeout,
1218 for (i = 0; i < 2; i++) {
1230 ufd.events = POLLIN;
1231 nfds = poll (&ufd, 1, 0);
1232 if (nfds == 1 && ufd.revents & POLLIN) {
1233 net_deliver_fn (sock, ufd.revents, instance);
1235 }
while (nfds == 1);
1251 unsigned int msg_len)
1256 ucast_sendmsg (instance, &instance->
token_target, msg, msg_len);
1263 unsigned int msg_len)
1268 mcast_sendmsg (instance, msg, msg_len);
1276 unsigned int msg_len)
1281 mcast_sendmsg (instance, msg, msg_len);
1291 timer_function_netif_check_timeout (instance);
1300 struct qb_list_head *list;
1328 unsigned int *iface_count)
1350 struct qb_list_head *list;
1376 struct msghdr msg_recv;
1379 int msg_processed = 0;
1386 memset(&msg_recv, 0,
sizeof(msg_recv));
1388 msg_recv.msg_namelen =
sizeof (
struct sockaddr_storage);
1390 msg_recv.msg_iovlen = 1;
1392 for (i = 0; i < 2; i++) {
1404 ufd.events = POLLIN;
1405 nfds = poll (&ufd, 1, 0);
1406 if (nfds == 1 && ufd.revents & POLLIN) {
1407 res = recvmsg (sock, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
1414 }
while (nfds == 1);
1417 return (msg_processed);
1432 if (new_member == NULL) {
1436 memset(new_member, 0,
sizeof(*new_member));
1438 qb_list_init (&new_member->
list);
1451 struct qb_list_head *list;
1458 qb_list_for_each(list, &(instance->
member_list)) {
1459 member = qb_list_entry (list,
1481 unsigned short ip_port,
1482 unsigned int iface_no)
#define PROCESSOR_COUNT_MAX
unsigned int broadcast_use
struct totem_logging_configuration totem_logging_configuration
unsigned int downcheck_timeout
struct totem_interface * interfaces
unsigned int clear_node_high_bit
struct totem_ip_address boundto
struct totem_ip_address bindnet
struct totem_ip_address mcast_addr
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 knet_link_status link_status[KNET_MAX_LINK]
uint32_t continuous_sendmsg_failures
qb_loop_timer_handle timer_netif_check_timeout
struct totem_ip_address mcast_address
struct qb_list_head member_list
qb_loop_t * totemudp_poll_handle
int totemudp_log_level_security
struct totem_ip_address my_id
struct totem_interface * totem_interface
struct iovec totemudp_iov_recv_flush
int totemudp_log_level_notice
void(* totemudp_log_printf)(int level, int subsys, const char *function, const char *file, int line, const char *format,...) __attribute__((format(printf
unsigned int my_memb_entries
int totemudp_log_level_debug
struct totem_config * totem_config
int totemudp_log_level_warning
struct totem_ip_address token_target
void(* totemudp_target_set_completed)(void *context)
char iov_buffer[UDP_RECEIVE_FRAME_SIZE_MAX+1]
int(* totemudp_iface_change_fn)(void *context, const struct totem_ip_address *iface_address, unsigned int ring_no)
struct iovec totemudp_iov_recv
struct totemudp_socket totemudp_sockets
struct timeval stats_tv_start
int(* totemudp_deliver_fn)(void *context, const void *msg, unsigned int msg_len, const struct sockaddr_storage *system_from)
char iov_buffer_flush[UDP_RECEIVE_FRAME_SIZE_MAX+1]
int totemudp_log_level_error
struct totem_ip_address member
struct totemknet_instance * instance
#define BIND_RETRIES_INTERVAL
#define UDP_RECEIVE_FRAME_SIZE_MAX
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_print(const struct totem_ip_address *addr)
int totemip_localhost(int family, struct totem_ip_address *localhost)
struct srp_addr system_from
int totemudp_iface_set(void *net_context, const struct totem_ip_address *local_addr, unsigned short ip_port, unsigned int iface_no)
#define NETIF_STATE_REPORT_UP
void totemudp_net_mtu_adjust(void *udp_context, struct totem_config *totem_config)
#define log_printf(level, format, args...)
void * totemudp_buffer_alloc(void)
int totemudp_send_flush(void *udp_context)
int totemudp_iface_check(void *udp_context)
int totemudp_recv_flush(void *udp_context)
#define BIND_STATE_LOOPBACK
#define NETIF_STATE_REPORT_DOWN
int totemudp_nodestatus_get(void *udp_context, unsigned int nodeid, struct totem_node_status *node_status)
int totemudp_token_target_set(void *udp_context, unsigned int nodeid)
int totemudp_ifaces_get(void *net_context, char ***status, unsigned int *iface_count)
int totemudp_reconfigure(void *udp_context, struct totem_config *totem_config)
#define MCAST_SOCKET_BUFFER_SIZE
int totemudp_mcast_flush_send(void *udp_context, const void *msg, unsigned int msg_len)
int totemudp_finalize(void *udp_context)
int totemudp_recv_mcast_empty(void *udp_context)
int totemudp_crypto_set(void *udp_context, const char *cipher_type, const char *hash_type)
#define LOGSYS_PERROR(err_num, level, fmt, args...)
int totemudp_processor_count_set(void *udp_context, int processor_count)
int totemudp_token_send(void *udp_context, const void *msg, unsigned int msg_len)
#define BIND_STATE_REGULAR
int totemudp_mcast_noflush_send(void *udp_context, const void *msg, unsigned int msg_len)
int totemudp_member_add(void *udp_context, const struct totem_ip_address *local, const struct totem_ip_address *member, int ring_no)
void totemudp_buffer_release(void *ptr)
int totemudp_initialize(qb_loop_t *poll_handle, void **udp_context, struct totem_config *totem_config, totemsrp_stats_t *stats, void *context, int(*deliver_fn)(void *context, const void *msg, unsigned int msg_len, const struct sockaddr_storage *system_from), int(*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 totemudp_member_remove(void *udp_context, const struct totem_ip_address *token_target, int ring_no)
int set_socket_dscp(int socket, unsigned char dscp)