Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Proxy variables and functions. |
| 3 | * |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <fcntl.h> |
| 14 | #include <unistd.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 15 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/defaults.h> |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 21 | #include <common/cfgparse.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 23 | #include <common/config.h> |
Willy Tarreau | d740bab | 2007-10-28 11:14:07 +0100 | [diff] [blame] | 24 | #include <common/errors.h> |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 25 | #include <common/memory.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
| 28 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 30 | #include <proto/backend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <proto/fd.h> |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 32 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/log.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 34 | #include <proto/protocols.h> |
| 35 | #include <proto/proto_tcp.h> |
Willy Tarreau | 39e4f62 | 2010-05-31 17:01:36 +0200 | [diff] [blame] | 36 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/proxy.h> |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 38 | #include <proto/signal.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
| 40 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 41 | int listeners; /* # of proxy listeners, set by cfgparse, unset by maintain_proxies */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
Willy Tarreau | 53fb4ae | 2009-10-04 23:04:08 +0200 | [diff] [blame] | 43 | struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 45 | /* |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 46 | * This function returns a string containing a name describing capabilities to |
| 47 | * report comprehensible error messages. Specifically, it will return the words |
| 48 | * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other |
| 49 | * cases including the proxies declared in "listen" mode. |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 50 | */ |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 51 | const char *proxy_cap_str(int cap) |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 52 | { |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 53 | if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) { |
| 54 | if (cap & PR_CAP_FE) |
| 55 | return "frontend"; |
| 56 | else if (cap & PR_CAP_BE) |
| 57 | return "backend"; |
| 58 | else if (cap & PR_CAP_RS) |
| 59 | return "ruleset"; |
| 60 | } |
| 61 | return "proxy"; |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 64 | /* |
| 65 | * This function returns a string containing the mode of the proxy in a format |
| 66 | * suitable for error messages. |
| 67 | */ |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 68 | const char *proxy_mode_str(int mode) { |
| 69 | |
| 70 | if (mode == PR_MODE_TCP) |
| 71 | return "tcp"; |
| 72 | else if (mode == PR_MODE_HTTP) |
| 73 | return "http"; |
| 74 | else if (mode == PR_MODE_HEALTH) |
| 75 | return "health"; |
| 76 | else |
| 77 | return "unknown"; |
| 78 | } |
| 79 | |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 80 | /* |
| 81 | * This function scans the list of backends and servers to retrieve the first |
| 82 | * backend and the first server with the given names, and sets them in both |
| 83 | * parameters. It returns zero if either is not found, or non-zero and sets |
| 84 | * the ones it did not found to NULL. If a NULL pointer is passed for the |
| 85 | * backend, only the pointer to the server will be updated. |
| 86 | */ |
| 87 | int get_backend_server(const char *bk_name, const char *sv_name, |
| 88 | struct proxy **bk, struct server **sv) |
| 89 | { |
| 90 | struct proxy *p; |
| 91 | struct server *s; |
Willy Tarreau | cfeaa47 | 2009-10-10 22:33:08 +0200 | [diff] [blame] | 92 | int pid, sid; |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 93 | |
| 94 | *sv = NULL; |
| 95 | |
Willy Tarreau | cfeaa47 | 2009-10-10 22:33:08 +0200 | [diff] [blame] | 96 | pid = 0; |
| 97 | if (*bk_name == '#') |
| 98 | pid = atoi(bk_name + 1); |
| 99 | sid = 0; |
| 100 | if (*sv_name == '#') |
| 101 | sid = atoi(sv_name + 1); |
| 102 | |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 103 | for (p = proxy; p; p = p->next) |
Willy Tarreau | cfeaa47 | 2009-10-10 22:33:08 +0200 | [diff] [blame] | 104 | if ((p->cap & PR_CAP_BE) && |
| 105 | ((pid && p->uuid == pid) || |
| 106 | (!pid && strcmp(p->id, bk_name) == 0))) |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 107 | break; |
| 108 | if (bk) |
| 109 | *bk = p; |
| 110 | if (!p) |
| 111 | return 0; |
| 112 | |
| 113 | for (s = p->srv; s; s = s->next) |
Willy Tarreau | cfeaa47 | 2009-10-10 22:33:08 +0200 | [diff] [blame] | 114 | if ((sid && s->puid == sid) || |
| 115 | (!sid && strcmp(s->id, sv_name) == 0)) |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 116 | break; |
| 117 | *sv = s; |
| 118 | if (!s) |
| 119 | return 0; |
| 120 | return 1; |
| 121 | } |
| 122 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 123 | /* This function parses a "timeout" statement in a proxy section. It returns |
| 124 | * -1 if there is any error, 1 for a warning, otherwise zero. If it does not |
| 125 | * return zero, it may write an error message into the <err> buffer, for at |
| 126 | * most <errlen> bytes, trailing zero included. The trailing '\n' must not |
| 127 | * be written. The function must be called with <args> pointing to the first |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 128 | * command line word, with <proxy> pointing to the proxy being parsed, and |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 129 | * <defpx> to the default proxy or NULL. As a special case for compatibility |
| 130 | * with older configs, it also accepts "{cli|srv|con}timeout" in args[0]. |
| 131 | */ |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 132 | static int proxy_parse_timeout(char **args, int section, struct proxy *proxy, |
| 133 | struct proxy *defpx, char *err, int errlen) |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 134 | { |
| 135 | unsigned timeout; |
| 136 | int retval, cap; |
| 137 | const char *res, *name; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 138 | int *tv = NULL; |
| 139 | int *td = NULL; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 140 | |
| 141 | retval = 0; |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 142 | |
| 143 | /* simply skip "timeout" but remain compatible with old form */ |
| 144 | if (strcmp(args[0], "timeout") == 0) |
| 145 | args++; |
| 146 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 147 | name = args[0]; |
| 148 | if (!strcmp(args[0], "client") || !strcmp(args[0], "clitimeout")) { |
| 149 | name = "client"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 150 | tv = &proxy->timeout.client; |
| 151 | td = &defpx->timeout.client; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 152 | cap = PR_CAP_FE; |
| 153 | } else if (!strcmp(args[0], "tarpit")) { |
| 154 | tv = &proxy->timeout.tarpit; |
| 155 | td = &defpx->timeout.tarpit; |
Willy Tarreau | 51c9bde | 2008-01-06 13:40:03 +0100 | [diff] [blame] | 156 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 157 | } else if (!strcmp(args[0], "http-keep-alive")) { |
| 158 | tv = &proxy->timeout.httpka; |
| 159 | td = &defpx->timeout.httpka; |
| 160 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 161 | } else if (!strcmp(args[0], "http-request")) { |
| 162 | tv = &proxy->timeout.httpreq; |
| 163 | td = &defpx->timeout.httpreq; |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 164 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 165 | } else if (!strcmp(args[0], "server") || !strcmp(args[0], "srvtimeout")) { |
| 166 | name = "server"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 167 | tv = &proxy->timeout.server; |
| 168 | td = &defpx->timeout.server; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 169 | cap = PR_CAP_BE; |
| 170 | } else if (!strcmp(args[0], "connect") || !strcmp(args[0], "contimeout")) { |
| 171 | name = "connect"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 172 | tv = &proxy->timeout.connect; |
| 173 | td = &defpx->timeout.connect; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 174 | cap = PR_CAP_BE; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 175 | } else if (!strcmp(args[0], "check")) { |
| 176 | tv = &proxy->timeout.check; |
| 177 | td = &defpx->timeout.check; |
| 178 | cap = PR_CAP_BE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 179 | } else if (!strcmp(args[0], "queue")) { |
| 180 | tv = &proxy->timeout.queue; |
| 181 | td = &defpx->timeout.queue; |
| 182 | cap = PR_CAP_BE; |
| 183 | } else { |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 184 | snprintf(err, errlen, |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 185 | "timeout '%s': must be 'client', 'server', 'connect', 'check', " |
Willy Tarreau | 76bfc95 | 2010-01-10 17:48:11 +0100 | [diff] [blame] | 186 | "'queue', 'http-keep-alive', 'http-request' or 'tarpit'", |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 187 | args[0]); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | if (*args[1] == 0) { |
| 192 | snprintf(err, errlen, "%s timeout expects an integer value (in milliseconds)", name); |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | res = parse_time_err(args[1], &timeout, TIME_UNIT_MS); |
| 197 | if (res) { |
Willy Tarreau | bb9251e | 2009-03-06 08:05:40 +0100 | [diff] [blame] | 198 | snprintf(err, errlen, "unexpected character '%c' in %s timeout", *res, name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | if (!(proxy->cap & cap)) { |
| 203 | snprintf(err, errlen, "%s timeout will be ignored because %s '%s' has no %s capability", |
| 204 | name, proxy_type_str(proxy), proxy->id, |
| 205 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
| 206 | retval = 1; |
| 207 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 208 | else if (defpx && *tv != *td) { |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 209 | snprintf(err, errlen, "overwriting %s timeout which was already specified", name); |
| 210 | retval = 1; |
| 211 | } |
| 212 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 213 | *tv = MS_TO_TICKS(timeout); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 214 | return retval; |
| 215 | } |
| 216 | |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 217 | /* This function parses a "rate-limit" statement in a proxy section. It returns |
| 218 | * -1 if there is any error, 1 for a warning, otherwise zero. If it does not |
| 219 | * return zero, it may write an error message into the <err> buffer, for at |
| 220 | * most <errlen> bytes, trailing zero included. The trailing '\n' must not |
| 221 | * be written. The function must be called with <args> pointing to the first |
| 222 | * command line word, with <proxy> pointing to the proxy being parsed, and |
| 223 | * <defpx> to the default proxy or NULL. |
| 224 | */ |
| 225 | static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy, |
| 226 | struct proxy *defpx, char *err, int errlen) |
| 227 | { |
| 228 | int retval, cap; |
| 229 | char *res, *name; |
| 230 | unsigned int *tv = NULL; |
| 231 | unsigned int *td = NULL; |
| 232 | unsigned int val; |
| 233 | |
| 234 | retval = 0; |
| 235 | |
| 236 | /* simply skip "rate-limit" */ |
| 237 | if (strcmp(args[0], "rate-limit") == 0) |
| 238 | args++; |
| 239 | |
| 240 | name = args[0]; |
| 241 | if (!strcmp(args[0], "sessions")) { |
| 242 | name = "sessions"; |
Willy Tarreau | 13a34bd | 2009-05-10 18:52:49 +0200 | [diff] [blame] | 243 | tv = &proxy->fe_sps_lim; |
| 244 | td = &defpx->fe_sps_lim; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 245 | cap = PR_CAP_FE; |
| 246 | } else { |
| 247 | snprintf(err, errlen, |
| 248 | "%s '%s': must be 'sessions'", |
| 249 | "rate-limit", args[0]); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | if (*args[1] == 0) { |
| 254 | snprintf(err, errlen, "%s %s expects expects an integer value (in sessions/second)", "rate-limit", name); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | val = strtoul(args[1], &res, 0); |
| 259 | if (*res) { |
| 260 | snprintf(err, errlen, "%s %s: unexpected character '%c' in integer value '%s'", "rate-limit", name, *res, args[1]); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | if (!(proxy->cap & cap)) { |
| 265 | snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability", |
| 266 | "rate-limit", name, proxy_type_str(proxy), proxy->id, |
| 267 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
| 268 | retval = 1; |
| 269 | } |
| 270 | else if (defpx && *tv != *td) { |
| 271 | snprintf(err, errlen, "overwriting %s %s which was already specified", "rate-limit", name); |
| 272 | retval = 1; |
| 273 | } |
| 274 | |
| 275 | *tv = val; |
| 276 | return retval; |
| 277 | } |
| 278 | |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 279 | /* |
| 280 | * This function finds a proxy with matching name, mode and with satisfying |
| 281 | * capabilities. It also checks if there are more matching proxies with |
| 282 | * requested name as this often leads into unexpected situations. |
| 283 | */ |
| 284 | |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 285 | struct proxy *findproxy_mode(const char *name, int mode, int cap) { |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 286 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 287 | struct proxy *curproxy, *target = NULL; |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 288 | |
| 289 | for (curproxy = proxy; curproxy; curproxy = curproxy->next) { |
| 290 | if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name)) |
| 291 | continue; |
| 292 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 293 | if (curproxy->mode != mode && |
| 294 | !(curproxy->mode == PR_MODE_HTTP && mode == PR_MODE_TCP)) { |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 295 | Alert("Unable to use proxy '%s' with wrong mode, required: %s, has: %s.\n", |
| 296 | name, proxy_mode_str(mode), proxy_mode_str(curproxy->mode)); |
| 297 | Alert("You may want to use 'mode %s'.\n", proxy_mode_str(mode)); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | if (!target) { |
| 302 | target = curproxy; |
| 303 | continue; |
| 304 | } |
| 305 | |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 306 | Alert("Refusing to use duplicated proxy '%s' with overlapping capabilities: %s/%s!\n", |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 307 | name, proxy_type_str(curproxy), proxy_type_str(target)); |
| 308 | |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | return target; |
| 313 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 314 | |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 315 | struct proxy *findproxy(const char *name, int cap) { |
| 316 | |
| 317 | struct proxy *curproxy, *target = NULL; |
| 318 | |
| 319 | for (curproxy = proxy; curproxy; curproxy = curproxy->next) { |
| 320 | if ((curproxy->cap & cap)!=cap || strcmp(curproxy->id, name)) |
| 321 | continue; |
| 322 | |
| 323 | if (!target) { |
| 324 | target = curproxy; |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | return target; |
| 332 | } |
| 333 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | /* |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 335 | * This function finds a server with matching name within selected proxy. |
| 336 | * It also checks if there are more matching servers with |
| 337 | * requested name as this often leads into unexpected situations. |
| 338 | */ |
| 339 | |
| 340 | struct server *findserver(const struct proxy *px, const char *name) { |
| 341 | |
| 342 | struct server *cursrv, *target = NULL; |
| 343 | |
| 344 | if (!px) |
| 345 | return NULL; |
| 346 | |
| 347 | for (cursrv = px->srv; cursrv; cursrv = cursrv->next) { |
| 348 | if (strcmp(cursrv->id, name)) |
| 349 | continue; |
| 350 | |
| 351 | if (!target) { |
| 352 | target = cursrv; |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | Alert("Refusing to use duplicated server '%s' fould in proxy: %s!\n", |
| 357 | name, px->id); |
| 358 | |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | return target; |
| 363 | } |
| 364 | |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 365 | /* This function checks that the designated proxy has no http directives |
| 366 | * enabled. It will output a warning if there are, and will fix some of them. |
| 367 | * It returns the number of fatal errors encountered. This should be called |
| 368 | * at the end of the configuration parsing if the proxy is not in http mode. |
| 369 | * The <file> argument is used to construct the error message. |
| 370 | */ |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 371 | int proxy_cfg_ensure_no_http(struct proxy *curproxy) |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 372 | { |
| 373 | if (curproxy->cookie_name != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 374 | Warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n", |
| 375 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 376 | } |
| 377 | if (curproxy->rsp_exp != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 378 | Warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n", |
| 379 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 380 | } |
| 381 | if (curproxy->req_exp != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 382 | Warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n", |
| 383 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 384 | } |
| 385 | if (curproxy->monitor_uri != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 386 | Warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n", |
| 387 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 388 | } |
Willy Tarreau | f3e49f9 | 2009-10-03 12:21:20 +0200 | [diff] [blame] | 389 | if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) { |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 390 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 391 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 392 | Warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n", |
| 393 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 394 | } |
Willy Tarreau | 1780416 | 2009-11-09 21:27:51 +0100 | [diff] [blame] | 395 | if (curproxy->to_log & (LW_REQ | LW_RESP)) { |
| 396 | curproxy->to_log &= ~(LW_REQ | LW_RESP); |
| 397 | Warning("config : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n", |
| 398 | proxy_type_str(curproxy), curproxy->id); |
| 399 | } |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 403 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 404 | * This function creates all proxy sockets. It should be done very early, |
| 405 | * typically before privileges are dropped. The sockets will be registered |
| 406 | * but not added to any fd_set, in order not to loose them across the fork(). |
| 407 | * The proxies also start in IDLE state, meaning that it will be |
| 408 | * maintain_proxies that will finally complete their loading. |
| 409 | * |
| 410 | * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 411 | * Retryable errors will only be printed if <verbose> is not zero. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | */ |
| 413 | int start_proxies(int verbose) |
| 414 | { |
| 415 | struct proxy *curproxy; |
| 416 | struct listener *listener; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 417 | int lerr, err = ERR_NONE; |
| 418 | int pxerr; |
| 419 | char msg[100]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 420 | |
| 421 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
| 422 | if (curproxy->state != PR_STNEW) |
| 423 | continue; /* already initialized */ |
| 424 | |
| 425 | pxerr = 0; |
| 426 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 427 | if (listener->state != LI_ASSIGNED) |
| 428 | continue; /* already started */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 429 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 430 | lerr = listener->proto->bind(listener, msg, sizeof(msg)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 431 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 432 | /* errors are reported if <verbose> is set or if they are fatal */ |
| 433 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 434 | if (lerr & ERR_ALERT) |
| 435 | Alert("Starting %s %s: %s\n", |
| 436 | proxy_type_str(curproxy), curproxy->id, msg); |
| 437 | else if (lerr & ERR_WARN) |
| 438 | Warning("Starting %s %s: %s\n", |
| 439 | proxy_type_str(curproxy), curproxy->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 442 | err |= lerr; |
| 443 | if (lerr & (ERR_ABORT | ERR_FATAL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 444 | pxerr |= 1; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 445 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 446 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 447 | else if (lerr & ERR_CODE) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 448 | pxerr |= 1; |
| 449 | continue; |
| 450 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (!pxerr) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 454 | curproxy->state = PR_STIDLE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 455 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
| 456 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 457 | |
| 458 | if (err & ERR_ABORT) |
| 459 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return err; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /* |
| 467 | * this function enables proxies when there are enough free sessions, |
| 468 | * or stops them when the table is full. It is designed to be called from the |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 469 | * select_loop(). It adjusts the date of next expiration event during stop |
| 470 | * time if appropriate. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 471 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 472 | void maintain_proxies(int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 473 | { |
| 474 | struct proxy *p; |
| 475 | struct listener *l; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 476 | unsigned int wait; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 477 | |
| 478 | p = proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 479 | |
| 480 | /* if there are enough free sessions, we'll activate proxies */ |
| 481 | if (actconn < global.maxconn) { |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 482 | for (; p; p = p->next) { |
| 483 | /* check the various reasons we may find to block the frontend */ |
| 484 | if (p->feconn >= p->maxconn) |
| 485 | goto do_block; |
| 486 | |
Willy Tarreau | 13a34bd | 2009-05-10 18:52:49 +0200 | [diff] [blame] | 487 | if (p->fe_sps_lim && |
Willy Tarreau | d9bbe17 | 2010-06-07 10:40:48 +0200 | [diff] [blame] | 488 | (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 1))) { |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 489 | /* we're blocking because a limit was reached on the number of |
| 490 | * requests/s on the frontend. We want to re-check ASAP, which |
Willy Tarreau | efcbc6e | 2009-03-06 08:27:10 +0100 | [diff] [blame] | 491 | * means in 1 ms before estimated expiration date, because the |
| 492 | * timer will have settled down. Note that we may already be in |
| 493 | * IDLE state here. |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 494 | */ |
Willy Tarreau | efcbc6e | 2009-03-06 08:27:10 +0100 | [diff] [blame] | 495 | *next = tick_first(*next, tick_add(now_ms, wait)); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 496 | goto do_block; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 497 | } |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 498 | |
| 499 | /* OK we have no reason to block, so let's unblock if we were blocking */ |
| 500 | if (p->state == PR_STIDLE) { |
| 501 | for (l = p->listen; l != NULL; l = l->next) |
| 502 | enable_listener(l); |
| 503 | p->state = PR_STRUN; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 504 | } |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 505 | continue; |
| 506 | |
| 507 | do_block: |
| 508 | if (p->state == PR_STRUN) { |
| 509 | for (l = p->listen; l != NULL; l = l->next) |
| 510 | disable_listener(l); |
| 511 | p->state = PR_STIDLE; |
| 512 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | else { /* block all proxies */ |
| 516 | while (p) { |
| 517 | if (p->state == PR_STRUN) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 518 | for (l = p->listen; l != NULL; l = l->next) |
| 519 | disable_listener(l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 520 | p->state = PR_STIDLE; |
| 521 | } |
| 522 | p = p->next; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (stopping) { |
| 527 | p = proxy; |
| 528 | while (p) { |
| 529 | if (p->state != PR_STSTOPPED) { |
| 530 | int t; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 531 | t = tick_remain(now_ms, p->stop_time); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 532 | if (t == 0) { |
Willy Tarreau | 1104614 | 2010-03-04 23:07:28 +0100 | [diff] [blame] | 533 | Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 534 | p->id, p->counters.cum_feconn, p->counters.cum_beconn); |
| 535 | send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 536 | p->id, p->counters.cum_feconn, p->counters.cum_beconn); |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 537 | stop_proxy(p); |
Willy Tarreau | 4d2d098 | 2007-05-14 00:39:29 +0200 | [diff] [blame] | 538 | /* try to free more memory */ |
| 539 | pool_gc2(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 540 | } |
| 541 | else { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 542 | *next = tick_first(*next, p->stop_time); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | p = p->next; |
| 546 | } |
| 547 | } |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 548 | return; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | |
| 552 | /* |
| 553 | * this function disables health-check servers so that the process will quickly be ignored |
| 554 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 555 | * time will not be used since it would already not listen anymore to the socket. |
| 556 | */ |
| 557 | void soft_stop(void) |
| 558 | { |
| 559 | struct proxy *p; |
| 560 | |
| 561 | stopping = 1; |
| 562 | p = proxy; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 563 | tv_update_date(0,1); /* else, the old time before select will be used */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 564 | while (p) { |
| 565 | if (p->state != PR_STSTOPPED) { |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 566 | Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); |
| 567 | send_log(p, LOG_WARNING, "Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 568 | p->stop_time = tick_add(now_ms, p->grace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 569 | } |
| 570 | p = p->next; |
| 571 | } |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 572 | /* signal zero is used to broadcast the "stopping" event */ |
| 573 | signal_handler(0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
| 577 | /* |
| 578 | * Linux unbinds the listen socket after a SHUT_RD, and ignores SHUT_WR. |
| 579 | * Solaris refuses either shutdown(). |
| 580 | * OpenBSD ignores SHUT_RD but closes upon SHUT_WR and refuses to rebind. |
| 581 | * So a common validation path involves SHUT_WR && listen && SHUT_RD. |
| 582 | * If disabling at least one listener returns an error, then the proxy |
| 583 | * state is set to PR_STERROR because we don't know how to resume from this. |
| 584 | */ |
| 585 | void pause_proxy(struct proxy *p) |
| 586 | { |
| 587 | struct listener *l; |
| 588 | for (l = p->listen; l != NULL; l = l->next) { |
| 589 | if (shutdown(l->fd, SHUT_WR) == 0 && |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 590 | listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0 && |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 591 | shutdown(l->fd, SHUT_RD) == 0) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 592 | EV_FD_CLR(l->fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 593 | if (p->state != PR_STERROR) |
| 594 | p->state = PR_STPAUSED; |
| 595 | } |
| 596 | else |
| 597 | p->state = PR_STERROR; |
| 598 | } |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
| 602 | /* |
| 603 | * This function completely stops a proxy and releases its listeners. It has |
| 604 | * to be called when going down in order to release the ports so that another |
| 605 | * process may bind to them. It must also be called on disabled proxies at the |
| 606 | * end of start-up. When all listeners are closed, the proxy is set to the |
| 607 | * PR_STSTOPPED state. |
| 608 | */ |
| 609 | void stop_proxy(struct proxy *p) |
| 610 | { |
| 611 | struct listener *l; |
| 612 | |
| 613 | for (l = p->listen; l != NULL; l = l->next) { |
| 614 | unbind_listener(l); |
| 615 | if (l->state >= LI_ASSIGNED) { |
| 616 | delete_listener(l); |
| 617 | listeners--; |
Willy Tarreau | af7ad00 | 2010-08-31 15:39:26 +0200 | [diff] [blame] | 618 | jobs--; |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | p->state = PR_STSTOPPED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* |
| 625 | * This function temporarily disables listening so that another new instance |
| 626 | * can start listening. It is designed to be called upon reception of a |
| 627 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
| 628 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 629 | */ |
| 630 | void pause_proxies(void) |
| 631 | { |
| 632 | int err; |
| 633 | struct proxy *p; |
| 634 | |
| 635 | err = 0; |
| 636 | p = proxy; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 637 | tv_update_date(0,1); /* else, the old time before select will be used */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | while (p) { |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 639 | if (p->cap & PR_CAP_FE && |
| 640 | p->state != PR_STERROR && |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 641 | p->state != PR_STSTOPPED && |
| 642 | p->state != PR_STPAUSED) { |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 643 | Warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 644 | send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 645 | pause_proxy(p); |
| 646 | if (p->state != PR_STPAUSED) { |
| 647 | err |= 1; |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 648 | Warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id); |
| 649 | send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | p = p->next; |
| 653 | } |
| 654 | if (err) { |
| 655 | Warning("Some proxies refused to pause, performing soft stop now.\n"); |
| 656 | send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n"); |
| 657 | soft_stop(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* |
| 663 | * This function reactivates listening. This can be used after a call to |
| 664 | * sig_pause(), for example when a new instance has failed starting up. |
| 665 | * It is designed to be called upon reception of a SIGTTIN. |
| 666 | */ |
| 667 | void listen_proxies(void) |
| 668 | { |
| 669 | struct proxy *p; |
| 670 | struct listener *l; |
| 671 | |
| 672 | p = proxy; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 673 | tv_update_date(0,1); /* else, the old time before select will be used */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 674 | while (p) { |
| 675 | if (p->state == PR_STPAUSED) { |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 676 | Warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 677 | send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 678 | |
| 679 | for (l = p->listen; l != NULL; l = l->next) { |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 680 | if (listen(l->fd, p->backlog ? p->backlog : p->maxconn) == 0) { |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 681 | if (actconn < global.maxconn && p->feconn < p->maxconn) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 682 | EV_FD_SET(l->fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 683 | p->state = PR_STRUN; |
| 684 | } |
| 685 | else |
| 686 | p->state = PR_STIDLE; |
| 687 | } else { |
| 688 | int port; |
| 689 | |
| 690 | if (l->addr.ss_family == AF_INET6) |
| 691 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 692 | else |
| 693 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 694 | |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 695 | Warning("Port %d busy while trying to enable %s %s.\n", |
| 696 | port, proxy_cap_str(p->cap), p->id); |
| 697 | send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n", |
| 698 | port, proxy_cap_str(p->cap), p->id); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 699 | /* Another port might have been enabled. Let's stop everything. */ |
| 700 | pause_proxy(p); |
| 701 | break; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | p = p->next; |
| 706 | } |
| 707 | } |
| 708 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 709 | /* Set current session's backend to <be>. Nothing is done if the |
| 710 | * session already had a backend assigned, which is indicated by |
| 711 | * s->flags & SN_BE_ASSIGNED. |
| 712 | * All flags, stats and counters which need be updated are updated. |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 713 | * Returns 1 if done, 0 in case of internal error, eg: lack of resource. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 714 | */ |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 715 | int session_set_backend(struct session *s, struct proxy *be) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 716 | { |
| 717 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 718 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 719 | s->be = be; |
| 720 | be->beconn++; |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 721 | if (be->beconn > be->counters.beconn_max) |
| 722 | be->counters.beconn_max = be->beconn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 723 | proxy_inc_be_ctr(be); |
| 724 | |
| 725 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 726 | s->si[1].flags &= ~SI_FL_INDEP_STR; |
| 727 | if (be->options2 & PR_O2_INDEPSTR) |
| 728 | s->si[1].flags |= SI_FL_INDEP_STR; |
| 729 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 730 | if (be->options2 & PR_O2_RSPBUG_OK) |
| 731 | s->txn.rsp.err_pos = -1; /* let buggy responses pass */ |
| 732 | s->flags |= SN_BE_ASSIGNED; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 733 | |
| 734 | /* If the target backend requires HTTP processing, we have to allocate |
| 735 | * a struct hdr_idx for it if we did not have one. |
| 736 | */ |
| 737 | if (unlikely(!s->txn.hdr_idx.v && (be->acl_requires & ACL_USE_L7_ANY))) { |
| 738 | if ((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL) |
| 739 | return 0; /* not enough memory */ |
Willy Tarreau | 39e4f62 | 2010-05-31 17:01:36 +0200 | [diff] [blame] | 740 | |
| 741 | /* and now initialize the HTTP transaction state */ |
| 742 | http_init_txn(s); |
| 743 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 744 | s->txn.hdr_idx.size = MAX_HTTP_HDR; |
| 745 | hdr_idx_init(&s->txn.hdr_idx); |
| 746 | } |
| 747 | |
Willy Tarreau | c1a2167 | 2009-08-16 22:37:44 +0200 | [diff] [blame] | 748 | /* We want to enable the backend-specific analysers except those which |
| 749 | * were already run as part of the frontend/listener. Note that it would |
| 750 | * be more reliable to store the list of analysers that have been run, |
| 751 | * but what we do here is OK for now. |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 752 | */ |
Willy Tarreau | c1a2167 | 2009-08-16 22:37:44 +0200 | [diff] [blame] | 753 | s->req->analysers |= be->be_req_ana & ~(s->listener->analysers); |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 754 | |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 755 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 758 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 759 | { CFG_LISTEN, "timeout", proxy_parse_timeout }, |
| 760 | { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, |
| 761 | { CFG_LISTEN, "contimeout", proxy_parse_timeout }, |
| 762 | { CFG_LISTEN, "srvtimeout", proxy_parse_timeout }, |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 763 | { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit }, |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 764 | { 0, NULL, NULL }, |
| 765 | }}; |
| 766 | |
| 767 | __attribute__((constructor)) |
| 768 | static void __proxy_module_init(void) |
| 769 | { |
| 770 | cfg_register_keywords(&cfg_kws); |
| 771 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 772 | |
| 773 | /* |
| 774 | * Local variables: |
| 775 | * c-indent-level: 8 |
| 776 | * c-basic-offset: 8 |
| 777 | * End: |
| 778 | */ |