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 | |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 28 | #include <eb32tree.h> |
| 29 | #include <ebistree.h> |
| 30 | |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 31 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <types/global.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 33 | #include <types/obj_type.h> |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 34 | #include <types/peers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 36 | #include <proto/backend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 38 | #include <proto/filters.h> |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 39 | #include <proto/hdr_idx.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 40 | #include <proto/listener.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | #include <proto/log.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 42 | #include <proto/proto_tcp.h> |
Willy Tarreau | 39e4f62 | 2010-05-31 17:01:36 +0200 | [diff] [blame] | 43 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/proxy.h> |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 45 | #include <proto/signal.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 46 | #include <proto/stream.h> |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 47 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
| 49 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 50 | int listeners; /* # of proxy listeners, set by cfgparse */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
Willy Tarreau | 53fb4ae | 2009-10-04 23:04:08 +0200 | [diff] [blame] | 52 | struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */ |
Willy Tarreau | f79d950 | 2014-03-15 07:22:35 +0100 | [diff] [blame] | 53 | struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */ |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 54 | unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 56 | /* |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 57 | * This function returns a string containing a name describing capabilities to |
| 58 | * report comprehensible error messages. Specifically, it will return the words |
| 59 | * "frontend", "backend", "ruleset" when appropriate, or "proxy" for all other |
| 60 | * cases including the proxies declared in "listen" mode. |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 61 | */ |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 62 | const char *proxy_cap_str(int cap) |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 63 | { |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 64 | if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) { |
| 65 | if (cap & PR_CAP_FE) |
| 66 | return "frontend"; |
| 67 | else if (cap & PR_CAP_BE) |
| 68 | return "backend"; |
| 69 | else if (cap & PR_CAP_RS) |
| 70 | return "ruleset"; |
| 71 | } |
| 72 | return "proxy"; |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 75 | /* |
| 76 | * This function returns a string containing the mode of the proxy in a format |
| 77 | * suitable for error messages. |
| 78 | */ |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 79 | const char *proxy_mode_str(int mode) { |
| 80 | |
| 81 | if (mode == PR_MODE_TCP) |
| 82 | return "tcp"; |
| 83 | else if (mode == PR_MODE_HTTP) |
| 84 | return "http"; |
| 85 | else if (mode == PR_MODE_HEALTH) |
| 86 | return "health"; |
| 87 | else |
| 88 | return "unknown"; |
| 89 | } |
| 90 | |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 91 | /* |
| 92 | * This function scans the list of backends and servers to retrieve the first |
| 93 | * backend and the first server with the given names, and sets them in both |
| 94 | * parameters. It returns zero if either is not found, or non-zero and sets |
| 95 | * the ones it did not found to NULL. If a NULL pointer is passed for the |
| 96 | * backend, only the pointer to the server will be updated. |
| 97 | */ |
| 98 | int get_backend_server(const char *bk_name, const char *sv_name, |
| 99 | struct proxy **bk, struct server **sv) |
| 100 | { |
| 101 | struct proxy *p; |
| 102 | struct server *s; |
Willy Tarreau | 7ecc420 | 2014-03-15 07:57:11 +0100 | [diff] [blame] | 103 | int sid; |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 104 | |
| 105 | *sv = NULL; |
| 106 | |
Willy Tarreau | 050536d | 2012-10-04 08:47:34 +0200 | [diff] [blame] | 107 | sid = -1; |
Willy Tarreau | cfeaa47 | 2009-10-10 22:33:08 +0200 | [diff] [blame] | 108 | if (*sv_name == '#') |
| 109 | sid = atoi(sv_name + 1); |
| 110 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 111 | p = proxy_be_by_name(bk_name); |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 112 | if (bk) |
| 113 | *bk = p; |
| 114 | if (!p) |
| 115 | return 0; |
| 116 | |
| 117 | for (s = p->srv; s; s = s->next) |
Willy Tarreau | 4055a10 | 2012-11-15 00:15:18 +0100 | [diff] [blame] | 118 | if ((sid >= 0 && s->puid == sid) || |
| 119 | (sid < 0 && strcmp(s->id, sv_name) == 0)) |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 120 | break; |
| 121 | *sv = s; |
| 122 | if (!s) |
| 123 | return 0; |
| 124 | return 1; |
| 125 | } |
| 126 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 127 | /* This function parses a "timeout" statement in a proxy section. It returns |
| 128 | * -1 if there is any error, 1 for a warning, otherwise zero. If it does not |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 129 | * return zero, it will write an error or warning message into a preallocated |
| 130 | * buffer returned at <err>. The trailing is not be written. The function must |
| 131 | * be called with <args> pointing to the first command line word, with <proxy> |
| 132 | * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL. |
| 133 | * As a special case for compatibility with older configs, it also accepts |
| 134 | * "{cli|srv|con}timeout" in args[0]. |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 135 | */ |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 136 | static int proxy_parse_timeout(char **args, int section, struct proxy *proxy, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 137 | struct proxy *defpx, const char *file, int line, |
| 138 | char **err) |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 139 | { |
| 140 | unsigned timeout; |
| 141 | int retval, cap; |
| 142 | const char *res, *name; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 143 | int *tv = NULL; |
| 144 | int *td = NULL; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 145 | int warn = 0; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 146 | |
| 147 | retval = 0; |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 148 | |
| 149 | /* simply skip "timeout" but remain compatible with old form */ |
| 150 | if (strcmp(args[0], "timeout") == 0) |
| 151 | args++; |
| 152 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 153 | name = args[0]; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 154 | if (!strcmp(args[0], "client") || (!strcmp(args[0], "clitimeout") && (warn = WARN_CLITO_DEPRECATED))) { |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 155 | name = "client"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 156 | tv = &proxy->timeout.client; |
| 157 | td = &defpx->timeout.client; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 158 | cap = PR_CAP_FE; |
| 159 | } else if (!strcmp(args[0], "tarpit")) { |
| 160 | tv = &proxy->timeout.tarpit; |
| 161 | td = &defpx->timeout.tarpit; |
Willy Tarreau | 51c9bde | 2008-01-06 13:40:03 +0100 | [diff] [blame] | 162 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 163 | } else if (!strcmp(args[0], "http-keep-alive")) { |
| 164 | tv = &proxy->timeout.httpka; |
| 165 | td = &defpx->timeout.httpka; |
| 166 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 167 | } else if (!strcmp(args[0], "http-request")) { |
| 168 | tv = &proxy->timeout.httpreq; |
| 169 | td = &defpx->timeout.httpreq; |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 170 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 171 | } else if (!strcmp(args[0], "server") || (!strcmp(args[0], "srvtimeout") && (warn = WARN_SRVTO_DEPRECATED))) { |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 172 | name = "server"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 173 | tv = &proxy->timeout.server; |
| 174 | td = &defpx->timeout.server; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 175 | cap = PR_CAP_BE; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 176 | } else if (!strcmp(args[0], "connect") || (!strcmp(args[0], "contimeout") && (warn = WARN_CONTO_DEPRECATED))) { |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 177 | name = "connect"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 178 | tv = &proxy->timeout.connect; |
| 179 | td = &defpx->timeout.connect; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 180 | cap = PR_CAP_BE; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 181 | } else if (!strcmp(args[0], "check")) { |
| 182 | tv = &proxy->timeout.check; |
| 183 | td = &defpx->timeout.check; |
| 184 | cap = PR_CAP_BE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 185 | } else if (!strcmp(args[0], "queue")) { |
| 186 | tv = &proxy->timeout.queue; |
| 187 | td = &defpx->timeout.queue; |
| 188 | cap = PR_CAP_BE; |
Willy Tarreau | ce887fd | 2012-05-12 12:50:00 +0200 | [diff] [blame] | 189 | } else if (!strcmp(args[0], "tunnel")) { |
| 190 | tv = &proxy->timeout.tunnel; |
| 191 | td = &defpx->timeout.tunnel; |
| 192 | cap = PR_CAP_BE; |
Willy Tarreau | 05cdd96 | 2014-05-10 14:30:07 +0200 | [diff] [blame] | 193 | } else if (!strcmp(args[0], "client-fin")) { |
| 194 | tv = &proxy->timeout.clientfin; |
| 195 | td = &defpx->timeout.clientfin; |
| 196 | cap = PR_CAP_FE; |
| 197 | } else if (!strcmp(args[0], "server-fin")) { |
| 198 | tv = &proxy->timeout.serverfin; |
| 199 | td = &defpx->timeout.serverfin; |
| 200 | cap = PR_CAP_BE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 201 | } else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 202 | memprintf(err, |
| 203 | "'timeout' supports 'client', 'server', 'connect', 'check', " |
Willy Tarreau | 05cdd96 | 2014-05-10 14:30:07 +0200 | [diff] [blame] | 204 | "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', " |
| 205 | "'client-fin' and 'server-fin' (got '%s')", |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 206 | args[0]); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | if (*args[1] == 0) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 211 | memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | res = parse_time_err(args[1], &timeout, TIME_UNIT_MS); |
| 216 | if (res) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 217 | memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | if (!(proxy->cap & cap)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 222 | memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability", |
| 223 | name, proxy_type_str(proxy), proxy->id, |
| 224 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 225 | retval = 1; |
| 226 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 227 | else if (defpx && *tv != *td) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 228 | memprintf(err, "overwriting 'timeout %s' which was already specified", name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 229 | retval = 1; |
| 230 | } |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 231 | else if (warn) { |
| 232 | if (!already_warned(warn)) { |
| 233 | memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.", |
| 234 | args[0], name); |
| 235 | retval = 1; |
| 236 | } |
| 237 | } |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 238 | |
Willy Tarreau | fac5b59 | 2014-05-22 08:24:46 +0200 | [diff] [blame] | 239 | if (*args[2] != 0) { |
| 240 | memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]); |
| 241 | retval = -1; |
| 242 | } |
| 243 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 244 | *tv = MS_TO_TICKS(timeout); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 245 | return retval; |
| 246 | } |
| 247 | |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 248 | /* This function parses a "rate-limit" statement in a proxy section. It returns |
| 249 | * -1 if there is any error, 1 for a warning, otherwise zero. If it does not |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 250 | * return zero, it will write an error or warning message into a preallocated |
| 251 | * buffer returned at <err>. The function must be called with <args> pointing |
| 252 | * to the first command line word, with <proxy> pointing to the proxy being |
| 253 | * parsed, and <defpx> to the default proxy or NULL. |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 254 | */ |
| 255 | static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 256 | struct proxy *defpx, const char *file, int line, |
| 257 | char **err) |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 258 | { |
| 259 | int retval, cap; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 260 | char *res; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 261 | unsigned int *tv = NULL; |
| 262 | unsigned int *td = NULL; |
| 263 | unsigned int val; |
| 264 | |
| 265 | retval = 0; |
| 266 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 267 | if (strcmp(args[1], "sessions") == 0) { |
Willy Tarreau | 13a34bd | 2009-05-10 18:52:49 +0200 | [diff] [blame] | 268 | tv = &proxy->fe_sps_lim; |
| 269 | td = &defpx->fe_sps_lim; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 270 | cap = PR_CAP_FE; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 271 | } |
| 272 | else { |
| 273 | memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 277 | if (*args[2] == 0) { |
| 278 | memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 279 | return -1; |
| 280 | } |
| 281 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 282 | val = strtoul(args[2], &res, 0); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 283 | if (*res) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 284 | memprintf(err, "'%s %s' : unexpected character '%c' in integer value '%s'", args[0], args[1], *res, args[2]); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | if (!(proxy->cap & cap)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 289 | memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability", |
| 290 | args[0], args[1], proxy_type_str(proxy), proxy->id, |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 291 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
| 292 | retval = 1; |
| 293 | } |
| 294 | else if (defpx && *tv != *td) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 295 | memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 296 | retval = 1; |
| 297 | } |
| 298 | |
| 299 | *tv = val; |
| 300 | return retval; |
| 301 | } |
| 302 | |
Willy Tarreau | c35362a | 2014-04-25 13:58:37 +0200 | [diff] [blame] | 303 | /* This function parses a "max-keep-alive-queue" statement in a proxy section. |
| 304 | * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it |
| 305 | * does not return zero, it will write an error or warning message into a |
| 306 | * preallocated buffer returned at <err>. The function must be called with |
| 307 | * <args> pointing to the first command line word, with <proxy> pointing to |
| 308 | * the proxy being parsed, and <defpx> to the default proxy or NULL. |
| 309 | */ |
| 310 | static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy, |
| 311 | struct proxy *defpx, const char *file, int line, |
| 312 | char **err) |
| 313 | { |
| 314 | int retval; |
| 315 | char *res; |
| 316 | unsigned int val; |
| 317 | |
| 318 | retval = 0; |
| 319 | |
| 320 | if (*args[1] == 0) { |
| 321 | memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | val = strtol(args[1], &res, 0); |
| 326 | if (*res) { |
| 327 | memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | if (!(proxy->cap & PR_CAP_BE)) { |
| 332 | memprintf(err, "%s will be ignored because %s '%s' has no backend capability", |
| 333 | args[0], proxy_type_str(proxy), proxy->id); |
| 334 | retval = 1; |
| 335 | } |
| 336 | |
| 337 | /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */ |
| 338 | proxy->max_ka_queue = val + 1; |
| 339 | return retval; |
| 340 | } |
| 341 | |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 342 | /* This function parses a "declare" statement in a proxy section. It returns -1 |
| 343 | * if there is any error, 1 for warning, otherwise 0. If it does not return zero, |
| 344 | * it will write an error or warning message into a preallocated buffer returned |
| 345 | * at <err>. The function must be called with <args> pointing to the first command |
| 346 | * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the |
| 347 | * default proxy or NULL. |
| 348 | */ |
| 349 | static int proxy_parse_declare(char **args, int section, struct proxy *curpx, |
| 350 | struct proxy *defpx, const char *file, int line, |
| 351 | char **err) |
| 352 | { |
| 353 | /* Capture keyword wannot be declared in a default proxy. */ |
| 354 | if (curpx == defpx) { |
| 355 | memprintf(err, "'%s' not avalaible in default section", args[0]); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | /* Capture keywork is only avalaible in frontend. */ |
| 360 | if (!(curpx->cap & PR_CAP_FE)) { |
| 361 | memprintf(err, "'%s' only avalaible in frontend or listen section", args[0]); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | /* Check mandatory second keyword. */ |
| 366 | if (!args[1] || !*args[1]) { |
| 367 | memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | /* Actually, declare is only avalaible for declaring capture |
| 372 | * slot, but in the future it can declare maps or variables. |
| 373 | * So, this section permits to check and switch acording with |
| 374 | * the second keyword. |
| 375 | */ |
| 376 | if (strcmp(args[1], "capture") == 0) { |
| 377 | char *error = NULL; |
| 378 | long len; |
| 379 | struct cap_hdr *hdr; |
| 380 | |
| 381 | /* Check the next keyword. */ |
| 382 | if (!args[2] || !*args[2] || |
| 383 | (strcmp(args[2], "response") != 0 && |
| 384 | strcmp(args[2], "request") != 0)) { |
| 385 | memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | /* Check the 'len' keyword. */ |
| 390 | if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) { |
| 391 | memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | /* Check the length value. */ |
| 396 | if (!args[4] || !*args[4]) { |
| 397 | memprintf(err, "'%s %s': 'len' requires a numeric value that represents the " |
| 398 | "capture length", |
| 399 | args[0], args[1]); |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | /* convert the length value. */ |
| 404 | len = strtol(args[4], &error, 10); |
| 405 | if (*error != '\0') { |
| 406 | memprintf(err, "'%s %s': cannot parse the length '%s'.", |
| 407 | args[0], args[1], args[3]); |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | /* check length. */ |
| 412 | if (len <= 0) { |
| 413 | memprintf(err, "length must be > 0"); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | /* register the capture. */ |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 418 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 419 | hdr->name = NULL; /* not a header capture */ |
| 420 | hdr->namelen = 0; |
| 421 | hdr->len = len; |
| 422 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 423 | |
| 424 | if (strcmp(args[2], "request") == 0) { |
| 425 | hdr->next = curpx->req_cap; |
| 426 | hdr->index = curpx->nb_req_cap++; |
| 427 | curpx->req_cap = hdr; |
| 428 | } |
| 429 | if (strcmp(args[2], "response") == 0) { |
| 430 | hdr->next = curpx->rsp_cap; |
| 431 | hdr->index = curpx->nb_rsp_cap++; |
| 432 | curpx->rsp_cap = hdr; |
| 433 | } |
| 434 | return 0; |
| 435 | } |
| 436 | else { |
| 437 | memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]); |
| 438 | return -1; |
| 439 | } |
| 440 | } |
| 441 | |
Willy Tarreau | f79d950 | 2014-03-15 07:22:35 +0100 | [diff] [blame] | 442 | /* This function inserts proxy <px> into the tree of known proxies. The proxy's |
| 443 | * name is used as the storing key so it must already have been initialized. |
| 444 | */ |
| 445 | void proxy_store_name(struct proxy *px) |
| 446 | { |
| 447 | px->conf.by_name.key = px->id; |
| 448 | ebis_insert(&proxy_by_name, &px->conf.by_name); |
| 449 | } |
| 450 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 451 | /* Returns a pointer to the first proxy matching capabilities <cap> and id |
| 452 | * <id>. NULL is returned if no match is found. If <table> is non-zero, it |
| 453 | * only considers proxies having a table. |
Willy Tarreau | bc216c4 | 2011-08-02 11:25:54 +0200 | [diff] [blame] | 454 | */ |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 455 | struct proxy *proxy_find_by_id(int id, int cap, int table) |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 456 | { |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 457 | struct eb32_node *n; |
Willy Tarreau | bc216c4 | 2011-08-02 11:25:54 +0200 | [diff] [blame] | 458 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 459 | for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) { |
| 460 | struct proxy *px = container_of(n, struct proxy, conf.id); |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 461 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 462 | if (px->uuid != id) |
| 463 | break; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 464 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 465 | if ((px->cap & cap) != cap) |
| 466 | continue; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 467 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 468 | if (table && !px->table.size) |
| 469 | continue; |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 470 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 471 | return px; |
| 472 | } |
| 473 | return NULL; |
| 474 | } |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 475 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 476 | /* Returns a pointer to the first proxy matching either name <name>, or id |
| 477 | * <name> if <name> begins with a '#'. NULL is returned if no match is found. |
| 478 | * If <table> is non-zero, it only considers proxies having a table. |
| 479 | */ |
| 480 | struct proxy *proxy_find_by_name(const char *name, int cap, int table) |
| 481 | { |
| 482 | struct proxy *curproxy; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 483 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 484 | if (*name == '#') { |
| 485 | curproxy = proxy_find_by_id(atoi(name + 1), cap, table); |
| 486 | if (curproxy) |
Willy Tarreau | c739aa8 | 2015-05-26 11:35:41 +0200 | [diff] [blame] | 487 | return curproxy; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 488 | } |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 489 | else { |
| 490 | struct ebpt_node *node; |
| 491 | |
| 492 | for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) { |
| 493 | curproxy = container_of(node, struct proxy, conf.by_name); |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 494 | |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 495 | if (strcmp(curproxy->id, name) != 0) |
| 496 | break; |
| 497 | |
| 498 | if ((curproxy->cap & cap) != cap) |
| 499 | continue; |
| 500 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 501 | if (table && !curproxy->table.size) |
| 502 | continue; |
| 503 | |
Willy Tarreau | c739aa8 | 2015-05-26 11:35:41 +0200 | [diff] [blame] | 504 | return curproxy; |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 505 | } |
| 506 | } |
Willy Tarreau | c739aa8 | 2015-05-26 11:35:41 +0200 | [diff] [blame] | 507 | return NULL; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 510 | /* Finds the best match for a proxy with capabilities <cap>, name <name> and id |
| 511 | * <id>. At most one of <id> or <name> may be different provided that <cap> is |
| 512 | * valid. Either <id> or <name> may be left unspecified (0). The purpose is to |
| 513 | * find a proxy based on some information from a previous configuration, across |
| 514 | * reloads or during information exchange between peers. |
| 515 | * |
| 516 | * Names are looked up first if present, then IDs are compared if present. In |
| 517 | * case of an inexact match whatever is forced in the configuration has |
| 518 | * precedence in the following order : |
| 519 | * - 1) forced ID (proves a renaming / change of proxy type) |
| 520 | * - 2) proxy name+type (may indicate a move if ID differs) |
| 521 | * - 3) automatic ID+type (may indicate a renaming) |
| 522 | * |
| 523 | * Depending on what is found, we can end up in the following situations : |
| 524 | * |
| 525 | * name id cap | possible causes |
| 526 | * -------------+----------------- |
| 527 | * -- -- -- | nothing found |
| 528 | * -- -- ok | nothing found |
| 529 | * -- ok -- | proxy deleted, ID points to next one |
| 530 | * -- ok ok | proxy renamed, or deleted with ID pointing to next one |
| 531 | * ok -- -- | proxy deleted, but other half with same name still here (before) |
| 532 | * ok -- ok | proxy's ID changed (proxy moved in the config file) |
| 533 | * ok ok -- | proxy deleted, but other half with same name still here (after) |
| 534 | * ok ok ok | perfect match |
| 535 | * |
| 536 | * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits : |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 537 | * - PR_FBM_MISMATCH_ID : proxy was found but ID differs |
| 538 | * (and ID was not zero) |
| 539 | * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs |
| 540 | * (and name was not NULL) |
| 541 | * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with |
| 542 | * the same name and/or id |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 543 | * |
| 544 | * Only a valid proxy is returned. If capabilities do not match, NULL is |
| 545 | * returned. The caller can check <diff> to report detailed warnings / errors, |
| 546 | * and decide whether or not to use what was found. |
| 547 | */ |
| 548 | struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff) |
| 549 | { |
| 550 | struct proxy *byname; |
| 551 | struct proxy *byid; |
| 552 | |
| 553 | if (!name && !id) |
| 554 | return NULL; |
| 555 | |
| 556 | if (diff) |
| 557 | *diff = 0; |
| 558 | |
| 559 | byname = byid = NULL; |
| 560 | |
| 561 | if (name) { |
| 562 | byname = proxy_find_by_name(name, cap, 0); |
| 563 | if (byname && (!id || byname->uuid == id)) |
| 564 | return byname; |
| 565 | } |
| 566 | |
| 567 | /* remaining possiblities : |
| 568 | * - name not set |
| 569 | * - name set but not found |
| 570 | * - name found, but ID doesn't match. |
| 571 | */ |
| 572 | if (id) { |
| 573 | byid = proxy_find_by_id(id, cap, 0); |
| 574 | if (byid) { |
| 575 | if (byname) { |
| 576 | /* id+type found, name+type found, but not all 3. |
| 577 | * ID wins only if forced, otherwise name wins. |
| 578 | */ |
| 579 | if (byid->options & PR_O_FORCED_ID) { |
| 580 | if (diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 581 | *diff |= PR_FBM_MISMATCH_NAME; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 582 | return byid; |
| 583 | } |
| 584 | else { |
| 585 | if (diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 586 | *diff |= PR_FBM_MISMATCH_ID; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 587 | return byname; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* remaining possiblities : |
| 592 | * - name not set |
| 593 | * - name set but not found |
| 594 | */ |
| 595 | if (name && diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 596 | *diff |= PR_FBM_MISMATCH_NAME; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 597 | return byid; |
| 598 | } |
| 599 | |
| 600 | /* ID not found */ |
| 601 | if (byname) { |
| 602 | if (diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 603 | *diff |= PR_FBM_MISMATCH_ID; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 604 | return byname; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* All remaining possiblities will lead to NULL. If we can report more |
| 609 | * detailed information to the caller about changed types and/or name, |
| 610 | * we'll do it. For example, we could detect that "listen foo" was |
| 611 | * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced. |
| 612 | * - name not set, ID not found |
| 613 | * - name not found, ID not set |
| 614 | * - name not found, ID not found |
| 615 | */ |
| 616 | if (!diff) |
| 617 | return NULL; |
| 618 | |
| 619 | if (name) { |
| 620 | byname = proxy_find_by_name(name, 0, 0); |
| 621 | if (byname && (!id || byname->uuid == id)) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 622 | *diff |= PR_FBM_MISMATCH_PROXYTYPE; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | if (id) { |
| 626 | byid = proxy_find_by_id(id, 0, 0); |
| 627 | if (byid) { |
| 628 | if (!name) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 629 | *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */ |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 630 | else if (byid->options & PR_O_FORCED_ID) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 631 | *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */ |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 632 | /* otherwise it's a different proxy that was returned */ |
| 633 | } |
| 634 | } |
| 635 | return NULL; |
| 636 | } |
| 637 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | /* |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 639 | * This function finds a server with matching name within selected proxy. |
| 640 | * It also checks if there are more matching servers with |
| 641 | * requested name as this often leads into unexpected situations. |
| 642 | */ |
| 643 | |
| 644 | struct server *findserver(const struct proxy *px, const char *name) { |
| 645 | |
| 646 | struct server *cursrv, *target = NULL; |
| 647 | |
| 648 | if (!px) |
| 649 | return NULL; |
| 650 | |
| 651 | for (cursrv = px->srv; cursrv; cursrv = cursrv->next) { |
| 652 | if (strcmp(cursrv->id, name)) |
| 653 | continue; |
| 654 | |
| 655 | if (!target) { |
| 656 | target = cursrv; |
| 657 | continue; |
| 658 | } |
| 659 | |
Cyril Bonté | 0bb519e | 2012-04-04 12:57:19 +0200 | [diff] [blame] | 660 | Alert("Refusing to use duplicated server '%s' found in proxy: %s!\n", |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 661 | name, px->id); |
| 662 | |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | return target; |
| 667 | } |
| 668 | |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 669 | /* This function checks that the designated proxy has no http directives |
| 670 | * enabled. It will output a warning if there are, and will fix some of them. |
| 671 | * It returns the number of fatal errors encountered. This should be called |
| 672 | * at the end of the configuration parsing if the proxy is not in http mode. |
| 673 | * The <file> argument is used to construct the error message. |
| 674 | */ |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 675 | int proxy_cfg_ensure_no_http(struct proxy *curproxy) |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 676 | { |
| 677 | if (curproxy->cookie_name != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 678 | Warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n", |
| 679 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 680 | } |
| 681 | if (curproxy->rsp_exp != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 682 | Warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n", |
| 683 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 684 | } |
| 685 | if (curproxy->req_exp != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 686 | Warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n", |
| 687 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 688 | } |
| 689 | if (curproxy->monitor_uri != NULL) { |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 690 | Warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n", |
| 691 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 692 | } |
Willy Tarreau | f3e49f9 | 2009-10-03 12:21:20 +0200 | [diff] [blame] | 693 | if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) { |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 694 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 695 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 696 | Warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n", |
| 697 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 698 | } |
Willy Tarreau | 1780416 | 2009-11-09 21:27:51 +0100 | [diff] [blame] | 699 | if (curproxy->to_log & (LW_REQ | LW_RESP)) { |
| 700 | curproxy->to_log &= ~(LW_REQ | LW_RESP); |
Willy Tarreau | 59ad1a2 | 2014-01-29 14:39:58 +0100 | [diff] [blame] | 701 | Warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n", |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 702 | curproxy->conf.lfs_file, curproxy->conf.lfs_line, |
Willy Tarreau | 1780416 | 2009-11-09 21:27:51 +0100 | [diff] [blame] | 703 | proxy_type_str(curproxy), curproxy->id); |
| 704 | } |
Willy Tarreau | 62a6123 | 2013-04-12 18:13:46 +0200 | [diff] [blame] | 705 | if (curproxy->conf.logformat_string == default_http_log_format || |
| 706 | curproxy->conf.logformat_string == clf_http_log_format) { |
| 707 | /* Note: we don't change the directive's file:line number */ |
| 708 | curproxy->conf.logformat_string = default_tcp_log_format; |
| 709 | Warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n", |
| 710 | curproxy->conf.lfs_file, curproxy->conf.lfs_line, |
Willy Tarreau | 196729e | 2012-05-31 19:30:26 +0200 | [diff] [blame] | 711 | proxy_type_str(curproxy), curproxy->id); |
| 712 | } |
| 713 | |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 717 | /* Perform the most basic initialization of a proxy : |
| 718 | * memset(), list_init(*), reset_timeouts(*). |
Willy Tarreau | b249e84 | 2011-09-07 18:41:08 +0200 | [diff] [blame] | 719 | * Any new proxy or peer should be initialized via this function. |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 720 | */ |
| 721 | void init_new_proxy(struct proxy *p) |
| 722 | { |
| 723 | memset(p, 0, sizeof(struct proxy)); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 724 | p->obj_type = OBJ_TYPE_PROXY; |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 725 | LIST_INIT(&p->pendconns); |
| 726 | LIST_INIT(&p->acl); |
| 727 | LIST_INIT(&p->http_req_rules); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 728 | LIST_INIT(&p->http_res_rules); |
Willy Tarreau | 353bc9f | 2014-04-28 22:05:31 +0200 | [diff] [blame] | 729 | LIST_INIT(&p->block_rules); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 730 | LIST_INIT(&p->redirect_rules); |
| 731 | LIST_INIT(&p->mon_fail_cond); |
| 732 | LIST_INIT(&p->switching_rules); |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 733 | LIST_INIT(&p->server_rules); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 734 | LIST_INIT(&p->persist_rules); |
| 735 | LIST_INIT(&p->sticking_rules); |
| 736 | LIST_INIT(&p->storersp_rules); |
| 737 | LIST_INIT(&p->tcp_req.inspect_rules); |
| 738 | LIST_INIT(&p->tcp_rep.inspect_rules); |
| 739 | LIST_INIT(&p->tcp_req.l4_rules); |
| 740 | LIST_INIT(&p->req_add); |
| 741 | LIST_INIT(&p->rsp_add); |
| 742 | LIST_INIT(&p->listener_queue); |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 743 | LIST_INIT(&p->logsrvs); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 744 | LIST_INIT(&p->logformat); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 745 | LIST_INIT(&p->logformat_sd); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 746 | LIST_INIT(&p->format_unique_id); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 747 | LIST_INIT(&p->conf.bind); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 748 | LIST_INIT(&p->conf.listeners); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 749 | LIST_INIT(&p->conf.args.list); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 750 | LIST_INIT(&p->tcpcheck_rules); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 751 | LIST_INIT(&p->filter_configs); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 752 | |
| 753 | /* Timeouts are defined as -1 */ |
| 754 | proxy_reset_timeouts(p); |
| 755 | p->tcp_rep.inspect_delay = TICK_ETERNITY; |
Willy Tarreau | 050536d | 2012-10-04 08:47:34 +0200 | [diff] [blame] | 756 | |
| 757 | /* initial uuid is unassigned (-1) */ |
| 758 | p->uuid = -1; |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 761 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 762 | * This function creates all proxy sockets. It should be done very early, |
| 763 | * typically before privileges are dropped. The sockets will be registered |
| 764 | * but not added to any fd_set, in order not to loose them across the fork(). |
Willy Tarreau | 562515c | 2011-07-25 08:11:52 +0200 | [diff] [blame] | 765 | * The proxies also start in READY state because they all have their listeners |
Willy Tarreau | f3f8c70 | 2011-07-25 07:37:28 +0200 | [diff] [blame] | 766 | * bound. |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 767 | * |
| 768 | * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 769 | * Retryable errors will only be printed if <verbose> is not zero. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 770 | */ |
| 771 | int start_proxies(int verbose) |
| 772 | { |
| 773 | struct proxy *curproxy; |
| 774 | struct listener *listener; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 775 | int lerr, err = ERR_NONE; |
| 776 | int pxerr; |
| 777 | char msg[100]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 778 | |
| 779 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
| 780 | if (curproxy->state != PR_STNEW) |
| 781 | continue; /* already initialized */ |
| 782 | |
| 783 | pxerr = 0; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 784 | list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 785 | if (listener->state != LI_ASSIGNED) |
| 786 | continue; /* already started */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 787 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 788 | lerr = listener->proto->bind(listener, msg, sizeof(msg)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 789 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 790 | /* errors are reported if <verbose> is set or if they are fatal */ |
| 791 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 792 | if (lerr & ERR_ALERT) |
| 793 | Alert("Starting %s %s: %s\n", |
| 794 | proxy_type_str(curproxy), curproxy->id, msg); |
| 795 | else if (lerr & ERR_WARN) |
| 796 | Warning("Starting %s %s: %s\n", |
| 797 | proxy_type_str(curproxy), curproxy->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 800 | err |= lerr; |
| 801 | if (lerr & (ERR_ABORT | ERR_FATAL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 802 | pxerr |= 1; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 803 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 804 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 805 | else if (lerr & ERR_CODE) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 806 | pxerr |= 1; |
| 807 | continue; |
| 808 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | if (!pxerr) { |
Willy Tarreau | 562515c | 2011-07-25 08:11:52 +0200 | [diff] [blame] | 812 | curproxy->state = PR_STREADY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 813 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
| 814 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 815 | |
| 816 | if (err & ERR_ABORT) |
| 817 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | return err; |
| 821 | } |
| 822 | |
| 823 | |
| 824 | /* |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 825 | * This is the proxy management task. It enables proxies when there are enough |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 826 | * free streams, or stops them when the table is full. It is designed to be |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 827 | * called as a task which is woken up upon stopping or when rate limiting must |
| 828 | * be enforced. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 829 | */ |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 830 | struct task *manage_proxy(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 831 | { |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 832 | struct proxy *p = t->context; |
| 833 | int next = TICK_ETERNITY; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 834 | unsigned int wait; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 835 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 836 | /* We should periodically try to enable listeners waiting for a |
| 837 | * global resource here. |
| 838 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 839 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 840 | /* first, let's check if we need to stop the proxy */ |
| 841 | if (unlikely(stopping && p->state != PR_STSTOPPED)) { |
| 842 | int t; |
| 843 | t = tick_remain(now_ms, p->stop_time); |
| 844 | if (t == 0) { |
| 845 | Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 846 | p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
| 847 | send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 848 | p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
| 849 | stop_proxy(p); |
| 850 | /* try to free more memory */ |
| 851 | pool_gc2(); |
| 852 | } |
| 853 | else { |
| 854 | next = tick_first(next, p->stop_time); |
| 855 | } |
| 856 | } |
Willy Tarreau | f3f8c70 | 2011-07-25 07:37:28 +0200 | [diff] [blame] | 857 | |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 858 | /* If the proxy holds a stick table, we need to purge all unused |
| 859 | * entries. These are all the ones in the table with ref_cnt == 0 |
| 860 | * and all the ones in the pool used to allocate new entries. Any |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 861 | * entry attached to an existing stream waiting for a store will |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 862 | * be in neither list. Any entry being dumped will have ref_cnt > 0. |
| 863 | * However we protect tables that are being synced to peers. |
| 864 | */ |
| 865 | if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) { |
| 866 | if (!p->table.syncing) { |
| 867 | stktable_trash_oldest(&p->table, p->table.current); |
| 868 | pool_gc2(); |
| 869 | } |
| 870 | if (p->table.current) { |
| 871 | /* some entries still remain, let's recheck in one second */ |
| 872 | next = tick_first(next, tick_add(now_ms, 1000)); |
| 873 | } |
| 874 | } |
| 875 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 876 | /* the rest below is just for frontends */ |
| 877 | if (!(p->cap & PR_CAP_FE)) |
| 878 | goto out; |
Willy Tarreau | f3f8c70 | 2011-07-25 07:37:28 +0200 | [diff] [blame] | 879 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 880 | /* check the various reasons we may find to block the frontend */ |
| 881 | if (unlikely(p->feconn >= p->maxconn)) { |
| 882 | if (p->state == PR_STREADY) |
| 883 | p->state = PR_STFULL; |
| 884 | goto out; |
| 885 | } |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 886 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 887 | /* OK we have no reason to block, so let's unblock if we were blocking */ |
| 888 | if (p->state == PR_STFULL) |
| 889 | p->state = PR_STREADY; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 890 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 891 | if (p->fe_sps_lim && |
| 892 | (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) { |
| 893 | /* we're blocking because a limit was reached on the number of |
| 894 | * requests/s on the frontend. We want to re-check ASAP, which |
| 895 | * means in 1 ms before estimated expiration date, because the |
| 896 | * timer will have settled down. |
| 897 | */ |
| 898 | next = tick_first(next, tick_add(now_ms, wait)); |
| 899 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 900 | } |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 901 | |
| 902 | /* The proxy is not limited so we can re-enable any waiting listener */ |
| 903 | if (!LIST_ISEMPTY(&p->listener_queue)) |
| 904 | dequeue_all_listeners(&p->listener_queue); |
| 905 | out: |
| 906 | t->expire = next; |
| 907 | task_queue(t); |
| 908 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | |
| 912 | /* |
| 913 | * this function disables health-check servers so that the process will quickly be ignored |
| 914 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 915 | * time will not be used since it would already not listen anymore to the socket. |
| 916 | */ |
| 917 | void soft_stop(void) |
| 918 | { |
| 919 | struct proxy *p; |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 920 | struct peers *prs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | |
| 922 | stopping = 1; |
| 923 | p = proxy; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 924 | 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] | 925 | while (p) { |
| 926 | if (p->state != PR_STSTOPPED) { |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 927 | Warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); |
| 928 | 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] | 929 | p->stop_time = tick_add(now_ms, p->grace); |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 930 | |
Willy Tarreau | 20b7afb | 2015-09-28 16:35:04 +0200 | [diff] [blame] | 931 | /* Note: do not wake up stopped proxies' task nor their tables' |
| 932 | * tasks as these ones might point to already released entries. |
| 933 | */ |
| 934 | if (p->table.size && p->table.sync_task) |
| 935 | task_wakeup(p->table.sync_task, TASK_WOKEN_MSG); |
| 936 | |
| 937 | if (p->task) |
| 938 | task_wakeup(p->task, TASK_WOKEN_MSG); |
| 939 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 940 | p = p->next; |
| 941 | } |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 942 | |
| 943 | prs = peers; |
| 944 | while (prs) { |
Willy Tarreau | 337a666 | 2015-09-28 16:27:44 +0200 | [diff] [blame] | 945 | if (prs->peers_fe) |
| 946 | stop_proxy(prs->peers_fe); |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 947 | prs = prs->next; |
| 948 | } |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 949 | /* signal zero is used to broadcast the "stopping" event */ |
| 950 | signal_handler(0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 954 | /* Temporarily disables listening on all of the proxy's listeners. Upon |
| 955 | * success, the proxy enters the PR_PAUSED state. If disabling at least one |
| 956 | * listener returns an error, then the proxy state is set to PR_STERROR |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 957 | * because we don't know how to resume from this. The function returns 0 |
| 958 | * if it fails, or non-zero on success. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 959 | */ |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 960 | int pause_proxy(struct proxy *p) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 961 | { |
| 962 | struct listener *l; |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 963 | |
| 964 | if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR || |
| 965 | p->state == PR_STSTOPPED || p->state == PR_STPAUSED) |
| 966 | return 1; |
| 967 | |
| 968 | Warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 969 | send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 970 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 971 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 972 | if (!pause_listener(l)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 973 | p->state = PR_STERROR; |
| 974 | } |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 975 | |
| 976 | if (p->state == PR_STERROR) { |
| 977 | Warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id); |
| 978 | send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id); |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | p->state = PR_STPAUSED; |
| 983 | return 1; |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | |
| 987 | /* |
| 988 | * This function completely stops a proxy and releases its listeners. It has |
| 989 | * to be called when going down in order to release the ports so that another |
| 990 | * process may bind to them. It must also be called on disabled proxies at the |
| 991 | * end of start-up. When all listeners are closed, the proxy is set to the |
| 992 | * PR_STSTOPPED state. |
| 993 | */ |
| 994 | void stop_proxy(struct proxy *p) |
| 995 | { |
| 996 | struct listener *l; |
| 997 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 998 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 999 | unbind_listener(l); |
| 1000 | if (l->state >= LI_ASSIGNED) { |
| 1001 | delete_listener(l); |
| 1002 | listeners--; |
Willy Tarreau | af7ad00 | 2010-08-31 15:39:26 +0200 | [diff] [blame] | 1003 | jobs--; |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | p->state = PR_STSTOPPED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1009 | /* This function resumes listening on the specified proxy. It scans all of its |
| 1010 | * listeners and tries to enable them all. If any of them fails, the proxy is |
| 1011 | * put back to the paused state. It returns 1 upon success, or zero if an error |
| 1012 | * is encountered. |
| 1013 | */ |
| 1014 | int resume_proxy(struct proxy *p) |
| 1015 | { |
| 1016 | struct listener *l; |
| 1017 | int fail; |
| 1018 | |
| 1019 | if (p->state != PR_STPAUSED) |
| 1020 | return 1; |
| 1021 | |
| 1022 | Warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 1023 | send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 1024 | |
| 1025 | fail = 0; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1026 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1027 | if (!resume_listener(l)) { |
| 1028 | int port; |
| 1029 | |
| 1030 | port = get_host_port(&l->addr); |
| 1031 | if (port) { |
| 1032 | Warning("Port %d busy while trying to enable %s %s.\n", |
| 1033 | port, proxy_cap_str(p->cap), p->id); |
| 1034 | send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n", |
| 1035 | port, proxy_cap_str(p->cap), p->id); |
| 1036 | } |
| 1037 | else { |
| 1038 | Warning("Bind on socket %d busy while trying to enable %s %s.\n", |
| 1039 | l->luid, proxy_cap_str(p->cap), p->id); |
| 1040 | send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n", |
| 1041 | l->luid, proxy_cap_str(p->cap), p->id); |
| 1042 | } |
| 1043 | |
| 1044 | /* Another port might have been enabled. Let's stop everything. */ |
| 1045 | fail = 1; |
| 1046 | break; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | p->state = PR_STREADY; |
| 1051 | if (fail) { |
| 1052 | pause_proxy(p); |
| 1053 | return 0; |
| 1054 | } |
| 1055 | return 1; |
| 1056 | } |
| 1057 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1058 | /* |
| 1059 | * This function temporarily disables listening so that another new instance |
| 1060 | * can start listening. It is designed to be called upon reception of a |
| 1061 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
| 1062 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 1063 | */ |
| 1064 | void pause_proxies(void) |
| 1065 | { |
| 1066 | int err; |
| 1067 | struct proxy *p; |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1068 | struct peers *prs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1069 | |
| 1070 | err = 0; |
| 1071 | p = proxy; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 1072 | 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] | 1073 | while (p) { |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1074 | err |= !pause_proxy(p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1075 | p = p->next; |
| 1076 | } |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1077 | |
| 1078 | prs = peers; |
| 1079 | while (prs) { |
Willy Tarreau | 337a666 | 2015-09-28 16:27:44 +0200 | [diff] [blame] | 1080 | if (prs->peers_fe) |
| 1081 | err |= !pause_proxy(prs->peers_fe); |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1082 | prs = prs->next; |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1085 | if (err) { |
| 1086 | Warning("Some proxies refused to pause, performing soft stop now.\n"); |
| 1087 | send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n"); |
| 1088 | soft_stop(); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | /* |
| 1094 | * This function reactivates listening. This can be used after a call to |
| 1095 | * sig_pause(), for example when a new instance has failed starting up. |
| 1096 | * It is designed to be called upon reception of a SIGTTIN. |
| 1097 | */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 1098 | void resume_proxies(void) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1099 | { |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1100 | int err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1101 | struct proxy *p; |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1102 | struct peers *prs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1103 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1104 | err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1105 | p = proxy; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 1106 | 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] | 1107 | while (p) { |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1108 | err |= !resume_proxy(p); |
| 1109 | p = p->next; |
| 1110 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1111 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1112 | prs = peers; |
| 1113 | while (prs) { |
Willy Tarreau | 337a666 | 2015-09-28 16:27:44 +0200 | [diff] [blame] | 1114 | if (prs->peers_fe) |
| 1115 | err |= !resume_proxy(prs->peers_fe); |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1116 | prs = prs->next; |
| 1117 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 1118 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1119 | if (err) { |
| 1120 | Warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n"); |
| 1121 | send_log(p, LOG_WARNING, "Some proxies refused to resume, a restart is probably needed to resume safe operations.\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1125 | /* Set current stream's backend to <be>. Nothing is done if the |
| 1126 | * stream already had a backend assigned, which is indicated by |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1127 | * s->flags & SF_BE_ASSIGNED. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1128 | * All flags, stats and counters which need be updated are updated. |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 1129 | * 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] | 1130 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1131 | int stream_set_backend(struct stream *s, struct proxy *be) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1132 | { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1133 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 1134 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1135 | s->be = be; |
| 1136 | be->beconn++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1137 | if (be->beconn > be->be_counters.conn_max) |
| 1138 | be->be_counters.conn_max = be->beconn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1139 | proxy_inc_be_ctr(be); |
| 1140 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 1141 | if (flt_set_stream_backend(s, be) < 0) |
| 1142 | return 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1143 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1144 | /* assign new parameters to the stream from the new backend */ |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 1145 | s->si[1].flags &= ~SI_FL_INDEP_STR; |
| 1146 | if (be->options2 & PR_O2_INDEPSTR) |
| 1147 | s->si[1].flags |= SI_FL_INDEP_STR; |
| 1148 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1149 | /* We want to enable the backend-specific analysers except those which |
| 1150 | * were already run as part of the frontend/listener. Note that it would |
| 1151 | * be more reliable to store the list of analysers that have been run, |
| 1152 | * but what we do here is OK for now. |
| 1153 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1154 | s->req.analysers |= be->be_req_ana & (strm_li(s) ? ~strm_li(s)->analysers : 0); |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1155 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1156 | /* If the target backend requires HTTP processing, we have to allocate |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1157 | * the HTTP transaction and hdr_idx if we did not have one. |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1158 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1159 | if (unlikely(!s->txn && be->http_needed)) { |
| 1160 | if (unlikely(!http_alloc_txn(s))) |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1161 | return 0; /* not enough memory */ |
Willy Tarreau | 39e4f62 | 2010-05-31 17:01:36 +0200 | [diff] [blame] | 1162 | |
| 1163 | /* and now initialize the HTTP transaction state */ |
| 1164 | http_init_txn(s); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 1167 | /* Be sure to filter request headers if the backend is an HTTP proxy and |
| 1168 | * if there are filters attached to the stream. */ |
| 1169 | if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s)) |
| 1170 | s->req.analysers |= AN_FLT_HTTP_HDRS; |
| 1171 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1172 | if (s->txn) { |
| 1173 | if (be->options2 & PR_O2_RSPBUG_OK) |
| 1174 | s->txn->rsp.err_pos = -1; /* let buggy responses pass */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1175 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1176 | /* If we chain to an HTTP backend running a different HTTP mode, we |
| 1177 | * have to re-adjust the desired keep-alive/close mode to accommodate |
| 1178 | * both the frontend's and the backend's modes. |
| 1179 | */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1180 | if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP && |
| 1181 | ((strm_fe(s)->options & PR_O_HTTP_MODE) != (be->options & PR_O_HTTP_MODE))) |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1182 | http_adjust_conn_mode(s, s->txn, &s->txn->req); |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 1183 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1184 | /* If an LB algorithm needs to access some pre-parsed body contents, |
| 1185 | * we must not start to forward anything until the connection is |
| 1186 | * confirmed otherwise we'll lose the pointer to these data and |
| 1187 | * prevent the hash from being doable again after a redispatch. |
| 1188 | */ |
| 1189 | if (be->mode == PR_MODE_HTTP && |
| 1190 | (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM)) |
| 1191 | s->txn->req.flags |= HTTP_MSGF_WAIT_CONN; |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1192 | |
| 1193 | /* we may request to parse a request body */ |
| 1194 | if ((be->options & PR_O_WREQ_BODY) && |
| 1195 | (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK))) |
| 1196 | s->req.analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | s->flags |= SF_BE_ASSIGNED; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 1200 | if (be->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1201 | s->req.flags |= CF_NEVER_WAIT; |
| 1202 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 1205 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1208 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 1209 | { CFG_LISTEN, "timeout", proxy_parse_timeout }, |
| 1210 | { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, |
| 1211 | { CFG_LISTEN, "contimeout", proxy_parse_timeout }, |
| 1212 | { CFG_LISTEN, "srvtimeout", proxy_parse_timeout }, |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 1213 | { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit }, |
Willy Tarreau | c35362a | 2014-04-25 13:58:37 +0200 | [diff] [blame] | 1214 | { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue }, |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 1215 | { CFG_LISTEN, "declare", proxy_parse_declare }, |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 1216 | { 0, NULL, NULL }, |
| 1217 | }}; |
| 1218 | |
| 1219 | __attribute__((constructor)) |
| 1220 | static void __proxy_module_init(void) |
| 1221 | { |
| 1222 | cfg_register_keywords(&cfg_kws); |
| 1223 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1224 | |
| 1225 | /* |
| 1226 | * Local variables: |
| 1227 | * c-indent-level: 8 |
| 1228 | * c-basic-offset: 8 |
| 1229 | * End: |
| 1230 | */ |