1 /*
2  * include/types/server.h
3  * This file defines everything related to servers.
4  *
5  * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation, version 2.1
10  * exclusively.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef _TYPES_SERVER_H
23 #define _TYPES_SERVER_H
24 
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 
28 #ifdef USE_OPENSSL
29 #include <openssl/ssl.h>
30 #endif
31 
32 #include <common/config.h>
33 #include <common/mini-clist.h>
34 #include <eb32tree.h>
35 
36 #include <types/connection.h>
37 #include <types/counters.h>
38 #include <types/dns.h>
39 #include <types/freq_ctr.h>
40 #include <types/obj_type.h>
41 #include <types/proxy.h>
42 #include <types/queue.h>
43 #include <types/task.h>
44 #include <types/checks.h>
45 
46 
47 /* server states. Only SRV_ST_STOPPED indicates a down server. */
48 enum srv_state {
49 	SRV_ST_STOPPED = 0,              /* the server is down. Please keep set to zero. */
50 	SRV_ST_STARTING,                 /* the server is warming up (up but throttled) */
51 	SRV_ST_RUNNING,                  /* the server is fully up */
52 	SRV_ST_STOPPING,                 /* the server is up but soft-stopping (eg: 404) */
53 };
54 
55 /* Administrative status : a server runs in one of these 3 stats :
56  *   - READY : normal mode
57  *   - DRAIN : takes no new visitor, equivalent to weight == 0
58  *   - MAINT : maintenance mode, no more traffic nor health checks.
59  *
60  * Each server may be in maintenance by itself or may inherit this status from
61  * another server it tracks. It can also be in drain mode by itself or inherit
62  * it from another server. Let's store these origins here as flags. These flags
63  * are combined this way :
64  *
65  *      FMAINT  IMAINT  FDRAIN  IDRAIN  Resulting state
66  *         0       0       0       0    READY
67  *         0       0       0       1    DRAIN
68  *         0       0       1       x    DRAIN
69  *         0       1       x       x    MAINT
70  *         1       x       x       x    MAINT
71  *
72  * This can be simplified this way :
73  *
74  *   state_str = (state & MAINT) ? "MAINT" : (state & DRAIN) : "DRAIN" : "READY"
75  */
76 enum srv_admin {
77 	SRV_ADMF_FMAINT    = 0x01,        /* the server was explicitly forced into maintenance */
78 	SRV_ADMF_IMAINT    = 0x02,        /* the server has inherited the maintenance status from a tracked server */
79 	SRV_ADMF_MAINT     = 0x23,        /* mask to check if any maintenance flag is present */
80 	SRV_ADMF_CMAINT    = 0x04,        /* the server is in maintenance because of the configuration */
81 	SRV_ADMF_FDRAIN    = 0x08,        /* the server was explicitly forced into drain state */
82 	SRV_ADMF_IDRAIN    = 0x10,        /* the server has inherited the drain status from a tracked server */
83 	SRV_ADMF_DRAIN     = 0x18,        /* mask to check if any drain flag is present */
84 	SRV_ADMF_RMAINT    = 0x20,        /* the server is down because of an IP address resolution failure */
85 };
86 
87 /* options for servers' "init-addr" parameter
88  * this parameter may be used to drive HAProxy's behavior when parsing a server
89  * address at start up time.
90  * These values are stored as a list into an integer ordered from first to last
91  * starting with the lowest to highest bits. SRV_IADDR_END (0) is used to
92  * indicate the end of the list. 3 bits are enough to store each value.
93  */
94 enum srv_initaddr {
95 	SRV_IADDR_END      = 0,           /* end of the list */
96 	SRV_IADDR_NONE     = 1,           /* the server won't have any address at start up */
97 	SRV_IADDR_LIBC     = 2,           /* address set using the libc DNS resolver */
98 	SRV_IADDR_LAST     = 3,           /* we set the IP address found in state-file for this server */
99 	SRV_IADDR_IP       = 4,           /* we set an arbitrary IP address to the server */
100 };
101 
102 /* server-state-file version */
103 #define SRV_STATE_FILE_VERSION 1
104 #define SRV_STATE_FILE_VERSION_MIN 1
105 #define SRV_STATE_FILE_VERSION_MAX 1
106 #define SRV_STATE_FILE_FIELD_NAMES "be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id"
107 #define SRV_STATE_FILE_MAX_FIELDS 18
108 #define SRV_STATE_FILE_NB_FIELDS_VERSION_1 18
109 #define SRV_STATE_LINE_MAXLEN 512
110 
111 /* server flags */
112 #define SRV_F_BACKUP       0x0001        /* this server is a backup server */
113 #define SRV_F_MAPPORTS     0x0002        /* this server uses mapped ports */
114 #define SRV_F_NON_STICK    0x0004        /* never add connections allocated to this server to a stick table */
115 #define SRV_F_USE_NS_FROM_PP 0x0008      /* use namespace associated with connection if present */
116 #define SRV_F_FORCED_ID    0x0010        /* server's ID was forced in the configuration */
117 #define SRV_F_CHECKADDR    0x0020        /* this server has a check addr configured */
118 #define SRV_F_CHECKPORT    0x0040        /* this server has a check port configured */
119 #define SRV_F_AGENTADDR    0x0080        /* this server has a agent addr configured */
120 
121 /* configured server options for send-proxy (server->pp_opts) */
122 #define SRV_PP_V1          0x0001        /* proxy protocol version 1 */
123 #define SRV_PP_V2          0x0002        /* proxy protocol version 2 */
124 #define SRV_PP_V2_SSL      0x0004        /* proxy protocol version 2 with SSL*/
125 #define SRV_PP_V2_SSL_CN   0x0008        /* proxy protocol version 2 with SSL and CN*/
126 
127 /* function which act on servers need to return various errors */
128 #define SRV_STATUS_OK       0   /* everything is OK. */
129 #define SRV_STATUS_INTERNAL 1   /* other unrecoverable errors. */
130 #define SRV_STATUS_NOSRV    2   /* no server is available */
131 #define SRV_STATUS_FULL     3   /* the/all server(s) are saturated */
132 #define SRV_STATUS_QUEUED   4   /* the/all server(s) are saturated but the connection was queued */
133 
134 /* various constants */
135 #define SRV_UWGHT_RANGE 256
136 #define SRV_UWGHT_MAX   (SRV_UWGHT_RANGE)
137 #define SRV_EWGHT_RANGE (SRV_UWGHT_RANGE * BE_WEIGHT_SCALE)
138 #define SRV_EWGHT_MAX   (SRV_UWGHT_MAX   * BE_WEIGHT_SCALE)
139 
140 #ifdef USE_OPENSSL
141 /* server ssl options */
142 #define SRV_SSL_O_NONE         0x0000
143 #define SRV_SSL_O_NO_VMASK     0x000F /* force version mask */
144 #define SRV_SSL_O_NO_SSLV3     0x0001 /* disable SSLv3 */
145 #define SRV_SSL_O_NO_TLSV10    0x0002 /* disable TLSv1.0 */
146 #define SRV_SSL_O_NO_TLSV11    0x0004 /* disable TLSv1.1 */
147 #define SRV_SSL_O_NO_TLSV12    0x0008 /* disable TLSv1.2 */
148 /* 0x000F reserved for 'no' protocol version options */
149 #define SRV_SSL_O_USE_VMASK    0x00F0 /* force version mask */
150 #define SRV_SSL_O_USE_SSLV3    0x0010 /* force SSLv3 */
151 #define SRV_SSL_O_USE_TLSV10   0x0020 /* force TLSv1.0 */
152 #define SRV_SSL_O_USE_TLSV11   0x0040 /* force TLSv1.1 */
153 #define SRV_SSL_O_USE_TLSV12   0x0080 /* force TLSv1.2 */
154 /* 0x00F0 reserved for 'force' protocol version options */
155 #define SRV_SSL_O_NO_TLS_TICKETS 0x0100 /* disable session resumption tickets */
156 #define SRV_SSL_O_NO_REUSE     0x200  /* disable session reuse */
157 #endif
158 
159 struct pid_list {
160 	struct list list;
161 	pid_t pid;
162 	struct task *t;
163 	int status;
164 	int exited;
165 };
166 
167 /* A tree occurrence is a descriptor of a place in a tree, with a pointer back
168  * to the server itself.
169  */
170 struct server;
171 struct tree_occ {
172 	struct server *server;
173 	struct eb32_node node;
174 };
175 
176 struct server {
177 	enum obj_type obj_type;                 /* object type == OBJ_TYPE_SERVER */
178 	enum srv_state state, prev_state;       /* server state among SRV_ST_* */
179 	enum srv_admin admin, prev_admin;       /* server maintenance status : SRV_ADMF_* */
180 	unsigned char flags;                    /* server flags (SRV_F_*) */
181 	struct server *next;
182 	int cklen;				/* the len of the cookie, to speed up checks */
183 	int rdr_len;				/* the length of the redirection prefix */
184 	char *cookie;				/* the id set in the cookie */
185 	char *rdr_pfx;				/* the redirection prefix */
186 	int pp_opts;				/* proxy protocol options (SRV_PP_*) */
187 
188 	struct proxy *proxy;			/* the proxy this server belongs to */
189 	int served;				/* # of active sessions currently being served (ie not pending) */
190 	int cur_sess;				/* number of currently active sessions (including syn_sent) */
191 	unsigned maxconn, minconn;		/* max # of active sessions (0 = unlimited), min# for dynamic limit. */
192 	int nbpend;				/* number of pending connections */
193 	int maxqueue;				/* maximum number of pending connections allowed */
194 	struct freq_ctr sess_per_sec;		/* sessions per second on this server */
195 	struct be_counters counters;		/* statistics counters */
196 
197 	struct list pendconns;			/* pending connections */
198 	struct list actconns;			/* active connections */
199 	struct list priv_conns;			/* private idle connections attached to stream interfaces */
200 	struct list idle_conns;			/* sharable idle connections attached or not to a stream interface */
201 	struct list safe_conns;			/* safe idle connections attached to stream interfaces, shared */
202 	struct task *warmup;                    /* the task dedicated to the warmup when slowstart is set */
203 
204 	struct conn_src conn_src;               /* connection source settings */
205 
206 	struct server *track;                   /* the server we're currently tracking, if any */
207 	struct server *trackers;                /* the list of servers tracking us, if any */
208 	struct server *tracknext;               /* next server tracking <track> in <track>'s trackers list */
209 	char *trackit;				/* temporary variable to make assignment deferrable */
210 	int consecutive_errors;			/* current number of consecutive errors */
211 	int consecutive_errors_limit;		/* number of consecutive errors that triggers an event */
212 	short observe, onerror;			/* observing mode: one of HANA_OBS_*; what to do on error: on of ANA_ONERR_* */
213 	short onmarkeddown;			/* what to do when marked down: one of HANA_ONMARKEDDOWN_* */
214 	short onmarkedup;			/* what to do when marked up: one of HANA_ONMARKEDUP_* */
215 	int slowstart;				/* slowstart time in seconds (ms in the conf) */
216 
217 	char *id;				/* just for identification */
218 	unsigned iweight,uweight, eweight;	/* initial weight, user-specified weight, and effective weight */
219 	unsigned wscore;			/* weight score, used during srv map computation */
220 	unsigned prev_eweight;			/* eweight before last change */
221 	unsigned rweight;			/* remainer of weight in the current LB tree */
222 	unsigned cumulative_weight;		/* weight of servers prior to this one in the same group, for chash balancing */
223 	unsigned npos, lpos;			/* next and last positions in the LB tree */
224 	struct eb32_node lb_node;               /* node used for tree-based load balancing */
225 	struct eb_root *lb_tree;                /* we want to know in what tree the server is */
226 	struct server *next_full;               /* next server in the temporary full list */
227 	unsigned lb_nodes_tot;                  /* number of allocated lb_nodes (C-HASH) */
228 	unsigned lb_nodes_now;                  /* number of lb_nodes placed in the tree (C-HASH) */
229 	struct tree_occ *lb_nodes;              /* lb_nodes_tot * struct tree_occ */
230 
231 	const struct netns_entry *netns;        /* contains network namespace name or NULL. Network namespace comes from configuration */
232 	/* warning, these structs are huge, keep them at the bottom */
233 	struct sockaddr_storage addr;           /* the address to connect to, doesn't include the port */
234 	unsigned int svc_port;                  /* the port to connect to (for relevant families) */
235 	struct xprt_ops *xprt;                  /* transport-layer operations */
236 	unsigned down_time;			/* total time the server was down */
237 	time_t last_change;			/* last time, when the state was changed */
238 
239 	int puid;				/* proxy-unique server ID, used for SNMP, and "first" LB algo */
240 	int tcp_ut;                             /* for TCP, user timeout */
241 
242 	struct check check;                     /* health-check specific configuration */
243 	struct check agent;                     /* agent specific configuration */
244 
245 	char *resolvers_id;			/* resolvers section used by this server */
246 	char *hostname;				/* server hostname */
247 	char *lastaddr;				/* the address string provided by the server-state file */
248 	struct dns_resolution *resolution;	/* server name resolution */
249 	struct dns_options dns_opts;
250 	struct sockaddr_storage init_addr;	/* plain IP address specified on the init-addr line */
251 	unsigned int init_addr_methods;		/* initial address setting, 3-bit per method, ends at 0, enough to store 10 entries */
252 
253 #ifdef USE_OPENSSL
254 	int use_ssl;				/* ssl enabled */
255 	struct {
256 		SSL_CTX *ctx;
257 		SSL_SESSION *reused_sess;
258 		char *ciphers;			/* cipher suite to use if non-null */
259 		int options;			/* ssl options */
260 		int verify;			/* verify method (set of SSL_VERIFY_* flags) */
261 		char *verify_host;              /* hostname of certificate must match this host */
262 		char *ca_file;			/* CAfile to use on verify */
263 		char *crl_file;			/* CRLfile to use on verify */
264 		char *client_crt;		/* client certificate to send */
265 		struct sample_expr *sni;        /* sample expression for SNI */
266 	} ssl_ctx;
267 #endif
268 	struct {
269 		const char *file;		/* file where the section appears */
270 		int line;			/* line where the section appears */
271 		struct eb32_node id;		/* place in the tree of used IDs */
272 	} conf;					/* config information */
273 };
274 
275 /* Descriptor for a "server" keyword. The ->parse() function returns 0 in case of
276  * success, or a combination of ERR_* flags if an error is encountered. The
277  * function pointer can be NULL if not implemented. The function also has an
278  * access to the current "server" config line. The ->skip value tells the parser
279  * how many words have to be skipped after the keyword. If the function needs to
280  * parse more keywords, it needs to update cur_arg.
281  */
282 struct srv_kw {
283 	const char *kw;
284 	int (*parse)(char **args, int *cur_arg, struct proxy *px, struct server *srv, char **err);
285 	int skip; /* nb min of args to skip, for use when kw is not handled */
286 	int default_ok; /* non-zero if kw is supported in default-server section */
287 };
288 
289 /*
290  * A keyword list. It is a NULL-terminated array of keywords. It embeds a
291  * struct list in order to be linked to other lists, allowing it to easily
292  * be declared where it is needed, and linked without duplicating data nor
293  * allocating memory. It is also possible to indicate a scope for the keywords.
294  */
295 struct srv_kw_list {
296 	const char *scope;
297 	struct list list;
298 	struct srv_kw kw[VAR_ARRAY];
299 };
300 
301 #endif /* _TYPES_SERVER_H */
302 
303 /*
304  * Local variables:
305  *  c-indent-level: 8
306  *  c-basic-offset: 8
307  * End:
308  */
309