1 /*
2  * Copyright (c) 2015-2017 Red Hat, Inc.
3  *
4  * All rights reserved.
5  *
6  * Author: Jan Friesse (jfriesse@redhat.com)
7  *
8  * This software licensed under BSD license, the text of which follows:
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * - Redistributions of source code must retain the above copyright notice,
14  *   this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright notice,
16  *   this list of conditions and the following disclaimer in the documentation
17  *   and/or other materials provided with the distribution.
18  * - Neither the name of the Red Hat, Inc. nor the names of its
19  *   contributors may be used to endorse or promote products derived from this
20  *   software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _QDEVICE_NET_INSTANCE_H_
36 #define _QDEVICE_NET_INSTANCE_H_
37 
38 #include <sys/types.h>
39 
40 #include <stdlib.h>
41 #include <stdint.h>
42 
43 #include "nss-sock.h"
44 
45 #include "qdevice-instance.h"
46 
47 #include "dynar.h"
48 #include "node-list.h"
49 #include "pr-poll-array.h"
50 #include "qdevice-net-disconnect-reason.h"
51 #include "send-buffer-list.h"
52 #include "tlv.h"
53 #include "timer-list.h"
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 enum qdevice_net_instance_state {
60 	QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT,
61 	QDEVICE_NET_INSTANCE_STATE_SENDING_PREINIT_REPLY,
62 	QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY,
63 	QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT,
64 	QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY,
65 	QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS,
66 };
67 
68 struct qdevice_net_instance {
69 	PRFileDesc *socket;
70 	struct dynar receive_buffer;
71 	struct send_buffer_list send_buffer_list;
72 	int skipping_msg;
73 	size_t msg_already_received_bytes;
74 	enum qdevice_net_instance_state state;
75 	uint32_t last_msg_seq_num;
76 	uint32_t echo_request_expected_msg_seq_num;
77 	uint32_t echo_reply_received_msg_seq_num;
78 	enum tlv_tls_supported tls_supported;
79 	int using_tls;
80 	int tls_client_cert_sent;
81 	uint32_t heartbeat_interval;		/* Adjusted heartbeat interval during normal operation */
82 	uint32_t sync_heartbeat_interval;	/* Adjusted heartbeat interval during corosync sync */
83 	uint32_t cast_vote_timer_interval;	/* Timer for cast vote */
84 	uint32_t connect_timeout;
85 	struct timer_list_entry *cast_vote_timer;
86 	enum tlv_vote cast_vote_timer_vote;
87 	int cast_vote_timer_paused;
88 	const char *host_addr;
89 	uint16_t host_port;
90 	const char *cluster_name;
91 	enum tlv_decision_algorithm_type decision_algorithm;
92 	struct timer_list main_timer_list;
93 	struct timer_list_entry *echo_request_timer;
94 	int schedule_disconnect;
95 	PRFileDesc *votequorum_poll_fd;
96 	PRFileDesc *cmap_poll_fd;
97 	PRFileDesc *ipc_socket_poll_fd;
98 	struct tlv_ring_id last_sent_ring_id;
99 	struct tlv_tie_breaker tie_breaker;
100 	void *algorithm_data;
101 	enum qdevice_net_disconnect_reason disconnect_reason;
102 	struct qdevice_instance *qdevice_instance_ptr;
103 	struct nss_sock_non_blocking_client non_blocking_client;
104 	struct timer_list_entry *connect_timer;
105 	int force_ip_version;
106 	struct pr_poll_array poll_array;
107 	time_t last_echo_reply_received_time;
108 	time_t connected_since_time;
109 	const struct qdevice_advanced_settings *advanced_settings;
110 	PRFileDesc *heuristics_pipe_cmd_send_poll_fd;
111 	PRFileDesc *heuristics_pipe_cmd_recv_poll_fd;
112 	PRFileDesc *heuristics_pipe_log_recv_poll_fd;
113 	struct timer_list_entry *regular_heuristics_timer;
114 	int server_supports_heuristics;
115 	enum tlv_heuristics latest_regular_heuristics_result;
116 	enum tlv_heuristics latest_connect_heuristics_result;
117 	enum tlv_heuristics latest_vq_heuristics_result;
118 	enum tlv_heuristics latest_heuristics_result;
119 };
120 
121 extern int		qdevice_net_instance_init(struct qdevice_net_instance *instance,
122     enum tlv_tls_supported tls_supported,
123     enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval,
124     uint32_t sync_heartbeat_interval, uint32_t cast_vote_timer_interval,
125     const char *host_addr, uint16_t host_port, const char *cluster_name,
126     const struct tlv_tie_breaker *tie_breaker, uint32_t connect_timeout, int force_ip_version,
127     int cmap_fd, int votequorum_fd, int local_socket_fd,
128     const struct qdevice_advanced_settings *advanced_settings,
129     int heuristics_pipe_cmd_send_fd, int heuristics_pipe_cmd_recv_fd,
130     int heuristics_pipe_log_recv_fd);
131 
132 extern void		qdevice_net_instance_clean(struct qdevice_net_instance *instance);
133 
134 extern int		qdevice_net_instance_destroy(struct qdevice_net_instance *instance);
135 
136 extern int		qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance);
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #endif /* _QDEVICE_NET_INSTANCE_H_ */
143