1 /*****************************************************************************\
2  *  slurm_persist_conn.h - Definitions for communicating over a persistent
3  *                         connection within Slurm.
4  ******************************************************************************
5  *  Copyright (C) 2016 SchedMD LLC
6  *  Written by Danny Auble da@schedmd.com, et. al.
7  *
8  *  This file is part of Slurm, a resource management program.
9  *  For details, see <https://slurm.schedmd.com/>.
10  *  Please also read the included file: DISCLAIMER.
11  *
12  *  Slurm is free software; you can redistribute it and/or modify it under
13  *  the terms of the GNU General Public License as published by the Free
14  *  Software Foundation; either version 2 of the License, or (at your option)
15  *  any later version.
16  *
17  *  In addition, as a special exception, the copyright holders give permission
18  *  to link the code of portions of this program with the OpenSSL library under
19  *  certain conditions as described in each individual source file, and
20  *  distribute linked combinations including the two. You must obey the GNU
21  *  General Public License in all respects for all of the code used other than
22  *  OpenSSL. If you modify file(s) with this exception, you may extend this
23  *  exception to your version of the file(s), but you are not obligated to do
24  *  so. If you do not wish to do so, delete this exception statement from your
25  *  version.  If you delete this exception statement from all source files in
26  *  the program, then also delete it here.
27  *
28  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
29  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
30  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
31  *  details.
32  *
33  *  You should have received a copy of the GNU General Public License along
34  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
35  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
36 \*****************************************************************************/
37 #ifndef _SLURM_PERSIST_CONN_H
38 #define _SLURM_PERSIST_CONN_H
39 
40 #include "slurm/slurm.h"
41 
42 #define PERSIST_FLAG_NONE           0x0000
43 #define PERSIST_FLAG_DBD            0x0001
44 #define PERSIST_FLAG_RECONNECT      0x0002
45 #define PERSIST_FLAG_ALREADY_INITED 0x0004
46 #define PERSIST_FLAG_P_USER_CASE    0x0008
47 #define PERSIST_FLAG_SUPPRESS_ERR   0x0010
48 
49 typedef enum {
50 	PERSIST_TYPE_NONE = 0,
51 	PERSIST_TYPE_DBD,
52 	PERSIST_TYPE_FED,
53 	PERSIST_TYPE_HA_CTL,
54 	PERSIST_TYPE_HA_DBD,
55 } persist_conn_type_t;
56 
57 typedef struct {
58 	void *conn;		/* slurm_persist_conn_t */
59 	void *data;		/* pointer to a message type below */
60 	uint32_t data_size;     /* size of data */
61 	uint16_t msg_type;	/* slurmdbd_msg_type_t / slurm_msg_type_t */
62 } persist_msg_t;
63 
64 typedef struct {
65 	void *auth_cred;
66 	int (*callback_proc)(void *arg,
67 			     persist_msg_t *msg,
68 			     Buf *out_buffer, uint32_t *uid);
69 	void (*callback_fini)(void *arg);
70 	char *cluster_name;
71 	time_t comm_fail_time;	/* avoid constant error messages */
72 	uint16_t my_port;
73 	int fd;
74 	uint16_t flags;
75 	bool inited;
76 	persist_conn_type_t persist_type;
77 	char *rem_host;
78 	uint16_t rem_port;
79 	time_t *shutdown;
80 	pthread_t thread_id;
81 	int timeout;
82 	slurm_trigger_callbacks_t trigger_callbacks;
83 	uint16_t version;
84 } slurm_persist_conn_t;
85 
86 typedef struct {
87 	char *cluster_name;     /* cluster this message is coming from */
88 	uint16_t persist_type;	/* really persist_conn_type_t, uint16_t on wire */
89 	uint16_t port;          /* If you want to open a new connection, this is
90 				 *  the port to talk to. */
91 	uint16_t version;	/* protocol version */
92 	uint32_t uid;		/* UID originating connection,
93 				 * filled by authtentication plugin*/
94 } persist_init_req_msg_t;
95 
96 typedef struct {
97 	char *comment;
98 	uint16_t flags;
99 	uint32_t rc;
100 	uint16_t ret_info; /* protocol version we are connecting to since we
101 			    * sent the lowest one to begin with, or the return
102 			    * of a message type sent. */
103 } persist_rc_msg_t;
104 
105 /* setup a daemon to receive incoming persistent connections. */
106 extern void slurm_persist_conn_recv_server_init(void);
107 
108 /* finish up any persistent connections we are listening to */
109 extern void slurm_persist_conn_recv_server_fini(void);
110 
111 /* Create a thread that will wait listening on the fd in the
112  * slurm_persist_conn_t.
113  * IN - persist_conn - persistent connection to listen to.  This will be freed
114  *                     internally, so forget about once it enters here.
115  * IN - thread_loc - location in the persist_conn thread pool.  This number can
116  *                   be got from slurm_persist_conn_wait_for_thread_loc or given
117  *                   -1 to get one inside the function.
118  * IN - arg - arbitrary argument that will be sent to the callback as well as
119  *            the callback in the persist_conn.
120  */
121 extern void slurm_persist_conn_recv_thread_init(slurm_persist_conn_t *persist_conn,
122 						int thread_loc, void *arg);
123 
124 /* Increment thread_count and don't return until its value is no larger
125  *	than MAX_THREAD_COUNT,
126  * RET index of free index in persist_pthread_id or -1 to exit */
127 extern int slurm_persist_conn_wait_for_thread_loc(void);
128 
129 /* Free the index given from slurm_persist_conn_wait_for_thread_loc */
130 extern void slurm_persist_conn_free_thread_loc(int thread_loc);
131 
132 
133 /* Open a persistent socket connection
134  * IN/OUT - persistent connection needing host and port filled in.  Returned
135  * mostly filled in without the version to use to communicate.
136  * Returns SLURM_SUCCESS on success or SLURM_ERROR on failure */
137 extern int slurm_persist_conn_open_without_init(
138 	slurm_persist_conn_t *persist_conn);
139 
140 /* Open a persistent socket connection and sends an init message to establish
141  * the connection.
142  * IN/OUT - persistent connection needing host and port filled in.  Returned
143  * completely filled in.
144  * Returns SLURM_SUCCESS on success or SLURM_ERROR on failure */
145 extern int slurm_persist_conn_open(slurm_persist_conn_t *persist_conn);
146 
147 /* Close the persistent connection don't free structure or members */
148 extern void slurm_persist_conn_close(slurm_persist_conn_t *persist_conn);
149 
150 extern int slurm_persist_conn_reopen(slurm_persist_conn_t *persist_conn,
151 				     bool with_init);
152 
153 /* Close the persistent connection members, but don't free structure */
154 extern void slurm_persist_conn_members_destroy(
155 	slurm_persist_conn_t *persist_conn);
156 
157 /* Close the persistent connection and free structure */
158 extern void slurm_persist_conn_destroy(slurm_persist_conn_t *persist_conn);
159 
160 extern int slurm_persist_conn_process_msg(slurm_persist_conn_t *persist_conn,
161 					  persist_msg_t *persist_msg,
162 					  char *msg_char, uint32_t msg_size,
163 					  Buf *out_buffer, bool first);
164 
165 extern int slurm_persist_conn_writeable(slurm_persist_conn_t *persist_conn);
166 
167 extern int slurm_persist_send_msg(
168 	slurm_persist_conn_t *persist_conn, Buf buffer);
169 extern Buf slurm_persist_recv_msg(slurm_persist_conn_t *persist_conn);
170 
171 
172 extern Buf slurm_persist_msg_pack(slurm_persist_conn_t *persist_conn,
173 				  persist_msg_t *req_msg);
174 extern int slurm_persist_msg_unpack(slurm_persist_conn_t *persist_conn,
175 				    persist_msg_t *resp_msg, Buf buffer);
176 
177 extern void slurm_persist_pack_init_req_msg(
178 	persist_init_req_msg_t *msg, Buf buffer);
179 extern int slurm_persist_unpack_init_req_msg(
180 	persist_init_req_msg_t **msg, Buf buffer);
181 extern void slurm_persist_free_init_req_msg(persist_init_req_msg_t *msg);
182 
183 extern void slurm_persist_pack_rc_msg(
184 	persist_rc_msg_t *msg, Buf buffer, uint16_t protocol_version);
185 extern int slurm_persist_unpack_rc_msg(
186 	persist_rc_msg_t **msg, Buf buffer, uint16_t protocol_version);
187 extern void slurm_persist_free_rc_msg(persist_rc_msg_t *msg);
188 
189 extern Buf slurm_persist_make_rc_msg(slurm_persist_conn_t *persist_conn,
190 				     uint32_t rc, char *comment,
191 				     uint16_t ret_info);
192 
193 extern Buf slurm_persist_make_rc_msg_flags(slurm_persist_conn_t *persist_conn,
194 					   uint32_t rc, char *comment,
195 					   uint16_t flags,
196 					   uint16_t ret_info);
197 
198 #endif
199