Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 5 | * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
| 15 | |
| 16 | #include <common/cfgparse.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 17 | #include <common/config.h> |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 18 | #include <common/errors.h> |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 19 | #include <common/time.h> |
| 20 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 21 | #include <types/global.h> |
| 22 | |
| 23 | #include <proto/port_range.h> |
| 24 | #include <proto/protocol.h> |
| 25 | #include <proto/raw_sock.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 26 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 28 | /* List head of all known server keywords */ |
| 29 | static struct srv_kw_list srv_keywords = { |
| 30 | .list = LIST_HEAD_INIT(srv_keywords.list) |
| 31 | }; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 32 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 33 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 34 | { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 35 | if ((s->state & SRV_STF_RUNNING) && s->last_change < now.tv_sec) // ignore negative time |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 36 | return s->down_time; |
| 37 | |
| 38 | return now.tv_sec - s->last_change + s->down_time; |
| 39 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 41 | int srv_lastsession(const struct server *s) |
| 42 | { |
| 43 | if (s->counters.last_sess) |
| 44 | return now.tv_sec - s->counters.last_sess; |
| 45 | |
| 46 | return -1; |
| 47 | } |
| 48 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 49 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 50 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 51 | const struct server *s = check->server; |
| 52 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 53 | if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 54 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 56 | if (!(s->state & SRV_STF_RUNNING) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 57 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 58 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 59 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 64 | * parsing sessions. |
| 65 | */ |
| 66 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 67 | { |
| 68 | LIST_ADDQ(&srv_keywords.list, &kwl->list); |
| 69 | } |
| 70 | |
| 71 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 72 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 73 | * find one with a valid ->parse() function. This way it is possible to declare |
| 74 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 75 | * if some options are met. Note that if the requested keyword contains an |
| 76 | * opening parenthesis, everything from this point is ignored. |
| 77 | */ |
| 78 | struct srv_kw *srv_find_kw(const char *kw) |
| 79 | { |
| 80 | int index; |
| 81 | const char *kwend; |
| 82 | struct srv_kw_list *kwl; |
| 83 | struct srv_kw *ret = NULL; |
| 84 | |
| 85 | kwend = strchr(kw, '('); |
| 86 | if (!kwend) |
| 87 | kwend = kw + strlen(kw); |
| 88 | |
| 89 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 90 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 91 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 92 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 93 | if (kwl->kw[index].parse) |
| 94 | return &kwl->kw[index]; /* found it !*/ |
| 95 | else |
| 96 | ret = &kwl->kw[index]; /* may be OK */ |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 104 | * unsupported keywords are only dumped if their supported form was not |
| 105 | * found. |
| 106 | */ |
| 107 | void srv_dump_kws(char **out) |
| 108 | { |
| 109 | struct srv_kw_list *kwl; |
| 110 | int index; |
| 111 | |
| 112 | *out = NULL; |
| 113 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 114 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 115 | if (kwl->kw[index].parse || |
| 116 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 117 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 118 | kwl->scope, |
| 119 | kwl->kw[index].kw, |
| 120 | kwl->kw[index].skip ? " <arg>" : "", |
| 121 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 122 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 127 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 128 | /* parse the "id" server keyword */ |
| 129 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 130 | { |
| 131 | struct eb32_node *node; |
| 132 | |
| 133 | if (!*args[*cur_arg + 1]) { |
| 134 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 135 | return ERR_ALERT | ERR_FATAL; |
| 136 | } |
| 137 | |
| 138 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 139 | newsrv->conf.id.key = newsrv->puid; |
| 140 | |
| 141 | if (newsrv->puid <= 0) { |
| 142 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 143 | return ERR_ALERT | ERR_FATAL; |
| 144 | } |
| 145 | |
| 146 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 147 | if (node) { |
| 148 | struct server *target = container_of(node, struct server, conf.id); |
| 149 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 150 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 151 | target->id); |
| 152 | return ERR_ALERT | ERR_FATAL; |
| 153 | } |
| 154 | |
| 155 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* Note: must not be declared <const> as its list will be overwritten. |
| 160 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 161 | * all code contributors. |
| 162 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 163 | * the config parser can report an appropriate error when a known keyword was |
| 164 | * not enabled. |
| 165 | */ |
| 166 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
| 167 | { "id", srv_parse_id, 1, 0 }, /* set id# of server */ |
| 168 | { NULL, NULL, 0 }, |
| 169 | }}; |
| 170 | |
| 171 | __attribute__((constructor)) |
| 172 | static void __listener_init(void) |
| 173 | { |
| 174 | srv_register_keywords(&srv_kws); |
| 175 | } |
| 176 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 177 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
| 178 | * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup |
| 179 | * state is automatically disabled if the time is elapsed. |
| 180 | */ |
| 181 | void server_recalc_eweight(struct server *sv) |
| 182 | { |
| 183 | struct proxy *px = sv->proxy; |
| 184 | unsigned w; |
| 185 | |
| 186 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 187 | /* go to full throttle if the slowstart interval is reached */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 188 | sv->state &= ~SRV_STF_WARMINGUP; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 192 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 193 | */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 194 | if ((sv->state & SRV_STF_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 195 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 196 | else |
| 197 | w = px->lbprm.wdiv; |
| 198 | |
| 199 | sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult; |
| 200 | |
| 201 | /* now propagate the status change to any LB algorithms */ |
| 202 | if (px->lbprm.update_server_eweight) |
| 203 | px->lbprm.update_server_eweight(sv); |
| 204 | else if (sv->eweight) { |
| 205 | if (px->lbprm.set_server_status_up) |
| 206 | px->lbprm.set_server_status_up(sv); |
| 207 | } |
| 208 | else { |
| 209 | if (px->lbprm.set_server_status_down) |
| 210 | px->lbprm.set_server_status_down(sv); |
| 211 | } |
| 212 | } |
| 213 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 214 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 215 | * Parses weight_str and configures sv accordingly. |
| 216 | * Returns NULL on success, error message string otherwise. |
| 217 | */ |
| 218 | const char *server_parse_weight_change_request(struct server *sv, |
| 219 | const char *weight_str) |
| 220 | { |
| 221 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 222 | long int w; |
| 223 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 224 | |
| 225 | px = sv->proxy; |
| 226 | |
| 227 | /* if the weight is terminated with '%', it is set relative to |
| 228 | * the initial weight, otherwise it is absolute. |
| 229 | */ |
| 230 | if (!*weight_str) |
| 231 | return "Require <weight> or <weight%>.\n"; |
| 232 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 233 | w = strtol(weight_str, &end, 10); |
| 234 | if (end == weight_str) |
| 235 | return "Empty weight string empty or preceded by garbage"; |
| 236 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 237 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 238 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 239 | /* Avoid integer overflow */ |
| 240 | if (w > 25600) |
| 241 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 242 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 243 | if (w > 256) |
| 244 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 245 | } |
| 246 | else if (w < 0 || w > 256) |
| 247 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 248 | else if (end[0] != '\0') |
| 249 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 250 | |
| 251 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 252 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 253 | |
| 254 | sv->uweight = w; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 255 | server_recalc_eweight(sv); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 256 | |
| 257 | return NULL; |
| 258 | } |
| 259 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 260 | static int init_check(struct check *check, int type, const char * file, int linenum) |
| 261 | { |
| 262 | check->type = type; |
| 263 | |
| 264 | /* Allocate buffer for requests... */ |
| 265 | if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) { |
| 266 | Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum); |
| 267 | return ERR_ALERT | ERR_ABORT; |
| 268 | } |
| 269 | check->bi->size = global.tune.chksize; |
| 270 | |
| 271 | /* Allocate buffer for responses... */ |
| 272 | if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) { |
| 273 | Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum); |
| 274 | return ERR_ALERT | ERR_ABORT; |
| 275 | } |
| 276 | check->bo->size = global.tune.chksize; |
| 277 | |
| 278 | /* Allocate buffer for partial results... */ |
| 279 | if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) { |
| 280 | Alert("parsing [%s:%d] : out of memory while allocating check connection.\n", file, linenum); |
| 281 | return ERR_ALERT | ERR_ABORT; |
| 282 | } |
| 283 | |
| 284 | check->conn->t.sock.fd = -1; /* no agent in progress yet */ |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy) |
| 290 | { |
| 291 | struct server *newsrv = NULL; |
| 292 | const char *err; |
| 293 | char *errmsg = NULL; |
| 294 | int err_code = 0; |
| 295 | unsigned val; |
| 296 | |
| 297 | if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */ |
| 298 | int cur_arg; |
| 299 | short realport = 0; |
| 300 | int do_agent = 0, do_check = 0, defsrv = (*args[0] == 'd'); |
| 301 | |
| 302 | if (!defsrv && curproxy == defproxy) { |
| 303 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 304 | err_code |= ERR_ALERT | ERR_FATAL; |
| 305 | goto out; |
| 306 | } |
| 307 | else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) |
| 308 | err_code |= ERR_ALERT | ERR_FATAL; |
| 309 | |
| 310 | if (!*args[2]) { |
| 311 | Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 312 | file, linenum, args[0]); |
| 313 | err_code |= ERR_ALERT | ERR_FATAL; |
| 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | err = invalid_char(args[1]); |
| 318 | if (err && !defsrv) { |
| 319 | Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n", |
| 320 | file, linenum, *err, args[1]); |
| 321 | err_code |= ERR_ALERT | ERR_FATAL; |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | if (!defsrv) { |
| 326 | struct sockaddr_storage *sk; |
| 327 | int port1, port2; |
| 328 | struct protocol *proto; |
| 329 | |
| 330 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 331 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 332 | err_code |= ERR_ALERT | ERR_ABORT; |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | /* the servers are linked backwards first */ |
| 337 | newsrv->next = curproxy->srv; |
| 338 | curproxy->srv = newsrv; |
| 339 | newsrv->proxy = curproxy; |
| 340 | newsrv->conf.file = strdup(file); |
| 341 | newsrv->conf.line = linenum; |
| 342 | |
| 343 | newsrv->obj_type = OBJ_TYPE_SERVER; |
| 344 | LIST_INIT(&newsrv->actconns); |
| 345 | LIST_INIT(&newsrv->pendconns); |
| 346 | do_check = 0; |
| 347 | do_agent = 0; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 348 | newsrv->flags = 0; |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame^] | 349 | newsrv->admin = 0; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 350 | newsrv->state = SRV_STF_RUNNING; /* early server setup */ |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 351 | newsrv->last_change = now.tv_sec; |
| 352 | newsrv->id = strdup(args[1]); |
| 353 | |
| 354 | /* several ways to check the port component : |
| 355 | * - IP => port=+0, relative (IPv4 only) |
| 356 | * - IP: => port=+0, relative |
| 357 | * - IP:N => port=N, absolute |
| 358 | * - IP:+N => port=+N, relative |
| 359 | * - IP:-N => port=-N, relative |
| 360 | */ |
| 361 | sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL); |
| 362 | if (!sk) { |
| 363 | Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
| 364 | err_code |= ERR_ALERT | ERR_FATAL; |
| 365 | goto out; |
| 366 | } |
| 367 | |
| 368 | proto = protocol_by_family(sk->ss_family); |
| 369 | if (!proto || !proto->connect) { |
| 370 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 371 | file, linenum, args[0], args[1]); |
| 372 | err_code |= ERR_ALERT | ERR_FATAL; |
| 373 | goto out; |
| 374 | } |
| 375 | |
| 376 | if (!port1 || !port2) { |
| 377 | /* no port specified, +offset, -offset */ |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 378 | newsrv->flags |= SRV_F_MAPPORTS; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 379 | } |
| 380 | else if (port1 != port2) { |
| 381 | /* port range */ |
| 382 | Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n", |
| 383 | file, linenum, args[0], args[1], args[2]); |
| 384 | err_code |= ERR_ALERT | ERR_FATAL; |
| 385 | goto out; |
| 386 | } |
| 387 | else { |
| 388 | /* used by checks */ |
| 389 | realport = port1; |
| 390 | } |
| 391 | |
| 392 | newsrv->addr = *sk; |
| 393 | newsrv->proto = newsrv->check_common.proto = protocol_by_family(newsrv->addr.ss_family); |
| 394 | newsrv->xprt = newsrv->check_common.xprt = &raw_sock; |
| 395 | |
| 396 | if (!newsrv->proto) { |
| 397 | Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n", |
| 398 | file, linenum, newsrv->addr.ss_family, args[2]); |
| 399 | err_code |= ERR_ALERT | ERR_FATAL; |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl; |
| 404 | newsrv->check.port = curproxy->defsrv.check.port; |
| 405 | newsrv->check.inter = curproxy->defsrv.check.inter; |
| 406 | newsrv->check.fastinter = curproxy->defsrv.check.fastinter; |
| 407 | newsrv->check.downinter = curproxy->defsrv.check.downinter; |
| 408 | newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl; |
| 409 | newsrv->agent.port = curproxy->defsrv.agent.port; |
| 410 | newsrv->agent.inter = curproxy->defsrv.agent.inter; |
| 411 | newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter; |
| 412 | newsrv->agent.downinter = curproxy->defsrv.agent.downinter; |
| 413 | newsrv->maxqueue = curproxy->defsrv.maxqueue; |
| 414 | newsrv->minconn = curproxy->defsrv.minconn; |
| 415 | newsrv->maxconn = curproxy->defsrv.maxconn; |
| 416 | newsrv->slowstart = curproxy->defsrv.slowstart; |
| 417 | newsrv->onerror = curproxy->defsrv.onerror; |
| 418 | newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown; |
| 419 | newsrv->onmarkedup = curproxy->defsrv.onmarkedup; |
| 420 | newsrv->consecutive_errors_limit |
| 421 | = curproxy->defsrv.consecutive_errors_limit; |
| 422 | #ifdef OPENSSL |
| 423 | newsrv->use_ssl = curproxy->defsrv.use_ssl; |
| 424 | #endif |
| 425 | newsrv->uweight = newsrv->iweight |
| 426 | = curproxy->defsrv.iweight; |
| 427 | |
| 428 | newsrv->check.status = HCHK_STATUS_INI; |
| 429 | newsrv->check.rise = curproxy->defsrv.check.rise; |
| 430 | newsrv->check.fall = curproxy->defsrv.check.fall; |
| 431 | newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */ |
| 432 | newsrv->check.server = newsrv; |
| 433 | |
| 434 | newsrv->agent.status = HCHK_STATUS_INI; |
| 435 | newsrv->agent.rise = curproxy->defsrv.agent.rise; |
| 436 | newsrv->agent.fall = curproxy->defsrv.agent.fall; |
| 437 | newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */ |
| 438 | newsrv->agent.server = newsrv; |
| 439 | |
| 440 | cur_arg = 3; |
| 441 | } else { |
| 442 | newsrv = &curproxy->defsrv; |
| 443 | cur_arg = 1; |
| 444 | } |
| 445 | |
| 446 | while (*args[cur_arg]) { |
| 447 | if (!strcmp(args[cur_arg], "agent-check")) { |
| 448 | global.maxsock++; |
| 449 | do_agent = 1; |
| 450 | cur_arg += 1; |
| 451 | } else if (!strcmp(args[cur_arg], "agent-inter")) { |
| 452 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 453 | if (err) { |
| 454 | Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n", |
| 455 | file, linenum, *err, newsrv->id); |
| 456 | err_code |= ERR_ALERT | ERR_FATAL; |
| 457 | goto out; |
| 458 | } |
| 459 | if (val <= 0) { |
| 460 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 461 | file, linenum, val, args[cur_arg], newsrv->id); |
| 462 | err_code |= ERR_ALERT | ERR_FATAL; |
| 463 | goto out; |
| 464 | } |
| 465 | newsrv->agent.inter = val; |
| 466 | cur_arg += 2; |
| 467 | } |
| 468 | else if (!strcmp(args[cur_arg], "agent-port")) { |
| 469 | global.maxsock++; |
| 470 | newsrv->agent.port = atol(args[cur_arg + 1]); |
| 471 | cur_arg += 2; |
| 472 | } |
| 473 | else if (!defsrv && !strcmp(args[cur_arg], "cookie")) { |
| 474 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 475 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 476 | cur_arg += 2; |
| 477 | } |
| 478 | else if (!defsrv && !strcmp(args[cur_arg], "redir")) { |
| 479 | newsrv->rdr_pfx = strdup(args[cur_arg + 1]); |
| 480 | newsrv->rdr_len = strlen(args[cur_arg + 1]); |
| 481 | cur_arg += 2; |
| 482 | } |
| 483 | else if (!strcmp(args[cur_arg], "rise")) { |
| 484 | if (!*args[cur_arg + 1]) { |
| 485 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 486 | file, linenum, args[cur_arg]); |
| 487 | err_code |= ERR_ALERT | ERR_FATAL; |
| 488 | goto out; |
| 489 | } |
| 490 | |
| 491 | newsrv->check.rise = atol(args[cur_arg + 1]); |
| 492 | if (newsrv->check.rise <= 0) { |
| 493 | Alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
| 494 | file, linenum, args[cur_arg]); |
| 495 | err_code |= ERR_ALERT | ERR_FATAL; |
| 496 | goto out; |
| 497 | } |
| 498 | |
| 499 | if (newsrv->check.health) |
| 500 | newsrv->check.health = newsrv->check.rise; |
| 501 | cur_arg += 2; |
| 502 | } |
| 503 | else if (!strcmp(args[cur_arg], "fall")) { |
| 504 | newsrv->check.fall = atol(args[cur_arg + 1]); |
| 505 | |
| 506 | if (!*args[cur_arg + 1]) { |
| 507 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 508 | file, linenum, args[cur_arg]); |
| 509 | err_code |= ERR_ALERT | ERR_FATAL; |
| 510 | goto out; |
| 511 | } |
| 512 | |
| 513 | if (newsrv->check.fall <= 0) { |
| 514 | Alert("parsing [%s:%d]: '%s' has to be > 0.\n", |
| 515 | file, linenum, args[cur_arg]); |
| 516 | err_code |= ERR_ALERT | ERR_FATAL; |
| 517 | goto out; |
| 518 | } |
| 519 | |
| 520 | cur_arg += 2; |
| 521 | } |
| 522 | else if (!strcmp(args[cur_arg], "inter")) { |
| 523 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 524 | if (err) { |
| 525 | Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n", |
| 526 | file, linenum, *err, newsrv->id); |
| 527 | err_code |= ERR_ALERT | ERR_FATAL; |
| 528 | goto out; |
| 529 | } |
| 530 | if (val <= 0) { |
| 531 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 532 | file, linenum, val, args[cur_arg], newsrv->id); |
| 533 | err_code |= ERR_ALERT | ERR_FATAL; |
| 534 | goto out; |
| 535 | } |
| 536 | newsrv->check.inter = val; |
| 537 | cur_arg += 2; |
| 538 | } |
| 539 | else if (!strcmp(args[cur_arg], "fastinter")) { |
| 540 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 541 | if (err) { |
| 542 | Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n", |
| 543 | file, linenum, *err, newsrv->id); |
| 544 | err_code |= ERR_ALERT | ERR_FATAL; |
| 545 | goto out; |
| 546 | } |
| 547 | if (val <= 0) { |
| 548 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 549 | file, linenum, val, args[cur_arg], newsrv->id); |
| 550 | err_code |= ERR_ALERT | ERR_FATAL; |
| 551 | goto out; |
| 552 | } |
| 553 | newsrv->check.fastinter = val; |
| 554 | cur_arg += 2; |
| 555 | } |
| 556 | else if (!strcmp(args[cur_arg], "downinter")) { |
| 557 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 558 | if (err) { |
| 559 | Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n", |
| 560 | file, linenum, *err, newsrv->id); |
| 561 | err_code |= ERR_ALERT | ERR_FATAL; |
| 562 | goto out; |
| 563 | } |
| 564 | if (val <= 0) { |
| 565 | Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n", |
| 566 | file, linenum, val, args[cur_arg], newsrv->id); |
| 567 | err_code |= ERR_ALERT | ERR_FATAL; |
| 568 | goto out; |
| 569 | } |
| 570 | newsrv->check.downinter = val; |
| 571 | cur_arg += 2; |
| 572 | } |
| 573 | else if (!defsrv && !strcmp(args[cur_arg], "addr")) { |
| 574 | struct sockaddr_storage *sk; |
| 575 | int port1, port2; |
| 576 | struct protocol *proto; |
| 577 | |
| 578 | sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL); |
| 579 | if (!sk) { |
| 580 | Alert("parsing [%s:%d] : '%s' : %s\n", |
| 581 | file, linenum, args[cur_arg], errmsg); |
| 582 | err_code |= ERR_ALERT | ERR_FATAL; |
| 583 | goto out; |
| 584 | } |
| 585 | |
| 586 | proto = protocol_by_family(sk->ss_family); |
| 587 | if (!proto || !proto->connect) { |
| 588 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 589 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 590 | err_code |= ERR_ALERT | ERR_FATAL; |
| 591 | goto out; |
| 592 | } |
| 593 | |
| 594 | if (port1 != port2) { |
| 595 | Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n", |
| 596 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 597 | err_code |= ERR_ALERT | ERR_FATAL; |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | newsrv->check_common.addr = *sk; |
Willy Tarreau | 640556c | 2014-05-09 23:38:15 +0200 | [diff] [blame] | 602 | newsrv->check_common.proto = protocol_by_family(sk->ss_family); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 603 | cur_arg += 2; |
| 604 | } |
| 605 | else if (!strcmp(args[cur_arg], "port")) { |
| 606 | newsrv->check.port = atol(args[cur_arg + 1]); |
| 607 | cur_arg += 2; |
| 608 | } |
| 609 | else if (!defsrv && !strcmp(args[cur_arg], "backup")) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 610 | newsrv->flags |= SRV_F_BACKUP; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 611 | cur_arg ++; |
| 612 | } |
| 613 | else if (!defsrv && !strcmp(args[cur_arg], "non-stick")) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 614 | newsrv->flags |= SRV_F_NON_STICK; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 615 | cur_arg ++; |
| 616 | } |
| 617 | else if (!defsrv && !strcmp(args[cur_arg], "send-proxy")) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 618 | newsrv->pp_opts |= SRV_PP_V1; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 619 | cur_arg ++; |
| 620 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 621 | else if (!defsrv && !strcmp(args[cur_arg], "send-proxy-v2")) { |
| 622 | newsrv->pp_opts |= SRV_PP_V2; |
| 623 | cur_arg ++; |
| 624 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 625 | else if (!defsrv && !strcmp(args[cur_arg], "check-send-proxy")) { |
| 626 | newsrv->check.send_proxy = 1; |
| 627 | cur_arg ++; |
| 628 | } |
| 629 | else if (!strcmp(args[cur_arg], "weight")) { |
| 630 | int w; |
| 631 | w = atol(args[cur_arg + 1]); |
| 632 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 633 | Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n", |
| 634 | file, linenum, newsrv->id, SRV_UWGHT_MAX, w); |
| 635 | err_code |= ERR_ALERT | ERR_FATAL; |
| 636 | goto out; |
| 637 | } |
| 638 | newsrv->uweight = newsrv->iweight = w; |
| 639 | cur_arg += 2; |
| 640 | } |
| 641 | else if (!strcmp(args[cur_arg], "minconn")) { |
| 642 | newsrv->minconn = atol(args[cur_arg + 1]); |
| 643 | cur_arg += 2; |
| 644 | } |
| 645 | else if (!strcmp(args[cur_arg], "maxconn")) { |
| 646 | newsrv->maxconn = atol(args[cur_arg + 1]); |
| 647 | cur_arg += 2; |
| 648 | } |
| 649 | else if (!strcmp(args[cur_arg], "maxqueue")) { |
| 650 | newsrv->maxqueue = atol(args[cur_arg + 1]); |
| 651 | cur_arg += 2; |
| 652 | } |
| 653 | else if (!strcmp(args[cur_arg], "slowstart")) { |
| 654 | /* slowstart is stored in seconds */ |
| 655 | const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS); |
| 656 | if (err) { |
| 657 | Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n", |
| 658 | file, linenum, *err, newsrv->id); |
| 659 | err_code |= ERR_ALERT | ERR_FATAL; |
| 660 | goto out; |
| 661 | } |
| 662 | newsrv->slowstart = (val + 999) / 1000; |
| 663 | cur_arg += 2; |
| 664 | } |
| 665 | else if (!defsrv && !strcmp(args[cur_arg], "track")) { |
| 666 | |
| 667 | if (!*args[cur_arg + 1]) { |
| 668 | Alert("parsing [%s:%d]: 'track' expects [<proxy>/]<server> as argument.\n", |
| 669 | file, linenum); |
| 670 | err_code |= ERR_ALERT | ERR_FATAL; |
| 671 | goto out; |
| 672 | } |
| 673 | |
| 674 | newsrv->trackit = strdup(args[cur_arg + 1]); |
| 675 | |
| 676 | cur_arg += 2; |
| 677 | } |
| 678 | else if (!defsrv && !strcmp(args[cur_arg], "check")) { |
| 679 | global.maxsock++; |
| 680 | do_check = 1; |
| 681 | cur_arg += 1; |
| 682 | } |
| 683 | else if (!defsrv && !strcmp(args[cur_arg], "disabled")) { |
Willy Tarreau | 2012521 | 2014-05-13 19:44:56 +0200 | [diff] [blame^] | 684 | newsrv->admin |= SRV_ADMF_FMAINT; |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 685 | newsrv->state &= ~SRV_STF_RUNNING; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 686 | newsrv->check.state |= CHK_ST_PAUSED; |
| 687 | newsrv->check.health = 0; |
| 688 | newsrv->agent.health = 0; |
| 689 | cur_arg += 1; |
| 690 | } |
| 691 | else if (!defsrv && !strcmp(args[cur_arg], "observe")) { |
| 692 | if (!strcmp(args[cur_arg + 1], "none")) |
| 693 | newsrv->observe = HANA_OBS_NONE; |
| 694 | else if (!strcmp(args[cur_arg + 1], "layer4")) |
| 695 | newsrv->observe = HANA_OBS_LAYER4; |
| 696 | else if (!strcmp(args[cur_arg + 1], "layer7")) { |
| 697 | if (curproxy->mode != PR_MODE_HTTP) { |
| 698 | Alert("parsing [%s:%d]: '%s' can only be used in http proxies.\n", |
| 699 | file, linenum, args[cur_arg + 1]); |
| 700 | err_code |= ERR_ALERT; |
| 701 | } |
| 702 | newsrv->observe = HANA_OBS_LAYER7; |
| 703 | } |
| 704 | else { |
| 705 | Alert("parsing [%s:%d]: '%s' expects one of 'none', " |
| 706 | "'layer4', 'layer7' but got '%s'\n", |
| 707 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 708 | err_code |= ERR_ALERT | ERR_FATAL; |
| 709 | goto out; |
| 710 | } |
| 711 | |
| 712 | cur_arg += 2; |
| 713 | } |
| 714 | else if (!strcmp(args[cur_arg], "on-error")) { |
| 715 | if (!strcmp(args[cur_arg + 1], "fastinter")) |
| 716 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 717 | else if (!strcmp(args[cur_arg + 1], "fail-check")) |
| 718 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 719 | else if (!strcmp(args[cur_arg + 1], "sudden-death")) |
| 720 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 721 | else if (!strcmp(args[cur_arg + 1], "mark-down")) |
| 722 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 723 | else { |
| 724 | Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', " |
| 725 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n", |
| 726 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 727 | err_code |= ERR_ALERT | ERR_FATAL; |
| 728 | goto out; |
| 729 | } |
| 730 | |
| 731 | cur_arg += 2; |
| 732 | } |
| 733 | else if (!strcmp(args[cur_arg], "on-marked-down")) { |
| 734 | if (!strcmp(args[cur_arg + 1], "shutdown-sessions")) |
| 735 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 736 | else { |
| 737 | Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n", |
| 738 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 739 | err_code |= ERR_ALERT | ERR_FATAL; |
| 740 | goto out; |
| 741 | } |
| 742 | |
| 743 | cur_arg += 2; |
| 744 | } |
| 745 | else if (!strcmp(args[cur_arg], "on-marked-up")) { |
| 746 | if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions")) |
| 747 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 748 | else { |
| 749 | Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n", |
| 750 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 751 | err_code |= ERR_ALERT | ERR_FATAL; |
| 752 | goto out; |
| 753 | } |
| 754 | |
| 755 | cur_arg += 2; |
| 756 | } |
| 757 | else if (!strcmp(args[cur_arg], "error-limit")) { |
| 758 | if (!*args[cur_arg + 1]) { |
| 759 | Alert("parsing [%s:%d]: '%s' expects an integer argument.\n", |
| 760 | file, linenum, args[cur_arg]); |
| 761 | err_code |= ERR_ALERT | ERR_FATAL; |
| 762 | goto out; |
| 763 | } |
| 764 | |
| 765 | newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]); |
| 766 | |
| 767 | if (newsrv->consecutive_errors_limit <= 0) { |
| 768 | Alert("parsing [%s:%d]: %s has to be > 0.\n", |
| 769 | file, linenum, args[cur_arg]); |
| 770 | err_code |= ERR_ALERT | ERR_FATAL; |
| 771 | goto out; |
| 772 | } |
| 773 | cur_arg += 2; |
| 774 | } |
| 775 | else if (!defsrv && !strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 776 | int port_low, port_high; |
| 777 | struct sockaddr_storage *sk; |
| 778 | struct protocol *proto; |
| 779 | |
| 780 | if (!*args[cur_arg + 1]) { |
| 781 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, and '%s' <name> as argument.\n", |
| 782 | file, linenum, "source", "usesrc", "interface"); |
| 783 | err_code |= ERR_ALERT | ERR_FATAL; |
| 784 | goto out; |
| 785 | } |
| 786 | |
| 787 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 788 | sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, &errmsg, NULL); |
| 789 | if (!sk) { |
| 790 | Alert("parsing [%s:%d] : '%s %s' : %s\n", |
| 791 | file, linenum, args[cur_arg], args[cur_arg+1], errmsg); |
| 792 | err_code |= ERR_ALERT | ERR_FATAL; |
| 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | proto = protocol_by_family(sk->ss_family); |
| 797 | if (!proto || !proto->connect) { |
| 798 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 799 | file, linenum, args[cur_arg], args[cur_arg+1]); |
| 800 | err_code |= ERR_ALERT | ERR_FATAL; |
| 801 | goto out; |
| 802 | } |
| 803 | |
| 804 | newsrv->conn_src.source_addr = *sk; |
| 805 | |
| 806 | if (port_low != port_high) { |
| 807 | int i; |
| 808 | |
| 809 | if (!port_low || !port_high) { |
| 810 | Alert("parsing [%s:%d] : %s does not support port offsets (found '%s').\n", |
| 811 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 812 | err_code |= ERR_ALERT | ERR_FATAL; |
| 813 | goto out; |
| 814 | } |
| 815 | |
| 816 | if (port_low <= 0 || port_low > 65535 || |
| 817 | port_high <= 0 || port_high > 65535 || |
| 818 | port_low > port_high) { |
| 819 | Alert("parsing [%s:%d] : invalid source port range %d-%d.\n", |
| 820 | file, linenum, port_low, port_high); |
| 821 | err_code |= ERR_ALERT | ERR_FATAL; |
| 822 | goto out; |
| 823 | } |
| 824 | newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1); |
| 825 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 826 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 827 | } |
| 828 | |
| 829 | cur_arg += 2; |
| 830 | while (*(args[cur_arg])) { |
| 831 | if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside */ |
| 832 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) |
| 833 | #if !defined(CONFIG_HAP_TRANSPARENT) |
Willy Tarreau | 9cf8d3f | 2014-05-09 22:56:10 +0200 | [diff] [blame] | 834 | if (!is_inet_addr(&newsrv->conn_src.source_addr)) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 835 | Alert("parsing [%s:%d] : '%s' requires an explicit '%s' address.\n", |
| 836 | file, linenum, "usesrc", "source"); |
| 837 | err_code |= ERR_ALERT | ERR_FATAL; |
| 838 | goto out; |
| 839 | } |
| 840 | #endif |
| 841 | if (!*args[cur_arg + 1]) { |
| 842 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>], 'client', 'clientip', or 'hdr_ip(name,#)' as argument.\n", |
| 843 | file, linenum, "usesrc"); |
| 844 | err_code |= ERR_ALERT | ERR_FATAL; |
| 845 | goto out; |
| 846 | } |
| 847 | if (!strcmp(args[cur_arg + 1], "client")) { |
| 848 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 849 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 850 | } else if (!strcmp(args[cur_arg + 1], "clientip")) { |
| 851 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 852 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 853 | } else if (!strncmp(args[cur_arg + 1], "hdr_ip(", 7)) { |
| 854 | char *name, *end; |
| 855 | |
| 856 | name = args[cur_arg+1] + 7; |
| 857 | while (isspace(*name)) |
| 858 | name++; |
| 859 | |
| 860 | end = name; |
| 861 | while (*end && !isspace(*end) && *end != ',' && *end != ')') |
| 862 | end++; |
| 863 | |
| 864 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 865 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 866 | newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1); |
| 867 | newsrv->conn_src.bind_hdr_len = end - name; |
| 868 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 869 | newsrv->conn_src.bind_hdr_name[end-name] = '\0'; |
| 870 | newsrv->conn_src.bind_hdr_occ = -1; |
| 871 | |
| 872 | /* now look for an occurrence number */ |
| 873 | while (isspace(*end)) |
| 874 | end++; |
| 875 | if (*end == ',') { |
| 876 | end++; |
| 877 | name = end; |
| 878 | if (*end == '-') |
| 879 | end++; |
| 880 | while (isdigit((int)*end)) |
| 881 | end++; |
| 882 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end-name); |
| 883 | } |
| 884 | |
| 885 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
| 886 | Alert("parsing [%s:%d] : usesrc hdr_ip(name,num) does not support negative" |
| 887 | " occurrences values smaller than %d.\n", |
| 888 | file, linenum, MAX_HDR_HISTORY); |
| 889 | err_code |= ERR_ALERT | ERR_FATAL; |
| 890 | goto out; |
| 891 | } |
| 892 | } else { |
| 893 | struct sockaddr_storage *sk; |
| 894 | int port1, port2; |
| 895 | |
| 896 | sk = str2sa_range(args[cur_arg + 1], &port1, &port2, &errmsg, NULL); |
| 897 | if (!sk) { |
| 898 | Alert("parsing [%s:%d] : '%s %s' : %s\n", |
| 899 | file, linenum, args[cur_arg], args[cur_arg+1], errmsg); |
| 900 | err_code |= ERR_ALERT | ERR_FATAL; |
| 901 | goto out; |
| 902 | } |
| 903 | |
| 904 | proto = protocol_by_family(sk->ss_family); |
| 905 | if (!proto || !proto->connect) { |
| 906 | Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n", |
| 907 | file, linenum, args[cur_arg], args[cur_arg+1]); |
| 908 | err_code |= ERR_ALERT | ERR_FATAL; |
| 909 | goto out; |
| 910 | } |
| 911 | |
| 912 | if (port1 != port2) { |
| 913 | Alert("parsing [%s:%d] : '%s' : port ranges and offsets are not allowed in '%s'\n", |
| 914 | file, linenum, args[cur_arg], args[cur_arg + 1]); |
| 915 | err_code |= ERR_ALERT | ERR_FATAL; |
| 916 | goto out; |
| 917 | } |
| 918 | newsrv->conn_src.tproxy_addr = *sk; |
| 919 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 920 | } |
| 921 | global.last_checks |= LSTCHK_NETADM; |
| 922 | #if !defined(CONFIG_HAP_TRANSPARENT) |
| 923 | global.last_checks |= LSTCHK_CTTPROXY; |
| 924 | #endif |
| 925 | cur_arg += 2; |
| 926 | continue; |
| 927 | #else /* no TPROXY support */ |
| 928 | Alert("parsing [%s:%d] : '%s' not allowed here because support for TPROXY was not compiled in.\n", |
| 929 | file, linenum, "usesrc"); |
| 930 | err_code |= ERR_ALERT | ERR_FATAL; |
| 931 | goto out; |
| 932 | #endif /* defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) */ |
| 933 | } /* "usesrc" */ |
| 934 | |
| 935 | if (!strcmp(args[cur_arg], "interface")) { /* specifically bind to this interface */ |
| 936 | #ifdef SO_BINDTODEVICE |
| 937 | if (!*args[cur_arg + 1]) { |
| 938 | Alert("parsing [%s:%d] : '%s' : missing interface name.\n", |
| 939 | file, linenum, args[0]); |
| 940 | err_code |= ERR_ALERT | ERR_FATAL; |
| 941 | goto out; |
| 942 | } |
| 943 | free(newsrv->conn_src.iface_name); |
| 944 | newsrv->conn_src.iface_name = strdup(args[cur_arg + 1]); |
| 945 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 946 | global.last_checks |= LSTCHK_NETADM; |
| 947 | #else |
| 948 | Alert("parsing [%s:%d] : '%s' : '%s' option not implemented.\n", |
| 949 | file, linenum, args[0], args[cur_arg]); |
| 950 | err_code |= ERR_ALERT | ERR_FATAL; |
| 951 | goto out; |
| 952 | #endif |
| 953 | cur_arg += 2; |
| 954 | continue; |
| 955 | } |
| 956 | /* this keyword in not an option of "source" */ |
| 957 | break; |
| 958 | } /* while */ |
| 959 | } |
| 960 | else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */ |
| 961 | Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n", |
| 962 | file, linenum, "usesrc", "source"); |
| 963 | err_code |= ERR_ALERT | ERR_FATAL; |
| 964 | goto out; |
| 965 | } |
| 966 | else { |
| 967 | static int srv_dumped; |
| 968 | struct srv_kw *kw; |
| 969 | char *err; |
| 970 | |
| 971 | kw = srv_find_kw(args[cur_arg]); |
| 972 | if (kw) { |
| 973 | char *err = NULL; |
| 974 | int code; |
| 975 | |
| 976 | if (!kw->parse) { |
| 977 | Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n", |
| 978 | file, linenum, args[0], args[1], args[cur_arg]); |
| 979 | cur_arg += 1 + kw->skip ; |
| 980 | err_code |= ERR_ALERT | ERR_FATAL; |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | if (defsrv && !kw->default_ok) { |
| 985 | Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n", |
| 986 | file, linenum, args[0], args[1], args[cur_arg]); |
| 987 | cur_arg += 1 + kw->skip ; |
| 988 | err_code |= ERR_ALERT; |
| 989 | continue; |
| 990 | } |
| 991 | |
| 992 | code = kw->parse(args, &cur_arg, curproxy, newsrv, &err); |
| 993 | err_code |= code; |
| 994 | |
| 995 | if (code) { |
| 996 | if (err && *err) { |
| 997 | indent_msg(&err, 2); |
| 998 | Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], err); |
| 999 | } |
| 1000 | else |
| 1001 | Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n", |
| 1002 | file, linenum, args[0], args[1], args[cur_arg]); |
| 1003 | if (code & ERR_FATAL) { |
| 1004 | free(err); |
| 1005 | cur_arg += 1 + kw->skip; |
| 1006 | goto out; |
| 1007 | } |
| 1008 | } |
| 1009 | free(err); |
| 1010 | cur_arg += 1 + kw->skip; |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | err = NULL; |
| 1015 | if (!srv_dumped) { |
| 1016 | srv_dump_kws(&err); |
| 1017 | indent_msg(&err, 4); |
| 1018 | srv_dumped = 1; |
| 1019 | } |
| 1020 | |
| 1021 | Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n", |
| 1022 | file, linenum, args[0], args[1], args[cur_arg], |
| 1023 | err ? " Registered keywords :" : "", err ? err : ""); |
| 1024 | free(err); |
| 1025 | |
| 1026 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1027 | goto out; |
| 1028 | } |
| 1029 | } |
| 1030 | |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1031 | if (do_check) { |
| 1032 | int ret; |
| 1033 | |
| 1034 | if (newsrv->trackit) { |
| 1035 | Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n", |
| 1036 | file, linenum); |
| 1037 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1038 | goto out; |
| 1039 | } |
| 1040 | |
| 1041 | /* If neither a port nor an addr was specified and no check transport |
| 1042 | * layer is forced, then the transport layer used by the checks is the |
| 1043 | * same as for the production traffic. Otherwise we use raw_sock by |
| 1044 | * default, unless one is specified. |
| 1045 | */ |
| 1046 | if (!newsrv->check.port && !is_addr(&newsrv->check_common.addr)) { |
| 1047 | #ifdef USE_OPENSSL |
| 1048 | newsrv->check.use_ssl |= (newsrv->use_ssl || (newsrv->proxy->options & PR_O_TCPCHK_SSL)); |
| 1049 | #endif |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1050 | newsrv->check.send_proxy |= (newsrv->pp_opts); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1051 | } |
| 1052 | /* try to get the port from check_core.addr if check.port not set */ |
| 1053 | if (!newsrv->check.port) |
| 1054 | newsrv->check.port = get_host_port(&newsrv->check_common.addr); |
| 1055 | |
| 1056 | if (!newsrv->check.port) |
| 1057 | newsrv->check.port = realport; /* by default */ |
| 1058 | |
| 1059 | if (!newsrv->check.port) { |
| 1060 | /* not yet valid, because no port was set on |
| 1061 | * the server either. We'll check if we have |
| 1062 | * a known port on the first listener. |
| 1063 | */ |
| 1064 | struct listener *l; |
| 1065 | |
| 1066 | list_for_each_entry(l, &curproxy->conf.listeners, by_fe) { |
| 1067 | newsrv->check.port = get_host_port(&l->addr); |
| 1068 | if (newsrv->check.port) |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | /* |
| 1073 | * We need at least a service port, a check port or the first tcp-check rule must |
Willy Tarreau | 5cf0b52 | 2014-05-09 23:59:19 +0200 | [diff] [blame] | 1074 | * be a 'connect' one when checking an IPv4/IPv6 server. |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1075 | */ |
Willy Tarreau | 5cf0b52 | 2014-05-09 23:59:19 +0200 | [diff] [blame] | 1076 | if (!newsrv->check.port && |
| 1077 | (is_inet_addr(&newsrv->check_common.addr) || |
| 1078 | (!is_addr(&newsrv->check_common.addr) && is_inet_addr(&newsrv->addr)))) { |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1079 | struct tcpcheck_rule *n = NULL, *r = NULL; |
| 1080 | struct list *l; |
| 1081 | |
| 1082 | r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n; |
| 1083 | if (!r) { |
| 1084 | Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n", |
| 1085 | file, linenum, newsrv->id); |
| 1086 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) { |
| 1090 | Alert("parsing [%s:%d] : server %s has neither service port nor check port nor tcp_check rule 'connect' with port information. Check has been disabled.\n", |
| 1091 | file, linenum, newsrv->id); |
| 1092 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1093 | goto out; |
| 1094 | } |
| 1095 | else { |
| 1096 | /* scan the tcp-check ruleset to ensure a port has been configured */ |
| 1097 | l = &newsrv->proxy->tcpcheck_rules; |
| 1098 | list_for_each_entry(n, l, list) { |
| 1099 | r = (struct tcpcheck_rule *)n->list.p; |
| 1100 | if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) { |
| 1101 | Alert("parsing [%s:%d] : server %s has neither service port nor check port, and a tcp_check rule 'connect' with no port information. Check has been disabled.\n", |
| 1102 | file, linenum, newsrv->id); |
| 1103 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1104 | goto out; |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | /* note: check type will be set during the config review phase */ |
| 1111 | ret = init_check(&newsrv->check, 0, file, linenum); |
| 1112 | if (ret) { |
| 1113 | err_code |= ret; |
| 1114 | goto out; |
| 1115 | } |
| 1116 | |
| 1117 | newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED; |
| 1118 | } |
| 1119 | |
| 1120 | if (do_agent) { |
| 1121 | int ret; |
| 1122 | |
| 1123 | if (!newsrv->agent.port) { |
| 1124 | Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n", |
| 1125 | file, linenum, newsrv->id); |
| 1126 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1127 | goto out; |
| 1128 | } |
| 1129 | |
| 1130 | if (!newsrv->agent.inter) |
| 1131 | newsrv->agent.inter = newsrv->check.inter; |
| 1132 | |
| 1133 | ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK, file, linenum); |
| 1134 | if (ret) { |
| 1135 | err_code |= ret; |
| 1136 | goto out; |
| 1137 | } |
| 1138 | |
| 1139 | newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT; |
| 1140 | } |
| 1141 | |
| 1142 | if (!defsrv) { |
Willy Tarreau | c93cd16 | 2014-05-13 15:54:22 +0200 | [diff] [blame] | 1143 | if (newsrv->flags & SRV_F_BACKUP) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1144 | curproxy->srv_bck++; |
| 1145 | else |
| 1146 | curproxy->srv_act++; |
| 1147 | |
Willy Tarreau | c5150da | 2014-05-13 19:27:31 +0200 | [diff] [blame] | 1148 | srv_lb_commit_status(newsrv); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | return 0; |
| 1152 | |
| 1153 | out: |
| 1154 | free(errmsg); |
| 1155 | return err_code; |
| 1156 | } |
| 1157 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1158 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1159 | * Local variables: |
| 1160 | * c-indent-level: 8 |
| 1161 | * c-basic-offset: 8 |
| 1162 | * End: |
| 1163 | */ |