blob: 57971a2eb2a5c38d094e1e8e599bccbc03689e69 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau5ab04ec2011-03-20 10:32:26 +01002 * include/types/server.h
3 * This file defines everything related to servers.
4 *
Willy Tarreauf09c6602012-02-13 17:12:08 +01005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreau5ab04ec2011-03-20 10:32:26 +01006 *
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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _TYPES_SERVER_H
23#define _TYPES_SERVER_H
24
25#include <netinet/in.h>
26#include <arpa/inet.h>
27
Emeric Brunc6545ac2012-05-18 15:46:21 +020028#ifdef USE_OPENSSL
29#include <openssl/ssl.h>
30#endif
31
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020032#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/mini-clist.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010034#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreauda92e2f2012-07-06 09:40:59 +020036#include <types/connection.h>
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +020037#include <types/counters.h>
Thierry Fournierada34842016-02-17 21:25:09 +010038#include <types/dns.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <types/freq_ctr.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010040#include <types/obj_type.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <types/proxy.h>
42#include <types/queue.h>
43#include <types/task.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <types/checks.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
46
Godbachf2dd68d2014-12-10 10:21:30 +080047/* server states. Only SRV_ST_STOPPED indicates a down server. */
Willy Tarreauc93cd162014-05-13 15:54:22 +020048enum srv_state {
Willy Tarreau892337c2014-05-13 23:41:20 +020049 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) */
Willy Tarreauc93cd162014-05-13 15:54:22 +020053};
54
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +020055/* 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"
Willy Tarreau20125212014-05-13 19:44:56 +020075 */
76enum srv_admin {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +020077 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 */
Baptiste Assmann89aa7f32016-11-02 21:31:27 +010079 SRV_ADMF_MAINT = 0x23, /* mask to check if any maintenance flag is present */
Baptiste Assmann9f5ada32015-08-08 15:49:13 +020080 SRV_ADMF_CMAINT = 0x04, /* the server is in maintenance because of the configuration */
Baptiste Assmann9f5ada32015-08-08 15:49:13 +020081 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 */
Baptiste Assmann89aa7f32016-11-02 21:31:27 +010084 SRV_ADMF_RMAINT = 0x20, /* the server is down because of an IP address resolution failure */
Willy Tarreau20125212014-05-13 19:44:56 +020085};
86
Baptiste Assmann41472f42015-05-08 23:34:06 +020087/* server-state-file version */
88#define SRV_STATE_FILE_VERSION 1
89#define SRV_STATE_FILE_VERSION_MIN 1
90#define SRV_STATE_FILE_VERSION_MAX 1
91#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"
92#define SRV_STATE_FILE_MAX_FIELDS 18
93#define SRV_STATE_FILE_NB_FIELDS_VERSION_1 18
94#define SRV_STATE_LINE_MAXLEN 512
95
Willy Tarreaubaaee002006-06-26 02:48:02 +020096/* server flags */
Willy Tarreauc93cd162014-05-13 15:54:22 +020097#define SRV_F_BACKUP 0x0001 /* this server is a backup server */
98#define SRV_F_MAPPORTS 0x0002 /* this server uses mapped ports */
99#define SRV_F_NON_STICK 0x0004 /* never add connections allocated to this server to a stick table */
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100100#define SRV_F_USE_NS_FROM_PP 0x0008 /* use namespace associated with connection if present */
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200101#define SRV_F_FORCED_ID 0x0010 /* server's ID was forced in the configuration */
Baptiste Assmann6b453f12016-08-11 23:12:18 +0200102#define SRV_F_CHECKADDR 0x0020 /* this server has a check addr configured */
103#define SRV_F_CHECKPORT 0x0040 /* this server has a check port configured */
104#define SRV_F_AGENTADDR 0x0080 /* this server has a agent addr configured */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105
David Safb76832014-05-08 23:42:08 -0400106/* configured server options for send-proxy (server->pp_opts) */
107#define SRV_PP_V1 0x0001 /* proxy protocol version 1 */
108#define SRV_PP_V2 0x0002 /* proxy protocol version 2 */
109#define SRV_PP_V2_SSL 0x0004 /* proxy protocol version 2 with SSL*/
110#define SRV_PP_V2_SSL_CN 0x0008 /* proxy protocol version 2 with SSL and CN*/
111
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112/* function which act on servers need to return various errors */
113#define SRV_STATUS_OK 0 /* everything is OK. */
114#define SRV_STATUS_INTERNAL 1 /* other unrecoverable errors. */
115#define SRV_STATUS_NOSRV 2 /* no server is available */
116#define SRV_STATUS_FULL 3 /* the/all server(s) are saturated */
117#define SRV_STATUS_QUEUED 4 /* the/all server(s) are saturated but the connection was queued */
118
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100119/* various constants */
120#define SRV_UWGHT_RANGE 256
Godbacha34bdc02013-07-22 07:44:53 +0800121#define SRV_UWGHT_MAX (SRV_UWGHT_RANGE)
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100122#define SRV_EWGHT_RANGE (SRV_UWGHT_RANGE * BE_WEIGHT_SCALE)
123#define SRV_EWGHT_MAX (SRV_UWGHT_MAX * BE_WEIGHT_SCALE)
124
Emeric Brun89675492012-10-05 13:48:26 +0200125#ifdef USE_OPENSSL
126/* server ssl options */
127#define SRV_SSL_O_NONE 0x0000
Emeric Brun992adc92012-10-11 18:36:21 +0200128#define SRV_SSL_O_NO_VMASK 0x000F /* force version mask */
Emeric Brun89675492012-10-05 13:48:26 +0200129#define SRV_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
130#define SRV_SSL_O_NO_TLSV10 0x0002 /* disable TLSv1.0 */
131#define SRV_SSL_O_NO_TLSV11 0x0004 /* disable TLSv1.1 */
132#define SRV_SSL_O_NO_TLSV12 0x0008 /* disable TLSv1.2 */
133/* 0x000F reserved for 'no' protocol version options */
Emeric Brun992adc92012-10-11 18:36:21 +0200134#define SRV_SSL_O_USE_VMASK 0x00F0 /* force version mask */
135#define SRV_SSL_O_USE_SSLV3 0x0010 /* force SSLv3 */
136#define SRV_SSL_O_USE_TLSV10 0x0020 /* force TLSv1.0 */
137#define SRV_SSL_O_USE_TLSV11 0x0040 /* force TLSv1.1 */
138#define SRV_SSL_O_USE_TLSV12 0x0080 /* force TLSv1.2 */
Emeric Brun8694b9a2012-10-05 14:39:07 +0200139/* 0x00F0 reserved for 'force' protocol version options */
Emeric Brunf9c5c472012-10-11 15:28:34 +0200140#define SRV_SSL_O_NO_TLS_TICKETS 0x0100 /* disable session resumption tickets */
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +0100141#define SRV_SSL_O_NO_REUSE 0x200 /* disable session reuse */
Emeric Brun89675492012-10-05 13:48:26 +0200142#endif
143
Simon Horman98637e52014-06-20 12:30:16 +0900144struct pid_list {
145 struct list list;
146 pid_t pid;
147 struct task *t;
148 int status;
149 int exited;
150};
151
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200152/* A tree occurrence is a descriptor of a place in a tree, with a pointer back
153 * to the server itself.
154 */
155struct server;
156struct tree_occ {
157 struct server *server;
158 struct eb32_node node;
159};
160
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161struct server {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100162 enum obj_type obj_type; /* object type == OBJ_TYPE_SERVER */
Willy Tarreau892337c2014-05-13 23:41:20 +0200163 enum srv_state state, prev_state; /* server state among SRV_ST_* */
Willy Tarreau20125212014-05-13 19:44:56 +0200164 enum srv_admin admin, prev_admin; /* server maintenance status : SRV_ADMF_* */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200165 unsigned char flags; /* server flags (SRV_F_*) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 struct server *next;
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200167 int cklen; /* the len of the cookie, to speed up checks */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100168 int rdr_len; /* the length of the redirection prefix */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200169 char *cookie; /* the id set in the cookie */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100170 char *rdr_pfx; /* the redirection prefix */
David Safb76832014-05-08 23:42:08 -0400171 int pp_opts; /* proxy protocol options (SRV_PP_*) */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200172
173 struct proxy *proxy; /* the proxy this server belongs to */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200174 int served; /* # of active sessions currently being served (ie not pending) */
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200175 int cur_sess; /* number of currently active sessions (including syn_sent) */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200176 unsigned maxconn, minconn; /* max # of active sessions (0 = unlimited), min# for dynamic limit. */
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200177 int nbpend; /* number of pending connections */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200178 int maxqueue; /* maximum number of pending connections allowed */
Willy Tarreau7b815632011-10-21 18:51:57 +0200179 struct freq_ctr sess_per_sec; /* sessions per second on this server */
Willy Tarreauac68c5d2009-10-04 23:12:44 +0200180 struct srvcounters counters; /* statistics counters */
181
Willy Tarreau91b6f322007-03-25 21:03:01 +0200182 struct list pendconns; /* pending connections */
Simon Hormanaf514952011-06-21 14:34:57 +0900183 struct list actconns; /* active connections */
Willy Tarreau600802a2015-08-04 17:19:06 +0200184 struct list priv_conns; /* private idle connections attached to stream interfaces */
Willy Tarreau173a1c62015-08-05 10:31:57 +0200185 struct list idle_conns; /* sharable idle connections attached or not to a stream interface */
Willy Tarreau7017cb02015-08-05 16:35:23 +0200186 struct list safe_conns; /* safe idle connections attached to stream interfaces, shared */
Willy Tarreau2e993902011-10-31 11:53:20 +0100187 struct task *warmup; /* the task dedicated to the warmup when slowstart is set */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200188
Willy Tarreauef9a3602012-12-08 22:29:20 +0100189 struct conn_src conn_src; /* connection source settings */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200190
Willy Tarreau1a53a3a2013-12-11 15:27:05 +0100191 struct server *track; /* the server we're currently tracking, if any */
192 struct server *trackers; /* the list of servers tracking us, if any */
193 struct server *tracknext; /* next server tracking <track> in <track>'s trackers list */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100194 char *trackit; /* temporary variable to make assignment deferrable */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100195 int consecutive_errors; /* current number of consecutive errors */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100196 int consecutive_errors_limit; /* number of consecutive errors that triggers an event */
197 short observe, onerror; /* observing mode: one of HANA_OBS_*; what to do on error: on of ANA_ONERR_* */
Justin Karnegeseb2c24a2012-05-24 15:28:52 -0700198 short onmarkeddown; /* what to do when marked down: one of HANA_ONMARKEDDOWN_* */
199 short onmarkedup; /* what to do when marked up: one of HANA_ONMARKEDUP_* */
Willy Tarreau9909fc12007-11-30 17:42:05 +0100200 int slowstart; /* slowstart time in seconds (ms in the conf) */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200201
202 char *id; /* just for identification */
Willy Tarreau975c50b2009-10-10 19:34:06 +0200203 unsigned iweight,uweight, eweight; /* initial weight, user-specified weight, and effective weight */
Willy Tarreau417fae02007-03-25 21:16:40 +0200204 unsigned wscore; /* weight score, used during srv map computation */
Willy Tarreaub625a082007-11-26 01:15:43 +0100205 unsigned prev_eweight; /* eweight before last change */
206 unsigned rweight; /* remainer of weight in the current LB tree */
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400207 unsigned cumulative_weight; /* weight of servers prior to this one in the same group, for chash balancing */
Willy Tarreaub625a082007-11-26 01:15:43 +0100208 unsigned npos, lpos; /* next and last positions in the LB tree */
209 struct eb32_node lb_node; /* node used for tree-based load balancing */
210 struct eb_root *lb_tree; /* we want to know in what tree the server is */
211 struct server *next_full; /* next server in the temporary full list */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200212 unsigned lb_nodes_tot; /* number of allocated lb_nodes (C-HASH) */
213 unsigned lb_nodes_now; /* number of lb_nodes placed in the tree (C-HASH) */
214 struct tree_occ *lb_nodes; /* lb_nodes_tot * struct tree_occ */
Willy Tarreau91b6f322007-03-25 21:03:01 +0200215
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100216 const struct netns_entry *netns; /* contains network namespace name or NULL. Network namespace comes from configuration */
Willy Tarreau7b815632011-10-21 18:51:57 +0200217 /* warning, these structs are huge, keep them at the bottom */
218 struct sockaddr_storage addr; /* the address to connect to */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200219 struct xprt_ops *xprt; /* transport-layer operations */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200220 unsigned down_time; /* total time the server was down */
221 time_t last_change; /* last time, when the state was changed */
222
Willy Tarreauf09c6602012-02-13 17:12:08 +0100223 int puid; /* proxy-unique server ID, used for SNMP, and "first" LB algo */
Willy Tarreau163d4622015-10-13 16:16:41 +0200224 int tcp_ut; /* for TCP, user timeout */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200225
Simon Horman66183002013-02-23 10:16:43 +0900226 struct check check; /* health-check specific configuration */
Simon Hormand60d6912013-11-25 10:46:36 +0900227 struct check agent; /* agent specific configuration */
Nick Chalk57b1bf72010-03-16 15:50:46 +0000228
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200229 char *resolvers_id; /* resolvers section used by this server */
230 char *hostname; /* server hostname */
Baptiste Assmann83cbaa52016-11-02 15:34:05 +0100231 char *lastaddr; /* the address string provided by the server-state file */
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200232 struct dns_resolution *resolution; /* server name resolution */
Thierry Fournierada34842016-02-17 21:25:09 +0100233 struct dns_options dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200234
Emeric Brunc6545ac2012-05-18 15:46:21 +0200235#ifdef USE_OPENSSL
Emeric Brun01f8e2f2012-05-18 16:02:00 +0200236 int use_ssl; /* ssl enabled */
Emeric Brunc6545ac2012-05-18 15:46:21 +0200237 struct {
238 SSL_CTX *ctx;
239 SSL_SESSION *reused_sess;
Willy Tarreaud7aacbf2012-09-03 23:34:19 +0200240 char *ciphers; /* cipher suite to use if non-null */
Emeric Brun89675492012-10-05 13:48:26 +0200241 int options; /* ssl options */
Emeric Brunef42d922012-10-11 16:11:36 +0200242 int verify; /* verify method (set of SSL_VERIFY_* flags) */
Evan Broderbe554312013-06-27 00:05:25 -0700243 char *verify_host; /* hostname of certificate must match this host */
Emeric Brunef42d922012-10-11 16:11:36 +0200244 char *ca_file; /* CAfile to use on verify */
245 char *crl_file; /* CRLfile to use on verify */
Emeric Bruna7aa3092012-10-26 12:58:00 +0200246 char *client_crt; /* client certificate to send */
Willy Tarreau732eac42015-07-09 11:40:25 +0200247 struct sample_expr *sni; /* sample expression for SNI */
Emeric Brunc6545ac2012-05-18 15:46:21 +0200248 } ssl_ctx;
249#endif
Willy Tarreau90a570f2009-10-04 20:54:54 +0200250 struct {
251 const char *file; /* file where the section appears */
252 int line; /* line where the section appears */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +0200253 struct eb32_node id; /* place in the tree of used IDs */
Willy Tarreau90a570f2009-10-04 20:54:54 +0200254 } conf; /* config information */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255};
256
Willy Tarreau21faa912012-10-10 08:27:36 +0200257/* Descriptor for a "server" keyword. The ->parse() function returns 0 in case of
258 * success, or a combination of ERR_* flags if an error is encountered. The
259 * function pointer can be NULL if not implemented. The function also has an
260 * access to the current "server" config line. The ->skip value tells the parser
261 * how many words have to be skipped after the keyword. If the function needs to
262 * parse more keywords, it needs to update cur_arg.
263 */
264struct srv_kw {
265 const char *kw;
266 int (*parse)(char **args, int *cur_arg, struct proxy *px, struct server *srv, char **err);
267 int skip; /* nb min of args to skip, for use when kw is not handled */
268 int default_ok; /* non-zero if kw is supported in default-server section */
269};
270
271/*
272 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
273 * struct list in order to be linked to other lists, allowing it to easily
274 * be declared where it is needed, and linked without duplicating data nor
275 * allocating memory. It is also possible to indicate a scope for the keywords.
276 */
277struct srv_kw_list {
278 const char *scope;
279 struct list list;
280 struct srv_kw kw[VAR_ARRAY];
281};
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282
283#endif /* _TYPES_SERVER_H */
284
285/*
286 * Local variables:
287 * c-indent-level: 8
288 * c-basic-offset: 8
289 * End:
290 */