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 | 960f2cb | 2016-11-24 12:02:29 +0100 | [diff] [blame] | 32 | #include <types/cli.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <types/global.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 34 | #include <types/obj_type.h> |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 35 | #include <types/peers.h> |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 36 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 38 | #include <proto/applet.h> |
| 39 | #include <proto/cli.h> |
Alexandre Cassen | 87ea548 | 2007-10-11 20:48:58 +0200 | [diff] [blame] | 40 | #include <proto/backend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 42 | #include <proto/filters.h> |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 43 | #include <proto/hdr_idx.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 44 | #include <proto/listener.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/log.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 46 | #include <proto/proto_tcp.h> |
Willy Tarreau | 39e4f62 | 2010-05-31 17:01:36 +0200 | [diff] [blame] | 47 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/proxy.h> |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 49 | #include <proto/server.h> |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 50 | #include <proto/signal.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 51 | #include <proto/stream.h> |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 52 | #include <proto/stream_interface.h> |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 53 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | |
| 55 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 56 | int listeners; /* # of proxy listeners, set by cfgparse */ |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 57 | struct proxy *proxies_list = NULL; /* list of all existing proxies */ |
Willy Tarreau | 53fb4ae | 2009-10-04 23:04:08 +0200 | [diff] [blame] | 58 | 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] | 59 | 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] | 60 | 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] | 61 | |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 62 | /* |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 63 | * This function returns a string containing a name describing capabilities to |
| 64 | * report comprehensible error messages. Specifically, it will return the words |
Christopher Faulet | 898566e | 2016-10-26 11:06:28 +0200 | [diff] [blame] | 65 | * "frontend", "backend" when appropriate, or "proxy" for all other |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 66 | * cases including the proxies declared in "listen" mode. |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 67 | */ |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 68 | const char *proxy_cap_str(int cap) |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 69 | { |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 70 | if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) { |
| 71 | if (cap & PR_CAP_FE) |
| 72 | return "frontend"; |
| 73 | else if (cap & PR_CAP_BE) |
| 74 | return "backend"; |
Willy Tarreau | 816eb54 | 2007-11-04 07:04:43 +0100 | [diff] [blame] | 75 | } |
| 76 | return "proxy"; |
Willy Tarreau | 977b8e4 | 2006-12-29 14:19:17 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 79 | /* |
| 80 | * This function returns a string containing the mode of the proxy in a format |
| 81 | * suitable for error messages. |
| 82 | */ |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 83 | const char *proxy_mode_str(int mode) { |
| 84 | |
| 85 | if (mode == PR_MODE_TCP) |
| 86 | return "tcp"; |
| 87 | else if (mode == PR_MODE_HTTP) |
| 88 | return "http"; |
| 89 | else if (mode == PR_MODE_HEALTH) |
| 90 | return "health"; |
William Lallemand | cf62f7e | 2018-10-26 14:47:40 +0200 | [diff] [blame] | 91 | else if (mode == PR_MODE_CLI) |
| 92 | return "cli"; |
Krzysztof Piotr Oledzki | 6eb730d | 2007-11-03 23:41:58 +0100 | [diff] [blame] | 93 | else |
| 94 | return "unknown"; |
| 95 | } |
| 96 | |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 97 | /* |
| 98 | * This function scans the list of backends and servers to retrieve the first |
| 99 | * backend and the first server with the given names, and sets them in both |
| 100 | * parameters. It returns zero if either is not found, or non-zero and sets |
| 101 | * the ones it did not found to NULL. If a NULL pointer is passed for the |
| 102 | * backend, only the pointer to the server will be updated. |
| 103 | */ |
| 104 | int get_backend_server(const char *bk_name, const char *sv_name, |
| 105 | struct proxy **bk, struct server **sv) |
| 106 | { |
| 107 | struct proxy *p; |
| 108 | struct server *s; |
Willy Tarreau | 7ecc420 | 2014-03-15 07:57:11 +0100 | [diff] [blame] | 109 | int sid; |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 110 | |
| 111 | *sv = NULL; |
| 112 | |
Willy Tarreau | 050536d | 2012-10-04 08:47:34 +0200 | [diff] [blame] | 113 | sid = -1; |
Willy Tarreau | cfeaa47 | 2009-10-10 22:33:08 +0200 | [diff] [blame] | 114 | if (*sv_name == '#') |
| 115 | sid = atoi(sv_name + 1); |
| 116 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 117 | p = proxy_be_by_name(bk_name); |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 118 | if (bk) |
| 119 | *bk = p; |
| 120 | if (!p) |
| 121 | return 0; |
| 122 | |
| 123 | for (s = p->srv; s; s = s->next) |
Willy Tarreau | 4055a10 | 2012-11-15 00:15:18 +0100 | [diff] [blame] | 124 | if ((sid >= 0 && s->puid == sid) || |
| 125 | (sid < 0 && strcmp(s->id, sv_name) == 0)) |
Willy Tarreau | f395017 | 2009-10-10 18:35:51 +0200 | [diff] [blame] | 126 | break; |
| 127 | *sv = s; |
| 128 | if (!s) |
| 129 | return 0; |
| 130 | return 1; |
| 131 | } |
| 132 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 133 | /* This function parses a "timeout" statement in a proxy section. It returns |
| 134 | * -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] | 135 | * return zero, it will write an error or warning message into a preallocated |
| 136 | * buffer returned at <err>. The trailing is not be written. The function must |
| 137 | * be called with <args> pointing to the first command line word, with <proxy> |
| 138 | * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL. |
| 139 | * As a special case for compatibility with older configs, it also accepts |
| 140 | * "{cli|srv|con}timeout" in args[0]. |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 141 | */ |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 142 | static int proxy_parse_timeout(char **args, int section, struct proxy *proxy, |
Willy Tarreau | 28a47d6 | 2012-09-18 20:02:48 +0200 | [diff] [blame] | 143 | struct proxy *defpx, const char *file, int line, |
| 144 | char **err) |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 145 | { |
| 146 | unsigned timeout; |
| 147 | int retval, cap; |
| 148 | const char *res, *name; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 149 | int *tv = NULL; |
| 150 | int *td = NULL; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 151 | int warn = 0; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 152 | |
| 153 | retval = 0; |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 154 | |
| 155 | /* simply skip "timeout" but remain compatible with old form */ |
| 156 | if (strcmp(args[0], "timeout") == 0) |
| 157 | args++; |
| 158 | |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 159 | name = args[0]; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 160 | 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] | 161 | name = "client"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 162 | tv = &proxy->timeout.client; |
| 163 | td = &defpx->timeout.client; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 164 | cap = PR_CAP_FE; |
| 165 | } else if (!strcmp(args[0], "tarpit")) { |
| 166 | tv = &proxy->timeout.tarpit; |
| 167 | td = &defpx->timeout.tarpit; |
Willy Tarreau | 51c9bde | 2008-01-06 13:40:03 +0100 | [diff] [blame] | 168 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 169 | } else if (!strcmp(args[0], "http-keep-alive")) { |
| 170 | tv = &proxy->timeout.httpka; |
| 171 | td = &defpx->timeout.httpka; |
| 172 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 173 | } else if (!strcmp(args[0], "http-request")) { |
| 174 | tv = &proxy->timeout.httpreq; |
| 175 | td = &defpx->timeout.httpreq; |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 176 | cap = PR_CAP_FE | PR_CAP_BE; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 177 | } 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] | 178 | name = "server"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 179 | tv = &proxy->timeout.server; |
| 180 | td = &defpx->timeout.server; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 181 | cap = PR_CAP_BE; |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 182 | } 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] | 183 | name = "connect"; |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 184 | tv = &proxy->timeout.connect; |
| 185 | td = &defpx->timeout.connect; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 186 | cap = PR_CAP_BE; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 187 | } else if (!strcmp(args[0], "check")) { |
| 188 | tv = &proxy->timeout.check; |
| 189 | td = &defpx->timeout.check; |
| 190 | cap = PR_CAP_BE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 191 | } else if (!strcmp(args[0], "queue")) { |
| 192 | tv = &proxy->timeout.queue; |
| 193 | td = &defpx->timeout.queue; |
| 194 | cap = PR_CAP_BE; |
Willy Tarreau | ce887fd | 2012-05-12 12:50:00 +0200 | [diff] [blame] | 195 | } else if (!strcmp(args[0], "tunnel")) { |
| 196 | tv = &proxy->timeout.tunnel; |
| 197 | td = &defpx->timeout.tunnel; |
| 198 | cap = PR_CAP_BE; |
Willy Tarreau | 05cdd96 | 2014-05-10 14:30:07 +0200 | [diff] [blame] | 199 | } else if (!strcmp(args[0], "client-fin")) { |
| 200 | tv = &proxy->timeout.clientfin; |
| 201 | td = &defpx->timeout.clientfin; |
| 202 | cap = PR_CAP_FE; |
| 203 | } else if (!strcmp(args[0], "server-fin")) { |
| 204 | tv = &proxy->timeout.serverfin; |
| 205 | td = &defpx->timeout.serverfin; |
| 206 | cap = PR_CAP_BE; |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 207 | } else { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 208 | memprintf(err, |
| 209 | "'timeout' supports 'client', 'server', 'connect', 'check', " |
Willy Tarreau | 05cdd96 | 2014-05-10 14:30:07 +0200 | [diff] [blame] | 210 | "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', " |
| 211 | "'client-fin' and 'server-fin' (got '%s')", |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 212 | args[0]); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | if (*args[1] == 0) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 217 | memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | res = parse_time_err(args[1], &timeout, TIME_UNIT_MS); |
| 222 | if (res) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 223 | memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | if (!(proxy->cap & cap)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 228 | memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability", |
| 229 | name, proxy_type_str(proxy), proxy->id, |
| 230 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 231 | retval = 1; |
| 232 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 233 | else if (defpx && *tv != *td) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 234 | memprintf(err, "overwriting 'timeout %s' which was already specified", name); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 235 | retval = 1; |
| 236 | } |
Willy Tarreau | ed44649 | 2014-04-28 22:56:38 +0200 | [diff] [blame] | 237 | else if (warn) { |
| 238 | if (!already_warned(warn)) { |
| 239 | memprintf(err, "the '%s' directive is now deprecated in favor of 'timeout %s', and will not be supported in future versions.", |
| 240 | args[0], name); |
| 241 | retval = 1; |
| 242 | } |
| 243 | } |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | fac5b59 | 2014-05-22 08:24:46 +0200 | [diff] [blame] | 245 | if (*args[2] != 0) { |
| 246 | memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]); |
| 247 | retval = -1; |
| 248 | } |
| 249 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 250 | *tv = MS_TO_TICKS(timeout); |
Willy Tarreau | e219db7 | 2007-12-03 01:30:13 +0100 | [diff] [blame] | 251 | return retval; |
| 252 | } |
| 253 | |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 254 | /* This function parses a "rate-limit" statement in a proxy section. It returns |
| 255 | * -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] | 256 | * return zero, it will write an error or warning message into a preallocated |
| 257 | * buffer returned at <err>. The function must be called with <args> pointing |
| 258 | * to the first command line word, with <proxy> pointing to the proxy being |
| 259 | * parsed, and <defpx> to the default proxy or NULL. |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 260 | */ |
| 261 | 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] | 262 | struct proxy *defpx, const char *file, int line, |
| 263 | char **err) |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 264 | { |
| 265 | int retval, cap; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 266 | char *res; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 267 | unsigned int *tv = NULL; |
| 268 | unsigned int *td = NULL; |
| 269 | unsigned int val; |
| 270 | |
| 271 | retval = 0; |
| 272 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 273 | if (strcmp(args[1], "sessions") == 0) { |
Willy Tarreau | 13a34bd | 2009-05-10 18:52:49 +0200 | [diff] [blame] | 274 | tv = &proxy->fe_sps_lim; |
| 275 | td = &defpx->fe_sps_lim; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 276 | cap = PR_CAP_FE; |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 277 | } |
| 278 | else { |
| 279 | memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 283 | if (*args[2] == 0) { |
| 284 | 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] | 285 | return -1; |
| 286 | } |
| 287 | |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 288 | val = strtoul(args[2], &res, 0); |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 289 | if (*res) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 290 | 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] | 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | if (!(proxy->cap & cap)) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 295 | memprintf(err, "%s %s will be ignored because %s '%s' has no %s capability", |
| 296 | args[0], args[1], proxy_type_str(proxy), proxy->id, |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 297 | (cap & PR_CAP_BE) ? "backend" : "frontend"); |
| 298 | retval = 1; |
| 299 | } |
| 300 | else if (defpx && *tv != *td) { |
Willy Tarreau | 0a3dd74 | 2012-05-08 19:47:01 +0200 | [diff] [blame] | 301 | 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] | 302 | retval = 1; |
| 303 | } |
| 304 | |
| 305 | *tv = val; |
| 306 | return retval; |
| 307 | } |
| 308 | |
Willy Tarreau | c35362a | 2014-04-25 13:58:37 +0200 | [diff] [blame] | 309 | /* This function parses a "max-keep-alive-queue" statement in a proxy section. |
| 310 | * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it |
| 311 | * does not return zero, it will write an error or warning message into a |
| 312 | * preallocated buffer returned at <err>. The function must be called with |
| 313 | * <args> pointing to the first command line word, with <proxy> pointing to |
| 314 | * the proxy being parsed, and <defpx> to the default proxy or NULL. |
| 315 | */ |
| 316 | static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy, |
| 317 | struct proxy *defpx, const char *file, int line, |
| 318 | char **err) |
| 319 | { |
| 320 | int retval; |
| 321 | char *res; |
| 322 | unsigned int val; |
| 323 | |
| 324 | retval = 0; |
| 325 | |
| 326 | if (*args[1] == 0) { |
| 327 | memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | val = strtol(args[1], &res, 0); |
| 332 | if (*res) { |
| 333 | memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | if (!(proxy->cap & PR_CAP_BE)) { |
| 338 | memprintf(err, "%s will be ignored because %s '%s' has no backend capability", |
| 339 | args[0], proxy_type_str(proxy), proxy->id); |
| 340 | retval = 1; |
| 341 | } |
| 342 | |
| 343 | /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */ |
| 344 | proxy->max_ka_queue = val + 1; |
| 345 | return retval; |
| 346 | } |
| 347 | |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 348 | /* This function parses a "declare" statement in a proxy section. It returns -1 |
| 349 | * if there is any error, 1 for warning, otherwise 0. If it does not return zero, |
| 350 | * it will write an error or warning message into a preallocated buffer returned |
| 351 | * at <err>. The function must be called with <args> pointing to the first command |
| 352 | * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the |
| 353 | * default proxy or NULL. |
| 354 | */ |
| 355 | static int proxy_parse_declare(char **args, int section, struct proxy *curpx, |
| 356 | struct proxy *defpx, const char *file, int line, |
| 357 | char **err) |
| 358 | { |
| 359 | /* Capture keyword wannot be declared in a default proxy. */ |
| 360 | if (curpx == defpx) { |
| 361 | memprintf(err, "'%s' not avalaible in default section", args[0]); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | /* Capture keywork is only avalaible in frontend. */ |
| 366 | if (!(curpx->cap & PR_CAP_FE)) { |
| 367 | memprintf(err, "'%s' only avalaible in frontend or listen section", args[0]); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | /* Check mandatory second keyword. */ |
| 372 | if (!args[1] || !*args[1]) { |
| 373 | memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]); |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | /* Actually, declare is only avalaible for declaring capture |
| 378 | * slot, but in the future it can declare maps or variables. |
| 379 | * So, this section permits to check and switch acording with |
| 380 | * the second keyword. |
| 381 | */ |
| 382 | if (strcmp(args[1], "capture") == 0) { |
| 383 | char *error = NULL; |
| 384 | long len; |
| 385 | struct cap_hdr *hdr; |
| 386 | |
| 387 | /* Check the next keyword. */ |
| 388 | if (!args[2] || !*args[2] || |
| 389 | (strcmp(args[2], "response") != 0 && |
| 390 | strcmp(args[2], "request") != 0)) { |
| 391 | memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | /* Check the 'len' keyword. */ |
| 396 | if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) { |
| 397 | memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]); |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | /* Check the length value. */ |
| 402 | if (!args[4] || !*args[4]) { |
| 403 | memprintf(err, "'%s %s': 'len' requires a numeric value that represents the " |
| 404 | "capture length", |
| 405 | args[0], args[1]); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | /* convert the length value. */ |
| 410 | len = strtol(args[4], &error, 10); |
| 411 | if (*error != '\0') { |
| 412 | memprintf(err, "'%s %s': cannot parse the length '%s'.", |
| 413 | args[0], args[1], args[3]); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | /* check length. */ |
| 418 | if (len <= 0) { |
| 419 | memprintf(err, "length must be > 0"); |
| 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | /* register the capture. */ |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 424 | hdr = calloc(1, sizeof(*hdr)); |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 425 | hdr->name = NULL; /* not a header capture */ |
| 426 | hdr->namelen = 0; |
| 427 | hdr->len = len; |
| 428 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 429 | |
| 430 | if (strcmp(args[2], "request") == 0) { |
| 431 | hdr->next = curpx->req_cap; |
| 432 | hdr->index = curpx->nb_req_cap++; |
| 433 | curpx->req_cap = hdr; |
| 434 | } |
| 435 | if (strcmp(args[2], "response") == 0) { |
| 436 | hdr->next = curpx->rsp_cap; |
| 437 | hdr->index = curpx->nb_rsp_cap++; |
| 438 | curpx->rsp_cap = hdr; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | else { |
| 443 | memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]); |
| 444 | return -1; |
| 445 | } |
| 446 | } |
| 447 | |
Willy Tarreau | f79d950 | 2014-03-15 07:22:35 +0100 | [diff] [blame] | 448 | /* This function inserts proxy <px> into the tree of known proxies. The proxy's |
| 449 | * name is used as the storing key so it must already have been initialized. |
| 450 | */ |
| 451 | void proxy_store_name(struct proxy *px) |
| 452 | { |
| 453 | px->conf.by_name.key = px->id; |
| 454 | ebis_insert(&proxy_by_name, &px->conf.by_name); |
| 455 | } |
| 456 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 457 | /* Returns a pointer to the first proxy matching capabilities <cap> and id |
| 458 | * <id>. NULL is returned if no match is found. If <table> is non-zero, it |
| 459 | * only considers proxies having a table. |
Willy Tarreau | bc216c4 | 2011-08-02 11:25:54 +0200 | [diff] [blame] | 460 | */ |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 461 | struct proxy *proxy_find_by_id(int id, int cap, int table) |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 462 | { |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 463 | struct eb32_node *n; |
Willy Tarreau | bc216c4 | 2011-08-02 11:25:54 +0200 | [diff] [blame] | 464 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 465 | for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) { |
| 466 | struct proxy *px = container_of(n, struct proxy, conf.id); |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 467 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 468 | if (px->uuid != id) |
| 469 | break; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 470 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 471 | if ((px->cap & cap) != cap) |
| 472 | continue; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 473 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 474 | if (table && !px->table.size) |
| 475 | continue; |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 476 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 477 | return px; |
| 478 | } |
| 479 | return NULL; |
| 480 | } |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 481 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 482 | /* Returns a pointer to the first proxy matching either name <name>, or id |
| 483 | * <name> if <name> begins with a '#'. NULL is returned if no match is found. |
| 484 | * If <table> is non-zero, it only considers proxies having a table. |
| 485 | */ |
| 486 | struct proxy *proxy_find_by_name(const char *name, int cap, int table) |
| 487 | { |
| 488 | struct proxy *curproxy; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 489 | |
Willy Tarreau | 3c56a7d | 2015-05-26 15:25:32 +0200 | [diff] [blame] | 490 | if (*name == '#') { |
| 491 | curproxy = proxy_find_by_id(atoi(name + 1), cap, table); |
| 492 | if (curproxy) |
Willy Tarreau | c739aa8 | 2015-05-26 11:35:41 +0200 | [diff] [blame] | 493 | return curproxy; |
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 | else { |
| 496 | struct ebpt_node *node; |
| 497 | |
| 498 | for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) { |
| 499 | curproxy = container_of(node, struct proxy, conf.by_name); |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 500 | |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 501 | if (strcmp(curproxy->id, name) != 0) |
| 502 | break; |
| 503 | |
| 504 | if ((curproxy->cap & cap) != cap) |
| 505 | continue; |
| 506 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 507 | if (table && !curproxy->table.size) |
| 508 | continue; |
| 509 | |
Willy Tarreau | c739aa8 | 2015-05-26 11:35:41 +0200 | [diff] [blame] | 510 | return curproxy; |
Willy Tarreau | cfd837f | 2014-03-15 07:43:51 +0100 | [diff] [blame] | 511 | } |
| 512 | } |
Willy Tarreau | c739aa8 | 2015-05-26 11:35:41 +0200 | [diff] [blame] | 513 | return NULL; |
Alex Williams | 96532db | 2009-11-01 21:27:13 -0500 | [diff] [blame] | 514 | } |
| 515 | |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 516 | /* Finds the best match for a proxy with capabilities <cap>, name <name> and id |
| 517 | * <id>. At most one of <id> or <name> may be different provided that <cap> is |
| 518 | * valid. Either <id> or <name> may be left unspecified (0). The purpose is to |
| 519 | * find a proxy based on some information from a previous configuration, across |
| 520 | * reloads or during information exchange between peers. |
| 521 | * |
| 522 | * Names are looked up first if present, then IDs are compared if present. In |
| 523 | * case of an inexact match whatever is forced in the configuration has |
| 524 | * precedence in the following order : |
| 525 | * - 1) forced ID (proves a renaming / change of proxy type) |
| 526 | * - 2) proxy name+type (may indicate a move if ID differs) |
| 527 | * - 3) automatic ID+type (may indicate a renaming) |
| 528 | * |
| 529 | * Depending on what is found, we can end up in the following situations : |
| 530 | * |
| 531 | * name id cap | possible causes |
| 532 | * -------------+----------------- |
| 533 | * -- -- -- | nothing found |
| 534 | * -- -- ok | nothing found |
| 535 | * -- ok -- | proxy deleted, ID points to next one |
| 536 | * -- ok ok | proxy renamed, or deleted with ID pointing to next one |
| 537 | * ok -- -- | proxy deleted, but other half with same name still here (before) |
| 538 | * ok -- ok | proxy's ID changed (proxy moved in the config file) |
| 539 | * ok ok -- | proxy deleted, but other half with same name still here (after) |
| 540 | * ok ok ok | perfect match |
| 541 | * |
| 542 | * 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] | 543 | * - PR_FBM_MISMATCH_ID : proxy was found but ID differs |
| 544 | * (and ID was not zero) |
| 545 | * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs |
| 546 | * (and name was not NULL) |
| 547 | * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with |
| 548 | * the same name and/or id |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 549 | * |
| 550 | * Only a valid proxy is returned. If capabilities do not match, NULL is |
| 551 | * returned. The caller can check <diff> to report detailed warnings / errors, |
| 552 | * and decide whether or not to use what was found. |
| 553 | */ |
| 554 | struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff) |
| 555 | { |
| 556 | struct proxy *byname; |
| 557 | struct proxy *byid; |
| 558 | |
| 559 | if (!name && !id) |
| 560 | return NULL; |
| 561 | |
| 562 | if (diff) |
| 563 | *diff = 0; |
| 564 | |
| 565 | byname = byid = NULL; |
| 566 | |
| 567 | if (name) { |
| 568 | byname = proxy_find_by_name(name, cap, 0); |
| 569 | if (byname && (!id || byname->uuid == id)) |
| 570 | return byname; |
| 571 | } |
| 572 | |
| 573 | /* remaining possiblities : |
| 574 | * - name not set |
| 575 | * - name set but not found |
| 576 | * - name found, but ID doesn't match. |
| 577 | */ |
| 578 | if (id) { |
| 579 | byid = proxy_find_by_id(id, cap, 0); |
| 580 | if (byid) { |
| 581 | if (byname) { |
| 582 | /* id+type found, name+type found, but not all 3. |
| 583 | * ID wins only if forced, otherwise name wins. |
| 584 | */ |
| 585 | if (byid->options & PR_O_FORCED_ID) { |
| 586 | if (diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 587 | *diff |= PR_FBM_MISMATCH_NAME; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 588 | return byid; |
| 589 | } |
| 590 | else { |
| 591 | if (diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 592 | *diff |= PR_FBM_MISMATCH_ID; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 593 | return byname; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /* remaining possiblities : |
| 598 | * - name not set |
| 599 | * - name set but not found |
| 600 | */ |
| 601 | if (name && diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 602 | *diff |= PR_FBM_MISMATCH_NAME; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 603 | return byid; |
| 604 | } |
| 605 | |
| 606 | /* ID not found */ |
| 607 | if (byname) { |
| 608 | if (diff) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 609 | *diff |= PR_FBM_MISMATCH_ID; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 610 | return byname; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /* All remaining possiblities will lead to NULL. If we can report more |
| 615 | * detailed information to the caller about changed types and/or name, |
| 616 | * we'll do it. For example, we could detect that "listen foo" was |
| 617 | * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced. |
| 618 | * - name not set, ID not found |
| 619 | * - name not found, ID not set |
| 620 | * - name not found, ID not found |
| 621 | */ |
| 622 | if (!diff) |
| 623 | return NULL; |
| 624 | |
| 625 | if (name) { |
| 626 | byname = proxy_find_by_name(name, 0, 0); |
| 627 | if (byname && (!id || byname->uuid == id)) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 628 | *diff |= PR_FBM_MISMATCH_PROXYTYPE; |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | if (id) { |
| 632 | byid = proxy_find_by_id(id, 0, 0); |
| 633 | if (byid) { |
| 634 | if (!name) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 635 | *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */ |
Willy Tarreau | eb3e348 | 2015-05-27 16:46:26 +0200 | [diff] [blame] | 636 | else if (byid->options & PR_O_FORCED_ID) |
Baptiste Assmann | 8a027cc | 2015-07-03 11:03:33 +0200 | [diff] [blame] | 637 | *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] | 638 | /* otherwise it's a different proxy that was returned */ |
| 639 | } |
| 640 | } |
| 641 | return NULL; |
| 642 | } |
| 643 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 644 | /* |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 645 | * This function finds a server with matching name within selected proxy. |
| 646 | * It also checks if there are more matching servers with |
| 647 | * requested name as this often leads into unexpected situations. |
| 648 | */ |
| 649 | |
| 650 | struct server *findserver(const struct proxy *px, const char *name) { |
| 651 | |
| 652 | struct server *cursrv, *target = NULL; |
| 653 | |
| 654 | if (!px) |
| 655 | return NULL; |
| 656 | |
| 657 | for (cursrv = px->srv; cursrv; cursrv = cursrv->next) { |
| 658 | if (strcmp(cursrv->id, name)) |
| 659 | continue; |
| 660 | |
| 661 | if (!target) { |
| 662 | target = cursrv; |
| 663 | continue; |
| 664 | } |
| 665 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 666 | ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n", |
| 667 | name, px->id); |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 668 | |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | return target; |
| 673 | } |
| 674 | |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 675 | /* This function checks that the designated proxy has no http directives |
| 676 | * enabled. It will output a warning if there are, and will fix some of them. |
| 677 | * It returns the number of fatal errors encountered. This should be called |
| 678 | * at the end of the configuration parsing if the proxy is not in http mode. |
| 679 | * The <file> argument is used to construct the error message. |
| 680 | */ |
Willy Tarreau | 915e1eb | 2009-06-22 15:48:36 +0200 | [diff] [blame] | 681 | int proxy_cfg_ensure_no_http(struct proxy *curproxy) |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 682 | { |
| 683 | if (curproxy->cookie_name != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 684 | ha_warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n", |
| 685 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 686 | } |
| 687 | if (curproxy->rsp_exp != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 688 | ha_warning("config : server regular expressions will be ignored for %s '%s' (needs 'mode http').\n", |
| 689 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 690 | } |
| 691 | if (curproxy->req_exp != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 692 | ha_warning("config : client regular expressions will be ignored for %s '%s' (needs 'mode http').\n", |
| 693 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 694 | } |
| 695 | if (curproxy->monitor_uri != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 696 | ha_warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n", |
| 697 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 698 | } |
Willy Tarreau | f3e49f9 | 2009-10-03 12:21:20 +0200 | [diff] [blame] | 699 | if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) { |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 700 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 701 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 702 | ha_warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n", |
| 703 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 704 | } |
Willy Tarreau | 1780416 | 2009-11-09 21:27:51 +0100 | [diff] [blame] | 705 | if (curproxy->to_log & (LW_REQ | LW_RESP)) { |
| 706 | curproxy->to_log &= ~(LW_REQ | LW_RESP); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 707 | ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n", |
| 708 | curproxy->conf.lfs_file, curproxy->conf.lfs_line, |
| 709 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | 1780416 | 2009-11-09 21:27:51 +0100 | [diff] [blame] | 710 | } |
Willy Tarreau | 62a6123 | 2013-04-12 18:13:46 +0200 | [diff] [blame] | 711 | if (curproxy->conf.logformat_string == default_http_log_format || |
| 712 | curproxy->conf.logformat_string == clf_http_log_format) { |
| 713 | /* Note: we don't change the directive's file:line number */ |
| 714 | curproxy->conf.logformat_string = default_tcp_log_format; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 715 | ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n", |
| 716 | curproxy->conf.lfs_file, curproxy->conf.lfs_line, |
| 717 | proxy_type_str(curproxy), curproxy->id); |
Willy Tarreau | 196729e | 2012-05-31 19:30:26 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Willy Tarreau | ff01a21 | 2009-03-15 13:46:16 +0100 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 723 | /* Perform the most basic initialization of a proxy : |
| 724 | * memset(), list_init(*), reset_timeouts(*). |
Willy Tarreau | b249e84 | 2011-09-07 18:41:08 +0200 | [diff] [blame] | 725 | * Any new proxy or peer should be initialized via this function. |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 726 | */ |
| 727 | void init_new_proxy(struct proxy *p) |
| 728 | { |
| 729 | memset(p, 0, sizeof(struct proxy)); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 730 | p->obj_type = OBJ_TYPE_PROXY; |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 731 | p->pendconns = EB_ROOT; |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 732 | LIST_INIT(&p->acl); |
| 733 | LIST_INIT(&p->http_req_rules); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 734 | LIST_INIT(&p->http_res_rules); |
Willy Tarreau | 353bc9f | 2014-04-28 22:05:31 +0200 | [diff] [blame] | 735 | LIST_INIT(&p->block_rules); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 736 | LIST_INIT(&p->redirect_rules); |
| 737 | LIST_INIT(&p->mon_fail_cond); |
| 738 | LIST_INIT(&p->switching_rules); |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 739 | LIST_INIT(&p->server_rules); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 740 | LIST_INIT(&p->persist_rules); |
| 741 | LIST_INIT(&p->sticking_rules); |
| 742 | LIST_INIT(&p->storersp_rules); |
| 743 | LIST_INIT(&p->tcp_req.inspect_rules); |
| 744 | LIST_INIT(&p->tcp_rep.inspect_rules); |
| 745 | LIST_INIT(&p->tcp_req.l4_rules); |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 746 | LIST_INIT(&p->tcp_req.l5_rules); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 747 | LIST_INIT(&p->req_add); |
| 748 | LIST_INIT(&p->rsp_add); |
| 749 | LIST_INIT(&p->listener_queue); |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 750 | LIST_INIT(&p->logsrvs); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 751 | LIST_INIT(&p->logformat); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 752 | LIST_INIT(&p->logformat_sd); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 753 | LIST_INIT(&p->format_unique_id); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 754 | LIST_INIT(&p->conf.bind); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 755 | LIST_INIT(&p->conf.listeners); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 756 | LIST_INIT(&p->conf.args.list); |
Baptiste Assmann | 5ecb77f | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 757 | LIST_INIT(&p->tcpcheck_rules); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 758 | LIST_INIT(&p->filter_configs); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 759 | |
| 760 | /* Timeouts are defined as -1 */ |
| 761 | proxy_reset_timeouts(p); |
| 762 | p->tcp_rep.inspect_delay = TICK_ETERNITY; |
Willy Tarreau | 050536d | 2012-10-04 08:47:34 +0200 | [diff] [blame] | 763 | |
| 764 | /* initial uuid is unassigned (-1) */ |
| 765 | p->uuid = -1; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 766 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 767 | HA_SPIN_INIT(&p->lock); |
Willy Tarreau | 237250c | 2011-07-29 01:49:03 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Krzysztof Piotr Oledzki | c8b16fc | 2008-02-18 01:26:35 +0100 | [diff] [blame] | 770 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 771 | * This function creates all proxy sockets. It should be done very early, |
| 772 | * typically before privileges are dropped. The sockets will be registered |
| 773 | * 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] | 774 | * 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] | 775 | * bound. |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 776 | * |
| 777 | * Its return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. |
| 778 | * Retryable errors will only be printed if <verbose> is not zero. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 779 | */ |
| 780 | int start_proxies(int verbose) |
| 781 | { |
| 782 | struct proxy *curproxy; |
| 783 | struct listener *listener; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 784 | int lerr, err = ERR_NONE; |
| 785 | int pxerr; |
| 786 | char msg[100]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 787 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 788 | for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 789 | if (curproxy->state != PR_STNEW) |
| 790 | continue; /* already initialized */ |
| 791 | |
| 792 | pxerr = 0; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 793 | list_for_each_entry(listener, &curproxy->conf.listeners, by_fe) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 794 | if (listener->state != LI_ASSIGNED) |
| 795 | continue; /* already started */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 796 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 797 | lerr = listener->proto->bind(listener, msg, sizeof(msg)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 798 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 799 | /* errors are reported if <verbose> is set or if they are fatal */ |
| 800 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
| 801 | if (lerr & ERR_ALERT) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 802 | ha_alert("Starting %s %s: %s\n", |
| 803 | proxy_type_str(curproxy), curproxy->id, msg); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 804 | else if (lerr & ERR_WARN) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 805 | ha_warning("Starting %s %s: %s\n", |
| 806 | proxy_type_str(curproxy), curproxy->id, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 809 | err |= lerr; |
| 810 | if (lerr & (ERR_ABORT | ERR_FATAL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 811 | pxerr |= 1; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 812 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 813 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 814 | else if (lerr & ERR_CODE) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 815 | pxerr |= 1; |
| 816 | continue; |
| 817 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if (!pxerr) { |
Willy Tarreau | 562515c | 2011-07-25 08:11:52 +0200 | [diff] [blame] | 821 | curproxy->state = PR_STREADY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 822 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
| 823 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 824 | |
| 825 | if (err & ERR_ABORT) |
| 826 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | return err; |
| 830 | } |
| 831 | |
| 832 | |
| 833 | /* |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 834 | * 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] | 835 | * 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] | 836 | * called as a task which is woken up upon stopping or when rate limiting must |
| 837 | * be enforced. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 838 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 839 | struct task *manage_proxy(struct task *t, void *context, unsigned short state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 840 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 841 | struct proxy *p = context; |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 842 | int next = TICK_ETERNITY; |
Willy Tarreau | 7958422 | 2009-03-06 09:18:27 +0100 | [diff] [blame] | 843 | unsigned int wait; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 844 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 845 | /* We should periodically try to enable listeners waiting for a |
| 846 | * global resource here. |
| 847 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 848 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 849 | /* first, let's check if we need to stop the proxy */ |
| 850 | if (unlikely(stopping && p->state != PR_STSTOPPED)) { |
| 851 | int t; |
| 852 | t = tick_remain(now_ms, p->stop_time); |
| 853 | if (t == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 854 | ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 855 | p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 856 | send_log(p, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 857 | p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn); |
| 858 | stop_proxy(p); |
| 859 | /* try to free more memory */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 860 | pool_gc(NULL); |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 861 | } |
| 862 | else { |
| 863 | next = tick_first(next, p->stop_time); |
| 864 | } |
| 865 | } |
Willy Tarreau | f3f8c70 | 2011-07-25 07:37:28 +0200 | [diff] [blame] | 866 | |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 867 | /* If the proxy holds a stick table, we need to purge all unused |
| 868 | * entries. These are all the ones in the table with ref_cnt == 0 |
| 869 | * 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] | 870 | * entry attached to an existing stream waiting for a store will |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 871 | * be in neither list. Any entry being dumped will have ref_cnt > 0. |
| 872 | * However we protect tables that are being synced to peers. |
| 873 | */ |
| 874 | if (unlikely(stopping && p->state == PR_STSTOPPED && p->table.current)) { |
| 875 | if (!p->table.syncing) { |
| 876 | stktable_trash_oldest(&p->table, p->table.current); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 877 | pool_gc(NULL); |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 878 | } |
| 879 | if (p->table.current) { |
| 880 | /* some entries still remain, let's recheck in one second */ |
| 881 | next = tick_first(next, tick_add(now_ms, 1000)); |
| 882 | } |
| 883 | } |
| 884 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 885 | /* the rest below is just for frontends */ |
| 886 | if (!(p->cap & PR_CAP_FE)) |
| 887 | goto out; |
Willy Tarreau | f3f8c70 | 2011-07-25 07:37:28 +0200 | [diff] [blame] | 888 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 889 | /* check the various reasons we may find to block the frontend */ |
| 890 | if (unlikely(p->feconn >= p->maxconn)) { |
| 891 | if (p->state == PR_STREADY) |
| 892 | p->state = PR_STFULL; |
| 893 | goto out; |
| 894 | } |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 895 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 896 | /* OK we have no reason to block, so let's unblock if we were blocking */ |
| 897 | if (p->state == PR_STFULL) |
| 898 | p->state = PR_STREADY; |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 899 | |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 900 | if (p->fe_sps_lim && |
| 901 | (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) { |
| 902 | /* we're blocking because a limit was reached on the number of |
| 903 | * requests/s on the frontend. We want to re-check ASAP, which |
| 904 | * means in 1 ms before estimated expiration date, because the |
| 905 | * timer will have settled down. |
| 906 | */ |
| 907 | next = tick_first(next, tick_add(now_ms, wait)); |
| 908 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 909 | } |
Willy Tarreau | 918ff60 | 2011-07-25 16:33:49 +0200 | [diff] [blame] | 910 | |
| 911 | /* The proxy is not limited so we can re-enable any waiting listener */ |
| 912 | if (!LIST_ISEMPTY(&p->listener_queue)) |
| 913 | dequeue_all_listeners(&p->listener_queue); |
| 914 | out: |
| 915 | t->expire = next; |
| 916 | task_queue(t); |
| 917 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 921 | static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx, |
| 922 | struct proxy *defpx, const char *file, int line, |
| 923 | char **err) |
| 924 | { |
| 925 | const char *res; |
| 926 | |
| 927 | if (!*args[1]) { |
| 928 | memprintf(err, "'%s' expects <time> as argument.\n", args[0]); |
| 929 | return -1; |
| 930 | } |
| 931 | res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS); |
| 932 | if (res) { |
| 933 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]); |
| 934 | return -1; |
| 935 | } |
| 936 | return 0; |
| 937 | } |
| 938 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 939 | struct task *hard_stop(struct task *t, void *context, unsigned short state) |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 940 | { |
| 941 | struct proxy *p; |
| 942 | struct stream *s; |
| 943 | |
| 944 | if (killed) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 945 | ha_warning("Some tasks resisted to hard-stop, exiting now.\n"); |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 946 | send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n"); |
| 947 | /* Do some cleanup and explicitely quit */ |
| 948 | deinit(); |
| 949 | exit(0); |
| 950 | } |
| 951 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 952 | ha_warning("soft-stop running for too long, performing a hard-stop.\n"); |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 953 | send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n"); |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 954 | p = proxies_list; |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 955 | while (p) { |
| 956 | if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 957 | ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n", |
| 958 | p->id, p->feconn); |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 959 | send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n", |
| 960 | p->id, p->feconn); |
| 961 | } |
| 962 | p = p->next; |
| 963 | } |
| 964 | list_for_each_entry(s, &streams, list) { |
| 965 | stream_shutdown(s, SF_ERR_KILLED); |
| 966 | } |
| 967 | |
| 968 | killed = 1; |
| 969 | t->expire = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 970 | return t; |
| 971 | } |
| 972 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 973 | /* |
| 974 | * this function disables health-check servers so that the process will quickly be ignored |
| 975 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 976 | * time will not be used since it would already not listen anymore to the socket. |
| 977 | */ |
| 978 | void soft_stop(void) |
| 979 | { |
| 980 | struct proxy *p; |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 981 | struct peers *prs; |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 982 | struct task *task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 983 | |
| 984 | stopping = 1; |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 985 | if (tick_isset(global.hard_stop_after)) { |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 986 | task = task_new(MAX_THREADS_MASK); |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 987 | if (task) { |
| 988 | task->process = hard_stop; |
| 989 | task_schedule(task, tick_add(now_ms, global.hard_stop_after)); |
| 990 | } |
| 991 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 992 | ha_alert("out of memory trying to allocate the hard-stop task.\n"); |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 993 | } |
| 994 | } |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 995 | p = proxies_list; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 996 | 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] | 997 | while (p) { |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 998 | /* Zombie proxy, let's close the file descriptors */ |
| 999 | if (p->state == PR_STSTOPPED && |
| 1000 | !LIST_ISEMPTY(&p->conf.listeners) && |
| 1001 | LIST_ELEM(p->conf.listeners.n, |
| 1002 | struct listener *, by_fe)->state >= LI_ZOMBIE) { |
| 1003 | struct listener *l; |
| 1004 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
| 1005 | if (l->state >= LI_ZOMBIE) |
| 1006 | close(l->fd); |
| 1007 | l->state = LI_INIT; |
| 1008 | } |
| 1009 | } |
| 1010 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1011 | if (p->state != PR_STSTOPPED) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1012 | ha_warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace); |
Willy Tarreau | f8fbcef | 2008-10-10 17:51:34 +0200 | [diff] [blame] | 1013 | 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] | 1014 | p->stop_time = tick_add(now_ms, p->grace); |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1015 | |
Willy Tarreau | 20b7afb | 2015-09-28 16:35:04 +0200 | [diff] [blame] | 1016 | /* Note: do not wake up stopped proxies' task nor their tables' |
| 1017 | * tasks as these ones might point to already released entries. |
| 1018 | */ |
| 1019 | if (p->table.size && p->table.sync_task) |
| 1020 | task_wakeup(p->table.sync_task, TASK_WOKEN_MSG); |
| 1021 | |
| 1022 | if (p->task) |
| 1023 | task_wakeup(p->task, TASK_WOKEN_MSG); |
| 1024 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1025 | p = p->next; |
| 1026 | } |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 1027 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1028 | prs = cfg_peers; |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 1029 | while (prs) { |
Willy Tarreau | 337a666 | 2015-09-28 16:27:44 +0200 | [diff] [blame] | 1030 | if (prs->peers_fe) |
| 1031 | stop_proxy(prs->peers_fe); |
Willy Tarreau | bbe11b1 | 2011-07-25 11:16:24 +0200 | [diff] [blame] | 1032 | prs = prs->next; |
| 1033 | } |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 1034 | /* signal zero is used to broadcast the "stopping" event */ |
| 1035 | signal_handler(0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 1039 | /* Temporarily disables listening on all of the proxy's listeners. Upon |
| 1040 | * success, the proxy enters the PR_PAUSED state. If disabling at least one |
| 1041 | * 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] | 1042 | * because we don't know how to resume from this. The function returns 0 |
| 1043 | * if it fails, or non-zero on success. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1044 | */ |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1045 | int pause_proxy(struct proxy *p) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1046 | { |
| 1047 | struct listener *l; |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1048 | |
| 1049 | if (!(p->cap & PR_CAP_FE) || p->state == PR_STERROR || |
| 1050 | p->state == PR_STSTOPPED || p->state == PR_STPAUSED) |
| 1051 | return 1; |
| 1052 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1053 | ha_warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id); |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1054 | send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 1055 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1056 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 1057 | if (!pause_listener(l)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1058 | p->state = PR_STERROR; |
| 1059 | } |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1060 | |
| 1061 | if (p->state == PR_STERROR) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1062 | ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id); |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1063 | send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | p->state = PR_STPAUSED; |
| 1068 | return 1; |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 1071 | /* This function makes the proxy unusable, but keeps the listening sockets |
| 1072 | * opened, so that if any process requests them, we are able to serve them. |
| 1073 | * This should only be called early, before we started accepting requests. |
| 1074 | */ |
| 1075 | void zombify_proxy(struct proxy *p) |
| 1076 | { |
| 1077 | struct listener *l; |
| 1078 | struct listener *first_to_listen = NULL; |
| 1079 | |
| 1080 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
| 1081 | enum li_state oldstate = l->state; |
| 1082 | |
| 1083 | unbind_listener_no_close(l); |
| 1084 | if (l->state >= LI_ASSIGNED) { |
| 1085 | delete_listener(l); |
Olivier Houchard | 1fc0516 | 2017-04-06 01:05:05 +0200 | [diff] [blame] | 1086 | } |
| 1087 | /* |
| 1088 | * Pretend we're still up and running so that the fd |
| 1089 | * will be sent if asked. |
| 1090 | */ |
| 1091 | l->state = LI_ZOMBIE; |
| 1092 | if (!first_to_listen && oldstate >= LI_LISTEN) |
| 1093 | first_to_listen = l; |
| 1094 | } |
| 1095 | /* Quick hack : at stop time, to know we have to close the sockets |
| 1096 | * despite the proxy being marked as stopped, make the first listener |
| 1097 | * of the listener list an active one, so that we don't have to |
| 1098 | * parse the whole list to be sure. |
| 1099 | */ |
| 1100 | if (first_to_listen && LIST_ELEM(p->conf.listeners.n, |
| 1101 | struct listener *, by_fe) != first_to_listen) { |
| 1102 | LIST_DEL(&l->by_fe); |
| 1103 | LIST_ADD(&p->conf.listeners, &l->by_fe); |
| 1104 | } |
| 1105 | |
| 1106 | p->state = PR_STSTOPPED; |
| 1107 | } |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * This function completely stops a proxy and releases its listeners. It has |
| 1111 | * to be called when going down in order to release the ports so that another |
| 1112 | * process may bind to them. It must also be called on disabled proxies at the |
William Lallemand | c59f988 | 2018-11-16 16:57:21 +0100 | [diff] [blame] | 1113 | * end of start-up. If all listeners are closed, the proxy is set to the |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1114 | * PR_STSTOPPED state. |
| 1115 | */ |
| 1116 | void stop_proxy(struct proxy *p) |
| 1117 | { |
| 1118 | struct listener *l; |
William Lallemand | c59f988 | 2018-11-16 16:57:21 +0100 | [diff] [blame] | 1119 | int nostop = 0; |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1120 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1121 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
William Lallemand | c59f988 | 2018-11-16 16:57:21 +0100 | [diff] [blame] | 1122 | if (l->options & LI_O_NOSTOP) { |
| 1123 | HA_ATOMIC_ADD(&unstoppable_jobs, 1); |
| 1124 | nostop = 1; |
| 1125 | continue; |
| 1126 | } |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1127 | unbind_listener(l); |
| 1128 | if (l->state >= LI_ASSIGNED) { |
| 1129 | delete_listener(l); |
Willy Tarreau | da250db | 2008-10-12 12:07:48 +0200 | [diff] [blame] | 1130 | } |
| 1131 | } |
William Lallemand | c59f988 | 2018-11-16 16:57:21 +0100 | [diff] [blame] | 1132 | if (!nostop) |
| 1133 | p->state = PR_STSTOPPED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1136 | /* This function resumes listening on the specified proxy. It scans all of its |
| 1137 | * listeners and tries to enable them all. If any of them fails, the proxy is |
| 1138 | * put back to the paused state. It returns 1 upon success, or zero if an error |
| 1139 | * is encountered. |
| 1140 | */ |
| 1141 | int resume_proxy(struct proxy *p) |
| 1142 | { |
| 1143 | struct listener *l; |
| 1144 | int fail; |
| 1145 | |
| 1146 | if (p->state != PR_STPAUSED) |
| 1147 | return 1; |
| 1148 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1149 | ha_warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id); |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1150 | send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id); |
| 1151 | |
| 1152 | fail = 0; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 1153 | list_for_each_entry(l, &p->conf.listeners, by_fe) { |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1154 | if (!resume_listener(l)) { |
| 1155 | int port; |
| 1156 | |
| 1157 | port = get_host_port(&l->addr); |
| 1158 | if (port) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1159 | ha_warning("Port %d busy while trying to enable %s %s.\n", |
| 1160 | port, proxy_cap_str(p->cap), p->id); |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1161 | send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n", |
| 1162 | port, proxy_cap_str(p->cap), p->id); |
| 1163 | } |
| 1164 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1165 | ha_warning("Bind on socket %d busy while trying to enable %s %s.\n", |
| 1166 | l->luid, proxy_cap_str(p->cap), p->id); |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1167 | send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n", |
| 1168 | l->luid, proxy_cap_str(p->cap), p->id); |
| 1169 | } |
| 1170 | |
| 1171 | /* Another port might have been enabled. Let's stop everything. */ |
| 1172 | fail = 1; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | p->state = PR_STREADY; |
| 1178 | if (fail) { |
| 1179 | pause_proxy(p); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | return 1; |
| 1183 | } |
| 1184 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1185 | /* |
| 1186 | * This function temporarily disables listening so that another new instance |
| 1187 | * can start listening. It is designed to be called upon reception of a |
| 1188 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
| 1189 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 1190 | */ |
| 1191 | void pause_proxies(void) |
| 1192 | { |
| 1193 | int err; |
| 1194 | struct proxy *p; |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1195 | struct peers *prs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1196 | |
| 1197 | err = 0; |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1198 | p = proxies_list; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 1199 | 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] | 1200 | while (p) { |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1201 | err |= !pause_proxy(p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1202 | p = p->next; |
| 1203 | } |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1204 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1205 | prs = cfg_peers; |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1206 | while (prs) { |
Willy Tarreau | 337a666 | 2015-09-28 16:27:44 +0200 | [diff] [blame] | 1207 | if (prs->peers_fe) |
| 1208 | err |= !pause_proxy(prs->peers_fe); |
Willy Tarreau | ce8fe25 | 2011-09-07 19:14:57 +0200 | [diff] [blame] | 1209 | prs = prs->next; |
Emeric Brun | 5a8c0a9 | 2010-09-23 18:44:36 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1212 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1213 | ha_warning("Some proxies refused to pause, performing soft stop now.\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1214 | send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n"); |
| 1215 | soft_stop(); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | /* |
| 1221 | * This function reactivates listening. This can be used after a call to |
| 1222 | * sig_pause(), for example when a new instance has failed starting up. |
| 1223 | * It is designed to be called upon reception of a SIGTTIN. |
| 1224 | */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 1225 | void resume_proxies(void) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1226 | { |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1227 | int err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1228 | struct proxy *p; |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1229 | struct peers *prs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1230 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1231 | err = 0; |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1232 | p = proxies_list; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 1233 | 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] | 1234 | while (p) { |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1235 | err |= !resume_proxy(p); |
| 1236 | p = p->next; |
| 1237 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1238 | |
Frédéric Lécaille | ed2b4a6 | 2017-07-13 09:07:09 +0200 | [diff] [blame] | 1239 | prs = cfg_peers; |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1240 | while (prs) { |
Willy Tarreau | 337a666 | 2015-09-28 16:27:44 +0200 | [diff] [blame] | 1241 | if (prs->peers_fe) |
| 1242 | err |= !resume_proxy(prs->peers_fe); |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1243 | prs = prs->next; |
| 1244 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 1245 | |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1246 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1247 | ha_warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n"); |
Willy Tarreau | c03ebbf | 2011-09-07 21:33:14 +0200 | [diff] [blame] | 1248 | 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] | 1249 | } |
| 1250 | } |
| 1251 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1252 | /* Set current stream's backend to <be>. Nothing is done if the |
| 1253 | * stream already had a backend assigned, which is indicated by |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1254 | * s->flags & SF_BE_ASSIGNED. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1255 | * All flags, stats and counters which need be updated are updated. |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 1256 | * 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] | 1257 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1258 | int stream_set_backend(struct stream *s, struct proxy *be) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1259 | { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1260 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 1261 | return 1; |
Christopher Faulet | 4117904 | 2016-06-21 11:54:52 +0200 | [diff] [blame] | 1262 | |
| 1263 | if (flt_set_stream_backend(s, be) < 0) |
| 1264 | return 0; |
| 1265 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1266 | s->be = be; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1267 | HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max, |
| 1268 | HA_ATOMIC_ADD(&be->beconn, 1)); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1269 | proxy_inc_be_ctr(be); |
| 1270 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1271 | /* assign new parameters to the stream from the new backend */ |
Willy Tarreau | f27b5ea | 2009-10-03 22:01:18 +0200 | [diff] [blame] | 1272 | s->si[1].flags &= ~SI_FL_INDEP_STR; |
| 1273 | if (be->options2 & PR_O2_INDEPSTR) |
| 1274 | s->si[1].flags |= SI_FL_INDEP_STR; |
| 1275 | |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 1276 | if (tick_isset(be->timeout.serverfin)) |
| 1277 | s->si[1].hcto = be->timeout.serverfin; |
| 1278 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1279 | /* We want to enable the backend-specific analysers except those which |
| 1280 | * were already run as part of the frontend/listener. Note that it would |
| 1281 | * be more reliable to store the list of analysers that have been run, |
| 1282 | * but what we do here is OK for now. |
| 1283 | */ |
Christopher Faulet | 70e2f27 | 2017-01-09 16:33:19 +0100 | [diff] [blame] | 1284 | 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] | 1285 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1286 | /* If the target backend requires HTTP processing, we have to allocate |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1287 | * the HTTP transaction and hdr_idx if we did not have one. |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1288 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1289 | if (unlikely(!s->txn && be->http_needed)) { |
| 1290 | if (unlikely(!http_alloc_txn(s))) |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1291 | return 0; /* not enough memory */ |
Willy Tarreau | 39e4f62 | 2010-05-31 17:01:36 +0200 | [diff] [blame] | 1292 | |
| 1293 | /* and now initialize the HTTP transaction state */ |
| 1294 | http_init_txn(s); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 1297 | /* Be sure to filter request headers if the backend is an HTTP proxy and |
| 1298 | * if there are filters attached to the stream. */ |
| 1299 | if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s)) |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1300 | s->req.analysers |= AN_REQ_FLT_HTTP_HDRS; |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 1301 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1302 | if (s->txn) { |
| 1303 | if (be->options2 & PR_O2_RSPBUG_OK) |
| 1304 | s->txn->rsp.err_pos = -1; /* let buggy responses pass */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1305 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1306 | /* If we chain to an HTTP backend running a different HTTP mode, we |
| 1307 | * have to re-adjust the desired keep-alive/close mode to accommodate |
| 1308 | * both the frontend's and the backend's modes. |
| 1309 | */ |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 1310 | if (strm_fe(s)->mode == PR_MODE_HTTP && be->mode == PR_MODE_HTTP && |
| 1311 | ((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] | 1312 | http_adjust_conn_mode(s, s->txn, &s->txn->req); |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 1313 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1314 | /* If an LB algorithm needs to access some pre-parsed body contents, |
| 1315 | * we must not start to forward anything until the connection is |
| 1316 | * confirmed otherwise we'll lose the pointer to these data and |
| 1317 | * prevent the hash from being doable again after a redispatch. |
| 1318 | */ |
| 1319 | if (be->mode == PR_MODE_HTTP && |
| 1320 | (be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_HI | BE_LB_HASH_PRM)) |
| 1321 | s->txn->req.flags |= HTTP_MSGF_WAIT_CONN; |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1322 | |
| 1323 | /* we may request to parse a request body */ |
| 1324 | if ((be->options & PR_O_WREQ_BODY) && |
| 1325 | (s->txn->req.body_len || (s->txn->req.flags & HTTP_MSGF_TE_CHNK))) |
| 1326 | s->req.analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | s->flags |= SF_BE_ASSIGNED; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 1330 | if (be->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1331 | s->req.flags |= CF_NEVER_WAIT; |
| 1332 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 1335 | return 1; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1338 | /* Capture a bad request or response and archive it in the proxy's structure. |
| 1339 | * It is relatively protocol-agnostic so it requires that a number of elements |
| 1340 | * are passed : |
| 1341 | * - <proxy> is the proxy where the error was detected and where the snapshot |
| 1342 | * needs to be stored |
| 1343 | * - <is_back> indicates that the error happend when receiving the response |
| 1344 | * - <other_end> is a pointer to the proxy on the other side when known |
| 1345 | * - <target> is the target of the connection, usually a server or a proxy |
| 1346 | * - <sess> is the session which experienced the error |
| 1347 | * - <ctx> may be NULL or should contain any info relevant to the protocol |
| 1348 | * - <buf> is the buffer containing the offending data |
| 1349 | * - <buf_ofs> is the position of this buffer's input data in the input |
| 1350 | * stream, starting at zero. It may be passed as zero if unknown. |
| 1351 | * - <buf_out> is the portion of <buf->data> which was already forwarded and |
| 1352 | * which precedes the buffer's input. The buffer's input starts at |
| 1353 | * buf->head + buf_out. |
| 1354 | * - <err_pos> is the pointer to the faulty byte in the buffer's input. |
| 1355 | * - <show> is the callback to use to display <ctx>. It may be NULL. |
| 1356 | */ |
| 1357 | void proxy_capture_error(struct proxy *proxy, int is_back, |
| 1358 | struct proxy *other_end, enum obj_type *target, |
| 1359 | const struct session *sess, |
| 1360 | const struct buffer *buf, long buf_ofs, |
| 1361 | unsigned int buf_out, unsigned int err_pos, |
| 1362 | const union error_snapshot_ctx *ctx, |
| 1363 | void (*show)(struct buffer *, const struct error_snapshot *)) |
| 1364 | { |
| 1365 | struct error_snapshot *es; |
| 1366 | unsigned int buf_len; |
| 1367 | int len1, len2; |
Willy Tarreau | c55015e | 2018-09-07 19:02:32 +0200 | [diff] [blame] | 1368 | unsigned int ev_id; |
| 1369 | |
| 1370 | ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1); |
| 1371 | |
Willy Tarreau | 4bc7d90 | 2018-09-07 20:07:17 +0200 | [diff] [blame] | 1372 | buf_len = b_data(buf) - buf_out; |
| 1373 | |
| 1374 | es = malloc(sizeof(*es) + buf_len); |
Willy Tarreau | c55015e | 2018-09-07 19:02:32 +0200 | [diff] [blame] | 1375 | if (!es) |
| 1376 | return; |
| 1377 | |
Willy Tarreau | 4bc7d90 | 2018-09-07 20:07:17 +0200 | [diff] [blame] | 1378 | es->buf_len = buf_len; |
| 1379 | es->ev_id = ev_id; |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1380 | |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1381 | len1 = b_size(buf) - buf_len; |
| 1382 | if (len1 > buf_len) |
| 1383 | len1 = buf_len; |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1384 | |
Willy Tarreau | 4bc7d90 | 2018-09-07 20:07:17 +0200 | [diff] [blame] | 1385 | if (len1) { |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1386 | memcpy(es->buf, b_peek(buf, buf_out), len1); |
Willy Tarreau | 4bc7d90 | 2018-09-07 20:07:17 +0200 | [diff] [blame] | 1387 | len2 = buf_len - len1; |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1388 | if (len2) |
| 1389 | memcpy(es->buf + len1, b_orig(buf), len2); |
| 1390 | } |
| 1391 | |
| 1392 | es->buf_err = err_pos; |
| 1393 | es->when = date; // user-visible date |
| 1394 | es->srv = objt_server(target); |
| 1395 | es->oe = other_end; |
| 1396 | if (objt_conn(sess->origin)) |
| 1397 | es->src = __objt_conn(sess->origin)->addr.from; |
| 1398 | else |
| 1399 | memset(&es->src, 0, sizeof(es->src)); |
| 1400 | |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1401 | es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out); |
| 1402 | es->buf_out = buf_out; |
| 1403 | es->buf_ofs = buf_ofs; |
| 1404 | |
| 1405 | /* be sure to indicate the offset of the first IN byte */ |
| 1406 | if (es->buf_ofs >= es->buf_len) |
| 1407 | es->buf_ofs -= es->buf_len; |
| 1408 | else |
| 1409 | es->buf_ofs = 0; |
| 1410 | |
| 1411 | /* protocol-specific part now */ |
| 1412 | if (ctx) |
| 1413 | es->ctx = *ctx; |
| 1414 | else |
| 1415 | memset(&es->ctx, 0, sizeof(es->ctx)); |
| 1416 | es->show = show; |
Willy Tarreau | c55015e | 2018-09-07 19:02:32 +0200 | [diff] [blame] | 1417 | |
| 1418 | /* note: we still lock since we have to be certain that nobody is |
| 1419 | * dumping the output while we free. |
| 1420 | */ |
| 1421 | HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock); |
| 1422 | if (is_back) { |
| 1423 | es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es); |
| 1424 | } else { |
| 1425 | es = HA_ATOMIC_XCHG(&proxy->invalid_req, es); |
| 1426 | } |
| 1427 | free(es); |
Willy Tarreau | 75fb65a | 2018-09-07 17:43:26 +0200 | [diff] [blame] | 1428 | HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock); |
| 1429 | } |
| 1430 | |
| 1431 | /* Config keywords below */ |
| 1432 | |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1433 | static struct cfg_kw_list cfg_kws = {ILH, { |
Cyril Bonté | 203ec5a | 2017-03-23 22:44:13 +0100 | [diff] [blame] | 1434 | { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after }, |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 1435 | { CFG_LISTEN, "timeout", proxy_parse_timeout }, |
| 1436 | { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, |
| 1437 | { CFG_LISTEN, "contimeout", proxy_parse_timeout }, |
| 1438 | { CFG_LISTEN, "srvtimeout", proxy_parse_timeout }, |
Willy Tarreau | 3a7d207 | 2009-03-05 23:48:25 +0100 | [diff] [blame] | 1439 | { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit }, |
Willy Tarreau | c35362a | 2014-04-25 13:58:37 +0200 | [diff] [blame] | 1440 | { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue }, |
Thierry FOURNIER | a0a1b75 | 2015-05-26 17:44:32 +0200 | [diff] [blame] | 1441 | { CFG_LISTEN, "declare", proxy_parse_declare }, |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 1442 | { 0, NULL, NULL }, |
| 1443 | }}; |
| 1444 | |
Willy Tarreau | 960f2cb | 2016-11-24 12:02:29 +0100 | [diff] [blame] | 1445 | /* Expects to find a frontend named <arg> and returns it, otherwise displays various |
| 1446 | * adequate error messages and returns NULL. This function is designed to be used by |
| 1447 | * functions requiring a frontend on the CLI. |
| 1448 | */ |
| 1449 | struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg) |
| 1450 | { |
| 1451 | struct proxy *px; |
| 1452 | |
| 1453 | if (!*arg) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1454 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 960f2cb | 2016-11-24 12:02:29 +0100 | [diff] [blame] | 1455 | appctx->ctx.cli.msg = "A frontend name is expected.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1456 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 960f2cb | 2016-11-24 12:02:29 +0100 | [diff] [blame] | 1457 | return NULL; |
| 1458 | } |
| 1459 | |
| 1460 | px = proxy_fe_by_name(arg); |
| 1461 | if (!px) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1462 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 960f2cb | 2016-11-24 12:02:29 +0100 | [diff] [blame] | 1463 | appctx->ctx.cli.msg = "No such frontend.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1464 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 960f2cb | 2016-11-24 12:02:29 +0100 | [diff] [blame] | 1465 | return NULL; |
| 1466 | } |
| 1467 | return px; |
| 1468 | } |
| 1469 | |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1470 | /* Expects to find a backend named <arg> and returns it, otherwise displays various |
| 1471 | * adequate error messages and returns NULL. This function is designed to be used by |
| 1472 | * functions requiring a frontend on the CLI. |
| 1473 | */ |
| 1474 | struct proxy *cli_find_backend(struct appctx *appctx, const char *arg) |
| 1475 | { |
| 1476 | struct proxy *px; |
| 1477 | |
| 1478 | if (!*arg) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1479 | appctx->ctx.cli.severity = LOG_ERR; |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1480 | appctx->ctx.cli.msg = "A backend name is expected.\n"; |
| 1481 | appctx->st0 = CLI_ST_PRINT; |
| 1482 | return NULL; |
| 1483 | } |
| 1484 | |
| 1485 | px = proxy_be_by_name(arg); |
| 1486 | if (!px) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1487 | appctx->ctx.cli.severity = LOG_ERR; |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1488 | appctx->ctx.cli.msg = "No such backend.\n"; |
| 1489 | appctx->st0 = CLI_ST_PRINT; |
| 1490 | return NULL; |
| 1491 | } |
| 1492 | return px; |
| 1493 | } |
| 1494 | |
| 1495 | |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1496 | /* parse a "show servers" CLI line, returns 0 if it wants to start the dump or |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1497 | * 1 if it stops immediately. If an argument is specified, it will set the proxy |
| 1498 | * pointer into cli.p0 and its ID into cli.i0. |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1499 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1500 | static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1501 | { |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1502 | struct proxy *px; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1503 | |
| 1504 | /* check if a backend name has been provided */ |
| 1505 | if (*args[3]) { |
| 1506 | /* read server state from local file */ |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1507 | px = proxy_be_by_name(args[3]); |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1508 | |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1509 | if (!px) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1510 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1511 | appctx->ctx.cli.msg = "Can't find backend.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1512 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1513 | return 1; |
| 1514 | } |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1515 | appctx->ctx.cli.p0 = px; |
| 1516 | appctx->ctx.cli.i0 = px->uuid; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1517 | } |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1521 | /* dumps server state information into <buf> for all the servers found in backend cli.p0. |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1522 | * These information are all the parameters which may change during HAProxy runtime. |
| 1523 | * By default, we only export to the last known server state file format. |
| 1524 | * These information can be used at next startup to recover same level of server state. |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1525 | * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's |
| 1526 | * pointer from cli.p1. |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1527 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1528 | static int dump_servers_state(struct stream_interface *si, struct buffer *buf) |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1529 | { |
| 1530 | struct appctx *appctx = __objt_appctx(si->end); |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1531 | struct proxy *px = appctx->ctx.cli.p0; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1532 | struct server *srv; |
| 1533 | char srv_addr[INET6_ADDRSTRLEN + 1]; |
| 1534 | time_t srv_time_since_last_change; |
| 1535 | int bk_f_forced_id, srv_f_forced_id; |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 1536 | char *srvrecord; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1537 | |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1538 | /* we don't want to report any state if the backend is not enabled on this process */ |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 1539 | if (px->bind_proc && !(px->bind_proc & pid_bit)) |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1540 | return 1; |
| 1541 | |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1542 | if (!appctx->ctx.cli.p1) |
| 1543 | appctx->ctx.cli.p1 = px->srv; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1544 | |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1545 | for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) { |
| 1546 | srv = appctx->ctx.cli.p1; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1547 | srv_addr[0] = '\0'; |
| 1548 | |
| 1549 | switch (srv->addr.ss_family) { |
| 1550 | case AF_INET: |
| 1551 | inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr, |
| 1552 | srv_addr, INET_ADDRSTRLEN + 1); |
| 1553 | break; |
| 1554 | case AF_INET6: |
| 1555 | inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr, |
| 1556 | srv_addr, INET6_ADDRSTRLEN + 1); |
| 1557 | break; |
Daniel Corbett | 9215ffa | 2018-05-19 19:43:24 -0400 | [diff] [blame] | 1558 | default: |
| 1559 | memcpy(srv_addr, "-\0", 2); |
| 1560 | break; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1561 | } |
| 1562 | srv_time_since_last_change = now.tv_sec - srv->last_change; |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1563 | bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1564 | srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0; |
| 1565 | |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 1566 | srvrecord = NULL; |
| 1567 | if (srv->srvrq && srv->srvrq->name) |
| 1568 | srvrecord = srv->srvrq->name; |
| 1569 | |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1570 | chunk_appendf(buf, |
| 1571 | "%d %s " |
| 1572 | "%d %s %s " |
| 1573 | "%d %d %d %d %ld " |
| 1574 | "%d %d %d %d %d " |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 1575 | "%d %d %s %u %s" |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1576 | "\n", |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1577 | px->uuid, px->id, |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1578 | srv->puid, srv->id, srv_addr, |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1579 | srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change, |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1580 | srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state, |
Baptiste Assmann | 6d0f38f | 2018-07-02 17:00:54 +0200 | [diff] [blame] | 1581 | bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port, |
| 1582 | srvrecord ? srvrecord : "-"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1583 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1584 | si_rx_room_blk(si); |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1585 | return 0; |
| 1586 | } |
| 1587 | } |
| 1588 | return 1; |
| 1589 | } |
| 1590 | |
| 1591 | /* Parses backend list or simply use backend name provided by the user to return |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1592 | * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is |
| 1593 | * non-null. |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1594 | */ |
| 1595 | static int cli_io_handler_servers_state(struct appctx *appctx) |
| 1596 | { |
| 1597 | struct stream_interface *si = appctx->owner; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1598 | struct proxy *curproxy; |
| 1599 | |
| 1600 | chunk_reset(&trash); |
| 1601 | |
| 1602 | if (appctx->st2 == STAT_ST_INIT) { |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1603 | if (!appctx->ctx.cli.p0) |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1604 | appctx->ctx.cli.p0 = proxies_list; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1605 | appctx->st2 = STAT_ST_HEAD; |
| 1606 | } |
| 1607 | |
| 1608 | if (appctx->st2 == STAT_ST_HEAD) { |
| 1609 | chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1610 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1611 | si_rx_room_blk(si); |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1612 | return 0; |
| 1613 | } |
| 1614 | appctx->st2 = STAT_ST_INFO; |
| 1615 | } |
| 1616 | |
| 1617 | /* STAT_ST_INFO */ |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1618 | for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) { |
| 1619 | curproxy = appctx->ctx.cli.p0; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1620 | /* servers are only in backends */ |
| 1621 | if (curproxy->cap & PR_CAP_BE) { |
| 1622 | if (!dump_servers_state(si, &trash)) |
| 1623 | return 0; |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1624 | } |
| 1625 | /* only the selected proxy is dumped */ |
Willy Tarreau | 3c92f2a | 2016-12-16 18:23:39 +0100 | [diff] [blame] | 1626 | if (appctx->ctx.cli.i0) |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1627 | break; |
| 1628 | } |
| 1629 | |
| 1630 | return 1; |
| 1631 | } |
| 1632 | |
Willy Tarreau | 608ea59 | 2016-12-16 18:01:15 +0100 | [diff] [blame] | 1633 | /* Parses backend list and simply report backend names. It keeps the proxy |
| 1634 | * pointer in cli.p0. |
| 1635 | */ |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1636 | static int cli_io_handler_show_backend(struct appctx *appctx) |
| 1637 | { |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1638 | struct stream_interface *si = appctx->owner; |
| 1639 | struct proxy *curproxy; |
| 1640 | |
| 1641 | chunk_reset(&trash); |
| 1642 | |
Willy Tarreau | 608ea59 | 2016-12-16 18:01:15 +0100 | [diff] [blame] | 1643 | if (!appctx->ctx.cli.p0) { |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1644 | chunk_printf(&trash, "# name\n"); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1645 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1646 | si_rx_room_blk(si); |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1647 | return 0; |
| 1648 | } |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 1649 | appctx->ctx.cli.p0 = proxies_list; |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1650 | } |
| 1651 | |
Willy Tarreau | 608ea59 | 2016-12-16 18:01:15 +0100 | [diff] [blame] | 1652 | for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) { |
| 1653 | curproxy = appctx->ctx.cli.p0; |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1654 | |
| 1655 | /* looking for backends only */ |
| 1656 | if (!(curproxy->cap & PR_CAP_BE)) |
| 1657 | continue; |
| 1658 | |
| 1659 | /* we don't want to list a backend which is bound to this process */ |
Willy Tarreau | 387bd4f | 2017-11-10 19:08:14 +0100 | [diff] [blame] | 1660 | if (curproxy->bind_proc && !(curproxy->bind_proc & pid_bit)) |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1661 | continue; |
| 1662 | |
| 1663 | chunk_appendf(&trash, "%s\n", curproxy->id); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1664 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1665 | si_rx_room_blk(si); |
William Lallemand | 933efcd | 2016-11-22 12:34:16 +0100 | [diff] [blame] | 1666 | return 0; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | return 1; |
| 1671 | } |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 1672 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1673 | /* Parses the "enable dynamic-cookies backend" directive, it always returns 1. |
| 1674 | * |
| 1675 | * Grabs the proxy lock and each server's lock. |
| 1676 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1677 | static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private) |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1678 | { |
| 1679 | struct proxy *px; |
| 1680 | struct server *s; |
| 1681 | |
| 1682 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1683 | return 1; |
| 1684 | |
| 1685 | px = cli_find_backend(appctx, args[3]); |
| 1686 | if (!px) |
| 1687 | return 1; |
| 1688 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1689 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 1690 | |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1691 | px->ck_opts |= PR_CK_DYNAMIC; |
| 1692 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1693 | for (s = px->srv; s != NULL; s = s->next) { |
| 1694 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1695 | srv_set_dyncookie(s); |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1696 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 1697 | } |
| 1698 | |
| 1699 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1700 | |
| 1701 | return 1; |
| 1702 | } |
| 1703 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1704 | /* Parses the "disable dynamic-cookies backend" directive, it always returns 1. |
| 1705 | * |
| 1706 | * Grabs the proxy lock and each server's lock. |
| 1707 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1708 | static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private) |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1709 | { |
| 1710 | struct proxy *px; |
| 1711 | struct server *s; |
| 1712 | |
| 1713 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1714 | return 1; |
| 1715 | |
| 1716 | px = cli_find_backend(appctx, args[3]); |
| 1717 | if (!px) |
| 1718 | return 1; |
| 1719 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1720 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 1721 | |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1722 | px->ck_opts &= ~PR_CK_DYNAMIC; |
| 1723 | |
| 1724 | for (s = px->srv; s != NULL; s = s->next) { |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1725 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1726 | if (!(s->flags & SRV_F_COOKIESET)) { |
| 1727 | free(s->cookie); |
| 1728 | s->cookie = NULL; |
| 1729 | } |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1730 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1731 | } |
| 1732 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1733 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
| 1734 | |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1735 | return 1; |
| 1736 | } |
| 1737 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1738 | /* Parses the "set dynamic-cookie-key backend" directive, it always returns 1. |
| 1739 | * |
| 1740 | * Grabs the proxy lock and each server's lock. |
| 1741 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1742 | static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private) |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1743 | { |
| 1744 | struct proxy *px; |
| 1745 | struct server *s; |
| 1746 | char *newkey; |
| 1747 | |
| 1748 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1749 | return 1; |
| 1750 | |
| 1751 | px = cli_find_backend(appctx, args[3]); |
| 1752 | if (!px) |
| 1753 | return 1; |
| 1754 | |
| 1755 | if (!*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1756 | appctx->ctx.cli.severity = LOG_ERR; |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1757 | appctx->ctx.cli.msg = "String value expected.\n"; |
| 1758 | appctx->st0 = CLI_ST_PRINT; |
| 1759 | return 1; |
| 1760 | } |
| 1761 | |
| 1762 | newkey = strdup(args[4]); |
| 1763 | if (!newkey) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1764 | appctx->ctx.cli.severity = LOG_ERR; |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1765 | appctx->ctx.cli.msg = "Failed to allocate memory.\n"; |
| 1766 | appctx->st0 = CLI_ST_PRINT; |
| 1767 | return 1; |
| 1768 | } |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1769 | |
| 1770 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 1771 | |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1772 | free(px->dyncookie_key); |
| 1773 | px->dyncookie_key = newkey; |
| 1774 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1775 | for (s = px->srv; s != NULL; s = s->next) { |
| 1776 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1777 | srv_set_dyncookie(s); |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1778 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 1779 | } |
| 1780 | |
| 1781 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 1782 | |
| 1783 | return 1; |
| 1784 | } |
| 1785 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1786 | /* Parses the "set maxconn frontend" directive, it always returns 1. |
| 1787 | * |
| 1788 | * Grabs the proxy lock. |
| 1789 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1790 | static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1791 | { |
| 1792 | struct proxy *px; |
| 1793 | struct listener *l; |
| 1794 | int v; |
| 1795 | |
| 1796 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1797 | return 1; |
| 1798 | |
| 1799 | px = cli_find_frontend(appctx, args[3]); |
| 1800 | if (!px) |
| 1801 | return 1; |
| 1802 | |
| 1803 | if (!*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1804 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1805 | appctx->ctx.cli.msg = "Integer value expected.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1806 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1807 | return 1; |
| 1808 | } |
| 1809 | |
| 1810 | v = atoi(args[4]); |
| 1811 | if (v < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1812 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1813 | appctx->ctx.cli.msg = "Value out of range.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1814 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1815 | return 1; |
| 1816 | } |
| 1817 | |
| 1818 | /* OK, the value is fine, so we assign it to the proxy and to all of |
| 1819 | * its listeners. The blocked ones will be dequeued. |
| 1820 | */ |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1821 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 1822 | |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1823 | px->maxconn = v; |
| 1824 | list_for_each_entry(l, &px->conf.listeners, by_fe) { |
| 1825 | l->maxconn = v; |
| 1826 | if (l->state == LI_FULL) |
| 1827 | resume_listener(l); |
| 1828 | } |
| 1829 | |
| 1830 | if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue)) |
| 1831 | dequeue_all_listeners(&px->listener_queue); |
| 1832 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1833 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
| 1834 | |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 1835 | return 1; |
| 1836 | } |
| 1837 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1838 | /* Parses the "shutdown frontend" directive, it always returns 1. |
| 1839 | * |
| 1840 | * Grabs the proxy lock. |
| 1841 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1842 | static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 1843 | { |
| 1844 | struct proxy *px; |
| 1845 | |
| 1846 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1847 | return 1; |
| 1848 | |
| 1849 | px = cli_find_frontend(appctx, args[2]); |
| 1850 | if (!px) |
| 1851 | return 1; |
| 1852 | |
| 1853 | if (px->state == PR_STSTOPPED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1854 | appctx->ctx.cli.severity = LOG_NOTICE; |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 1855 | appctx->ctx.cli.msg = "Frontend was already shut down.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1856 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 1857 | return 1; |
| 1858 | } |
| 1859 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1860 | ha_warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 1861 | px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn); |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 1862 | send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n", |
| 1863 | px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn); |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1864 | |
| 1865 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 1866 | stop_proxy(px); |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1867 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
| 1868 | |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 1869 | return 1; |
| 1870 | } |
| 1871 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1872 | /* Parses the "disable frontend" directive, it always returns 1. |
| 1873 | * |
| 1874 | * Grabs the proxy lock. |
| 1875 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1876 | static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1877 | { |
| 1878 | struct proxy *px; |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1879 | int ret; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1880 | |
| 1881 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1882 | return 1; |
| 1883 | |
| 1884 | px = cli_find_frontend(appctx, args[2]); |
| 1885 | if (!px) |
| 1886 | return 1; |
| 1887 | |
| 1888 | if (px->state == PR_STSTOPPED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1889 | appctx->ctx.cli.severity = LOG_NOTICE; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1890 | appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1891 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1892 | return 1; |
| 1893 | } |
| 1894 | |
| 1895 | if (px->state == PR_STPAUSED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1896 | appctx->ctx.cli.severity = LOG_NOTICE; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1897 | appctx->ctx.cli.msg = "Frontend is already disabled.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1898 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1899 | return 1; |
| 1900 | } |
| 1901 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1902 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 1903 | ret = pause_proxy(px); |
| 1904 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
| 1905 | |
| 1906 | if (!ret) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1907 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1908 | appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1909 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1910 | return 1; |
| 1911 | } |
| 1912 | return 1; |
| 1913 | } |
| 1914 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1915 | /* Parses the "enable frontend" directive, it always returns 1. |
| 1916 | * |
| 1917 | * Grabs the proxy lock. |
| 1918 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1919 | static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1920 | { |
| 1921 | struct proxy *px; |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1922 | int ret; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1923 | |
| 1924 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1925 | return 1; |
| 1926 | |
| 1927 | px = cli_find_frontend(appctx, args[2]); |
| 1928 | if (!px) |
| 1929 | return 1; |
| 1930 | |
| 1931 | if (px->state == PR_STSTOPPED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1932 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1933 | appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1934 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1935 | return 1; |
| 1936 | } |
| 1937 | |
| 1938 | if (px->state != PR_STPAUSED) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1939 | appctx->ctx.cli.severity = LOG_NOTICE; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1940 | appctx->ctx.cli.msg = "Frontend is already enabled.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1941 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1942 | return 1; |
| 1943 | } |
| 1944 | |
Willy Tarreau | a275a37 | 2018-08-21 14:50:44 +0200 | [diff] [blame] | 1945 | HA_SPIN_LOCK(PROXY_LOCK, &px->lock); |
| 1946 | ret = resume_proxy(px); |
| 1947 | HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock); |
| 1948 | |
| 1949 | if (!ret) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 1950 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1951 | appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 1952 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 1953 | return 1; |
| 1954 | } |
| 1955 | return 1; |
| 1956 | } |
| 1957 | |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 1958 | /* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop |
| 1959 | * now. |
| 1960 | */ |
| 1961 | static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private) |
| 1962 | { |
| 1963 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1964 | return 1; |
| 1965 | |
| 1966 | if (*args[2]) { |
| 1967 | struct proxy *px; |
| 1968 | |
| 1969 | px = proxy_find_by_name(args[2], 0, 0); |
| 1970 | if (px) |
| 1971 | appctx->ctx.errors.iid = px->uuid; |
| 1972 | else |
| 1973 | appctx->ctx.errors.iid = atoi(args[2]); |
| 1974 | |
| 1975 | if (!appctx->ctx.errors.iid) { |
| 1976 | appctx->ctx.cli.severity = LOG_ERR; |
| 1977 | appctx->ctx.cli.msg = "No such proxy.\n"; |
| 1978 | appctx->st0 = CLI_ST_PRINT; |
| 1979 | return 1; |
| 1980 | } |
| 1981 | } |
| 1982 | else |
| 1983 | appctx->ctx.errors.iid = -1; // dump all proxies |
| 1984 | |
| 1985 | appctx->ctx.errors.flag = 0; |
| 1986 | if (strcmp(args[3], "request") == 0) |
| 1987 | appctx->ctx.errors.flag |= 4; // ignore response |
| 1988 | else if (strcmp(args[3], "response") == 0) |
| 1989 | appctx->ctx.errors.flag |= 2; // ignore request |
| 1990 | appctx->ctx.errors.px = NULL; |
| 1991 | return 0; |
| 1992 | } |
| 1993 | |
| 1994 | /* This function dumps all captured errors onto the stream interface's |
| 1995 | * read buffer. It returns 0 if the output buffer is full and it needs |
| 1996 | * to be called again, otherwise non-zero. |
| 1997 | */ |
| 1998 | static int cli_io_handler_show_errors(struct appctx *appctx) |
| 1999 | { |
| 2000 | struct stream_interface *si = appctx->owner; |
| 2001 | extern const char *monthname[12]; |
| 2002 | |
| 2003 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 2004 | return 1; |
| 2005 | |
| 2006 | chunk_reset(&trash); |
| 2007 | |
| 2008 | if (!appctx->ctx.errors.px) { |
| 2009 | /* the function had not been called yet, let's prepare the |
| 2010 | * buffer for a response. |
| 2011 | */ |
| 2012 | struct tm tm; |
| 2013 | |
| 2014 | get_localtime(date.tv_sec, &tm); |
| 2015 | chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n", |
| 2016 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 2017 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000), |
| 2018 | error_snapshot_id); |
| 2019 | |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2020 | if (ci_putchk(si_ic(si), &trash) == -1) |
| 2021 | goto cant_send; |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2022 | |
| 2023 | appctx->ctx.errors.px = proxies_list; |
| 2024 | appctx->ctx.errors.bol = 0; |
| 2025 | appctx->ctx.errors.ptr = -1; |
| 2026 | } |
| 2027 | |
| 2028 | /* we have two inner loops here, one for the proxy, the other one for |
| 2029 | * the buffer. |
| 2030 | */ |
| 2031 | while (appctx->ctx.errors.px) { |
| 2032 | struct error_snapshot *es; |
| 2033 | |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2034 | HA_SPIN_LOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock); |
| 2035 | |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2036 | if ((appctx->ctx.errors.flag & 1) == 0) { |
Willy Tarreau | c55015e | 2018-09-07 19:02:32 +0200 | [diff] [blame] | 2037 | es = appctx->ctx.errors.px->invalid_req; |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2038 | if (appctx->ctx.errors.flag & 2) // skip req |
| 2039 | goto next; |
| 2040 | } |
| 2041 | else { |
Willy Tarreau | c55015e | 2018-09-07 19:02:32 +0200 | [diff] [blame] | 2042 | es = appctx->ctx.errors.px->invalid_rep; |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2043 | if (appctx->ctx.errors.flag & 4) // skip resp |
| 2044 | goto next; |
| 2045 | } |
| 2046 | |
Willy Tarreau | c55015e | 2018-09-07 19:02:32 +0200 | [diff] [blame] | 2047 | if (!es) |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2048 | goto next; |
| 2049 | |
| 2050 | if (appctx->ctx.errors.iid >= 0 && |
| 2051 | appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid && |
| 2052 | es->oe->uuid != appctx->ctx.errors.iid) |
| 2053 | goto next; |
| 2054 | |
| 2055 | if (appctx->ctx.errors.ptr < 0) { |
| 2056 | /* just print headers now */ |
| 2057 | |
| 2058 | char pn[INET6_ADDRSTRLEN]; |
| 2059 | struct tm tm; |
| 2060 | int port; |
| 2061 | |
| 2062 | get_localtime(es->when.tv_sec, &tm); |
| 2063 | chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]", |
| 2064 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 2065 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000)); |
| 2066 | |
| 2067 | switch (addr_to_str(&es->src, pn, sizeof(pn))) { |
| 2068 | case AF_INET: |
| 2069 | case AF_INET6: |
| 2070 | port = get_host_port(&es->src); |
| 2071 | break; |
| 2072 | default: |
| 2073 | port = 0; |
| 2074 | } |
| 2075 | |
| 2076 | switch (appctx->ctx.errors.flag & 1) { |
| 2077 | case 0: |
| 2078 | chunk_appendf(&trash, |
| 2079 | " frontend %s (#%d): invalid request\n" |
| 2080 | " backend %s (#%d)", |
| 2081 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 2082 | (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>", |
| 2083 | (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1); |
| 2084 | break; |
| 2085 | case 1: |
| 2086 | chunk_appendf(&trash, |
| 2087 | " backend %s (#%d): invalid response\n" |
| 2088 | " frontend %s (#%d)", |
| 2089 | appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid, |
| 2090 | es->oe->id, es->oe->uuid); |
| 2091 | break; |
| 2092 | } |
| 2093 | |
| 2094 | chunk_appendf(&trash, |
| 2095 | ", server %s (#%d), event #%u, src %s:%d\n" |
| 2096 | " buffer starts at %llu (including %u out), %u free,\n" |
| 2097 | " len %u, wraps at %u, error at position %u\n", |
| 2098 | es->srv ? es->srv->id : "<NONE>", |
| 2099 | es->srv ? es->srv->puid : -1, |
| 2100 | es->ev_id, pn, port, |
| 2101 | es->buf_ofs, es->buf_out, |
| 2102 | global.tune.bufsize - es->buf_out - es->buf_len, |
| 2103 | es->buf_len, es->buf_wrap, es->buf_err); |
| 2104 | |
| 2105 | if (es->show) |
| 2106 | es->show(&trash, es); |
| 2107 | |
| 2108 | chunk_appendf(&trash, " \n"); |
| 2109 | |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2110 | if (ci_putchk(si_ic(si), &trash) == -1) |
| 2111 | goto cant_send_unlock; |
| 2112 | |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2113 | appctx->ctx.errors.ptr = 0; |
| 2114 | appctx->ctx.errors.ev_id = es->ev_id; |
| 2115 | } |
| 2116 | |
| 2117 | if (appctx->ctx.errors.ev_id != es->ev_id) { |
| 2118 | /* the snapshot changed while we were dumping it */ |
| 2119 | chunk_appendf(&trash, |
| 2120 | " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n"); |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2121 | if (ci_putchk(si_ic(si), &trash) == -1) |
| 2122 | goto cant_send_unlock; |
| 2123 | |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2124 | goto next; |
| 2125 | } |
| 2126 | |
| 2127 | /* OK, ptr >= 0, so we have to dump the current line */ |
Willy Tarreau | 4bc7d90 | 2018-09-07 20:07:17 +0200 | [diff] [blame] | 2128 | while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) { |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2129 | int newptr; |
| 2130 | int newline; |
| 2131 | |
| 2132 | newline = appctx->ctx.errors.bol; |
| 2133 | newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr); |
| 2134 | if (newptr == appctx->ctx.errors.ptr) |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2135 | goto cant_send_unlock; |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2136 | |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2137 | if (ci_putchk(si_ic(si), &trash) == -1) |
| 2138 | goto cant_send_unlock; |
| 2139 | |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2140 | appctx->ctx.errors.ptr = newptr; |
| 2141 | appctx->ctx.errors.bol = newline; |
| 2142 | }; |
| 2143 | next: |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2144 | HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock); |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2145 | appctx->ctx.errors.bol = 0; |
| 2146 | appctx->ctx.errors.ptr = -1; |
| 2147 | appctx->ctx.errors.flag ^= 1; |
| 2148 | if (!(appctx->ctx.errors.flag & 1)) |
| 2149 | appctx->ctx.errors.px = appctx->ctx.errors.px->next; |
| 2150 | } |
| 2151 | |
| 2152 | /* dump complete */ |
| 2153 | return 1; |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2154 | |
| 2155 | cant_send_unlock: |
| 2156 | HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock); |
| 2157 | cant_send: |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2158 | si_rx_room_blk(si); |
Willy Tarreau | 36b2736 | 2018-09-07 19:55:44 +0200 | [diff] [blame] | 2159 | return 0; |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2160 | } |
| 2161 | |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 2162 | /* register cli keywords */ |
| 2163 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 15b9e68 | 2016-11-24 11:55:28 +0100 | [diff] [blame] | 2164 | { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL }, |
| 2165 | { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL }, |
Willy Tarreau | c429a1f | 2016-11-23 16:22:04 +0100 | [diff] [blame] | 2166 | { { "set", "maxconn", "frontend", NULL }, "set maxconn frontend : change a frontend's maxconn setting", cli_parse_set_maxconn_frontend, NULL }, |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 2167 | { { "show","servers", "state", NULL }, "show servers state [id]: dump volatile server information (for backend <id>)", cli_parse_show_servers, cli_io_handler_servers_state }, |
Willy Tarreau | 608ea59 | 2016-12-16 18:01:15 +0100 | [diff] [blame] | 2168 | { { "show", "backend", NULL }, "show backend : list backends in the current running config", NULL, cli_io_handler_show_backend }, |
Willy Tarreau | 5212d7f | 2016-11-24 11:13:06 +0100 | [diff] [blame] | 2169 | { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL }, |
Olivier Houchard | 614f8d7 | 2017-03-14 20:08:46 +0100 | [diff] [blame] | 2170 | { { "set", "dynamic-cookie-key", "backend", NULL }, "set dynamic-cookie-key backend : change a backend secret key for dynamic cookies", cli_parse_set_dyncookie_key_backend, NULL }, |
| 2171 | { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL }, |
| 2172 | { { "disable", "dynamic-cookie", "backend", NULL }, "disable dynamic-cookie backend : disable dynamic cookies on a specific backend", cli_parse_disable_dyncookie_backend, NULL }, |
Willy Tarreau | ddb68ac | 2018-09-07 18:34:24 +0200 | [diff] [blame] | 2173 | { { "show", "errors", NULL }, "show errors : report last request and response errors for each proxy", cli_parse_show_errors, cli_io_handler_show_errors, NULL }, |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 2174 | {{},} |
| 2175 | }}; |
| 2176 | |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 2177 | __attribute__((constructor)) |
| 2178 | static void __proxy_module_init(void) |
| 2179 | { |
| 2180 | cfg_register_keywords(&cfg_kws); |
William Lallemand | a6c5f33 | 2016-11-19 02:25:36 +0100 | [diff] [blame] | 2181 | cli_register_kw(&cli_kws); |
Willy Tarreau | 9de1bbd | 2008-07-09 20:34:27 +0200 | [diff] [blame] | 2182 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2183 | |
| 2184 | /* |
| 2185 | * Local variables: |
| 2186 | * c-indent-level: 8 |
| 2187 | * c-basic-offset: 8 |
| 2188 | * End: |
| 2189 | */ |