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