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