Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 5 | * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
Willy Tarreau | 0a4b0ab | 2020-06-11 11:22:44 +0200 | [diff] [blame] | 14 | #include <sys/types.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 15 | #include <netinet/tcp.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 16 | #include <ctype.h> |
Baptiste Assmann | e11cfcd | 2015-08-19 16:44:03 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 19 | #include <import/ebmbtree.h> |
| 20 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/api.h> |
Willy Tarreau | 3f0f82e | 2020-06-04 19:42:41 +0200 | [diff] [blame] | 22 | #include <haproxy/applet-t.h> |
Willy Tarreau | 4980160 | 2020-06-04 22:50:02 +0200 | [diff] [blame] | 23 | #include <haproxy/backend.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 25 | #include <haproxy/check.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 26 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 27 | #include <haproxy/connection.h> |
Willy Tarreau | 3afc4c4 | 2020-06-03 18:23:19 +0200 | [diff] [blame] | 28 | #include <haproxy/dict-t.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 29 | #include <haproxy/errors.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 30 | #include <haproxy/global.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
Willy Tarreau | cee013e | 2020-06-05 11:40:38 +0200 | [diff] [blame] | 32 | #include <haproxy/mailers.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 33 | #include <haproxy/namespace.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 34 | #include <haproxy/port_range.h> |
| 35 | #include <haproxy/protocol.h> |
Willy Tarreau | b00a8e3 | 2021-05-08 20:18:59 +0200 | [diff] [blame] | 36 | #include <haproxy/proxy.h> |
Willy Tarreau | a55c454 | 2020-06-04 22:59:39 +0200 | [diff] [blame] | 37 | #include <haproxy/queue.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 38 | #include <haproxy/resolvers.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 39 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 40 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 41 | #include <haproxy/server.h> |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 42 | #include <haproxy/stats.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 43 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 44 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 45 | #include <haproxy/task.h> |
Willy Tarreau | 51cd595 | 2020-06-05 12:25:38 +0200 | [diff] [blame] | 46 | #include <haproxy/tcpcheck.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 47 | #include <haproxy/time.h> |
Willy Tarreau | ba6300e | 2021-05-08 14:09:40 +0200 | [diff] [blame] | 48 | #include <haproxy/tools.h> |
Tim Duesterhus | d5fc8fc | 2021-09-11 17:51:13 +0200 | [diff] [blame] | 49 | #include <haproxy/xxhash.h> |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame^] | 50 | #include <haproxy/event_hdl.h> |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 51 | |
Baptiste Assmann | da29fe2 | 2019-06-13 13:24:29 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 53 | static void srv_update_status(struct server *s); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 54 | static int srv_apply_lastaddr(struct server *srv, int *err_code); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 55 | static void srv_cleanup_connections(struct server *srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 57 | /* extra keywords used as value for other arguments. They are used as |
| 58 | * suggestions for mistyped words. |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 59 | */ |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 60 | static const char *extra_kw_list[] = { |
| 61 | "ipv4", "ipv6", "legacy", "octet-count", |
Amaury Denoyelle | 3b89c11 | 2021-03-12 16:04:00 +0100 | [diff] [blame] | 62 | "fail-check", "sudden-death", "mark-down", |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 63 | NULL /* must be last */ |
| 64 | }; |
| 65 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 66 | /* List head of all known server keywords */ |
Willy Tarreau | ca1acd6 | 2022-03-29 15:02:44 +0200 | [diff] [blame] | 67 | struct srv_kw_list srv_keywords = { |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 68 | .list = LIST_HEAD_INIT(srv_keywords.list) |
| 69 | }; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 70 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 71 | __decl_thread(HA_SPINLOCK_T idle_conn_srv_lock); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 72 | struct eb_root idle_conn_srv = EB_ROOT; |
Willy Tarreau | 14015b8 | 2021-04-10 17:33:15 +0200 | [diff] [blame] | 73 | struct task *idle_conn_task __read_mostly = NULL; |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 74 | struct list servers_list = LIST_HEAD_INIT(servers_list); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 75 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 76 | /* SERVER DELETE(n)->ADD global tracker: |
| 77 | * This is meant to provide srv->rid (revision id) value. |
| 78 | * Revision id allows to differentiate between a previously existing |
| 79 | * deleted server and a new server reusing deleted server name/id. |
| 80 | * |
| 81 | * start value is 0 (even value) |
| 82 | * LSB is used to specify that one or multiple srv delete in a row |
| 83 | * were performed. |
| 84 | * When adding a new server, increment by 1 if current |
| 85 | * value is odd (odd = LSB set), |
| 86 | * because adding a new server after one or |
| 87 | * multiple deletions means we could potentially be reusing old names: |
| 88 | * Increase the revision id to prevent mixups between old and new names. |
| 89 | * |
| 90 | * srv->rid is calculated from cnt even values only. |
| 91 | * sizeof(srv_id_reuse_cnt) must be twice sizeof(srv->rid) |
| 92 | * |
| 93 | * Wraparound is expected and should not cause issues |
| 94 | * (with current design we allow up to 4 billion unique revisions) |
| 95 | * |
| 96 | * Counter is only used under thread_isolate (cli_add/cli_del), |
| 97 | * no need for atomic ops. |
| 98 | */ |
| 99 | static uint64_t srv_id_reuse_cnt = 0; |
| 100 | |
Frédéric Lécaille | 7da7129 | 2019-05-20 09:47:07 +0200 | [diff] [blame] | 101 | /* The server names dictionary */ |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 102 | struct dict server_key_dict = { |
| 103 | .name = "server keys", |
Frédéric Lécaille | 7da7129 | 2019-05-20 09:47:07 +0200 | [diff] [blame] | 104 | .values = EB_ROOT_UNIQUE, |
| 105 | }; |
| 106 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 107 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 108 | { |
Amaury Denoyelle | e6ba791 | 2020-10-29 15:59:05 +0100 | [diff] [blame] | 109 | if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 110 | return s->down_time; |
| 111 | |
| 112 | return now.tv_sec - s->last_change + s->down_time; |
| 113 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 115 | int srv_lastsession(const struct server *s) |
| 116 | { |
| 117 | if (s->counters.last_sess) |
| 118 | return now.tv_sec - s->counters.last_sess; |
| 119 | |
| 120 | return -1; |
| 121 | } |
| 122 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 123 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 124 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 125 | const struct server *s = check->server; |
| 126 | |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 127 | if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 128 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 129 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 130 | if ((s->next_state == SRV_ST_STOPPED) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 131 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 132 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 133 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 136 | /* |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame^] | 137 | * Use this to publish EVENT_HDL_SUB_SERVER family type event |
| 138 | * from srv facility |
| 139 | * Event will be published in both global subscription list and |
| 140 | * server dedicated subscription list |
| 141 | * server ptr must be valid |
| 142 | */ |
| 143 | static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate) |
| 144 | { |
| 145 | struct event_hdl_cb_data_server cb_data; |
| 146 | |
| 147 | /* safe data assignments */ |
| 148 | cb_data.safe.puid = srv->puid; |
| 149 | cb_data.safe.rid = srv->rid; |
| 150 | cb_data.safe.flags = srv->flags; |
| 151 | snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id); |
| 152 | if (srv->proxy) |
| 153 | snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id); |
| 154 | /* unsafe data assignments */ |
| 155 | cb_data.unsafe.ptr = srv; |
| 156 | cb_data.unsafe.thread_isolate = thread_isolate; |
| 157 | /* publish in server dedicated sub list */ |
| 158 | event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data)); |
| 159 | /* publish in global subscription list */ |
| 160 | event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data)); |
| 161 | } |
| 162 | |
| 163 | /* |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 164 | * Check that we did not get a hash collision. |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 165 | * Unlikely, but it can happen. The server's proxy must be at least |
| 166 | * read-locked. |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 167 | */ |
| 168 | static inline void srv_check_for_dup_dyncookie(struct server *s) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 169 | { |
| 170 | struct proxy *p = s->proxy; |
| 171 | struct server *tmpserv; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 172 | |
| 173 | for (tmpserv = p->srv; tmpserv != NULL; |
| 174 | tmpserv = tmpserv->next) { |
| 175 | if (tmpserv == s) |
| 176 | continue; |
| 177 | if (tmpserv->next_admin & SRV_ADMF_FMAINT) |
| 178 | continue; |
| 179 | if (tmpserv->cookie && |
| 180 | strcmp(tmpserv->cookie, s->cookie) == 0) { |
| 181 | ha_warning("We generated two equal cookies for two different servers.\n" |
| 182 | "Please change the secret key for '%s'.\n", |
| 183 | s->proxy->id); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 189 | /* |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 190 | * Must be called with the server lock held, and will read-lock the proxy. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 191 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 192 | void srv_set_dyncookie(struct server *s) |
| 193 | { |
| 194 | struct proxy *p = s->proxy; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 195 | char *tmpbuf; |
| 196 | unsigned long long hash_value; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 197 | size_t key_len; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 198 | size_t buffer_len; |
| 199 | int addr_len; |
| 200 | int port; |
| 201 | |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 202 | HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 203 | |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 204 | if ((s->flags & SRV_F_COOKIESET) || |
| 205 | !(s->proxy->ck_opts & PR_CK_DYNAMIC) || |
| 206 | s->proxy->dyncookie_key == NULL) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 207 | goto out; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 208 | key_len = strlen(p->dyncookie_key); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 209 | |
| 210 | if (s->addr.ss_family != AF_INET && |
| 211 | s->addr.ss_family != AF_INET6) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 212 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 213 | /* |
| 214 | * Buffer to calculate the cookie value. |
| 215 | * The buffer contains the secret key + the server IP address |
| 216 | * + the TCP port. |
| 217 | */ |
| 218 | addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16; |
| 219 | /* |
| 220 | * The TCP port should use only 2 bytes, but is stored in |
| 221 | * an unsigned int in struct server, so let's use 4, to be |
| 222 | * on the safe side. |
| 223 | */ |
| 224 | buffer_len = key_len + addr_len + 4; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 225 | tmpbuf = trash.area; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 226 | memcpy(tmpbuf, p->dyncookie_key, key_len); |
| 227 | memcpy(&(tmpbuf[key_len]), |
| 228 | s->addr.ss_family == AF_INET ? |
| 229 | (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr : |
| 230 | (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr), |
| 231 | addr_len); |
| 232 | /* |
| 233 | * Make sure it's the same across all the load balancers, |
| 234 | * no matter their endianness. |
| 235 | */ |
| 236 | port = htonl(s->svc_port); |
| 237 | memcpy(&tmpbuf[key_len + addr_len], &port, 4); |
| 238 | hash_value = XXH64(tmpbuf, buffer_len, 0); |
| 239 | memprintf(&s->cookie, "%016llx", hash_value); |
| 240 | if (!s->cookie) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 241 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 242 | s->cklen = 16; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 243 | |
| 244 | /* Don't bother checking if the dyncookie is duplicated if |
| 245 | * the server is marked as "disabled", maybe it doesn't have |
| 246 | * its real IP yet, but just a place holder. |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 247 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 248 | if (!(s->next_admin & SRV_ADMF_FMAINT)) |
| 249 | srv_check_for_dup_dyncookie(s); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 250 | out: |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 251 | HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 254 | /* Returns true if it's possible to reuse an idle connection from server <srv> |
| 255 | * for a websocket stream. This is the case if server is configured to use the |
| 256 | * same protocol for both HTTP and websocket streams. This depends on the value |
| 257 | * of "proto", "alpn" and "ws" keywords. |
| 258 | */ |
| 259 | int srv_check_reuse_ws(struct server *srv) |
| 260 | { |
| 261 | if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) { |
| 262 | /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used |
| 263 | * for mux selection. |
| 264 | */ |
| 265 | const struct ist srv_mux = srv->mux_proto ? |
| 266 | srv->mux_proto->token : IST_NULL; |
| 267 | |
| 268 | switch (srv->ws) { |
| 269 | /* "auto" means use the same protocol : reuse is possible. */ |
| 270 | case SRV_WS_AUTO: |
| 271 | return 1; |
| 272 | |
| 273 | /* "h2" means use h2 for websocket : reuse is possible if |
| 274 | * server mux is h2. |
| 275 | */ |
| 276 | case SRV_WS_H2: |
| 277 | if (srv->mux_proto && isteq(srv_mux, ist("h2"))) |
| 278 | return 1; |
| 279 | break; |
| 280 | |
| 281 | /* "h1" means use h1 for websocket : reuse is possible if |
| 282 | * server mux is h1. |
| 283 | */ |
| 284 | case SRV_WS_H1: |
| 285 | if (!srv->mux_proto || isteq(srv_mux, ist("h1"))) |
| 286 | return 1; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | else { |
| 291 | /* ALPN selection. |
| 292 | * Based on the assumption that only "h2" and "http/1.1" token |
| 293 | * are used on server ALPN. |
| 294 | */ |
| 295 | const struct ist alpn = ist2(srv->ssl_ctx.alpn_str, |
| 296 | srv->ssl_ctx.alpn_len); |
| 297 | |
| 298 | switch (srv->ws) { |
| 299 | case SRV_WS_AUTO: |
| 300 | /* for auto mode, consider reuse as possible if the |
| 301 | * server uses a single protocol ALPN |
| 302 | */ |
| 303 | if (!istchr(alpn, ',')) |
| 304 | return 1; |
| 305 | break; |
| 306 | |
| 307 | case SRV_WS_H2: |
| 308 | return isteq(alpn, ist("\x02h2")); |
| 309 | |
| 310 | case SRV_WS_H1: |
| 311 | return isteq(alpn, ist("\x08http/1.1")); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* Return the proto to used for a websocket stream on <srv> without ALPN. NULL |
| 319 | * is a valid value indicating to use the fallback mux. |
| 320 | */ |
| 321 | const struct mux_ops *srv_get_ws_proto(struct server *srv) |
| 322 | { |
| 323 | const struct mux_proto_list *mux = NULL; |
| 324 | |
| 325 | switch (srv->ws) { |
| 326 | case SRV_WS_AUTO: |
| 327 | mux = srv->mux_proto; |
| 328 | break; |
| 329 | |
| 330 | case SRV_WS_H1: |
| 331 | mux = get_mux_proto(ist("h1")); |
| 332 | break; |
| 333 | |
| 334 | case SRV_WS_H2: |
| 335 | mux = get_mux_proto(ist("h2")); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return mux ? mux->mux : NULL; |
| 340 | } |
| 341 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 342 | /* |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 343 | * Must be called with the server lock held. The server is first removed from |
| 344 | * the proxy tree if it was already attached. If <reattach> is true, the server |
| 345 | * will then be attached in the proxy tree. The proxy lock is held to |
| 346 | * manipulate the tree. |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 347 | */ |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 348 | static void srv_set_addr_desc(struct server *s, int reattach) |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 349 | { |
| 350 | struct proxy *p = s->proxy; |
| 351 | char *key; |
| 352 | |
| 353 | key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS); |
| 354 | |
| 355 | if (s->addr_node.key) { |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 356 | if (key && strcmp(key, s->addr_node.key) == 0) { |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 357 | free(key); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 362 | ebpt_delete(&s->addr_node); |
| 363 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 364 | |
| 365 | free(s->addr_node.key); |
| 366 | } |
| 367 | |
| 368 | s->addr_node.key = key; |
| 369 | |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 370 | if (reattach) { |
| 371 | if (s->addr_node.key) { |
| 372 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 373 | ebis_insert(&p->used_server_addr, &s->addr_node); |
| 374 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 375 | } |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 376 | } |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 380 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 381 | * parsing sessions. |
| 382 | */ |
| 383 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 384 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 385 | LIST_APPEND(&srv_keywords.list, &kwl->list); |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 389 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 390 | * find one with a valid ->parse() function. This way it is possible to declare |
| 391 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 392 | * if some options are met. Note that if the requested keyword contains an |
| 393 | * opening parenthesis, everything from this point is ignored. |
| 394 | */ |
| 395 | struct srv_kw *srv_find_kw(const char *kw) |
| 396 | { |
| 397 | int index; |
| 398 | const char *kwend; |
| 399 | struct srv_kw_list *kwl; |
| 400 | struct srv_kw *ret = NULL; |
| 401 | |
| 402 | kwend = strchr(kw, '('); |
| 403 | if (!kwend) |
| 404 | kwend = kw + strlen(kw); |
| 405 | |
| 406 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 407 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 408 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 409 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 410 | if (kwl->kw[index].parse) |
| 411 | return &kwl->kw[index]; /* found it !*/ |
| 412 | else |
| 413 | ret = &kwl->kw[index]; /* may be OK */ |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 421 | * unsupported keywords are only dumped if their supported form was not |
| 422 | * found. |
| 423 | */ |
| 424 | void srv_dump_kws(char **out) |
| 425 | { |
| 426 | struct srv_kw_list *kwl; |
| 427 | int index; |
| 428 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 429 | if (!out) |
| 430 | return; |
| 431 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 432 | *out = NULL; |
| 433 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 434 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 435 | if (kwl->kw[index].parse || |
| 436 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 437 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 438 | kwl->scope, |
| 439 | kwl->kw[index].kw, |
| 440 | kwl->kw[index].skip ? " <arg>" : "", |
| 441 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 442 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 443 | } |
| 444 | } |
| 445 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Try to find in srv_keyword the word that looks closest to <word> by counting |
| 449 | * transitions between letters, digits and other characters. Will return the |
| 450 | * best matching word if found, otherwise NULL. An optional array of extra |
| 451 | * words to compare may be passed in <extra>, but it must then be terminated |
| 452 | * by a NULL entry. If unused it may be NULL. |
| 453 | */ |
| 454 | static const char *srv_find_best_kw(const char *word) |
| 455 | { |
| 456 | uint8_t word_sig[1024]; |
| 457 | uint8_t list_sig[1024]; |
| 458 | const struct srv_kw_list *kwl; |
| 459 | const char *best_ptr = NULL; |
| 460 | int dist, best_dist = INT_MAX; |
| 461 | const char **extra; |
| 462 | int index; |
| 463 | |
| 464 | make_word_fingerprint(word_sig, word); |
| 465 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 466 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 467 | make_word_fingerprint(list_sig, kwl->kw[index].kw); |
| 468 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 469 | if (dist < best_dist) { |
| 470 | best_dist = dist; |
| 471 | best_ptr = kwl->kw[index].kw; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 476 | for (extra = extra_kw_list; *extra; extra++) { |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 477 | make_word_fingerprint(list_sig, *extra); |
| 478 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 479 | if (dist < best_dist) { |
| 480 | best_dist = dist; |
| 481 | best_ptr = *extra; |
| 482 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr))) |
| 486 | best_ptr = NULL; |
| 487 | |
| 488 | return best_ptr; |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 489 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 490 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 491 | /* Parse the "backup" server keyword */ |
| 492 | static int srv_parse_backup(char **args, int *cur_arg, |
| 493 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 494 | { |
| 495 | newsrv->flags |= SRV_F_BACKUP; |
| 496 | return 0; |
| 497 | } |
| 498 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 499 | |
Frédéric Lécaille | 9d1b95b | 2017-03-15 09:13:33 +0100 | [diff] [blame] | 500 | /* Parse the "cookie" server keyword */ |
| 501 | static int srv_parse_cookie(char **args, int *cur_arg, |
| 502 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 503 | { |
| 504 | char *arg; |
| 505 | |
| 506 | arg = args[*cur_arg + 1]; |
| 507 | if (!*arg) { |
| 508 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 509 | return ERR_ALERT | ERR_FATAL; |
| 510 | } |
| 511 | |
| 512 | free(newsrv->cookie); |
| 513 | newsrv->cookie = strdup(arg); |
| 514 | newsrv->cklen = strlen(arg); |
| 515 | newsrv->flags |= SRV_F_COOKIESET; |
| 516 | return 0; |
| 517 | } |
| 518 | |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 519 | /* Parse the "disabled" server keyword */ |
| 520 | static int srv_parse_disabled(char **args, int *cur_arg, |
| 521 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 522 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 523 | newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 524 | newsrv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 525 | newsrv->check.state |= CHK_ST_PAUSED; |
| 526 | newsrv->check.health = 0; |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /* Parse the "enabled" server keyword */ |
| 531 | static int srv_parse_enabled(char **args, int *cur_arg, |
| 532 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 533 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 534 | newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT; |
| 535 | newsrv->next_state = SRV_ST_RUNNING; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 536 | newsrv->check.state &= ~CHK_ST_PAUSED; |
| 537 | newsrv->check.health = newsrv->check.rise; |
| 538 | return 0; |
| 539 | } |
| 540 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 541 | /* Parse the "error-limit" server keyword */ |
| 542 | static int srv_parse_error_limit(char **args, int *cur_arg, |
| 543 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 544 | { |
| 545 | if (!*args[*cur_arg + 1]) { |
| 546 | memprintf(err, "'%s' expects an integer argument.", |
| 547 | args[*cur_arg]); |
| 548 | return ERR_ALERT | ERR_FATAL; |
| 549 | } |
| 550 | |
| 551 | newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]); |
| 552 | |
| 553 | if (newsrv->consecutive_errors_limit <= 0) { |
| 554 | memprintf(err, "%s has to be > 0.", |
| 555 | args[*cur_arg]); |
| 556 | return ERR_ALERT | ERR_FATAL; |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Amaury Denoyelle | f9d5957 | 2021-10-18 14:40:29 +0200 | [diff] [blame] | 562 | /* Parse the "ws" keyword */ |
| 563 | static int srv_parse_ws(char **args, int *cur_arg, |
| 564 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 565 | { |
| 566 | if (!args[*cur_arg + 1]) { |
| 567 | memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]); |
| 568 | return ERR_ALERT | ERR_FATAL; |
| 569 | } |
| 570 | |
| 571 | if (strcmp(args[*cur_arg + 1], "h1") == 0) { |
| 572 | newsrv->ws = SRV_WS_H1; |
| 573 | } |
| 574 | else if (strcmp(args[*cur_arg + 1], "h2") == 0) { |
| 575 | newsrv->ws = SRV_WS_H2; |
| 576 | } |
| 577 | else if (strcmp(args[*cur_arg + 1], "auto") == 0) { |
| 578 | newsrv->ws = SRV_WS_AUTO; |
| 579 | } |
| 580 | else { |
| 581 | memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]); |
| 582 | return ERR_ALERT | ERR_FATAL; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 589 | /* Parse the "init-addr" server keyword */ |
| 590 | static int srv_parse_init_addr(char **args, int *cur_arg, |
| 591 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 592 | { |
| 593 | char *p, *end; |
| 594 | int done; |
| 595 | struct sockaddr_storage sa; |
| 596 | |
| 597 | newsrv->init_addr_methods = 0; |
| 598 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 599 | |
| 600 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 601 | /* cut on next comma */ |
| 602 | for (end = p; *end && *end != ','; end++); |
| 603 | if (*end) |
| 604 | *(end++) = 0; |
| 605 | |
| 606 | memset(&sa, 0, sizeof(sa)); |
| 607 | if (strcmp(p, "libc") == 0) { |
| 608 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 609 | } |
| 610 | else if (strcmp(p, "last") == 0) { |
| 611 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 612 | } |
| 613 | else if (strcmp(p, "none") == 0) { |
| 614 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 615 | } |
| 616 | else if (str2ip2(p, &sa, 0)) { |
| 617 | if (is_addr(&newsrv->init_addr)) { |
| 618 | memprintf(err, "'%s' : initial address already specified, cannot add '%s'.", |
| 619 | args[*cur_arg], p); |
| 620 | return ERR_ALERT | ERR_FATAL; |
| 621 | } |
| 622 | newsrv->init_addr = sa; |
| 623 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 624 | } |
| 625 | else { |
| 626 | memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.", |
| 627 | args[*cur_arg], p); |
| 628 | return ERR_ALERT | ERR_FATAL; |
| 629 | } |
| 630 | if (!done) { |
| 631 | memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'", |
| 632 | args[*cur_arg], p); |
| 633 | return ERR_ALERT | ERR_FATAL; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | /* Parse the "log-proto" server keyword */ |
| 641 | static int srv_parse_log_proto(char **args, int *cur_arg, |
| 642 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 643 | { |
| 644 | if (strcmp(args[*cur_arg + 1], "legacy") == 0) |
| 645 | newsrv->log_proto = SRV_LOG_PROTO_LEGACY; |
| 646 | else if (strcmp(args[*cur_arg + 1], "octet-count") == 0) |
| 647 | newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING; |
| 648 | else { |
| 649 | memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'", |
| 650 | args[*cur_arg], args[*cur_arg + 1]); |
| 651 | return ERR_ALERT | ERR_FATAL; |
| 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* Parse the "maxconn" server keyword */ |
| 658 | static int srv_parse_maxconn(char **args, int *cur_arg, |
| 659 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 660 | { |
| 661 | newsrv->maxconn = atol(args[*cur_arg + 1]); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* Parse the "maxqueue" server keyword */ |
| 666 | static int srv_parse_maxqueue(char **args, int *cur_arg, |
| 667 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 668 | { |
| 669 | newsrv->maxqueue = atol(args[*cur_arg + 1]); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* Parse the "minconn" server keyword */ |
| 674 | static int srv_parse_minconn(char **args, int *cur_arg, |
| 675 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 676 | { |
| 677 | newsrv->minconn = atol(args[*cur_arg + 1]); |
| 678 | return 0; |
| 679 | } |
| 680 | |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 681 | static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 682 | { |
| 683 | char *arg; |
| 684 | |
| 685 | arg = args[*cur_arg + 1]; |
| 686 | if (!*arg) { |
| 687 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 688 | return ERR_ALERT | ERR_FATAL; |
| 689 | } |
| 690 | newsrv->max_reuse = atoi(arg); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 695 | static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 696 | { |
| 697 | const char *res; |
| 698 | char *arg; |
| 699 | unsigned int time; |
| 700 | |
| 701 | arg = args[*cur_arg + 1]; |
| 702 | if (!*arg) { |
| 703 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 704 | return ERR_ALERT | ERR_FATAL; |
| 705 | } |
| 706 | res = parse_time_err(arg, &time, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 707 | if (res == PARSE_TIME_OVER) { |
| 708 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 709 | args[*cur_arg+1], args[*cur_arg]); |
| 710 | return ERR_ALERT | ERR_FATAL; |
| 711 | } |
| 712 | else if (res == PARSE_TIME_UNDER) { |
| 713 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 714 | args[*cur_arg+1], args[*cur_arg]); |
| 715 | return ERR_ALERT | ERR_FATAL; |
| 716 | } |
| 717 | else if (res) { |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 718 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", |
| 719 | *res, args[*cur_arg]); |
| 720 | return ERR_ALERT | ERR_FATAL; |
| 721 | } |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 722 | newsrv->pool_purge_delay = time; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 727 | static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 728 | { |
| 729 | char *arg; |
| 730 | |
| 731 | arg = args[*cur_arg + 1]; |
| 732 | if (!*arg) { |
| 733 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 734 | return ERR_ALERT | ERR_FATAL; |
| 735 | } |
| 736 | |
| 737 | newsrv->low_idle_conns = atoi(arg); |
| 738 | return 0; |
| 739 | } |
| 740 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 741 | static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 742 | { |
| 743 | char *arg; |
| 744 | |
| 745 | arg = args[*cur_arg + 1]; |
| 746 | if (!*arg) { |
| 747 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 748 | return ERR_ALERT | ERR_FATAL; |
| 749 | } |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 750 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 751 | newsrv->max_idle_conns = atoi(arg); |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 752 | if ((int)newsrv->max_idle_conns < -1) { |
| 753 | memprintf(err, "'%s' must be >= -1", args[*cur_arg]); |
| 754 | return ERR_ALERT | ERR_FATAL; |
| 755 | } |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 760 | /* parse the "id" server keyword */ |
| 761 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 762 | { |
| 763 | struct eb32_node *node; |
| 764 | |
| 765 | if (!*args[*cur_arg + 1]) { |
| 766 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 767 | return ERR_ALERT | ERR_FATAL; |
| 768 | } |
| 769 | |
| 770 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 771 | newsrv->conf.id.key = newsrv->puid; |
| 772 | |
| 773 | if (newsrv->puid <= 0) { |
| 774 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 775 | return ERR_ALERT | ERR_FATAL; |
| 776 | } |
| 777 | |
| 778 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 779 | if (node) { |
| 780 | struct server *target = container_of(node, struct server, conf.id); |
| 781 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 782 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 783 | target->id); |
| 784 | return ERR_ALERT | ERR_FATAL; |
| 785 | } |
| 786 | |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 787 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 788 | return 0; |
| 789 | } |
| 790 | |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 791 | /* Parse the "namespace" server keyword */ |
| 792 | static int srv_parse_namespace(char **args, int *cur_arg, |
| 793 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 794 | { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 795 | #ifdef USE_NS |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 796 | char *arg; |
| 797 | |
| 798 | arg = args[*cur_arg + 1]; |
| 799 | if (!*arg) { |
| 800 | memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]); |
| 801 | return ERR_ALERT | ERR_FATAL; |
| 802 | } |
| 803 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 804 | if (strcmp(arg, "*") == 0) { |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 805 | /* Use the namespace associated with the connection (if present). */ |
| 806 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * As this parser may be called several times for the same 'default-server' |
| 812 | * object, or for a new 'server' instance deriving from a 'default-server' |
| 813 | * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it. |
| 814 | */ |
| 815 | newsrv->flags &= ~SRV_F_USE_NS_FROM_PP; |
| 816 | |
| 817 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 818 | if (!newsrv->netns) |
| 819 | newsrv->netns = netns_store_insert(arg); |
| 820 | |
| 821 | if (!newsrv->netns) { |
| 822 | memprintf(err, "Cannot open namespace '%s'", arg); |
| 823 | return ERR_ALERT | ERR_FATAL; |
| 824 | } |
| 825 | |
| 826 | return 0; |
| 827 | #else |
| 828 | memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]); |
| 829 | return ERR_ALERT | ERR_FATAL; |
| 830 | #endif |
| 831 | } |
| 832 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 833 | /* Parse the "no-backup" server keyword */ |
| 834 | static int srv_parse_no_backup(char **args, int *cur_arg, |
| 835 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 836 | { |
| 837 | newsrv->flags &= ~SRV_F_BACKUP; |
| 838 | return 0; |
| 839 | } |
| 840 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 841 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 842 | /* Disable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 843 | static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags) |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 844 | { |
| 845 | srv->pp_opts &= ~flags; |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* Parse the "no-send-proxy" server keyword */ |
| 850 | static int srv_parse_no_send_proxy(char **args, int *cur_arg, |
| 851 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 852 | { |
| 853 | return srv_disable_pp_flags(newsrv, SRV_PP_V1); |
| 854 | } |
| 855 | |
| 856 | /* Parse the "no-send-proxy-v2" server keyword */ |
| 857 | static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg, |
| 858 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 859 | { |
| 860 | return srv_disable_pp_flags(newsrv, SRV_PP_V2); |
| 861 | } |
| 862 | |
Frédéric Lécaille | 36d1565 | 2022-10-17 14:58:19 +0200 | [diff] [blame] | 863 | /* Parse the "shard" server keyword */ |
| 864 | static int srv_parse_shard(char **args, int *cur_arg, |
| 865 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 866 | { |
| 867 | newsrv->shard = atol(args[*cur_arg + 1]); |
| 868 | return 0; |
| 869 | } |
| 870 | |
Frédéric Lécaille | 1b9423d | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 871 | /* Parse the "no-tfo" server keyword */ |
| 872 | static int srv_parse_no_tfo(char **args, int *cur_arg, |
| 873 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 874 | { |
| 875 | newsrv->flags &= ~SRV_F_FASTOPEN; |
| 876 | return 0; |
| 877 | } |
| 878 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 879 | /* Parse the "non-stick" server keyword */ |
| 880 | static int srv_parse_non_stick(char **args, int *cur_arg, |
| 881 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 882 | { |
| 883 | newsrv->flags |= SRV_F_NON_STICK; |
| 884 | return 0; |
| 885 | } |
| 886 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 887 | /* Enable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 888 | static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags) |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 889 | { |
| 890 | srv->pp_opts |= flags; |
| 891 | return 0; |
| 892 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 893 | /* parse the "proto" server keyword */ |
| 894 | static int srv_parse_proto(char **args, int *cur_arg, |
| 895 | struct proxy *px, struct server *newsrv, char **err) |
| 896 | { |
| 897 | struct ist proto; |
| 898 | |
| 899 | if (!*args[*cur_arg + 1]) { |
| 900 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 901 | return ERR_ALERT | ERR_FATAL; |
| 902 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 903 | proto = ist(args[*cur_arg + 1]); |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 904 | newsrv->mux_proto = get_mux_proto(proto); |
| 905 | if (!newsrv->mux_proto) { |
| 906 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 907 | return ERR_ALERT | ERR_FATAL; |
| 908 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 909 | return 0; |
| 910 | } |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 911 | |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 912 | /* parse the "proxy-v2-options" */ |
| 913 | static int srv_parse_proxy_v2_options(char **args, int *cur_arg, |
| 914 | struct proxy *px, struct server *newsrv, char **err) |
| 915 | { |
| 916 | char *p, *n; |
| 917 | for (p = args[*cur_arg+1]; p; p = n) { |
| 918 | n = strchr(p, ','); |
| 919 | if (n) |
| 920 | *n++ = '\0'; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 921 | if (strcmp(p, "ssl") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 922 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 923 | } else if (strcmp(p, "cert-cn") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 924 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 925 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 926 | } else if (strcmp(p, "cert-key") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 927 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 928 | newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 929 | } else if (strcmp(p, "cert-sig") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 930 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 931 | newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 932 | } else if (strcmp(p, "ssl-cipher") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 933 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 934 | newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 935 | } else if (strcmp(p, "authority") == 0) { |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 936 | newsrv->pp_opts |= SRV_PP_V2_AUTHORITY; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 937 | } else if (strcmp(p, "crc32c") == 0) { |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 938 | newsrv->pp_opts |= SRV_PP_V2_CRC32C; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 939 | } else if (strcmp(p, "unique-id") == 0) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 940 | newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID; |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 941 | } else |
| 942 | goto fail; |
| 943 | } |
| 944 | return 0; |
| 945 | fail: |
| 946 | if (err) |
| 947 | memprintf(err, "'%s' : proxy v2 option not implemented", p); |
| 948 | return ERR_ALERT | ERR_FATAL; |
| 949 | } |
| 950 | |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 951 | /* Parse the "observe" server keyword */ |
| 952 | static int srv_parse_observe(char **args, int *cur_arg, |
| 953 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 954 | { |
| 955 | char *arg; |
| 956 | |
| 957 | arg = args[*cur_arg + 1]; |
| 958 | if (!*arg) { |
| 959 | memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]); |
| 960 | return ERR_ALERT | ERR_FATAL; |
| 961 | } |
| 962 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 963 | if (strcmp(arg, "none") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 964 | newsrv->observe = HANA_OBS_NONE; |
| 965 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 966 | else if (strcmp(arg, "layer4") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 967 | newsrv->observe = HANA_OBS_LAYER4; |
| 968 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 969 | else if (strcmp(arg, "layer7") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 970 | if (curproxy->mode != PR_MODE_HTTP) { |
| 971 | memprintf(err, "'%s' can only be used in http proxies.\n", arg); |
| 972 | return ERR_ALERT; |
| 973 | } |
| 974 | newsrv->observe = HANA_OBS_LAYER7; |
| 975 | } |
| 976 | else { |
| 977 | memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' " |
| 978 | "but got '%s'\n", args[*cur_arg], arg); |
| 979 | return ERR_ALERT | ERR_FATAL; |
| 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 985 | /* Parse the "on-error" server keyword */ |
| 986 | static int srv_parse_on_error(char **args, int *cur_arg, |
| 987 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 988 | { |
| 989 | if (strcmp(args[*cur_arg + 1], "fastinter") == 0) |
| 990 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 991 | else if (strcmp(args[*cur_arg + 1], "fail-check") == 0) |
| 992 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 993 | else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0) |
| 994 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 995 | else if (strcmp(args[*cur_arg + 1], "mark-down") == 0) |
| 996 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 997 | else { |
| 998 | memprintf(err, "'%s' expects one of 'fastinter', " |
| 999 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'", |
| 1000 | args[*cur_arg], args[*cur_arg + 1]); |
| 1001 | return ERR_ALERT | ERR_FATAL; |
| 1002 | } |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | /* Parse the "on-marked-down" server keyword */ |
| 1008 | static int srv_parse_on_marked_down(char **args, int *cur_arg, |
| 1009 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1010 | { |
| 1011 | if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0) |
| 1012 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1013 | else { |
| 1014 | memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'", |
| 1015 | args[*cur_arg], args[*cur_arg + 1]); |
| 1016 | return ERR_ALERT | ERR_FATAL; |
| 1017 | } |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | /* Parse the "on-marked-up" server keyword */ |
| 1023 | static int srv_parse_on_marked_up(char **args, int *cur_arg, |
| 1024 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1025 | { |
| 1026 | if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0) |
| 1027 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1028 | else { |
| 1029 | memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'", |
| 1030 | args[*cur_arg], args[*cur_arg + 1]); |
| 1031 | return ERR_ALERT | ERR_FATAL; |
| 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 1037 | /* Parse the "redir" server keyword */ |
| 1038 | static int srv_parse_redir(char **args, int *cur_arg, |
| 1039 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1040 | { |
| 1041 | char *arg; |
| 1042 | |
| 1043 | arg = args[*cur_arg + 1]; |
| 1044 | if (!*arg) { |
| 1045 | memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]); |
| 1046 | return ERR_ALERT | ERR_FATAL; |
| 1047 | } |
| 1048 | |
| 1049 | free(newsrv->rdr_pfx); |
| 1050 | newsrv->rdr_pfx = strdup(arg); |
| 1051 | newsrv->rdr_len = strlen(arg); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1056 | /* Parse the "resolvers" server keyword */ |
| 1057 | static int srv_parse_resolvers(char **args, int *cur_arg, |
| 1058 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1059 | { |
| 1060 | free(newsrv->resolvers_id); |
| 1061 | newsrv->resolvers_id = strdup(args[*cur_arg + 1]); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | /* Parse the "resolve-net" server keyword */ |
| 1066 | static int srv_parse_resolve_net(char **args, int *cur_arg, |
| 1067 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1068 | { |
| 1069 | char *p, *e; |
| 1070 | unsigned char mask; |
| 1071 | struct resolv_options *opt; |
| 1072 | |
| 1073 | if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') { |
| 1074 | memprintf(err, "'%s' expects a list of networks.", |
| 1075 | args[*cur_arg]); |
| 1076 | return ERR_ALERT | ERR_FATAL; |
| 1077 | } |
| 1078 | |
| 1079 | opt = &newsrv->resolv_opts; |
| 1080 | |
| 1081 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 1082 | * string network in in_addr or in6_addr. |
| 1083 | */ |
| 1084 | p = args[*cur_arg + 1]; |
| 1085 | e = p; |
| 1086 | while (*p != '\0') { |
| 1087 | /* If no room available, return error. */ |
| 1088 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
| 1089 | memprintf(err, "'%s' exceed %d networks.", |
| 1090 | args[*cur_arg], SRV_MAX_PREF_NET); |
| 1091 | return ERR_ALERT | ERR_FATAL; |
| 1092 | } |
| 1093 | /* look for end or comma. */ |
| 1094 | while (*e != ',' && *e != '\0') |
| 1095 | e++; |
| 1096 | if (*e == ',') { |
| 1097 | *e = '\0'; |
| 1098 | e++; |
| 1099 | } |
| 1100 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 1101 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 1102 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 1103 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 1104 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 1105 | &mask)) { |
| 1106 | /* Try to convert input string from ipv6 network. */ |
| 1107 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 1108 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 1109 | } else { |
| 1110 | /* All network conversions fail, return error. */ |
| 1111 | memprintf(err, "'%s' invalid network '%s'.", |
| 1112 | args[*cur_arg], p); |
| 1113 | return ERR_ALERT | ERR_FATAL; |
| 1114 | } |
| 1115 | opt->pref_net_nb++; |
| 1116 | p = e; |
| 1117 | } |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | /* Parse the "resolve-opts" server keyword */ |
| 1123 | static int srv_parse_resolve_opts(char **args, int *cur_arg, |
| 1124 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1125 | { |
| 1126 | char *p, *end; |
| 1127 | |
| 1128 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 1129 | /* cut on next comma */ |
| 1130 | for (end = p; *end && *end != ','; end++); |
| 1131 | if (*end) |
| 1132 | *(end++) = 0; |
| 1133 | |
| 1134 | if (strcmp(p, "allow-dup-ip") == 0) { |
| 1135 | newsrv->resolv_opts.accept_duplicate_ip = 1; |
| 1136 | } |
| 1137 | else if (strcmp(p, "ignore-weight") == 0) { |
| 1138 | newsrv->resolv_opts.ignore_weight = 1; |
| 1139 | } |
| 1140 | else if (strcmp(p, "prevent-dup-ip") == 0) { |
| 1141 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
| 1142 | } |
| 1143 | else { |
| 1144 | memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.", |
| 1145 | args[*cur_arg], p); |
| 1146 | return ERR_ALERT | ERR_FATAL; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /* Parse the "resolve-prefer" server keyword */ |
| 1154 | static int srv_parse_resolve_prefer(char **args, int *cur_arg, |
| 1155 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1156 | { |
| 1157 | if (strcmp(args[*cur_arg + 1], "ipv4") == 0) |
| 1158 | newsrv->resolv_opts.family_prio = AF_INET; |
| 1159 | else if (strcmp(args[*cur_arg + 1], "ipv6") == 0) |
| 1160 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 1161 | else { |
| 1162 | memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.", |
| 1163 | args[*cur_arg]); |
| 1164 | return ERR_ALERT | ERR_FATAL; |
| 1165 | } |
| 1166 | |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1170 | /* Parse the "send-proxy" server keyword */ |
| 1171 | static int srv_parse_send_proxy(char **args, int *cur_arg, |
| 1172 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1173 | { |
| 1174 | return srv_enable_pp_flags(newsrv, SRV_PP_V1); |
| 1175 | } |
| 1176 | |
| 1177 | /* Parse the "send-proxy-v2" server keyword */ |
| 1178 | static int srv_parse_send_proxy_v2(char **args, int *cur_arg, |
| 1179 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1180 | { |
| 1181 | return srv_enable_pp_flags(newsrv, SRV_PP_V2); |
| 1182 | } |
| 1183 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1184 | /* Parse the "slowstart" server keyword */ |
| 1185 | static int srv_parse_slowstart(char **args, int *cur_arg, |
| 1186 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1187 | { |
| 1188 | /* slowstart is stored in seconds */ |
| 1189 | unsigned int val; |
| 1190 | const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS); |
| 1191 | |
| 1192 | if (time_err == PARSE_TIME_OVER) { |
| 1193 | memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1194 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1195 | return ERR_ALERT | ERR_FATAL; |
| 1196 | } |
| 1197 | else if (time_err == PARSE_TIME_UNDER) { |
| 1198 | memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1199 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1200 | return ERR_ALERT | ERR_FATAL; |
| 1201 | } |
| 1202 | else if (time_err) { |
| 1203 | memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.", |
| 1204 | *time_err, newsrv->id); |
| 1205 | return ERR_ALERT | ERR_FATAL; |
| 1206 | } |
| 1207 | newsrv->slowstart = (val + 999) / 1000; |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1211 | |
| 1212 | /* Parse the "source" server keyword */ |
| 1213 | static int srv_parse_source(char **args, int *cur_arg, |
| 1214 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1215 | { |
| 1216 | char *errmsg; |
| 1217 | int port_low, port_high; |
| 1218 | struct sockaddr_storage *sk; |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1219 | |
| 1220 | errmsg = NULL; |
| 1221 | |
| 1222 | if (!*args[*cur_arg + 1]) { |
| 1223 | memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, " |
| 1224 | "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface"); |
| 1225 | goto err; |
| 1226 | } |
| 1227 | |
| 1228 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1229 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1230 | &errmsg, NULL, NULL, |
| 1231 | PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1232 | if (!sk) { |
| 1233 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1234 | goto err; |
| 1235 | } |
| 1236 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1237 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1238 | newsrv->conn_src.source_addr = *sk; |
| 1239 | |
| 1240 | if (port_low != port_high) { |
| 1241 | int i; |
| 1242 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1243 | newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1); |
Remi Tricot-Le Breton | f1800e6 | 2021-05-12 09:44:06 +0200 | [diff] [blame] | 1244 | if (!newsrv->conn_src.sport_range) { |
| 1245 | ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]); |
| 1246 | goto err; |
| 1247 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1248 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1249 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1250 | } |
| 1251 | |
| 1252 | *cur_arg += 2; |
| 1253 | while (*(args[*cur_arg])) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1254 | if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */ |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1255 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 1256 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1257 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 1258 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1259 | goto err; |
| 1260 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1261 | if (strcmp(args[*cur_arg + 1], "client") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1262 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1263 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1264 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1265 | else if (strcmp(args[*cur_arg + 1], "clientip") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1266 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1267 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1268 | } |
| 1269 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 1270 | char *name, *end; |
| 1271 | |
| 1272 | name = args[*cur_arg + 1] + 7; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1273 | while (isspace((unsigned char)*name)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1274 | name++; |
| 1275 | |
| 1276 | end = name; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1277 | while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')') |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1278 | end++; |
| 1279 | |
| 1280 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1281 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1282 | free(newsrv->conn_src.bind_hdr_name); |
| 1283 | newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1); |
Remi Tricot-Le Breton | f1800e6 | 2021-05-12 09:44:06 +0200 | [diff] [blame] | 1284 | if (!newsrv->conn_src.bind_hdr_name) { |
| 1285 | ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]); |
| 1286 | goto err; |
| 1287 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1288 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1289 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1290 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 1291 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1292 | |
| 1293 | /* now look for an occurrence number */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1294 | while (isspace((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1295 | end++; |
| 1296 | if (*end == ',') { |
| 1297 | end++; |
| 1298 | name = end; |
| 1299 | if (*end == '-') |
| 1300 | end++; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1301 | while (isdigit((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1302 | end++; |
| 1303 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 1304 | } |
| 1305 | |
| 1306 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1307 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 1308 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1309 | goto err; |
| 1310 | } |
| 1311 | } |
| 1312 | else { |
| 1313 | struct sockaddr_storage *sk; |
| 1314 | int port1, port2; |
| 1315 | |
| 1316 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1317 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, |
| 1318 | &errmsg, NULL, NULL, |
| 1319 | PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1320 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1321 | ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1322 | goto err; |
| 1323 | } |
| 1324 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1325 | newsrv->conn_src.tproxy_addr = *sk; |
| 1326 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1327 | } |
| 1328 | global.last_checks |= LSTCHK_NETADM; |
| 1329 | *cur_arg += 2; |
| 1330 | continue; |
| 1331 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1332 | ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1333 | goto err; |
| 1334 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 1335 | } /* "usesrc" */ |
| 1336 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1337 | if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */ |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1338 | #ifdef SO_BINDTODEVICE |
| 1339 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1340 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1341 | goto err; |
| 1342 | } |
| 1343 | free(newsrv->conn_src.iface_name); |
| 1344 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 1345 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1346 | global.last_checks |= LSTCHK_NETADM; |
| 1347 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1348 | ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1349 | goto err; |
| 1350 | #endif |
| 1351 | *cur_arg += 2; |
| 1352 | continue; |
| 1353 | } |
| 1354 | /* this keyword in not an option of "source" */ |
| 1355 | break; |
| 1356 | } /* while */ |
| 1357 | |
| 1358 | return 0; |
| 1359 | |
| 1360 | err: |
| 1361 | free(errmsg); |
| 1362 | return ERR_ALERT | ERR_FATAL; |
| 1363 | } |
| 1364 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1365 | /* Parse the "stick" server keyword */ |
| 1366 | static int srv_parse_stick(char **args, int *cur_arg, |
| 1367 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1368 | { |
| 1369 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1373 | /* Parse the "track" server keyword */ |
| 1374 | static int srv_parse_track(char **args, int *cur_arg, |
| 1375 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1376 | { |
| 1377 | char *arg; |
| 1378 | |
| 1379 | arg = args[*cur_arg + 1]; |
| 1380 | if (!*arg) { |
| 1381 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 1382 | return ERR_ALERT | ERR_FATAL; |
| 1383 | } |
| 1384 | |
| 1385 | free(newsrv->trackit); |
| 1386 | newsrv->trackit = strdup(arg); |
| 1387 | |
| 1388 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /* Parse the "socks4" server keyword */ |
| 1392 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 1393 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1394 | { |
| 1395 | char *errmsg; |
| 1396 | int port_low, port_high; |
| 1397 | struct sockaddr_storage *sk; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1398 | |
| 1399 | errmsg = NULL; |
| 1400 | |
| 1401 | if (!*args[*cur_arg + 1]) { |
| 1402 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 1403 | goto err; |
| 1404 | } |
| 1405 | |
| 1406 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1407 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1408 | &errmsg, NULL, NULL, |
| 1409 | PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1410 | if (!sk) { |
| 1411 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1412 | goto err; |
| 1413 | } |
| 1414 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1415 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 1416 | newsrv->socks4_addr = *sk; |
| 1417 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1418 | return 0; |
| 1419 | |
| 1420 | err: |
| 1421 | free(errmsg); |
| 1422 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1425 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 1426 | /* parse the "tfo" server keyword */ |
| 1427 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1428 | { |
| 1429 | newsrv->flags |= SRV_F_FASTOPEN; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1433 | /* parse the "usesrc" server keyword */ |
| 1434 | static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1435 | { |
| 1436 | memprintf(err, "'%s' only allowed after a '%s' statement.", |
| 1437 | "usesrc", "source"); |
| 1438 | return ERR_ALERT | ERR_FATAL; |
| 1439 | } |
| 1440 | |
| 1441 | /* parse the "weight" server keyword */ |
| 1442 | static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1443 | { |
| 1444 | int w; |
| 1445 | |
| 1446 | w = atol(args[*cur_arg + 1]); |
| 1447 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1448 | memprintf(err, "weight of server %s is not within 0 and %d (%d).", |
| 1449 | newsrv->id, SRV_UWGHT_MAX, w); |
| 1450 | return ERR_ALERT | ERR_FATAL; |
| 1451 | } |
| 1452 | newsrv->uweight = newsrv->iweight = w; |
| 1453 | |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1457 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1458 | * code in <why>, which must be one of SF_ERR_* indicating the reason for the |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1459 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1460 | * |
| 1461 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1462 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1463 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1464 | { |
Willy Tarreau | 751153e | 2021-02-17 13:33:24 +0100 | [diff] [blame] | 1465 | struct stream *stream; |
| 1466 | struct mt_list *elt1, elt2; |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1467 | int thr; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1468 | |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1469 | for (thr = 0; thr < global.nbthread; thr++) |
| 1470 | mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2) |
| 1471 | if (stream->srv_conn == srv) |
| 1472 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* Shutdown all connections of all backup servers of a proxy. The caller must |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1476 | * pass a termination code in <why>, which must be one of SF_ERR_* indicating |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1477 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1478 | * |
| 1479 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1480 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1481 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1482 | { |
| 1483 | struct server *srv; |
| 1484 | |
| 1485 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1486 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1487 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1490 | /* Appends some information to a message string related to a server going UP or |
| 1491 | * DOWN. If both <forced> and <reason> are null and the server tracks another |
| 1492 | * one, a "via" information will be provided to know where the status came from. |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1493 | * If <check> is non-null, an entire string describing the check result will be |
| 1494 | * appended after a comma and a space (eg: to report some information from the |
| 1495 | * check that changed the state). In the other case, the string will be built |
| 1496 | * using the check results stored into the struct server if present. |
| 1497 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1498 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1499 | * |
| 1500 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1501 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1502 | void srv_append_status(struct buffer *msg, struct server *s, |
| 1503 | struct check *check, int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1504 | { |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1505 | short status = s->op_st_chg.status; |
| 1506 | short code = s->op_st_chg.code; |
| 1507 | long duration = s->op_st_chg.duration; |
| 1508 | char *desc = s->op_st_chg.reason; |
| 1509 | |
| 1510 | if (check) { |
| 1511 | status = check->status; |
| 1512 | code = check->code; |
| 1513 | duration = check->duration; |
| 1514 | desc = check->desc; |
| 1515 | } |
| 1516 | |
| 1517 | if (status != -1) { |
| 1518 | chunk_appendf(msg, ", reason: %s", get_check_status_description(status)); |
| 1519 | |
| 1520 | if (status >= HCHK_STATUS_L57DATA) |
| 1521 | chunk_appendf(msg, ", code: %d", code); |
| 1522 | |
| 1523 | if (desc && *desc) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1524 | struct buffer src; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1525 | |
| 1526 | chunk_appendf(msg, ", info: \""); |
| 1527 | |
| 1528 | chunk_initlen(&src, desc, 0, strlen(desc)); |
| 1529 | chunk_asciiencode(msg, &src, '"'); |
| 1530 | |
| 1531 | chunk_appendf(msg, "\""); |
| 1532 | } |
| 1533 | |
| 1534 | if (duration >= 0) |
| 1535 | chunk_appendf(msg, ", check duration: %ldms", duration); |
| 1536 | } |
| 1537 | else if (desc && *desc) { |
| 1538 | chunk_appendf(msg, ", %s", desc); |
| 1539 | } |
| 1540 | else if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1541 | chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1542 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1543 | |
| 1544 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1545 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1546 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1547 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1548 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1549 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1550 | s->cur_sess, xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1551 | else |
| 1552 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1553 | " %d sessions requeued, %d total in queue", |
| 1554 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1555 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1556 | xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1560 | /* Marks server <s> down, regardless of its checks' statuses. The server is |
| 1561 | * registered in a list to postpone the counting of the remaining servers on |
| 1562 | * the proxy and transfers queued streams whenever possible to other servers at |
| 1563 | * a sync point. Maintenance servers are ignored. It stores the <reason> if |
| 1564 | * non-null as the reason for going down or the available data from the check |
| 1565 | * struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1566 | * |
| 1567 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1568 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1569 | void srv_set_stopped(struct server *s, const char *reason, struct check *check) |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1570 | { |
| 1571 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1572 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1573 | if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1574 | return; |
| 1575 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1576 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1577 | *s->op_st_chg.reason = 0; |
| 1578 | s->op_st_chg.status = -1; |
| 1579 | if (reason) { |
| 1580 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1581 | } |
| 1582 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1583 | strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason)); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1584 | s->op_st_chg.code = check->code; |
| 1585 | s->op_st_chg.status = check->status; |
| 1586 | s->op_st_chg.duration = check->duration; |
| 1587 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1588 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1589 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1590 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1591 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1592 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1593 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1594 | srv_set_stopped(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1595 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1596 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1599 | /* Marks server <s> up regardless of its checks' statuses and provided it isn't |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1600 | * in maintenance. The server is registered in a list to postpone the counting |
| 1601 | * of the remaining servers on the proxy and tries to grab requests from the |
| 1602 | * proxy at a sync point. Maintenance servers are ignored. It stores the |
| 1603 | * <reason> if non-null as the reason for going down or the available data |
| 1604 | * from the check struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1605 | * |
| 1606 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1607 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1608 | void srv_set_running(struct server *s, const char *reason, struct check *check) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1609 | { |
| 1610 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1611 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1612 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1613 | return; |
| 1614 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1615 | if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1616 | return; |
| 1617 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1618 | s->next_state = SRV_ST_STARTING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1619 | *s->op_st_chg.reason = 0; |
| 1620 | s->op_st_chg.status = -1; |
| 1621 | if (reason) { |
| 1622 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1623 | } |
| 1624 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1625 | strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason)); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1626 | s->op_st_chg.code = check->code; |
| 1627 | s->op_st_chg.status = check->status; |
| 1628 | s->op_st_chg.duration = check->duration; |
| 1629 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1630 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1631 | if (s->slowstart <= 0) |
| 1632 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1633 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1634 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1635 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1636 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1637 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1638 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1639 | srv_set_running(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1640 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1641 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1644 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1645 | * isn't in maintenance. The server is registered in a list to postpone the |
| 1646 | * counting of the remaining servers on the proxy and tries to grab requests |
| 1647 | * from the proxy. Maintenance servers are ignored. It stores the |
| 1648 | * <reason> if non-null as the reason for going down or the available data |
| 1649 | * from the check struct to recompute this reason later. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1650 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 1651 | * must not be placed there. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1652 | * |
| 1653 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1654 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1655 | void srv_set_stopping(struct server *s, const char *reason, struct check *check) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1656 | { |
| 1657 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1658 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1659 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1660 | return; |
| 1661 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1662 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1663 | return; |
| 1664 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1665 | s->next_state = SRV_ST_STOPPING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1666 | *s->op_st_chg.reason = 0; |
| 1667 | s->op_st_chg.status = -1; |
| 1668 | if (reason) { |
| 1669 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1670 | } |
| 1671 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1672 | strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason)); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1673 | s->op_st_chg.code = check->code; |
| 1674 | s->op_st_chg.status = check->status; |
| 1675 | s->op_st_chg.duration = check->duration; |
| 1676 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1677 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1678 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1679 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1680 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1681 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1682 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1683 | srv_set_stopping(srv, NULL, NULL); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1684 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1685 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1686 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1687 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1688 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1689 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1690 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1691 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1692 | * checks). When either the flag is already set or no flag is passed, nothing |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1693 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1694 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1695 | * |
| 1696 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1697 | */ |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1698 | void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1699 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1700 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1701 | |
| 1702 | if (!mode) |
| 1703 | return; |
| 1704 | |
| 1705 | /* stop going down as soon as we meet a server already in the same state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1706 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1707 | return; |
| 1708 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1709 | s->next_admin |= mode; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1710 | if (cause) |
| 1711 | strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause)); |
| 1712 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1713 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1714 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1715 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1716 | /* stop going down if the equivalent flag was already present (forced or inherited) */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1717 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1718 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1719 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1720 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1721 | /* compute the inherited flag to propagate */ |
| 1722 | if (mode & SRV_ADMF_MAINT) |
| 1723 | mode = SRV_ADMF_IMAINT; |
| 1724 | else if (mode & SRV_ADMF_DRAIN) |
| 1725 | mode = SRV_ADMF_IDRAIN; |
| 1726 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1727 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1728 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1729 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1730 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1731 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1734 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1735 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1736 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1737 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1738 | * either the flag is already cleared or no flag is passed, nothing is done. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1739 | * |
| 1740 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1741 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1742 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1743 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1744 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1745 | |
| 1746 | if (!mode) |
| 1747 | return; |
| 1748 | |
| 1749 | /* stop going down as soon as we see the flag is not there anymore */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1750 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1751 | return; |
| 1752 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1753 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1754 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1755 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1756 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1757 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1758 | /* stop going down if the equivalent flag is still present (forced or inherited) */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1759 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1760 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1761 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1762 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1763 | if (mode & SRV_ADMF_MAINT) |
| 1764 | mode = SRV_ADMF_IMAINT; |
| 1765 | else if (mode & SRV_ADMF_DRAIN) |
| 1766 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1767 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1768 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1769 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1770 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1771 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1772 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1775 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1776 | * upon startup so that inherited states are correct. |
| 1777 | */ |
| 1778 | static void srv_propagate_admin_state(struct server *srv) |
| 1779 | { |
| 1780 | struct server *srv2; |
| 1781 | |
| 1782 | if (!srv->trackers) |
| 1783 | return; |
| 1784 | |
| 1785 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1786 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1787 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1788 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1789 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1790 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1791 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1792 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1797 | * Only servers *not* tracking another one are considered, because other |
| 1798 | * ones will be handled when the server they track is visited. |
| 1799 | */ |
| 1800 | void srv_compute_all_admin_states(struct proxy *px) |
| 1801 | { |
| 1802 | struct server *srv; |
| 1803 | |
| 1804 | for (srv = px->srv; srv; srv = srv->next) { |
| 1805 | if (srv->track) |
| 1806 | continue; |
| 1807 | srv_propagate_admin_state(srv); |
| 1808 | } |
| 1809 | } |
| 1810 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1811 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1812 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1813 | * all code contributors. |
| 1814 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1815 | * the config parser can report an appropriate error when a known keyword was |
| 1816 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1817 | * Note: -1 as ->skip value means that the number of arguments are variable. |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1818 | */ |
| 1819 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1820 | { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1821 | { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1822 | { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */ |
| 1823 | { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */ |
Amaury Denoyelle | 725f8d2 | 2021-09-20 15:16:12 +0200 | [diff] [blame] | 1824 | { "error-limit", srv_parse_error_limit, 1, 1, 1 }, /* Configure the consecutive count of check failures to consider a server on error */ |
Amaury Denoyelle | f9d5957 | 2021-10-18 14:40:29 +0200 | [diff] [blame] | 1825 | { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1826 | { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1827 | { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */ |
| 1828 | { "log-proto", srv_parse_log_proto, 1, 1, 0 }, /* Set the protocol for event messages, only relevant in a ring section */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1829 | { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */ |
| 1830 | { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1831 | { "max-reuse", srv_parse_max_reuse, 1, 1, 0 }, /* Set the max number of requests on a connection, -1 means unlimited */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1832 | { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1833 | { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1834 | { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */ |
| 1835 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1836 | { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */ |
| 1837 | { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1838 | { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */ |
Amaury Denoyelle | 725f8d2 | 2021-09-20 15:16:12 +0200 | [diff] [blame] | 1839 | { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */ |
| 1840 | { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */ |
| 1841 | { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */ |
| 1842 | { "on-marked-up", srv_parse_on_marked_up, 1, 1, 1 }, /* Configure the action when a server is marked up */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1843 | { "pool-low-conn", srv_parse_pool_low_conn, 1, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */ |
| 1844 | { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */ |
| 1845 | { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */ |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 1846 | { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1847 | { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1848 | { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */ |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 1849 | { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1850 | { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */ |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 1851 | { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1852 | { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1853 | { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1854 | { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1, 1 }, /* Enforce use of PROXY V2 protocol */ |
Frédéric Lécaille | 36d1565 | 2022-10-17 14:58:19 +0200 | [diff] [blame] | 1855 | { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */ |
Amaury Denoyelle | cd8a6f2 | 2021-09-21 11:51:54 +0200 | [diff] [blame] | 1856 | { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1857 | { "source", srv_parse_source, -1, 1, 1 }, /* Set the source address to be used to connect to the server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1858 | { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1859 | { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */ |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 1860 | { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1861 | { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1862 | { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */ |
| 1863 | { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */ |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1864 | { NULL, NULL, 0 }, |
| 1865 | }}; |
| 1866 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1867 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1868 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1869 | /* Recomputes the server's eweight based on its state, uweight, the current time, |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1870 | * and the proxy's algorithm. To be used after updating sv->uweight. The warmup |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1871 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1872 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1873 | * |
| 1874 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1875 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1876 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1877 | { |
| 1878 | struct proxy *px = sv->proxy; |
| 1879 | unsigned w; |
| 1880 | |
| 1881 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 1882 | /* go to full throttle if the slowstart interval is reached */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1883 | if (sv->next_state == SRV_ST_STARTING) |
| 1884 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1888 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1889 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1890 | if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1891 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 1892 | else |
| 1893 | w = px->lbprm.wdiv; |
| 1894 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1895 | sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1896 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1897 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1898 | if (must_update) |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1899 | srv_update_status(sv); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1902 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1903 | * Parses weight_str and configures sv accordingly. |
| 1904 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1905 | * |
| 1906 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1907 | */ |
| 1908 | const char *server_parse_weight_change_request(struct server *sv, |
| 1909 | const char *weight_str) |
| 1910 | { |
| 1911 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1912 | long int w; |
| 1913 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1914 | |
| 1915 | px = sv->proxy; |
| 1916 | |
| 1917 | /* if the weight is terminated with '%', it is set relative to |
| 1918 | * the initial weight, otherwise it is absolute. |
| 1919 | */ |
| 1920 | if (!*weight_str) |
| 1921 | return "Require <weight> or <weight%>.\n"; |
| 1922 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1923 | w = strtol(weight_str, &end, 10); |
| 1924 | if (end == weight_str) |
| 1925 | return "Empty weight string empty or preceded by garbage"; |
| 1926 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1927 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1928 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1929 | /* Avoid integer overflow */ |
| 1930 | if (w > 25600) |
| 1931 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1932 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1933 | if (w > 256) |
| 1934 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1935 | } |
| 1936 | else if (w < 0 || w > 256) |
| 1937 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1938 | else if (end[0] != '\0') |
| 1939 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1940 | |
| 1941 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 1942 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 1943 | |
| 1944 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1945 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1946 | |
| 1947 | return NULL; |
| 1948 | } |
| 1949 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1950 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1951 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 1952 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1953 | * Returns: |
| 1954 | * - error string on error |
| 1955 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1956 | * |
| 1957 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1958 | */ |
| 1959 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1960 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1961 | { |
| 1962 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 1963 | |
| 1964 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1965 | srv_update_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1966 | return NULL; |
| 1967 | } |
| 1968 | if (inet_pton(AF_INET, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1969 | srv_update_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1970 | return NULL; |
| 1971 | } |
| 1972 | |
| 1973 | return "Could not understand IP address format.\n"; |
| 1974 | } |
| 1975 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1976 | /* |
| 1977 | * Must be called with the server lock held. |
| 1978 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1979 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 1980 | const char *maxconn_str) |
| 1981 | { |
| 1982 | long int v; |
| 1983 | char *end; |
| 1984 | |
| 1985 | if (!*maxconn_str) |
| 1986 | return "Require <maxconn>.\n"; |
| 1987 | |
| 1988 | v = strtol(maxconn_str, &end, 10); |
| 1989 | if (end == maxconn_str) |
| 1990 | return "maxconn string empty or preceded by garbage"; |
| 1991 | else if (end[0] != '\0') |
| 1992 | return "Trailing garbage in maxconn string"; |
| 1993 | |
| 1994 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 1995 | sv->maxconn = sv->minconn = v; |
| 1996 | } else { // dynamic maxconn |
| 1997 | sv->maxconn = v; |
| 1998 | } |
| 1999 | |
| 2000 | if (may_dequeue_tasks(sv, sv->proxy)) |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 2001 | process_srv_queue(sv); |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 2002 | |
| 2003 | return NULL; |
| 2004 | } |
| 2005 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2006 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 2007 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2008 | { |
| 2009 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2010 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2011 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2012 | NULL, |
| 2013 | }; |
| 2014 | |
| 2015 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 2016 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2017 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2018 | return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL); |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2019 | } |
| 2020 | |
William Lallemand | 0d05867 | 2022-03-16 15:44:42 +0100 | [diff] [blame] | 2021 | int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err) |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2022 | { |
| 2023 | struct sample_expr *expr; |
| 2024 | |
| 2025 | expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2026 | if (!expr) { |
| 2027 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 2028 | return ERR_ALERT | ERR_FATAL; |
| 2029 | } |
| 2030 | |
| 2031 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 2032 | memprintf(err, "error detected while parsing sni expression : " |
| 2033 | " fetch method '%s' extracts information from '%s', " |
Amaury Denoyelle | c008a63 | 2021-05-28 11:01:22 +0200 | [diff] [blame] | 2034 | "none of which is available here.", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2035 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2036 | return ERR_ALERT | ERR_FATAL; |
| 2037 | } |
| 2038 | |
| 2039 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 2040 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2041 | newsrv->ssl_ctx.sni = expr; |
| 2042 | |
| 2043 | return 0; |
| 2044 | } |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2045 | |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2046 | static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2047 | { |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2048 | char *msg = "error encountered while processing "; |
| 2049 | char *quote = "'"; |
| 2050 | char *token = args[cur_arg]; |
| 2051 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2052 | if (err && *err) { |
| 2053 | indent_msg(err, 2); |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2054 | msg = *err; |
| 2055 | quote = ""; |
| 2056 | token = ""; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2057 | } |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2058 | |
| 2059 | if (err_code & ERR_WARN && !(err_code & ERR_ALERT)) |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2060 | ha_warning("%s%s%s%s.\n", msg, quote, token, quote); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2061 | else |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2062 | ha_alert("%s%s%s%s.\n", msg, quote, token, quote); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2065 | static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2066 | { |
| 2067 | int range_sz; |
| 2068 | |
| 2069 | range_sz = src->conn_src.sport_range->size; |
| 2070 | if (range_sz > 0) { |
| 2071 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 2072 | if (srv->conn_src.sport_range != NULL) { |
| 2073 | int i; |
| 2074 | |
| 2075 | for (i = 0; i < range_sz; i++) { |
| 2076 | srv->conn_src.sport_range->ports[i] = |
| 2077 | src->conn_src.sport_range->ports[i]; |
| 2078 | } |
| 2079 | } |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 2085 | */ |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2086 | static void srv_conn_src_cpy(struct server *srv, const struct server *src) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2087 | { |
| 2088 | srv->conn_src.opts = src->conn_src.opts; |
| 2089 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 2090 | |
| 2091 | /* Source port range copy. */ |
| 2092 | if (src->conn_src.sport_range != NULL) |
| 2093 | srv_conn_src_sport_range_cpy(srv, src); |
| 2094 | |
| 2095 | #ifdef CONFIG_HAP_TRANSPARENT |
| 2096 | if (src->conn_src.bind_hdr_name != NULL) { |
| 2097 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 2098 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 2099 | } |
| 2100 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 2101 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 2102 | #endif |
| 2103 | if (src->conn_src.iface_name != NULL) |
| 2104 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
| 2105 | } |
| 2106 | |
| 2107 | /* |
| 2108 | * Copy <src> server SSL settings to <srv> server allocating |
| 2109 | * everything needed. |
| 2110 | */ |
| 2111 | #if defined(USE_OPENSSL) |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2112 | static void srv_ssl_settings_cpy(struct server *srv, const struct server *src) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2113 | { |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2114 | /* <src> is the current proxy's default server and SSL is enabled */ |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2115 | BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */ |
| 2116 | |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2117 | if (src == &srv->proxy->defsrv && src->use_ssl == 1) |
| 2118 | srv->flags |= SRV_F_DEFSRV_USE_SSL; |
| 2119 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2120 | if (src->ssl_ctx.ca_file != NULL) |
| 2121 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 2122 | if (src->ssl_ctx.crl_file != NULL) |
| 2123 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2124 | if (src->ssl_ctx.client_crt != NULL) |
| 2125 | srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2126 | |
| 2127 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 2128 | |
Remi Tricot-Le Breton | 0498fa4 | 2021-07-13 18:28:22 +0200 | [diff] [blame] | 2129 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2130 | if (src->ssl_ctx.verify_host != NULL) |
| 2131 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 2132 | if (src->ssl_ctx.ciphers != NULL) |
| 2133 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 2e8d52f | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 2134 | if (src->ssl_ctx.options) |
| 2135 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 2136 | if (src->ssl_ctx.methods.flags) |
| 2137 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 2138 | if (src->ssl_ctx.methods.min) |
| 2139 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 2140 | if (src->ssl_ctx.methods.max) |
| 2141 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 2142 | |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 2143 | if (src->ssl_ctx.ciphersuites != NULL) |
| 2144 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2145 | if (src->sni_expr != NULL) |
| 2146 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2147 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2148 | if (src->ssl_ctx.alpn_str) { |
| 2149 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 2150 | if (srv->ssl_ctx.alpn_str) { |
| 2151 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 2152 | src->ssl_ctx.alpn_len); |
| 2153 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 2154 | } |
| 2155 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2156 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2157 | if (src->ssl_ctx.npn_str) { |
| 2158 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 2159 | if (srv->ssl_ctx.npn_str) { |
| 2160 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 2161 | src->ssl_ctx.npn_len); |
| 2162 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 2163 | } |
| 2164 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2165 | } |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2166 | |
| 2167 | /* Activate ssl on server <s>. |
| 2168 | * do nothing if there is no change to apply |
| 2169 | * |
| 2170 | * Must be called with the server lock held. |
| 2171 | */ |
| 2172 | void srv_set_ssl(struct server *s, int use_ssl) |
| 2173 | { |
| 2174 | if (s->use_ssl == use_ssl) |
| 2175 | return; |
| 2176 | |
| 2177 | s->use_ssl = use_ssl; |
| 2178 | if (s->use_ssl) |
| 2179 | s->xprt = xprt_get(XPRT_SSL); |
| 2180 | else |
William Dauchy | a087f87 | 2022-01-06 16:57:15 +0100 | [diff] [blame] | 2181 | s->xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | #endif /* USE_OPENSSL */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2185 | |
| 2186 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2187 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2188 | * May be safely called with a default server as <src> argument (without hostname). |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2189 | * Returns -1 in case of any allocation failure, 0 if not. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2190 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2191 | int srv_prepare_for_resolution(struct server *srv, const char *hostname) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2192 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2193 | char *hostname_dn; |
| 2194 | int hostname_len, hostname_dn_len; |
| 2195 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2196 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2197 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2198 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2199 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2200 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 2201 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 2202 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2203 | if (hostname_dn_len == -1) |
| 2204 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2205 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2206 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2207 | free(srv->hostname); |
| 2208 | free(srv->hostname_dn); |
| 2209 | srv->hostname = strdup(hostname); |
| 2210 | srv->hostname_dn = strdup(hostname_dn); |
| 2211 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2212 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2213 | goto err; |
| 2214 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2215 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2216 | |
| 2217 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2218 | ha_free(&srv->hostname); |
| 2219 | ha_free(&srv->hostname_dn); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2220 | return -1; |
| 2221 | } |
| 2222 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2223 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2224 | * Copy <src> server settings to <srv> server allocating |
| 2225 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2226 | * This function is not supposed to be called at any time, but only |
| 2227 | * during server settings parsing or during server allocations from |
| 2228 | * a server template, and just after having calloc()'ed a new server. |
| 2229 | * So, <src> may only be a default server (when parsing server settings) |
| 2230 | * or a server template (during server allocations from a server template). |
| 2231 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 2232 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2233 | */ |
Christopher Faulet | b32cb9b | 2022-08-03 11:28:08 +0200 | [diff] [blame] | 2234 | void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2235 | { |
| 2236 | /* Connection source settings copy */ |
| 2237 | srv_conn_src_cpy(srv, src); |
| 2238 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2239 | if (srv_tmpl) { |
| 2240 | srv->addr = src->addr; |
| 2241 | srv->svc_port = src->svc_port; |
| 2242 | } |
| 2243 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2244 | srv->pp_opts = src->pp_opts; |
| 2245 | if (src->rdr_pfx != NULL) { |
| 2246 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 2247 | srv->rdr_len = src->rdr_len; |
| 2248 | } |
| 2249 | if (src->cookie != NULL) { |
| 2250 | srv->cookie = strdup(src->cookie); |
| 2251 | srv->cklen = src->cklen; |
| 2252 | } |
| 2253 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | 2444108 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 2254 | srv->check.addr = src->check.addr; |
| 2255 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2256 | srv->check.use_ssl = src->check.use_ssl; |
| 2257 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 2258 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 2259 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 2260 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2261 | /* Note: 'flags' field has potentially been already initialized. */ |
| 2262 | srv->flags |= src->flags; |
| 2263 | srv->do_check = src->do_check; |
| 2264 | srv->do_agent = src->do_agent; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2265 | srv->check.inter = src->check.inter; |
| 2266 | srv->check.fastinter = src->check.fastinter; |
| 2267 | srv->check.downinter = src->check.downinter; |
| 2268 | srv->agent.use_ssl = src->agent.use_ssl; |
| 2269 | srv->agent.port = src->agent.port; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2270 | |
| 2271 | if (src->agent.tcpcheck_rules) { |
| 2272 | srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules)); |
| 2273 | if (srv->agent.tcpcheck_rules) { |
| 2274 | srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags; |
| 2275 | srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list; |
| 2276 | LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars); |
| 2277 | dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars, |
| 2278 | &src->agent.tcpcheck_rules->preset_vars); |
| 2279 | } |
| 2280 | } |
| 2281 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2282 | srv->agent.inter = src->agent.inter; |
| 2283 | srv->agent.fastinter = src->agent.fastinter; |
| 2284 | srv->agent.downinter = src->agent.downinter; |
| 2285 | srv->maxqueue = src->maxqueue; |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 2286 | srv->ws = src->ws; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2287 | srv->minconn = src->minconn; |
| 2288 | srv->maxconn = src->maxconn; |
| 2289 | srv->slowstart = src->slowstart; |
| 2290 | srv->observe = src->observe; |
| 2291 | srv->onerror = src->onerror; |
| 2292 | srv->onmarkeddown = src->onmarkeddown; |
| 2293 | srv->onmarkedup = src->onmarkedup; |
| 2294 | if (src->trackit != NULL) |
| 2295 | srv->trackit = strdup(src->trackit); |
| 2296 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 2297 | srv->uweight = srv->iweight = src->iweight; |
| 2298 | |
| 2299 | srv->check.send_proxy = src->check.send_proxy; |
| 2300 | /* health: up, but will fall down at first failure */ |
| 2301 | srv->check.rise = srv->check.health = src->check.rise; |
| 2302 | srv->check.fall = src->check.fall; |
| 2303 | |
| 2304 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2305 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 2306 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 2307 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2308 | srv->check.state |= CHK_ST_PAUSED; |
| 2309 | srv->check.health = 0; |
| 2310 | } |
| 2311 | |
| 2312 | /* health: up but will fall down at first failure */ |
| 2313 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 2314 | srv->agent.fall = src->agent.fall; |
| 2315 | |
| 2316 | if (src->resolvers_id != NULL) |
| 2317 | srv->resolvers_id = strdup(src->resolvers_id); |
Emeric Brun | 21fbeed | 2020-12-23 18:01:04 +0100 | [diff] [blame] | 2318 | srv->resolv_opts.family_prio = src->resolv_opts.family_prio; |
| 2319 | srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip; |
| 2320 | srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight; |
| 2321 | if (srv->resolv_opts.family_prio == AF_UNSPEC) |
| 2322 | srv->resolv_opts.family_prio = AF_INET6; |
| 2323 | memcpy(srv->resolv_opts.pref_net, |
| 2324 | src->resolv_opts.pref_net, |
| 2325 | sizeof srv->resolv_opts.pref_net); |
| 2326 | srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2327 | |
| 2328 | srv->init_addr_methods = src->init_addr_methods; |
| 2329 | srv->init_addr = src->init_addr; |
| 2330 | #if defined(USE_OPENSSL) |
| 2331 | srv_ssl_settings_cpy(srv, src); |
| 2332 | #endif |
| 2333 | #ifdef TCP_USER_TIMEOUT |
| 2334 | srv->tcp_ut = src->tcp_ut; |
| 2335 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2336 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 2337 | srv->pool_purge_delay = src->pool_purge_delay; |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 2338 | srv->low_idle_conns = src->low_idle_conns; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 2339 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 2340 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2341 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2342 | if (srv_tmpl) |
| 2343 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2344 | |
| 2345 | srv->check.via_socks4 = src->check.via_socks4; |
| 2346 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 2349 | /* allocate a server and attach it to the global servers_list. Returns |
| 2350 | * the server on success, otherwise NULL. |
| 2351 | */ |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 2352 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2353 | { |
| 2354 | struct server *srv; |
| 2355 | |
| 2356 | srv = calloc(1, sizeof *srv); |
| 2357 | if (!srv) |
| 2358 | return NULL; |
| 2359 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2360 | srv_take(srv); |
| 2361 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2362 | srv->obj_type = OBJ_TYPE_SERVER; |
| 2363 | srv->proxy = proxy; |
Willy Tarreau | cdc83e0 | 2021-06-23 16:11:02 +0200 | [diff] [blame] | 2364 | queue_init(&srv->queue, proxy, srv); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2365 | LIST_APPEND(&servers_list, &srv->global_list); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2366 | LIST_INIT(&srv->srv_rec_item); |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 2367 | LIST_INIT(&srv->ip_rec_item); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame^] | 2368 | event_hdl_sub_list_init(&srv->e_subs); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 2369 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2370 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2371 | srv->last_change = now.tv_sec; |
| 2372 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2373 | srv->check.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2374 | srv->check.status = HCHK_STATUS_INI; |
| 2375 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 2376 | srv->check.proxy = proxy; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 2377 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2378 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2379 | srv->agent.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2380 | srv->agent.status = HCHK_STATUS_INI; |
| 2381 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 2382 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2383 | srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW); |
Frédéric Lécaille | f46c10c | 2020-11-23 14:29:28 +0100 | [diff] [blame] | 2384 | #if defined(USE_QUIC) |
| 2385 | srv->cids = EB_ROOT_UNIQUE; |
| 2386 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2387 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2388 | srv->extra_counters = NULL; |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 2389 | #ifdef USE_OPENSSL |
| 2390 | HA_RWLOCK_INIT(&srv->ssl_ctx.lock); |
| 2391 | #endif |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2392 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2393 | /* please don't put default server settings here, they are set in |
Willy Tarreau | 144289b | 2021-02-12 08:19:01 +0100 | [diff] [blame] | 2394 | * proxy_preset_defaults(). |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2395 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2396 | return srv; |
| 2397 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2398 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2399 | /* Increment the server refcount. */ |
| 2400 | void srv_take(struct server *srv) |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2401 | { |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2402 | HA_ATOMIC_INC(&srv->refcount); |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Amaury Denoyelle | 13f2e2c | 2021-08-09 15:08:54 +0200 | [diff] [blame] | 2405 | /* Deallocate a server <srv> and its member. <srv> must be allocated. For |
| 2406 | * dynamic servers, its refcount is decremented first. The free operations are |
| 2407 | * conducted only if the refcount is nul, unless the process is stopping. |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2408 | * |
| 2409 | * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2410 | * when calling srv_drop on the list of servers. |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2411 | */ |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2412 | struct server *srv_drop(struct server *srv) |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2413 | { |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2414 | struct server *next = NULL; |
| 2415 | |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2416 | if (!srv) |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2417 | goto end; |
| 2418 | |
| 2419 | next = srv->next; |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2420 | |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2421 | /* For dynamic servers, decrement the reference counter. Only free the |
| 2422 | * server when reaching zero. |
| 2423 | */ |
Amaury Denoyelle | 13f2e2c | 2021-08-09 15:08:54 +0200 | [diff] [blame] | 2424 | if (likely(!(global.mode & MODE_STOPPING))) { |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2425 | if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1)) |
| 2426 | goto end; |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2429 | task_destroy(srv->warmup); |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 2430 | task_destroy(srv->srvrq_check); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2431 | |
| 2432 | free(srv->id); |
| 2433 | free(srv->cookie); |
| 2434 | free(srv->hostname); |
| 2435 | free(srv->hostname_dn); |
| 2436 | free((char*)srv->conf.file); |
| 2437 | free(srv->per_thr); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 2438 | free(srv->per_tgrp); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2439 | free(srv->curr_idle_thr); |
| 2440 | free(srv->resolvers_id); |
| 2441 | free(srv->addr_node.key); |
Amaury Denoyelle | fb24794 | 2021-04-20 16:48:22 +0200 | [diff] [blame] | 2442 | free(srv->lb_nodes); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2443 | |
| 2444 | if (srv->use_ssl == 1 || srv->check.use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL)) { |
| 2445 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2446 | xprt_get(XPRT_SSL)->destroy_srv(srv); |
| 2447 | } |
| 2448 | HA_SPIN_DESTROY(&srv->lock); |
| 2449 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2450 | LIST_DELETE(&srv->global_list); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame^] | 2451 | event_hdl_sub_list_destroy(&srv->e_subs); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2452 | |
| 2453 | EXTRA_COUNTERS_FREE(srv->extra_counters); |
| 2454 | |
Tim Duesterhus | 025b93e | 2021-11-04 21:03:52 +0100 | [diff] [blame] | 2455 | ha_free(&srv); |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2456 | |
| 2457 | end: |
| 2458 | return next; |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2459 | } |
| 2460 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 2461 | /* Remove a server <srv> from a tracking list if <srv> is tracking another |
| 2462 | * server. No special care is taken if <srv> is tracked itself by another one : |
| 2463 | * this situation should be avoided by the caller. |
| 2464 | * |
| 2465 | * Not thread-safe. |
| 2466 | */ |
| 2467 | static void release_server_track(struct server *srv) |
| 2468 | { |
| 2469 | struct server *strack = srv->track; |
| 2470 | struct server **base; |
| 2471 | |
| 2472 | if (!strack) |
| 2473 | return; |
| 2474 | |
| 2475 | for (base = &strack->trackers; *base; base = &((*base)->tracknext)) { |
| 2476 | if (*base == srv) { |
| 2477 | *base = srv->tracknext; |
| 2478 | return; |
| 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | /* srv not found on the tracking list, this should never happen */ |
| 2483 | BUG_ON(!*base); |
| 2484 | } |
| 2485 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2486 | /* |
| 2487 | * Parse as much as possible such a range string argument: low[-high] |
| 2488 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2489 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2490 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2491 | * Returns 0 if succeeded, -1 if not. |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2492 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2493 | static int _srv_parse_tmpl_range(struct server *srv, const char *arg, |
| 2494 | int *nb_low, int *nb_high) |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2495 | { |
| 2496 | char *nb_high_arg; |
| 2497 | |
| 2498 | *nb_high = 0; |
| 2499 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2500 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2501 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2502 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2503 | *nb_high_arg++ = '\0'; |
| 2504 | *nb_high = atoi(nb_high_arg); |
| 2505 | } |
| 2506 | else { |
| 2507 | *nb_high += *nb_low; |
| 2508 | *nb_low = 1; |
| 2509 | } |
| 2510 | |
| 2511 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 2512 | return -1; |
| 2513 | |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2517 | /* Parse as much as possible such a range string argument: low[-high] |
| 2518 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2519 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2520 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2521 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2522 | * initialize a new server on startup. |
| 2523 | * |
| 2524 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2525 | * Returns 0 if succeeded, -1 if not. |
| 2526 | */ |
| 2527 | static inline void _srv_parse_set_id_from_prefix(struct server *srv, |
| 2528 | const char *prefix, int nb) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2529 | { |
| 2530 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 2531 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2532 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2533 | } |
| 2534 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2535 | /* Initialize as much as possible servers from <srv> server template. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2536 | * Note that a server template is a special server with |
| 2537 | * a few different parameters than a server which has |
| 2538 | * been parsed mostly the same way as a server. |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2539 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2540 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2541 | * initialize a new server on startup. |
| 2542 | * |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2543 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2544 | * 'srv' template included. |
| 2545 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2546 | static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2547 | { |
| 2548 | int i; |
| 2549 | struct server *newsrv; |
| 2550 | |
| 2551 | for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2552 | newsrv = new_server(px); |
| 2553 | if (!newsrv) |
| 2554 | goto err; |
| 2555 | |
Christopher Faulet | 75bef00 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 2556 | newsrv->conf.file = strdup(srv->conf.file); |
| 2557 | newsrv->conf.line = srv->conf.line; |
| 2558 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2559 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2560 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2561 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2562 | if (newsrv->sni_expr) { |
| 2563 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 2564 | if (!newsrv->ssl_ctx.sni) |
| 2565 | goto err; |
| 2566 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2567 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2568 | /* append to list of servers available to receive an hostname */ |
Emeric Brun | caef19e | 2021-06-14 10:02:18 +0200 | [diff] [blame] | 2569 | if (newsrv->srvrq) |
| 2570 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2571 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2572 | /* Set this new server ID. */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2573 | _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2574 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2575 | /* Linked backwards first. This will be restablished after parsing. */ |
| 2576 | newsrv->next = px->srv; |
| 2577 | px->srv = newsrv; |
| 2578 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2579 | _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2580 | |
| 2581 | return i - srv->tmpl_info.nb_low; |
| 2582 | |
| 2583 | err: |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2584 | _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2585 | if (newsrv) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2586 | release_sample_expr(newsrv->ssl_ctx.sni); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2587 | free_check(&newsrv->agent); |
| 2588 | free_check(&newsrv->check); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2589 | LIST_DELETE(&newsrv->global_list); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2590 | } |
| 2591 | free(newsrv); |
| 2592 | return i - srv->tmpl_info.nb_low; |
| 2593 | } |
| 2594 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2595 | /* Allocate a new server pointed by <srv> and try to parse the first arguments |
| 2596 | * in <args> as an address for a server or an address-range for a template or |
| 2597 | * nothing for a default-server. <cur_arg> is incremented to the next argument. |
| 2598 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2599 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2600 | * initialize a new server on startup. |
| 2601 | * |
| 2602 | * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case |
| 2603 | * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated, |
| 2604 | * <srv> will be set to NULL. |
| 2605 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2606 | static int _srv_parse_init(struct server **srv, char **args, int *cur_arg, |
| 2607 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2608 | int parse_flags) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2609 | { |
| 2610 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2611 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2612 | int err_code = 0; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2613 | char *fqdn = NULL; |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2614 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2615 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2616 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2617 | *srv = NULL; |
| 2618 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2619 | /* There is no mandatory first arguments for default server. */ |
| 2620 | if (parse_flags & SRV_PARSE_PARSE_ADDR) { |
| 2621 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2622 | if (!*args[3]) { |
| 2623 | /* 'server-template' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2624 | ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n", |
| 2625 | args[0]); |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2626 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2627 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2628 | } |
| 2629 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2630 | err = invalid_prefix_char(args[1]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2631 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2632 | else { |
| 2633 | if (!*args[2]) { |
| 2634 | /* 'server' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2635 | ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2636 | args[0]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2637 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2638 | goto out; |
| 2639 | } |
| 2640 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2641 | err = invalid_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2644 | if (err) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2645 | ha_alert("character '%c' is not permitted in %s %s '%s'.\n", |
| 2646 | *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2647 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2648 | goto out; |
| 2649 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2650 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2651 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2652 | *cur_arg = 2; |
| 2653 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2654 | /* Parse server-template <nb | range> arg. */ |
| 2655 | if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2656 | ha_alert("Wrong %s number or range arg '%s'.\n", |
| 2657 | args[0], args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2658 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2659 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2660 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2661 | (*cur_arg)++; |
| 2662 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2663 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2664 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2665 | struct sockaddr_storage *sk; |
| 2666 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2667 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2668 | *srv = newsrv = new_server(curproxy); |
| 2669 | if (!newsrv) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2670 | ha_alert("out of memory.\n"); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2671 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2672 | goto out; |
| 2673 | } |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2674 | register_parsing_obj(&newsrv->obj_type); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2675 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2676 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2677 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2678 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2679 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2680 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2681 | if (parse_flags & SRV_PARSE_DYNAMIC) |
| 2682 | newsrv->flags |= SRV_F_DYNAMIC; |
| 2683 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2684 | /* Note: for a server template, its id is its prefix. |
| 2685 | * This is a temporary id which will be used for server allocations to come |
| 2686 | * after parsing. |
| 2687 | */ |
| 2688 | if (!(parse_flags & SRV_PARSE_TEMPLATE)) |
| 2689 | newsrv->id = strdup(args[1]); |
| 2690 | else |
| 2691 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2692 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 2693 | /* revision defaults to 0 */ |
| 2694 | newsrv->rid = 0; |
| 2695 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2696 | /* several ways to check the port component : |
| 2697 | * - IP => port=+0, relative (IPv4 only) |
| 2698 | * - IP: => port=+0, relative |
| 2699 | * - IP:N => port=N, absolute |
| 2700 | * - IP:+N => port=+N, relative |
| 2701 | * - IP:-N => port=-N, relative |
| 2702 | */ |
| 2703 | if (!(parse_flags & SRV_PARSE_PARSE_ADDR)) |
| 2704 | goto skip_addr; |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2705 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2706 | sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2707 | &errmsg, NULL, &fqdn, |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2708 | (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT); |
| 2709 | if (!sk) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2710 | ha_alert("%s\n", errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2711 | err_code |= ERR_ALERT | ERR_FATAL; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2712 | ha_free(&errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2713 | goto out; |
| 2714 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2715 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2716 | if (!port1 || !port2) { |
| 2717 | /* no port specified, +offset, -offset */ |
| 2718 | newsrv->flags |= SRV_F_MAPPORTS; |
| 2719 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2720 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2721 | /* save hostname and create associated name resolution */ |
| 2722 | if (fqdn) { |
| 2723 | if (fqdn[0] == '_') { /* SRV record */ |
| 2724 | /* Check if a SRV request already exists, and if not, create it */ |
| 2725 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2726 | newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn); |
| 2727 | if (newsrv->srvrq == NULL) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2728 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2729 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2730 | } |
Christopher Faulet | 81ba74a | 2021-06-29 20:52:35 +0200 | [diff] [blame] | 2731 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2732 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2733 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2734 | ha_alert("Can't create DNS resolution for server '%s'\n", |
| 2735 | newsrv->id); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2736 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2737 | goto out; |
| 2738 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2739 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2740 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2741 | newsrv->addr = *sk; |
| 2742 | newsrv->svc_port = port; |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 2743 | /* |
| 2744 | * we don't need to lock the server here, because |
| 2745 | * we are in the process of initializing. |
| 2746 | * |
| 2747 | * Note that the server is not attached into the proxy tree if |
| 2748 | * this is a dynamic server. |
| 2749 | */ |
| 2750 | srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC)); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2751 | |
Willy Tarreau | 14e7f29 | 2021-10-27 17:41:07 +0200 | [diff] [blame] | 2752 | if (!newsrv->srvrq && !newsrv->hostname && |
| 2753 | !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2754 | ha_alert("Unknown protocol family %d '%s'\n", |
| 2755 | newsrv->addr.ss_family, args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2756 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2757 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2758 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2759 | |
| 2760 | (*cur_arg)++; |
| 2761 | skip_addr: |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2762 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2763 | /* Copy default server settings to new server */ |
| 2764 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
| 2765 | } else { |
| 2766 | /* Initialize dynamic server weight to 1 */ |
| 2767 | newsrv->uweight = newsrv->iweight = 1; |
| 2768 | |
| 2769 | /* A dynamic server is disabled on startup */ |
| 2770 | newsrv->next_admin = SRV_ADMF_FMAINT; |
| 2771 | newsrv->next_state = SRV_ST_STOPPED; |
| 2772 | server_recalc_eweight(newsrv, 0); |
Amaury Denoyelle | fca1817 | 2021-07-22 16:03:36 +0200 | [diff] [blame] | 2773 | |
| 2774 | /* Set default values for checks */ |
| 2775 | newsrv->check.inter = DEF_CHKINTR; |
| 2776 | newsrv->check.rise = DEF_RISETIME; |
| 2777 | newsrv->check.fall = DEF_FALLTIME; |
| 2778 | |
| 2779 | newsrv->agent.inter = DEF_CHKINTR; |
| 2780 | newsrv->agent.rise = DEF_AGENT_RISETIME; |
| 2781 | newsrv->agent.fall = DEF_AGENT_FALLTIME; |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2782 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2783 | HA_SPIN_INIT(&newsrv->lock); |
| 2784 | } |
| 2785 | else { |
| 2786 | *srv = newsrv = &curproxy->defsrv; |
| 2787 | *cur_arg = 1; |
| 2788 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 2789 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2790 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2791 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2792 | free(fqdn); |
| 2793 | return 0; |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2794 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2795 | out: |
| 2796 | free(fqdn); |
| 2797 | return err_code; |
| 2798 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2799 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2800 | /* Parse the server keyword in <args>. |
| 2801 | * <cur_arg> is incremented beyond the keyword optional value. Note that this |
| 2802 | * might not be the case if an error is reported. |
| 2803 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2804 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2805 | * initialize a new server on startup. |
| 2806 | * |
| 2807 | * A mask of errors is returned. ERR_FATAL is set if the parsing should be |
| 2808 | * interrupted. |
| 2809 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2810 | static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg, |
| 2811 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2812 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2813 | { |
| 2814 | int err_code = 0; |
| 2815 | struct srv_kw *kw; |
| 2816 | const char *best; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2817 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2818 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2819 | kw = srv_find_kw(args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2820 | if (!kw) { |
| 2821 | best = srv_find_best_kw(args[*cur_arg]); |
| 2822 | if (best) |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2823 | ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n", |
| 2824 | args[*cur_arg], best, |
| 2825 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2826 | " Hint: no address was expected for this server."); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2827 | else |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2828 | ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg], |
| 2829 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2830 | " Hint: no address was expected for this server."); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2831 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2832 | return ERR_ALERT | ERR_FATAL; |
| 2833 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2834 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2835 | if (!kw->parse) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2836 | ha_alert("'%s' option is not implemented in this version (check build options)\n", |
| 2837 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2838 | err_code = ERR_ALERT | ERR_FATAL; |
| 2839 | goto out; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2840 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2841 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2842 | if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2843 | ha_alert("'%s' option is not accepted in default-server sections\n", |
| 2844 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2845 | err_code = ERR_ALERT; |
| 2846 | goto out; |
| 2847 | } |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2848 | else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2849 | ha_alert("'%s' option is not accepted for dynamic server\n", |
| 2850 | args[*cur_arg]); |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2851 | err_code |= ERR_ALERT; |
| 2852 | goto out; |
| 2853 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2854 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2855 | err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg); |
| 2856 | if (err_code) { |
| 2857 | display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg); |
| 2858 | free(errmsg); |
| 2859 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2860 | |
| 2861 | out: |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2862 | if (kw->skip != -1) |
| 2863 | *cur_arg += 1 + kw->skip; |
| 2864 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2865 | return err_code; |
| 2866 | } |
| 2867 | |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2868 | /* This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2869 | * initialize a new server on startup. |
| 2870 | */ |
| 2871 | static int _srv_parse_sni_expr_init(char **args, int cur_arg, |
| 2872 | struct server *srv, struct proxy *proxy, |
| 2873 | char **errmsg) |
| 2874 | { |
| 2875 | int ret; |
| 2876 | |
| 2877 | if (!srv->sni_expr) |
| 2878 | return 0; |
| 2879 | |
| 2880 | ret = server_parse_sni_expr(srv, proxy, errmsg); |
| 2881 | if (!ret) |
| 2882 | return 0; |
| 2883 | |
| 2884 | return ret; |
| 2885 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2886 | |
| 2887 | /* Server initializations finalization. |
| 2888 | * Initialize health check, agent check and SNI expression if enabled. |
| 2889 | * Must not be called for a default server instance. |
| 2890 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2891 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2892 | * initialize a new server on startup. |
| 2893 | */ |
| 2894 | static int _srv_parse_finalize(char **args, int cur_arg, |
| 2895 | struct server *srv, struct proxy *px, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2896 | int parse_flags) |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2897 | { |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2898 | int ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2899 | char *errmsg = NULL; |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2900 | |
| 2901 | if (srv->do_check && srv->trackit) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2902 | ha_alert("unable to enable checks and tracking at the same time!\n"); |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2903 | return ERR_ALERT | ERR_FATAL; |
| 2904 | } |
| 2905 | |
| 2906 | if (srv->do_agent && !srv->agent.port) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2907 | ha_alert("server %s does not have agent port. Agent check has been disabled.\n", |
| 2908 | srv->id); |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2909 | return ERR_ALERT | ERR_FATAL; |
| 2910 | } |
| 2911 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2912 | if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) { |
| 2913 | if (errmsg) { |
| 2914 | ha_alert("%s\n", errmsg); |
| 2915 | free(errmsg); |
| 2916 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2917 | return ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2918 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2919 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2920 | /* A dynamic server is disabled on startup. It must not be counted as |
| 2921 | * an active backend entry. |
| 2922 | */ |
| 2923 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2924 | if (srv->flags & SRV_F_BACKUP) |
| 2925 | px->srv_bck++; |
| 2926 | else |
| 2927 | px->srv_act++; |
| 2928 | } |
| 2929 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2930 | srv_lb_commit_status(srv); |
| 2931 | |
| 2932 | return 0; |
| 2933 | } |
| 2934 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2935 | int parse_server(const char *file, int linenum, char **args, |
| 2936 | struct proxy *curproxy, const struct proxy *defproxy, |
| 2937 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2938 | { |
| 2939 | struct server *newsrv = NULL; |
| 2940 | int err_code = 0; |
| 2941 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2942 | int cur_arg; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2943 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2944 | set_usermsgs_ctx(file, linenum, NULL); |
| 2945 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2946 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) { |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2947 | ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2948 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2949 | goto out; |
| 2950 | } |
| 2951 | else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) { |
| 2952 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2953 | goto out; |
| 2954 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2955 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2956 | if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) == |
| 2957 | (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) { |
| 2958 | if (!*args[2]) |
| 2959 | return 0; |
| 2960 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2961 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2962 | err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2963 | parse_flags); |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 2964 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2965 | /* the servers are linked backwards first */ |
| 2966 | if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2967 | newsrv->next = curproxy->srv; |
| 2968 | curproxy->srv = newsrv; |
| 2969 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2970 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2971 | if (err_code & ERR_CODE) |
| 2972 | goto out; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2973 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2974 | newsrv->conf.file = strdup(file); |
| 2975 | newsrv->conf.line = linenum; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2976 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2977 | while (*args[cur_arg]) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2978 | err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2979 | parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2980 | if (err_code & ERR_FATAL) |
| 2981 | goto out; |
| 2982 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2983 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2984 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2985 | err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2986 | if (err_code & ERR_FATAL) |
| 2987 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2988 | } |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 2989 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2990 | if (parse_flags & SRV_PARSE_TEMPLATE) |
| 2991 | _srv_parse_tmpl_init(newsrv, curproxy); |
| 2992 | |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 2993 | /* If the server id is fixed, insert it in the proxy used_id tree. |
| 2994 | * This is needed to detect a later duplicate id via srv_parse_id. |
| 2995 | * |
| 2996 | * If no is specified, a dynamic one is generated in |
| 2997 | * check_config_validity. |
| 2998 | */ |
| 2999 | if (newsrv->flags & SRV_F_FORCED_ID) |
| 3000 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 3001 | |
Amaury Denoyelle | 24abb0c | 2021-05-07 15:13:51 +0200 | [diff] [blame] | 3002 | HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight, |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3003 | "configured with weight of 0 will never be selected by load balancing algorithms\n"); |
Amaury Denoyelle | da0e7f6 | 2021-03-30 10:26:27 +0200 | [diff] [blame] | 3004 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3005 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3006 | return 0; |
| 3007 | |
| 3008 | out: |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3009 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3010 | return err_code; |
| 3011 | } |
| 3012 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 3013 | /* Returns a pointer to the first server matching either id <id>. |
| 3014 | * NULL is returned if no match is found. |
| 3015 | * the lookup is performed in the backend <bk> |
| 3016 | */ |
| 3017 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 3018 | { |
| 3019 | struct eb32_node *eb32; |
| 3020 | struct server *curserver; |
| 3021 | |
| 3022 | if (!bk || (id ==0)) |
| 3023 | return NULL; |
| 3024 | |
| 3025 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3026 | if (!(bk->cap & PR_CAP_BE)) |
| 3027 | return NULL; |
| 3028 | |
| 3029 | curserver = NULL; |
| 3030 | |
| 3031 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 3032 | if (eb32) |
| 3033 | curserver = container_of(eb32, struct server, conf.id); |
| 3034 | |
| 3035 | return curserver; |
| 3036 | } |
| 3037 | |
| 3038 | /* Returns a pointer to the first server matching either name <name>, or id |
| 3039 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 3040 | * the lookup is performed in the backend <bk> |
| 3041 | */ |
| 3042 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 3043 | { |
| 3044 | struct server *curserver; |
| 3045 | |
| 3046 | if (!bk || !name) |
| 3047 | return NULL; |
| 3048 | |
| 3049 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3050 | if (!(bk->cap & PR_CAP_BE)) |
| 3051 | return NULL; |
| 3052 | |
| 3053 | curserver = NULL; |
| 3054 | if (*name == '#') { |
| 3055 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 3056 | if (curserver) |
| 3057 | return curserver; |
| 3058 | } |
| 3059 | else { |
| 3060 | curserver = bk->srv; |
| 3061 | |
| 3062 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 3063 | curserver = curserver->next; |
| 3064 | |
| 3065 | if (curserver) |
| 3066 | return curserver; |
| 3067 | } |
| 3068 | |
| 3069 | return NULL; |
| 3070 | } |
| 3071 | |
| 3072 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 3073 | { |
| 3074 | struct server *byname; |
| 3075 | struct server *byid; |
| 3076 | |
| 3077 | if (!name && !id) |
| 3078 | return NULL; |
| 3079 | |
| 3080 | if (diff) |
| 3081 | *diff = 0; |
| 3082 | |
| 3083 | byname = byid = NULL; |
| 3084 | |
| 3085 | if (name) { |
| 3086 | byname = server_find_by_name(bk, name); |
| 3087 | if (byname && (!id || byname->puid == id)) |
| 3088 | return byname; |
| 3089 | } |
| 3090 | |
| 3091 | /* remaining possibilities : |
| 3092 | * - name not set |
| 3093 | * - name set but not found |
| 3094 | * - name found but ID doesn't match |
| 3095 | */ |
| 3096 | if (id) { |
| 3097 | byid = server_find_by_id(bk, id); |
| 3098 | if (byid) { |
| 3099 | if (byname) { |
| 3100 | /* use id only if forced by configuration */ |
| 3101 | if (byid->flags & SRV_F_FORCED_ID) { |
| 3102 | if (diff) |
| 3103 | *diff |= 2; |
| 3104 | return byid; |
| 3105 | } |
| 3106 | else { |
| 3107 | if (diff) |
| 3108 | *diff |= 1; |
| 3109 | return byname; |
| 3110 | } |
| 3111 | } |
| 3112 | |
| 3113 | /* remaining possibilities: |
| 3114 | * - name not set |
| 3115 | * - name set but not found |
| 3116 | */ |
| 3117 | if (name && diff) |
| 3118 | *diff |= 2; |
| 3119 | return byid; |
| 3120 | } |
| 3121 | |
| 3122 | /* id bot found */ |
| 3123 | if (byname) { |
| 3124 | if (diff) |
| 3125 | *diff |= 1; |
| 3126 | return byname; |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | return NULL; |
| 3131 | } |
| 3132 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3133 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3134 | * update a server's current IP address. |
| 3135 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3136 | * ip is in network format. |
| 3137 | * updater is a string which contains an information about the requester of the update. |
| 3138 | * updater is used if not NULL. |
| 3139 | * |
| 3140 | * A log line and a stderr warning message is generated based on server's backend options. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3141 | * |
| 3142 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3143 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3144 | int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater) |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3145 | { |
Christopher Faulet | d6c6b5f | 2020-09-08 10:27:24 +0200 | [diff] [blame] | 3146 | /* save the new IP family & address if necessary */ |
| 3147 | switch (ip_sin_family) { |
| 3148 | case AF_INET: |
| 3149 | if (s->addr.ss_family == ip_sin_family && |
| 3150 | !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4)) |
| 3151 | return 0; |
| 3152 | break; |
| 3153 | case AF_INET6: |
| 3154 | if (s->addr.ss_family == ip_sin_family && |
| 3155 | !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16)) |
| 3156 | return 0; |
| 3157 | break; |
| 3158 | }; |
| 3159 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3160 | /* generates a log line and a warning on stderr */ |
| 3161 | if (1) { |
| 3162 | /* book enough space for both IPv4 and IPv6 */ |
| 3163 | char oldip[INET6_ADDRSTRLEN]; |
| 3164 | char newip[INET6_ADDRSTRLEN]; |
| 3165 | |
| 3166 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3167 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3168 | |
| 3169 | /* copy old IP address in a string */ |
| 3170 | switch (s->addr.ss_family) { |
| 3171 | case AF_INET: |
| 3172 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3173 | break; |
| 3174 | case AF_INET6: |
| 3175 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3176 | break; |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3177 | default: |
| 3178 | strcpy(oldip, "(none)"); |
| 3179 | break; |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3180 | }; |
| 3181 | |
| 3182 | /* copy new IP address in a string */ |
| 3183 | switch (ip_sin_family) { |
| 3184 | case AF_INET: |
| 3185 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3186 | break; |
| 3187 | case AF_INET6: |
| 3188 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3189 | break; |
| 3190 | }; |
| 3191 | |
| 3192 | /* save log line into a buffer */ |
| 3193 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3194 | s->proxy->id, s->id, oldip, newip, updater); |
| 3195 | |
| 3196 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3197 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3198 | |
| 3199 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3200 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | /* save the new IP family */ |
| 3204 | s->addr.ss_family = ip_sin_family; |
| 3205 | /* save the new IP address */ |
| 3206 | switch (ip_sin_family) { |
| 3207 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3208 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3209 | break; |
| 3210 | case AF_INET6: |
| 3211 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3212 | break; |
| 3213 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3214 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3215 | srv_set_addr_desc(s, 1); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3216 | |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 3220 | /* update agent health check address and port |
| 3221 | * addr can be ip4/ip6 or a hostname |
| 3222 | * if one error occurs, don't apply anything |
| 3223 | * must be called with the server lock held. |
| 3224 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3225 | const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port) |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 3226 | { |
| 3227 | struct sockaddr_storage sk; |
| 3228 | struct buffer *msg; |
| 3229 | int new_port; |
| 3230 | |
| 3231 | msg = get_trash_chunk(); |
| 3232 | chunk_reset(msg); |
| 3233 | |
| 3234 | if (!(s->agent.state & CHK_ST_ENABLED)) { |
| 3235 | chunk_strcat(msg, "agent checks are not enabled on this server"); |
| 3236 | goto out; |
| 3237 | } |
| 3238 | if (addr) { |
| 3239 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3240 | if (str2ip(addr, &sk) == NULL) { |
| 3241 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3242 | goto out; |
| 3243 | } |
| 3244 | } |
| 3245 | if (port) { |
| 3246 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3247 | chunk_appendf(msg, "provided port is not an integer"); |
| 3248 | goto out; |
| 3249 | } |
| 3250 | if (new_port < 0 || new_port > 65535) { |
| 3251 | chunk_appendf(msg, "provided port is invalid"); |
| 3252 | goto out; |
| 3253 | } |
| 3254 | } |
| 3255 | out: |
| 3256 | if (msg->data) |
| 3257 | return msg->area; |
| 3258 | else { |
| 3259 | if (addr) |
| 3260 | set_srv_agent_addr(s, &sk); |
| 3261 | if (port) |
| 3262 | set_srv_agent_port(s, new_port); |
| 3263 | } |
| 3264 | return NULL; |
| 3265 | } |
| 3266 | |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 3267 | /* update server health check address and port |
| 3268 | * addr must be ip4 or ip6, it won't be resolved |
| 3269 | * if one error occurs, don't apply anything |
| 3270 | * must be called with the server lock held. |
| 3271 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3272 | const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port) |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 3273 | { |
| 3274 | struct sockaddr_storage sk; |
| 3275 | struct buffer *msg; |
| 3276 | int new_port; |
| 3277 | |
| 3278 | msg = get_trash_chunk(); |
| 3279 | chunk_reset(msg); |
| 3280 | |
| 3281 | if (!(s->check.state & CHK_ST_ENABLED)) { |
| 3282 | chunk_strcat(msg, "health checks are not enabled on this server"); |
| 3283 | goto out; |
| 3284 | } |
| 3285 | if (addr) { |
| 3286 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3287 | if (str2ip2(addr, &sk, 0) == NULL) { |
| 3288 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3289 | goto out; |
| 3290 | } |
| 3291 | } |
| 3292 | if (port) { |
| 3293 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3294 | chunk_appendf(msg, "provided port is not an integer"); |
| 3295 | goto out; |
| 3296 | } |
| 3297 | if (new_port < 0 || new_port > 65535) { |
| 3298 | chunk_appendf(msg, "provided port is invalid"); |
| 3299 | goto out; |
| 3300 | } |
| 3301 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 3302 | if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) { |
| 3303 | chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use"); |
| 3304 | goto out; |
| 3305 | } |
| 3306 | } |
| 3307 | out: |
| 3308 | if (msg->data) |
| 3309 | return msg->area; |
| 3310 | else { |
| 3311 | if (addr) |
| 3312 | s->check.addr = sk; |
| 3313 | if (port) |
| 3314 | s->check.port = new_port; |
| 3315 | } |
| 3316 | return NULL; |
| 3317 | } |
| 3318 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3319 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3320 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3321 | * |
| 3322 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3323 | * returned by the function. |
| 3324 | * |
| 3325 | * The function first does the following, in that order: |
| 3326 | * - validates the new addr and/or port |
| 3327 | * - checks if an update is required (new IP or port is different than current ones) |
| 3328 | * - checks the update is allowed: |
| 3329 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3330 | * - allow all changes if no CHECKS are configured |
| 3331 | * - if CHECK is configured: |
| 3332 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3333 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3334 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3335 | * |
| 3336 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3337 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3338 | const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater) |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3339 | { |
| 3340 | struct sockaddr_storage sa; |
| 3341 | int ret, port_change_required; |
| 3342 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3343 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3344 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3345 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3346 | |
| 3347 | msg = get_trash_chunk(); |
| 3348 | chunk_reset(msg); |
| 3349 | |
| 3350 | if (addr) { |
| 3351 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3352 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3353 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3354 | goto out; |
| 3355 | } |
| 3356 | |
| 3357 | /* changes are allowed on AF_INET* families only */ |
| 3358 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3359 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3360 | goto out; |
| 3361 | } |
| 3362 | |
| 3363 | /* collecting data currently setup */ |
| 3364 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3365 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3366 | /* changes are allowed on AF_INET* families only */ |
| 3367 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3368 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3369 | goto out; |
| 3370 | } |
| 3371 | |
| 3372 | /* applying ADDR changes if required and allowed |
| 3373 | * ipcmp returns 0 when both ADDR are the same |
| 3374 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3375 | if (ipcmp(&s->addr, &sa, 0) == 0) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3376 | chunk_appendf(msg, "no need to change the addr"); |
| 3377 | goto port; |
| 3378 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3379 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3380 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3381 | |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3382 | /* update report for caller */ |
| 3383 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3384 | } |
| 3385 | |
| 3386 | port: |
| 3387 | if (port) { |
| 3388 | char sign = '\0'; |
| 3389 | char *endptr; |
| 3390 | |
| 3391 | if (addr) |
| 3392 | chunk_appendf(msg, ", "); |
| 3393 | |
| 3394 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3395 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3396 | |
| 3397 | /* check if PORT change is required */ |
| 3398 | port_change_required = 0; |
| 3399 | |
| 3400 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3401 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3402 | new_port = strtol(port, &endptr, 10); |
| 3403 | if ((errno != 0) || (port == endptr)) { |
| 3404 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3405 | goto out; |
| 3406 | } |
| 3407 | |
| 3408 | /* check if caller triggers a port mapped or offset */ |
| 3409 | if (sign == '-' || (sign == '+')) { |
| 3410 | /* check if server currently uses port map */ |
| 3411 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3412 | /* switch from fixed port to port map mandatorily triggers |
| 3413 | * a port change */ |
| 3414 | port_change_required = 1; |
| 3415 | /* check is configured |
| 3416 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3417 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3418 | * to port mapping */ |
William Dauchy | 69f118d | 2021-02-03 22:30:07 +0100 | [diff] [blame] | 3419 | if (!s->check.port) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3420 | chunk_appendf(msg, "can't change <port> to port map because it is incompatible with current health check port configuration (use 'port' statement from the 'server' directive."); |
| 3421 | goto out; |
| 3422 | } |
| 3423 | } |
| 3424 | /* we're already using port maps */ |
| 3425 | else { |
| 3426 | port_change_required = current_port != new_port; |
| 3427 | } |
| 3428 | } |
| 3429 | /* fixed port */ |
| 3430 | else { |
| 3431 | port_change_required = current_port != new_port; |
| 3432 | } |
| 3433 | |
| 3434 | /* applying PORT changes if required and update response message */ |
| 3435 | if (port_change_required) { |
| 3436 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3437 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3438 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3439 | |
| 3440 | /* prepare message */ |
| 3441 | chunk_appendf(msg, "port changed from '"); |
| 3442 | if (s->flags & SRV_F_MAPPORTS) |
| 3443 | chunk_appendf(msg, "+"); |
| 3444 | chunk_appendf(msg, "%d' to '", current_port); |
| 3445 | |
| 3446 | if (sign == '-') { |
| 3447 | s->flags |= SRV_F_MAPPORTS; |
| 3448 | chunk_appendf(msg, "%c", sign); |
| 3449 | /* just use for result output */ |
| 3450 | new_port = -new_port; |
| 3451 | } |
| 3452 | else if (sign == '+') { |
| 3453 | s->flags |= SRV_F_MAPPORTS; |
| 3454 | chunk_appendf(msg, "%c", sign); |
| 3455 | } |
| 3456 | else { |
| 3457 | s->flags &= ~SRV_F_MAPPORTS; |
| 3458 | } |
| 3459 | |
| 3460 | chunk_appendf(msg, "%d'", new_port); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3461 | } |
| 3462 | else { |
| 3463 | chunk_appendf(msg, "no need to change the port"); |
| 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | out: |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3468 | if (changed) { |
| 3469 | /* force connection cleanup on the given server */ |
| 3470 | srv_cleanup_connections(s); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3471 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3472 | srv_set_addr_desc(s, 1); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3473 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3474 | if (updater) |
| 3475 | chunk_appendf(msg, " by '%s'", updater); |
| 3476 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3477 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3478 | } |
| 3479 | |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3480 | /* |
| 3481 | * update server status based on result of SRV resolution |
| 3482 | * returns: |
| 3483 | * 0 if server status is updated |
| 3484 | * 1 if server status has not changed |
| 3485 | * |
| 3486 | * Must be called with the server lock held. |
| 3487 | */ |
| 3488 | int srvrq_update_srv_status(struct server *s, int has_no_ip) |
| 3489 | { |
| 3490 | if (!s->srvrq) |
| 3491 | return 1; |
| 3492 | |
| 3493 | /* since this server has an IP, it can go back in production */ |
| 3494 | if (has_no_ip == 0) { |
| 3495 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3496 | return 1; |
| 3497 | } |
| 3498 | |
| 3499 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3500 | return 1; |
| 3501 | |
| 3502 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record"); |
| 3503 | return 0; |
| 3504 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3505 | |
| 3506 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3507 | * update server status based on result of name resolution |
| 3508 | * returns: |
| 3509 | * 0 if server status is updated |
| 3510 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3511 | * |
| 3512 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3513 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3514 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3515 | { |
Emeric Brun | 750fe79 | 2020-12-23 16:51:12 +0100 | [diff] [blame] | 3516 | struct resolvers *resolvers = s->resolvers; |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3517 | struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3518 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3519 | |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3520 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3521 | if (resolution == NULL) |
| 3522 | return srvrq_update_srv_status(s, has_no_ip); |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3523 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3524 | switch (resolution->status) { |
| 3525 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3526 | /* status when HAProxy has just (re)started. |
| 3527 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3528 | break; |
| 3529 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3530 | case RSLV_STATUS_VALID: |
| 3531 | /* |
| 3532 | * resume health checks |
| 3533 | * server will be turned back on if health check is safe |
| 3534 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3535 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3536 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3537 | return 1; |
| 3538 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, |
| 3539 | "No IP for server "); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3540 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3541 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3542 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3543 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3544 | return 1; |
| 3545 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3546 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3547 | s->proxy->id, s->id); |
| 3548 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3549 | ha_warning("%s.\n", trash.area); |
| 3550 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3551 | return 0; |
| 3552 | |
| 3553 | case RSLV_STATUS_NX: |
| 3554 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3555 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3556 | if (!tick_is_expired(exp, now_ms)) |
| 3557 | break; |
| 3558 | |
| 3559 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3560 | return 1; |
| 3561 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status"); |
| 3562 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3563 | |
| 3564 | case RSLV_STATUS_TIMEOUT: |
| 3565 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3566 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3567 | if (!tick_is_expired(exp, now_ms)) |
| 3568 | break; |
| 3569 | |
| 3570 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3571 | return 1; |
| 3572 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status"); |
| 3573 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3574 | |
| 3575 | case RSLV_STATUS_REFUSED: |
| 3576 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3577 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3578 | if (!tick_is_expired(exp, now_ms)) |
| 3579 | break; |
| 3580 | |
| 3581 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3582 | return 1; |
| 3583 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status"); |
| 3584 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3585 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3586 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3587 | /* stop server if resolution failed for a long enough period */ |
| 3588 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3589 | if (!tick_is_expired(exp, now_ms)) |
| 3590 | break; |
| 3591 | |
| 3592 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3593 | return 1; |
| 3594 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error"); |
| 3595 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | return 1; |
| 3599 | } |
| 3600 | |
| 3601 | /* |
| 3602 | * Server Name Resolution valid response callback |
| 3603 | * It expects: |
| 3604 | * - <nameserver>: the name server which answered the valid response |
| 3605 | * - <response>: buffer containing a valid DNS response |
| 3606 | * - <response_len>: size of <response> |
| 3607 | * It performs the following actions: |
| 3608 | * - ignore response if current ip found and server family not met |
| 3609 | * - update with first new ip found if family is met and current IP is not found |
| 3610 | * returns: |
| 3611 | * 0 on error |
| 3612 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3613 | * |
| 3614 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3615 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3616 | int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3617 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3618 | struct server *s = NULL; |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3619 | struct resolv_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3620 | void *serverip, *firstip; |
| 3621 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3622 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3623 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3624 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3625 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3626 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3627 | if (!s) |
| 3628 | return 1; |
| 3629 | |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3630 | if (s->srvrq) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3631 | /* If DNS resolution is disabled ignore it. |
| 3632 | * This is the case if the server was associated to |
| 3633 | * a SRV record and this record is now expired. |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3634 | */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3635 | if (s->flags & SRV_F_NO_RESOLUTION) |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3636 | return 1; |
| 3637 | } |
| 3638 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3639 | resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3640 | if (!resolution) |
| 3641 | return 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3642 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3643 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3644 | firstip = NULL; /* pointer to the first valid response found */ |
| 3645 | /* it will be used as the new IP if a change is required */ |
| 3646 | firstip_sin_family = AF_UNSPEC; |
| 3647 | serverip = NULL; /* current server IP address */ |
| 3648 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3649 | /* initializing server IP pointer */ |
| 3650 | server_sin_family = s->addr.ss_family; |
| 3651 | switch (server_sin_family) { |
| 3652 | case AF_INET: |
| 3653 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3654 | break; |
| 3655 | |
| 3656 | case AF_INET6: |
| 3657 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3658 | break; |
| 3659 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3660 | case AF_UNSPEC: |
| 3661 | break; |
| 3662 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3663 | default: |
| 3664 | goto invalid; |
| 3665 | } |
| 3666 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3667 | ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts, |
| 3668 | serverip, server_sin_family, &firstip, |
| 3669 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3670 | |
| 3671 | switch (ret) { |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3672 | case RSLV_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3673 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3674 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3675 | case RSLV_UPD_SRVIP_NOT_FOUND: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3676 | goto save_ip; |
| 3677 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3678 | case RSLV_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3679 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3680 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3681 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3682 | case RSLV_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3683 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3684 | resolution->status = RSLV_STATUS_OTHER; |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3685 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3686 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3687 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3688 | default: |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3689 | has_no_ip = 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3690 | goto invalid; |
| 3691 | |
| 3692 | } |
| 3693 | |
| 3694 | save_ip: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3695 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3696 | counters->app.resolver.update++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3697 | /* save the first ip we found */ |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3698 | chunk_printf(chk, "%s/%s", counters->pid, counters->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3699 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3700 | else |
| 3701 | chunk_printf(chk, "DNS cache"); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3702 | srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3703 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3704 | update_status: |
Christopher Faulet | a8ce497 | 2021-06-24 15:26:03 +0200 | [diff] [blame] | 3705 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3706 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3707 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3708 | |
| 3709 | invalid: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3710 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3711 | counters->app.resolver.invalid++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3712 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 3713 | } |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3714 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3715 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3716 | return 0; |
| 3717 | } |
| 3718 | |
| 3719 | /* |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3720 | * SRV record error management callback |
| 3721 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3722 | * 0 if we can trash answser items. |
| 3723 | * 1 when safely ignored and we must kept answer items |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3724 | * |
| 3725 | * Grabs the server's lock. |
| 3726 | */ |
| 3727 | int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code) |
| 3728 | { |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3729 | struct resolv_srvrq *srvrq; |
| 3730 | struct resolv_resolution *res; |
| 3731 | struct resolvers *resolvers; |
| 3732 | int exp; |
| 3733 | |
| 3734 | /* SRV records */ |
| 3735 | srvrq = objt_resolv_srvrq(requester->owner); |
| 3736 | if (!srvrq) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3737 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3738 | |
| 3739 | resolvers = srvrq->resolvers; |
| 3740 | res = requester->resolution; |
| 3741 | |
| 3742 | switch (res->status) { |
| 3743 | |
| 3744 | case RSLV_STATUS_NX: |
| 3745 | /* stop server if resolution is NX for a long enough period */ |
| 3746 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 3747 | if (!tick_is_expired(exp, now_ms)) |
| 3748 | return 1; |
| 3749 | break; |
| 3750 | |
| 3751 | case RSLV_STATUS_TIMEOUT: |
| 3752 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 3753 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 3754 | if (!tick_is_expired(exp, now_ms)) |
| 3755 | return 1; |
| 3756 | break; |
| 3757 | |
| 3758 | case RSLV_STATUS_REFUSED: |
| 3759 | /* stop server if resolution is REFUSED for a long enough period */ |
| 3760 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 3761 | if (!tick_is_expired(exp, now_ms)) |
| 3762 | return 1; |
| 3763 | break; |
| 3764 | |
| 3765 | default: |
| 3766 | /* stop server if resolution failed for a long enough period */ |
| 3767 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 3768 | if (!tick_is_expired(exp, now_ms)) |
| 3769 | return 1; |
| 3770 | } |
| 3771 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3772 | /* Remove any associated server ref */ |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3773 | resolv_detach_from_resolution_answer_items(res, requester); |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3774 | |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3775 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3779 | * Server Name Resolution error management callback |
| 3780 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3781 | * 0 if we can trash answser items. |
| 3782 | * 1 when safely ignored and we must kept answer items |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3783 | * |
| 3784 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3785 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3786 | int snr_resolution_error_cb(struct resolv_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3787 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3788 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3789 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3790 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3791 | if (!s) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3792 | return 0; |
| 3793 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3794 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3795 | if (!snr_update_srv_status(s, 1)) { |
Christopher Faulet | 5130c21 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 3796 | memset(&s->addr, 0, sizeof(s->addr)); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3797 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3798 | resolv_detach_from_resolution_answer_items(requester->resolution, requester); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3799 | return 0; |
| 3800 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3801 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3802 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3803 | return 1; |
| 3804 | } |
| 3805 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3806 | /* |
| 3807 | * Function to check if <ip> is already affected to a server in the backend |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3808 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3809 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 3810 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3811 | * |
| 3812 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3813 | */ |
| 3814 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 3815 | { |
| 3816 | struct server *tmpsrv; |
| 3817 | struct proxy *be; |
| 3818 | |
| 3819 | if (!srv) |
| 3820 | return NULL; |
| 3821 | |
| 3822 | be = srv->proxy; |
| 3823 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3824 | /* we found the current server is the same, ignore it */ |
| 3825 | if (srv == tmpsrv) |
| 3826 | continue; |
| 3827 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3828 | /* We want to compare the IP in the record with the IP of the servers in the |
| 3829 | * same backend, only if: |
| 3830 | * * DNS resolution is enabled on the server |
| 3831 | * * the hostname used for the resolution by our server is the same than the |
| 3832 | * one used for the server found in the backend |
| 3833 | * * the server found in the backend is not our current server |
| 3834 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3835 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3836 | if ((tmpsrv->hostname_dn == NULL) || |
| 3837 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3838 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3839 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3840 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3841 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3842 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3843 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3844 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3845 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3846 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3847 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3848 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3849 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3850 | /* At this point, we have 2 different servers using the same DNS hostname |
| 3851 | * for their respective resolution. |
| 3852 | */ |
| 3853 | if (*ip_family == tmpsrv->addr.ss_family && |
| 3854 | ((tmpsrv->addr.ss_family == AF_INET && |
| 3855 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 3856 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 3857 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3858 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3859 | return tmpsrv; |
| 3860 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3861 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3864 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3865 | return NULL; |
| 3866 | } |
| 3867 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3868 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 3869 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 3870 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 3871 | * not NULL, is filled up. |
| 3872 | */ |
| 3873 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 3874 | { |
| 3875 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3876 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 3877 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3878 | return 1; |
| 3879 | } |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3883 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 3884 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3885 | * |
| 3886 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3887 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3888 | int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3889 | { |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3890 | struct resolv_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3891 | char *hostname_dn; |
| 3892 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3893 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 3894 | /* Note that the server lock is already held. */ |
| 3895 | if (!srv->resolvers) |
| 3896 | return -1; |
| 3897 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3898 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3899 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3900 | /* run time DNS/SRV resolution was not active for this server |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3901 | * and we can't enable it at run time for now. |
| 3902 | */ |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3903 | if (!srv->resolv_requester && !srv->srvrq) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3904 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3905 | |
| 3906 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3907 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3908 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 3909 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3910 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3911 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3912 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3913 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3914 | resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3915 | if (resolution && |
| 3916 | resolution->hostname_dn && |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3917 | resolution->hostname_dn_len == hostname_dn_len && |
| 3918 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3919 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3920 | |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3921 | resolv_unlink_resolution(srv->resolv_requester); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3922 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3923 | free(srv->hostname); |
| 3924 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3925 | srv->hostname = strdup(hostname); |
| 3926 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3927 | srv->hostname_dn_len = hostname_dn_len; |
| 3928 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3929 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3930 | |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 3931 | if (srv->flags & SRV_F_NO_RESOLUTION) |
| 3932 | goto end; |
| 3933 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3934 | if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3935 | goto err; |
| 3936 | |
| 3937 | end: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3938 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3939 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3940 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3941 | |
| 3942 | err: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3943 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3944 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3945 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3948 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 3949 | * from the state file. This is suited for initial address configuration. |
| 3950 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 3951 | * *err_code, if not NULL, is filled up. |
| 3952 | */ |
| 3953 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 3954 | { |
| 3955 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 3956 | if (err_code) |
| 3957 | *err_code |= ERR_WARN; |
| 3958 | return 1; |
| 3959 | } |
| 3960 | return 0; |
| 3961 | } |
| 3962 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3963 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 3964 | static int srv_iterate_initaddr(struct server *srv) |
| 3965 | { |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3966 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3967 | int return_code = 0; |
| 3968 | int err_code; |
| 3969 | unsigned int methods; |
| 3970 | |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3971 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 3972 | if (!name && srv->srvrq) |
| 3973 | name = srv->srvrq->name; |
| 3974 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3975 | methods = srv->init_addr_methods; |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3976 | if (!methods) { |
| 3977 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3978 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 3979 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3980 | if (srv->resolvers_id) { |
| 3981 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 3982 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 3983 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 3986 | /* "-dr" : always append "none" so that server addresses resolution |
| 3987 | * failures are silently ignored, this is convenient to validate some |
| 3988 | * configs out of their environment. |
| 3989 | */ |
| 3990 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 3991 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 3992 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3993 | while (methods) { |
| 3994 | err_code = 0; |
| 3995 | switch (srv_get_next_initaddr(&methods)) { |
| 3996 | case SRV_IADDR_LAST: |
| 3997 | if (!srv->lastaddr) |
| 3998 | continue; |
| 3999 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4000 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4001 | return_code |= err_code; |
| 4002 | break; |
| 4003 | |
| 4004 | case SRV_IADDR_LIBC: |
| 4005 | if (!srv->hostname) |
| 4006 | continue; |
| 4007 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4008 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4009 | return_code |= err_code; |
| 4010 | break; |
| 4011 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4012 | case SRV_IADDR_NONE: |
| 4013 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4014 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4015 | ha_warning("could not resolve address '%s', disabling server.\n", |
| 4016 | name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4017 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4018 | return return_code; |
| 4019 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4020 | case SRV_IADDR_IP: |
| 4021 | ipcpy(&srv->init_addr, &srv->addr); |
| 4022 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4023 | ha_warning("could not resolve address '%s', falling back to configured address.\n", |
| 4024 | name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4025 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4026 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4027 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4028 | default: /* unhandled method */ |
| 4029 | break; |
| 4030 | } |
| 4031 | } |
| 4032 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4033 | if (!return_code) |
| 4034 | ha_alert("no method found to resolve address '%s'.\n", name); |
| 4035 | else |
| 4036 | ha_alert("could not resolve address '%s'.\n", name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4037 | |
| 4038 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4039 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4040 | out: |
| 4041 | srv_set_dyncookie(srv); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 4042 | srv_set_addr_desc(srv, 1); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4043 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4044 | } |
| 4045 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4046 | /* |
| 4047 | * This function parses all backends and all servers within each backend |
| 4048 | * and performs servers' addr resolution based on information provided by: |
| 4049 | * - configuration file |
| 4050 | * - server-state file (states provided by an 'old' haproxy process) |
| 4051 | * |
| 4052 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4053 | */ |
| 4054 | int srv_init_addr(void) |
| 4055 | { |
| 4056 | struct proxy *curproxy; |
| 4057 | int return_code = 0; |
| 4058 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4059 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4060 | while (curproxy) { |
| 4061 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4062 | |
| 4063 | /* servers are in backend only */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4064 | if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4065 | goto srv_init_addr_next; |
| 4066 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4067 | for (srv = curproxy->srv; srv; srv = srv->next) { |
| 4068 | set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4069 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4070 | return_code |= srv_iterate_initaddr(srv); |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4071 | reset_usermsgs_ctx(); |
| 4072 | } |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4073 | |
| 4074 | srv_init_addr_next: |
| 4075 | curproxy = curproxy->next; |
| 4076 | } |
| 4077 | |
| 4078 | return return_code; |
| 4079 | } |
| 4080 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4081 | /* |
| 4082 | * Must be called with the server lock held. |
| 4083 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4084 | const char *srv_update_fqdn(struct server *server, const char *fqdn, const char *updater, int resolv_locked) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4085 | { |
| 4086 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4087 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4088 | |
| 4089 | msg = get_trash_chunk(); |
| 4090 | chunk_reset(msg); |
| 4091 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4092 | if (server->hostname && strcmp(fqdn, server->hostname) == 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4093 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4094 | goto out; |
| 4095 | } |
| 4096 | |
| 4097 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4098 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4099 | goto out; |
| 4100 | } |
| 4101 | |
| 4102 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4103 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4104 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4105 | if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4106 | chunk_reset(msg); |
| 4107 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4108 | server->proxy->id, server->id); |
| 4109 | goto out; |
| 4110 | } |
| 4111 | |
| 4112 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4113 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4114 | |
| 4115 | out: |
| 4116 | if (updater) |
| 4117 | chunk_appendf(msg, " by '%s'", updater); |
| 4118 | chunk_appendf(msg, "\n"); |
| 4119 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4120 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4124 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4125 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4126 | * on the CLI, sets the CLI's state to CLI_ST_PRINT and returns NULL. This is only |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4127 | * used for CLI commands requiring a server name. |
| 4128 | * Important: the <arg> is modified to remove the '/'. |
| 4129 | */ |
| 4130 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4131 | { |
| 4132 | struct proxy *px; |
| 4133 | struct server *sv; |
| 4134 | char *line; |
| 4135 | |
| 4136 | /* split "backend/server" and make <line> point to server */ |
| 4137 | for (line = arg; *line; line++) |
| 4138 | if (*line == '/') { |
| 4139 | *line++ = '\0'; |
| 4140 | break; |
| 4141 | } |
| 4142 | |
| 4143 | if (!*line || !*arg) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4144 | cli_err(appctx, "Require 'backend/server'.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4145 | return NULL; |
| 4146 | } |
| 4147 | |
| 4148 | if (!get_backend_server(arg, line, &px, &sv)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4149 | cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4150 | return NULL; |
| 4151 | } |
| 4152 | |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4153 | if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4154 | cli_err(appctx, "Proxy is disabled.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4155 | return NULL; |
| 4156 | } |
| 4157 | |
| 4158 | return sv; |
| 4159 | } |
| 4160 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4161 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4162 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4163 | static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4164 | { |
| 4165 | struct server *sv; |
| 4166 | const char *warning; |
| 4167 | |
| 4168 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4169 | return 1; |
| 4170 | |
| 4171 | sv = cli_find_server(appctx, args[2]); |
| 4172 | if (!sv) |
| 4173 | return 1; |
| 4174 | |
| 4175 | if (strcmp(args[3], "weight") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4176 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4177 | warning = server_parse_weight_change_request(sv, args[4]); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4178 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4179 | if (warning) |
| 4180 | cli_err(appctx, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4181 | } |
| 4182 | else if (strcmp(args[3], "state") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4183 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4184 | if (strcmp(args[4], "ready") == 0) |
| 4185 | srv_adm_set_ready(sv); |
| 4186 | else if (strcmp(args[4], "drain") == 0) |
| 4187 | srv_adm_set_drain(sv); |
| 4188 | else if (strcmp(args[4], "maint") == 0) |
| 4189 | srv_adm_set_maint(sv); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4190 | else |
| 4191 | cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4192 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4193 | } |
| 4194 | else if (strcmp(args[3], "health") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4195 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4196 | if (sv->track) |
| 4197 | cli_err(appctx, "cannot change health on a tracking server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4198 | else if (strcmp(args[4], "up") == 0) { |
| 4199 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4200 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4201 | } |
| 4202 | else if (strcmp(args[4], "stopping") == 0) { |
| 4203 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4204 | srv_set_stopping(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4205 | } |
| 4206 | else if (strcmp(args[4], "down") == 0) { |
| 4207 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4208 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4209 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4210 | else |
| 4211 | cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4212 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4213 | } |
| 4214 | else if (strcmp(args[3], "agent") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4215 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4216 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4217 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4218 | else if (strcmp(args[4], "up") == 0) { |
| 4219 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4220 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4221 | } |
| 4222 | else if (strcmp(args[4], "down") == 0) { |
| 4223 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4224 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4225 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4226 | else |
| 4227 | cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4228 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4229 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4230 | else if (strcmp(args[3], "agent-addr") == 0) { |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4231 | char *addr = NULL; |
| 4232 | char *port = NULL; |
| 4233 | if (strlen(args[4]) == 0) { |
| 4234 | cli_err(appctx, "set server <b>/<s> agent-addr requires" |
| 4235 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4236 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4237 | } |
| 4238 | addr = args[4]; |
| 4239 | if (strcmp(args[5], "port") == 0) |
| 4240 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4241 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4242 | warning = srv_update_agent_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4243 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4244 | if (warning) |
| 4245 | cli_msg(appctx, LOG_WARNING, warning); |
| 4246 | } |
| 4247 | else if (strcmp(args[3], "agent-port") == 0) { |
| 4248 | char *port = NULL; |
| 4249 | if (strlen(args[4]) == 0) { |
| 4250 | cli_err(appctx, "set server <b>/<s> agent-port requires" |
| 4251 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4252 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4253 | } |
| 4254 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4255 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4256 | warning = srv_update_agent_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4257 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4258 | if (warning) |
| 4259 | cli_msg(appctx, LOG_WARNING, warning); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4260 | } |
| 4261 | else if (strcmp(args[3], "agent-send") == 0) { |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4262 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4263 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4264 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
| 4265 | else { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4266 | if (!set_srv_agent_send(sv, args[4])) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4267 | cli_err(appctx, "cannot allocate memory for new string.\n"); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4268 | } |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4269 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4270 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4271 | else if (strcmp(args[3], "check-addr") == 0) { |
| 4272 | char *addr = NULL; |
| 4273 | char *port = NULL; |
| 4274 | if (strlen(args[4]) == 0) { |
| 4275 | cli_err(appctx, "set server <b>/<s> check-addr requires" |
| 4276 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4277 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4278 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4279 | addr = args[4]; |
| 4280 | if (strcmp(args[5], "port") == 0) |
| 4281 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4282 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4283 | warning = srv_update_check_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4284 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4285 | if (warning) |
| 4286 | cli_msg(appctx, LOG_WARNING, warning); |
| 4287 | } |
| 4288 | else if (strcmp(args[3], "check-port") == 0) { |
| 4289 | char *port = NULL; |
| 4290 | if (strlen(args[4]) == 0) { |
| 4291 | cli_err(appctx, "set server <b>/<s> check-port requires" |
| 4292 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4293 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4294 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4295 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4296 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4297 | warning = srv_update_check_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4298 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4299 | if (warning) |
| 4300 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4301 | } |
| 4302 | else if (strcmp(args[3], "addr") == 0) { |
| 4303 | char *addr = NULL; |
| 4304 | char *port = NULL; |
| 4305 | if (strlen(args[4]) == 0) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4306 | cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4307 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4308 | } |
| 4309 | else { |
| 4310 | addr = args[4]; |
| 4311 | } |
| 4312 | if (strcmp(args[5], "port") == 0) { |
| 4313 | port = args[6]; |
| 4314 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4315 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4316 | warning = srv_update_addr_port(sv, addr, port, "stats socket command"); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4317 | if (warning) |
| 4318 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4319 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4320 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4321 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4322 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4323 | if (!*args[4]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4324 | cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4325 | goto out; |
| 4326 | } |
| 4327 | if (!sv->resolvers) { |
| 4328 | cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n"); |
| 4329 | goto out; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4330 | } |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4331 | if (sv->srvrq) { |
| 4332 | cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4333 | goto out; |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4334 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4335 | HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock); |
| 4336 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4337 | /* ensure runtime resolver will process this new fqdn */ |
| 4338 | if (sv->flags & SRV_F_NO_RESOLUTION) { |
| 4339 | sv->flags &= ~SRV_F_NO_RESOLUTION; |
| 4340 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4341 | warning = srv_update_fqdn(sv, args[4], "stats socket command", 1); |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4342 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4343 | HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4344 | if (warning) |
| 4345 | cli_msg(appctx, LOG_WARNING, warning); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4346 | } |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4347 | else if (strcmp(args[3], "ssl") == 0) { |
| 4348 | #ifdef USE_OPENSSL |
Amaury Denoyelle | b89d3d3 | 2021-05-19 15:00:54 +0200 | [diff] [blame] | 4349 | if (sv->flags & SRV_F_DYNAMIC) { |
| 4350 | cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n"); |
| 4351 | goto out; |
| 4352 | } |
| 4353 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4354 | if (sv->ssl_ctx.ctx == NULL) { |
| 4355 | cli_err(appctx, "'set server <srv> ssl' cannot be set. " |
| 4356 | " default-server should define ssl settings\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4357 | goto out; |
| 4358 | } |
| 4359 | |
| 4360 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4361 | if (strcmp(args[4], "on") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4362 | srv_set_ssl(sv, 1); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4363 | } else if (strcmp(args[4], "off") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4364 | srv_set_ssl(sv, 0); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4365 | } else { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4366 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4367 | cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4368 | goto out; |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4369 | } |
| 4370 | srv_cleanup_connections(sv); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4371 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4372 | cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n"); |
| 4373 | #else |
| 4374 | cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n"); |
| 4375 | #endif |
| 4376 | } else { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4377 | cli_err(appctx, |
William Dauchy | 3f4ec7d | 2021-02-15 17:22:16 +0100 | [diff] [blame] | 4378 | "usage: set server <backend>/<server> " |
| 4379 | "addr | agent | agent-addr | agent-port | agent-send | " |
| 4380 | "check-addr | check-port | fqdn | health | ssl | " |
| 4381 | "state | weight\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4382 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4383 | out: |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4384 | return 1; |
| 4385 | } |
| 4386 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4387 | static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4388 | { |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4389 | struct proxy *px; |
| 4390 | struct server *sv; |
| 4391 | char *line; |
| 4392 | |
| 4393 | |
| 4394 | /* split "backend/server" and make <line> point to server */ |
| 4395 | for (line = args[2]; *line; line++) |
| 4396 | if (*line == '/') { |
| 4397 | *line++ = '\0'; |
| 4398 | break; |
| 4399 | } |
| 4400 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4401 | if (!*line) |
| 4402 | return cli_err(appctx, "Require 'backend/server'.\n"); |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4403 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4404 | if (!get_backend_server(args[2], line, &px, &sv)) |
| 4405 | return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4406 | |
| 4407 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4408 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4409 | sv->iweight); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 4410 | if (applet_putstr(appctx, trash.area) == -1) |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4411 | return 0; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4412 | return 1; |
| 4413 | } |
| 4414 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4415 | /* Parse a "set weight" command. |
| 4416 | * |
| 4417 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4418 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4419 | static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4420 | { |
| 4421 | struct server *sv; |
| 4422 | const char *warning; |
| 4423 | |
| 4424 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4425 | return 1; |
| 4426 | |
| 4427 | sv = cli_find_server(appctx, args[2]); |
| 4428 | if (!sv) |
| 4429 | return 1; |
| 4430 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4431 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4432 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4433 | warning = server_parse_weight_change_request(sv, args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4434 | if (warning) |
| 4435 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4436 | |
| 4437 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4438 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4439 | return 1; |
| 4440 | } |
| 4441 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4442 | /* parse a "set maxconn server" command. It always returns 1. |
| 4443 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4444 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4445 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4446 | static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4447 | { |
| 4448 | struct server *sv; |
| 4449 | const char *warning; |
| 4450 | |
| 4451 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4452 | return 1; |
| 4453 | |
| 4454 | sv = cli_find_server(appctx, args[3]); |
| 4455 | if (!sv) |
| 4456 | return 1; |
| 4457 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4458 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4459 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4460 | warning = server_parse_maxconn_change_request(sv, args[4]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4461 | if (warning) |
| 4462 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4463 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4464 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4465 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4466 | return 1; |
| 4467 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4468 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4469 | /* parse a "disable agent" command. It always returns 1. |
| 4470 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4471 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4472 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4473 | static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4474 | { |
| 4475 | struct server *sv; |
| 4476 | |
| 4477 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4478 | return 1; |
| 4479 | |
| 4480 | sv = cli_find_server(appctx, args[2]); |
| 4481 | if (!sv) |
| 4482 | return 1; |
| 4483 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4484 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4485 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4486 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4487 | return 1; |
| 4488 | } |
| 4489 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4490 | /* parse a "disable health" command. It always returns 1. |
| 4491 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4492 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4493 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4494 | static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4495 | { |
| 4496 | struct server *sv; |
| 4497 | |
| 4498 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4499 | return 1; |
| 4500 | |
| 4501 | sv = cli_find_server(appctx, args[2]); |
| 4502 | if (!sv) |
| 4503 | return 1; |
| 4504 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4505 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4506 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4507 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4508 | return 1; |
| 4509 | } |
| 4510 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4511 | /* parse a "disable server" command. It always returns 1. |
| 4512 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4513 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4514 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4515 | static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4516 | { |
| 4517 | struct server *sv; |
| 4518 | |
| 4519 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4520 | return 1; |
| 4521 | |
| 4522 | sv = cli_find_server(appctx, args[2]); |
| 4523 | if (!sv) |
| 4524 | return 1; |
| 4525 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4526 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4527 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4528 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4529 | return 1; |
| 4530 | } |
| 4531 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4532 | /* parse a "enable agent" command. It always returns 1. |
| 4533 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4534 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4535 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4536 | static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4537 | { |
| 4538 | struct server *sv; |
| 4539 | |
| 4540 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4541 | return 1; |
| 4542 | |
| 4543 | sv = cli_find_server(appctx, args[2]); |
| 4544 | if (!sv) |
| 4545 | return 1; |
| 4546 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4547 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) |
| 4548 | return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n"); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4549 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4550 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4551 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4552 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4553 | return 1; |
| 4554 | } |
| 4555 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4556 | /* parse a "enable health" command. It always returns 1. |
| 4557 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4558 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4559 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4560 | static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4561 | { |
| 4562 | struct server *sv; |
| 4563 | |
| 4564 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4565 | return 1; |
| 4566 | |
| 4567 | sv = cli_find_server(appctx, args[2]); |
| 4568 | if (!sv) |
| 4569 | return 1; |
| 4570 | |
Amaury Denoyelle | 0f456d5 | 2021-09-21 10:29:09 +0200 | [diff] [blame] | 4571 | if (!(sv->check.state & CHK_ST_CONFIGURED)) |
| 4572 | return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n"); |
| 4573 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4574 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4575 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4576 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4577 | return 1; |
| 4578 | } |
| 4579 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4580 | /* parse a "enable server" command. It always returns 1. |
| 4581 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4582 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4583 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4584 | static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private) |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4585 | { |
| 4586 | struct server *sv; |
| 4587 | |
| 4588 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4589 | return 1; |
| 4590 | |
| 4591 | sv = cli_find_server(appctx, args[2]); |
| 4592 | if (!sv) |
| 4593 | return 1; |
| 4594 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4595 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4596 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4597 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4598 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4599 | sv->cookie) |
| 4600 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4601 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4602 | return 1; |
| 4603 | } |
| 4604 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4605 | /* Allocates data structure related to load balancing for the server <sv>. It |
| 4606 | * is only required for dynamic servers. |
| 4607 | * |
| 4608 | * At the moment, the server lock is not used as this function is only called |
| 4609 | * for a dynamic server not yet registered. |
| 4610 | * |
| 4611 | * Returns 1 on success, 0 on allocation failure. |
| 4612 | */ |
| 4613 | static int srv_alloc_lb(struct server *sv, struct proxy *be) |
| 4614 | { |
| 4615 | int node; |
| 4616 | |
| 4617 | sv->lb_tree = (sv->flags & SRV_F_BACKUP) ? |
| 4618 | &be->lbprm.chash.bck : &be->lbprm.chash.act; |
| 4619 | sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE; |
| 4620 | sv->lb_nodes_now = 0; |
| 4621 | |
Willy Tarreau | dcb121f | 2021-04-20 11:37:45 +0200 | [diff] [blame] | 4622 | if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) || |
| 4623 | ((be->lbprm.algo & (BE_LB_KIND | BE_LB_HASH_TYPE)) == (BE_LB_KIND_HI | BE_LB_HASH_CONS))) { |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4624 | sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes)); |
| 4625 | |
| 4626 | if (!sv->lb_nodes) |
| 4627 | return 0; |
| 4628 | |
| 4629 | for (node = 0; node < sv->lb_nodes_tot; node++) { |
| 4630 | sv->lb_nodes[node].server = sv; |
| 4631 | sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node); |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | return 1; |
| 4636 | } |
| 4637 | |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4638 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 4639 | * reached, the task automatically stops. Note that any server status change |
| 4640 | * must have updated s->last_change accordingly. |
| 4641 | */ |
| 4642 | static struct task *server_warmup(struct task *t, void *context, unsigned int state) |
| 4643 | { |
| 4644 | struct server *s = context; |
| 4645 | |
| 4646 | /* by default, plan on stopping the task */ |
| 4647 | t->expire = TICK_ETERNITY; |
| 4648 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 4649 | (s->next_state != SRV_ST_STARTING)) |
| 4650 | return t; |
| 4651 | |
| 4652 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4653 | |
| 4654 | /* recalculate the weights and update the state */ |
| 4655 | server_recalc_eweight(s, 1); |
| 4656 | |
| 4657 | /* probably that we can refill this server with a bit more connections */ |
| 4658 | pendconn_grab_from_px(s); |
| 4659 | |
| 4660 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4661 | |
| 4662 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 4663 | * whichever is greater, resulting in small 5% steps. |
| 4664 | */ |
| 4665 | if (s->next_state == SRV_ST_STARTING) |
| 4666 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 4667 | return t; |
| 4668 | } |
| 4669 | |
| 4670 | /* Allocate the slowstart task if the server is configured with a slowstart |
| 4671 | * timer. If server next_state is SRV_ST_STARTING, the task is scheduled. |
| 4672 | * |
| 4673 | * Returns 0 on success else non-zero. |
| 4674 | */ |
| 4675 | static int init_srv_slowstart(struct server *srv) |
| 4676 | { |
| 4677 | struct task *t; |
| 4678 | |
| 4679 | if (srv->slowstart) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4680 | if ((t = task_new_anywhere()) == NULL) { |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4681 | ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id); |
| 4682 | return ERR_ALERT | ERR_FATAL; |
| 4683 | } |
| 4684 | |
| 4685 | /* We need a warmup task that will be called when the server |
| 4686 | * state switches from down to up. |
| 4687 | */ |
| 4688 | srv->warmup = t; |
| 4689 | t->process = server_warmup; |
| 4690 | t->context = srv; |
| 4691 | |
| 4692 | /* server can be in this state only because of */ |
| 4693 | if (srv->next_state == SRV_ST_STARTING) { |
| 4694 | task_schedule(srv->warmup, |
| 4695 | tick_add(now_ms, |
| 4696 | MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20))); |
| 4697 | } |
| 4698 | } |
| 4699 | |
| 4700 | return ERR_NONE; |
| 4701 | } |
| 4702 | REGISTER_POST_SERVER_CHECK(init_srv_slowstart); |
| 4703 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4704 | /* Memory allocation and initialization of the per_thr field. |
| 4705 | * Returns 0 if the field has been successfully initialized, -1 on failure. |
| 4706 | */ |
| 4707 | int srv_init_per_thr(struct server *srv) |
| 4708 | { |
| 4709 | int i; |
| 4710 | |
| 4711 | srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr)); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 4712 | srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp)); |
| 4713 | if (!srv->per_thr || !srv->per_tgrp) |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4714 | return -1; |
| 4715 | |
| 4716 | for (i = 0; i < global.nbthread; i++) { |
| 4717 | srv->per_thr[i].idle_conns = EB_ROOT; |
| 4718 | srv->per_thr[i].safe_conns = EB_ROOT; |
| 4719 | srv->per_thr[i].avail_conns = EB_ROOT; |
| 4720 | MT_LIST_INIT(&srv->per_thr[i].streams); |
| 4721 | } |
| 4722 | |
| 4723 | return 0; |
| 4724 | } |
| 4725 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4726 | /* Parse a "add server" command |
| 4727 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4728 | */ |
| 4729 | static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4730 | { |
| 4731 | struct proxy *be; |
| 4732 | struct server *srv; |
| 4733 | char *be_name, *sv_name; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4734 | int errcode, argc; |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4735 | int next_id; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4736 | const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR; |
| 4737 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4738 | usermsgs_clr("CLI"); |
| 4739 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4740 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4741 | return 1; |
| 4742 | |
| 4743 | ++args; |
| 4744 | |
| 4745 | sv_name = be_name = args[1]; |
| 4746 | /* split backend/server arg */ |
| 4747 | while (*sv_name && *(++sv_name)) { |
| 4748 | if (*sv_name == '/') { |
| 4749 | *sv_name = '\0'; |
| 4750 | ++sv_name; |
| 4751 | break; |
| 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | if (!*sv_name) |
| 4756 | return cli_err(appctx, "Require 'backend/server'."); |
| 4757 | |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4758 | be = proxy_be_by_name(be_name); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4759 | if (!be) |
| 4760 | return cli_err(appctx, "No such backend."); |
| 4761 | |
| 4762 | if (!(be->lbprm.algo & BE_LB_PROP_DYN)) { |
Amaury Denoyelle | eafd701 | 2021-04-29 14:59:42 +0200 | [diff] [blame] | 4763 | cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers."); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4764 | return 1; |
| 4765 | } |
| 4766 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4767 | /* At this point, some operations might not be thread-safe anymore. This |
| 4768 | * might be the case for parsing handlers which were designed to run |
| 4769 | * only at the starting stage on single-thread mode. |
| 4770 | * |
| 4771 | * Activate thread isolation to ensure thread-safety. |
| 4772 | */ |
| 4773 | thread_isolate(); |
| 4774 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4775 | args[1] = sv_name; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4776 | errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags); |
| 4777 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4778 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4779 | |
| 4780 | while (*args[argc]) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4781 | errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4782 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4783 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4784 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4785 | } |
| 4786 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4787 | errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags); |
| 4788 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4789 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4790 | |
Amaury Denoyelle | efbf35c | 2021-06-10 17:34:10 +0200 | [diff] [blame] | 4791 | /* A dynamic server does not currently support resolution. |
| 4792 | * |
| 4793 | * Initialize it explicitly to the "none" method to ensure no |
| 4794 | * resolution will ever be executed. |
| 4795 | */ |
| 4796 | srv->init_addr_methods = SRV_IADDR_NONE; |
| 4797 | |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4798 | if (srv->mux_proto) { |
| 4799 | if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4800 | ha_alert("MUX protocol is not usable for server.\n"); |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4801 | goto out; |
| 4802 | } |
| 4803 | } |
| 4804 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4805 | if (srv_init_per_thr(srv) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4806 | ha_alert("failed to allocate per-thread lists for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4807 | goto out; |
| 4808 | } |
| 4809 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4810 | if (srv->max_idle_conns != 0) { |
| 4811 | srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr)); |
| 4812 | if (!srv->curr_idle_thr) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4813 | ha_alert("failed to allocate counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4814 | goto out; |
| 4815 | } |
| 4816 | } |
| 4817 | |
| 4818 | if (!srv_alloc_lb(srv, be)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4819 | ha_alert("Failed to initialize load-balancing data.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4820 | goto out; |
| 4821 | } |
| 4822 | |
| 4823 | if (!stats_allocate_proxy_counters_internal(&srv->extra_counters, |
| 4824 | COUNTERS_SV, |
| 4825 | STATS_PX_CAP_SRV)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4826 | ha_alert("failed to allocate extra counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4827 | goto out; |
| 4828 | } |
| 4829 | |
Amaury Denoyelle | 79b90e8 | 2021-09-20 15:15:19 +0200 | [diff] [blame] | 4830 | if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) || |
| 4831 | srv->check.use_ssl == 1) { |
Amaury Denoyelle | 34897d2 | 2021-05-19 09:49:41 +0200 | [diff] [blame] | 4832 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 4833 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) |
| 4834 | goto out; |
| 4835 | } |
| 4836 | } |
| 4837 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4838 | if (srv->trackit) { |
| 4839 | if (srv_apply_track(srv, be)) |
| 4840 | goto out; |
| 4841 | } |
| 4842 | |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4843 | /* Init check/agent if configured. The check is manually disabled |
| 4844 | * because a dynamic server is started in a disable state. It must be |
| 4845 | * manually activated via a "enable health/agent" command. |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4846 | */ |
| 4847 | if (srv->do_check) { |
| 4848 | if (init_srv_check(srv)) |
| 4849 | goto out; |
| 4850 | |
| 4851 | srv->check.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4852 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4853 | |
| 4854 | if (srv->do_agent) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4855 | if (init_srv_agent_check(srv)) |
| 4856 | goto out; |
| 4857 | |
| 4858 | srv->agent.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4859 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4860 | |
Amaury Denoyelle | cd8a6f2 | 2021-09-21 11:51:54 +0200 | [diff] [blame] | 4861 | /* Init slowstart if needed. */ |
| 4862 | if (init_srv_slowstart(srv)) |
| 4863 | goto out; |
| 4864 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4865 | /* Attach the server to the end of the proxy linked list. Note that this |
| 4866 | * operation is not thread-safe so this is executed under thread |
| 4867 | * isolation. |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4868 | * |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4869 | * If a server with the same name is found, reject the new one. |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4870 | */ |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4871 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4872 | /* TODO use a double-linked list for px->srv */ |
| 4873 | if (be->srv) { |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4874 | struct server *next = be->srv; |
| 4875 | |
| 4876 | while (1) { |
| 4877 | /* check for duplicate server */ |
Tim Duesterhus | c5aa113 | 2021-10-16 17:48:15 +0200 | [diff] [blame] | 4878 | if (strcmp(srv->id, next->id) == 0) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4879 | ha_alert("Already exists a server with the same name in backend.\n"); |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4880 | goto out; |
| 4881 | } |
| 4882 | |
| 4883 | if (!next->next) |
| 4884 | break; |
| 4885 | |
| 4886 | next = next->next; |
| 4887 | } |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4888 | |
| 4889 | next->next = srv; |
| 4890 | } |
| 4891 | else { |
| 4892 | srv->next = be->srv; |
| 4893 | be->srv = srv; |
| 4894 | } |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4895 | |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4896 | /* generate the server id if not manually specified */ |
| 4897 | if (!srv->puid) { |
| 4898 | next_id = get_next_id(&be->conf.used_server_id, 1); |
| 4899 | if (!next_id) { |
| 4900 | ha_alert("Cannot attach server : no id left in proxy\n"); |
| 4901 | goto out; |
| 4902 | } |
| 4903 | |
| 4904 | srv->conf.id.key = srv->puid = next_id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4905 | } |
Christopher Faulet | 70f8948 | 2021-12-07 18:49:44 +0100 | [diff] [blame] | 4906 | srv->conf.name.key = srv->id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4907 | |
| 4908 | /* insert the server in the backend trees */ |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 4909 | eb32_insert(&be->conf.used_server_id, &srv->conf.id); |
| 4910 | ebis_insert(&be->conf.used_server_name, &srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 4911 | /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */ |
| 4912 | if (srv->addr_node.key) |
| 4913 | ebis_insert(&be->used_server_addr, &srv->addr_node); |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4914 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 4915 | /* check if LSB bit (odd bit) is set for reuse_cnt */ |
| 4916 | if (srv_id_reuse_cnt & 1) { |
| 4917 | /* cnt must be increased */ |
| 4918 | srv_id_reuse_cnt++; |
| 4919 | } |
| 4920 | /* srv_id_reuse_cnt is always even at this stage, divide by 2 to |
| 4921 | * save some space |
| 4922 | * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt)) |
| 4923 | */ |
| 4924 | srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0; |
| 4925 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame^] | 4926 | /* adding server cannot fail when we reach this: |
| 4927 | * publishing EVENT_HDL_SUB_SERVER_ADD |
| 4928 | */ |
| 4929 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1); |
| 4930 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4931 | thread_release(); |
| 4932 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4933 | /* Start the check task. The server must be fully initialized. |
| 4934 | * |
| 4935 | * <srvpos> and <nbcheck> parameters are set to 1 as there should be no |
| 4936 | * need to randomly spread the task interval for dynamic servers. |
| 4937 | */ |
| 4938 | if (srv->check.state & CHK_ST_CONFIGURED) { |
| 4939 | if (!start_check_task(&srv->check, 0, 1, 1)) |
| 4940 | ha_alert("System might be unstable, consider to execute a reload"); |
| 4941 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4942 | if (srv->agent.state & CHK_ST_CONFIGURED) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4943 | if (!start_check_task(&srv->agent, 0, 1, 1)) |
| 4944 | ha_alert("System might be unstable, consider to execute a reload"); |
| 4945 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4946 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4947 | ha_notice("New server registered.\n"); |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 4948 | cli_umsg(appctx, LOG_INFO); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4949 | |
| 4950 | return 0; |
| 4951 | |
| 4952 | out: |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 4953 | if (srv) { |
| 4954 | if (srv->track) |
| 4955 | release_server_track(srv); |
| 4956 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4957 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 4958 | free_check(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4959 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4960 | free_check(&srv->agent); |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4961 | |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 4962 | /* remove the server from the proxy linked list */ |
| 4963 | if (be->srv == srv) { |
| 4964 | be->srv = srv->next; |
| 4965 | } |
| 4966 | else { |
| 4967 | struct server *prev; |
| 4968 | for (prev = be->srv; prev && prev->next != srv; prev = prev->next) |
| 4969 | ; |
| 4970 | if (prev) |
| 4971 | prev->next = srv->next; |
| 4972 | } |
| 4973 | |
| 4974 | } |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 4975 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4976 | thread_release(); |
| 4977 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4978 | if (!usermsgs_empty()) |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 4979 | cli_umsgerr(appctx); |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4980 | |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 4981 | if (srv) |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 4982 | srv_drop(srv); |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4983 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4984 | return 1; |
| 4985 | } |
| 4986 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 4987 | /* Parse a "del server" command |
| 4988 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4989 | */ |
| 4990 | static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4991 | { |
| 4992 | struct proxy *be; |
| 4993 | struct server *srv; |
| 4994 | char *be_name, *sv_name; |
| 4995 | |
| 4996 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4997 | return 1; |
| 4998 | |
| 4999 | ++args; |
| 5000 | |
| 5001 | sv_name = be_name = args[1]; |
| 5002 | /* split backend/server arg */ |
| 5003 | while (*sv_name && *(++sv_name)) { |
| 5004 | if (*sv_name == '/') { |
| 5005 | *sv_name = '\0'; |
| 5006 | ++sv_name; |
| 5007 | break; |
| 5008 | } |
| 5009 | } |
| 5010 | |
| 5011 | if (!*sv_name) |
| 5012 | return cli_err(appctx, "Require 'backend/server'."); |
| 5013 | |
| 5014 | /* The proxy servers list is currently not protected by a lock so this |
Willy Tarreau | ba3ab79 | 2021-08-04 14:42:37 +0200 | [diff] [blame] | 5015 | * requires thread isolation. In addition, any place referencing the |
| 5016 | * server about to be deleted would be unsafe after our operation, so |
| 5017 | * we must be certain to be alone so that no other thread has even |
| 5018 | * started to grab a temporary reference to this server. |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5019 | */ |
Willy Tarreau | ba3ab79 | 2021-08-04 14:42:37 +0200 | [diff] [blame] | 5020 | thread_isolate_full(); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5021 | |
| 5022 | get_backend_server(be_name, sv_name, &be, &srv); |
| 5023 | if (!be) { |
| 5024 | cli_err(appctx, "No such backend."); |
| 5025 | goto out; |
| 5026 | } |
| 5027 | |
| 5028 | if (!srv) { |
| 5029 | cli_err(appctx, "No such server."); |
| 5030 | goto out; |
| 5031 | } |
| 5032 | |
Amaury Denoyelle | 14c3c5c | 2021-08-23 14:10:51 +0200 | [diff] [blame] | 5033 | if (srv->flags & SRV_F_NON_PURGEABLE) { |
| 5034 | cli_err(appctx, "This server cannot be removed at runtime due to other configuration elements pointing to it."); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5035 | goto out; |
| 5036 | } |
| 5037 | |
| 5038 | /* Only servers in maintenance can be deleted. This ensures that the |
| 5039 | * server is not present anymore in the lb structures (through |
| 5040 | * lbprm.set_server_status_down). |
| 5041 | */ |
| 5042 | if (!(srv->cur_admin & SRV_ADMF_MAINT)) { |
| 5043 | cli_err(appctx, "Only servers in maintenance mode can be deleted."); |
| 5044 | goto out; |
| 5045 | } |
| 5046 | |
| 5047 | /* Ensure that there is no active/idle/pending connection on the server. |
| 5048 | * |
| 5049 | * TODO idle connections should not prevent server deletion. A proper |
| 5050 | * cleanup function should be implemented to be used here. |
| 5051 | */ |
| 5052 | if (srv->cur_sess || srv->curr_idle_conns || |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 5053 | !eb_is_empty(&srv->queue.head)) { |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5054 | cli_err(appctx, "Server still has connections attached to it, cannot remove it."); |
| 5055 | goto out; |
| 5056 | } |
| 5057 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame^] | 5058 | /* removing cannot fail anymore when we reach this: |
| 5059 | * publishing EVENT_HDL_SUB_SERVER_DEL |
| 5060 | */ |
| 5061 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1); |
| 5062 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5063 | /* remove srv from tracking list */ |
| 5064 | if (srv->track) |
| 5065 | release_server_track(srv); |
| 5066 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5067 | /* stop the check task if running */ |
| 5068 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5069 | check_purge(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5070 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5071 | check_purge(&srv->agent); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5072 | |
| 5073 | /* detach the server from the proxy linked list |
| 5074 | * The proxy servers list is currently not protected by a lock, so this |
| 5075 | * requires thread_isolate/release. |
| 5076 | */ |
| 5077 | |
| 5078 | /* be->srv cannot be empty since we have already found the server with |
| 5079 | * get_backend_server */ |
| 5080 | BUG_ON(!be->srv); |
| 5081 | if (be->srv == srv) { |
| 5082 | be->srv = srv->next; |
| 5083 | } |
| 5084 | else { |
| 5085 | struct server *next; |
Amaury Denoyelle | d6b4b6d | 2021-04-21 11:50:26 +0200 | [diff] [blame] | 5086 | for (next = be->srv; srv != next->next; next = next->next) { |
| 5087 | /* srv cannot be not found since we have already found |
| 5088 | * it with get_backend_server */ |
| 5089 | BUG_ON(!next); |
| 5090 | } |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5091 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5092 | next->next = srv->next; |
| 5093 | } |
| 5094 | |
| 5095 | /* remove srv from addr_node tree */ |
Amaury Denoyelle | 82d7f77 | 2021-06-09 16:00:43 +0200 | [diff] [blame] | 5096 | eb32_delete(&srv->conf.id); |
| 5097 | ebpt_delete(&srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5098 | if (srv->addr_node.key) |
| 5099 | ebpt_delete(&srv->addr_node); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5100 | |
| 5101 | /* remove srv from idle_node tree for idle conn cleanup */ |
| 5102 | eb32_delete(&srv->idle_node); |
| 5103 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5104 | /* set LSB bit (odd bit) for reuse_cnt */ |
| 5105 | srv_id_reuse_cnt |= 1; |
| 5106 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5107 | thread_release(); |
| 5108 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5109 | ha_notice("Server deleted.\n"); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5110 | srv_drop(srv); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5111 | |
| 5112 | cli_msg(appctx, LOG_INFO, "Server deleted."); |
| 5113 | |
| 5114 | return 0; |
| 5115 | |
| 5116 | out: |
| 5117 | thread_release(); |
| 5118 | |
| 5119 | return 1; |
| 5120 | } |
| 5121 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 5122 | /* register cli keywords */ |
| 5123 | static struct cli_kw_list cli_kws = {{ },{ |
Amaury Denoyelle | c755efd | 2021-08-03 18:05:37 +0200 | [diff] [blame] | 5124 | { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL }, |
| 5125 | { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL }, |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 5126 | { { "disable", "server", NULL }, "disable server (DEPRECATED) : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL }, |
Amaury Denoyelle | c755efd | 2021-08-03 18:05:37 +0200 | [diff] [blame] | 5127 | { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL }, |
| 5128 | { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL }, |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 5129 | { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL }, |
| 5130 | { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL }, |
| 5131 | { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server }, |
| 5132 | { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight }, |
| 5133 | { { "set", "weight", NULL }, "set weight <bk>/<srv> (DEPRECATED) : change a server's weight (use 'set server' instead)", cli_parse_set_weight }, |
Amaury Denoyelle | 76e8b70 | 2022-03-09 15:07:31 +0100 | [diff] [blame] | 5134 | { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL }, |
| 5135 | { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL }, |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 5136 | {{},} |
| 5137 | }}; |
| 5138 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5139 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 5140 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5141 | /* Prepare a server <srv> to track check status of another one. <srv>.<trackit> |
| 5142 | * field is used to retrieve the identifier of the tracked server, either with |
| 5143 | * the format "proxy/server" or just "server". <curproxy> must point to the |
| 5144 | * backend owning <srv>; if no proxy is specified in <trackit>, it will be used |
| 5145 | * to find the tracked server. |
| 5146 | * |
| 5147 | * Returns 0 if the server track has been activated else non-zero. |
| 5148 | * |
| 5149 | * Not thread-safe. |
| 5150 | */ |
| 5151 | int srv_apply_track(struct server *srv, struct proxy *curproxy) |
| 5152 | { |
| 5153 | struct proxy *px; |
| 5154 | struct server *strack, *loop; |
| 5155 | char *pname, *sname; |
| 5156 | |
| 5157 | if (!srv->trackit) |
| 5158 | return 1; |
| 5159 | |
| 5160 | pname = srv->trackit; |
| 5161 | sname = strrchr(pname, '/'); |
| 5162 | |
| 5163 | if (sname) { |
| 5164 | *sname++ = '\0'; |
| 5165 | } |
| 5166 | else { |
| 5167 | sname = pname; |
| 5168 | pname = NULL; |
| 5169 | } |
| 5170 | |
| 5171 | if (pname) { |
| 5172 | px = proxy_be_by_name(pname); |
| 5173 | if (!px) { |
| 5174 | ha_alert("unable to find required proxy '%s' for tracking.\n", |
| 5175 | pname); |
| 5176 | return 1; |
| 5177 | } |
| 5178 | } |
| 5179 | else { |
| 5180 | px = curproxy; |
| 5181 | } |
| 5182 | |
| 5183 | strack = findserver(px, sname); |
| 5184 | if (!strack) { |
| 5185 | ha_alert("unable to find required server '%s' for tracking.\n", |
| 5186 | sname); |
| 5187 | return 1; |
| 5188 | } |
| 5189 | |
Amaury Denoyelle | 79f68be | 2021-07-13 10:35:50 +0200 | [diff] [blame] | 5190 | if (strack->flags & SRV_F_DYNAMIC) { |
| 5191 | ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n", |
| 5192 | px->id, strack->id); |
| 5193 | return 1; |
| 5194 | } |
| 5195 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5196 | if (!strack->do_check && !strack->do_agent && !strack->track && |
| 5197 | !strack->trackit) { |
| 5198 | ha_alert("unable to use %s/%s for " |
| 5199 | "tracking as it does not have any check nor agent enabled.\n", |
| 5200 | px->id, strack->id); |
| 5201 | return 1; |
| 5202 | } |
| 5203 | |
| 5204 | for (loop = strack->track; loop && loop != srv; loop = loop->track) |
| 5205 | ; |
| 5206 | |
| 5207 | if (srv == strack || loop) { |
| 5208 | ha_alert("unable to track %s/%s as it " |
| 5209 | "belongs to a tracking chain looping back to %s/%s.\n", |
| 5210 | px->id, strack->id, px->id, |
| 5211 | srv == strack ? strack->id : loop->id); |
| 5212 | return 1; |
| 5213 | } |
| 5214 | |
| 5215 | if (curproxy != px && |
| 5216 | (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) { |
| 5217 | ha_alert("unable to use %s/%s for" |
| 5218 | "tracking: disable-on-404 option inconsistency.\n", |
| 5219 | px->id, strack->id); |
| 5220 | return 1; |
| 5221 | } |
| 5222 | |
| 5223 | srv->track = strack; |
| 5224 | srv->tracknext = strack->trackers; |
| 5225 | strack->trackers = srv; |
Amaury Denoyelle | 0626961 | 2021-08-23 14:05:07 +0200 | [diff] [blame] | 5226 | strack->flags |= SRV_F_NON_PURGEABLE; |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5227 | |
| 5228 | ha_free(&srv->trackit); |
| 5229 | |
| 5230 | return 0; |
| 5231 | } |
| 5232 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5233 | /* |
| 5234 | * This function applies server's status changes, it is |
| 5235 | * is designed to be called asynchronously. |
| 5236 | * |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5237 | * Must be called with the server lock held. This may also be called at init |
| 5238 | * time as the result of parsing the state file, in which case no lock will be |
| 5239 | * held, and the server's warmup task can be null. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5240 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5241 | static void srv_update_status(struct server *s) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5242 | { |
| 5243 | struct check *check = &s->check; |
| 5244 | int xferred; |
| 5245 | struct proxy *px = s->proxy; |
| 5246 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 5247 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 5248 | int log_level; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5249 | struct buffer *tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5250 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5251 | /* If currently main is not set we try to apply pending state changes */ |
| 5252 | if (!(s->cur_admin & SRV_ADMF_MAINT)) { |
| 5253 | int next_admin; |
| 5254 | |
| 5255 | /* Backup next admin */ |
| 5256 | next_admin = s->next_admin; |
| 5257 | |
| 5258 | /* restore current admin state */ |
| 5259 | s->next_admin = s->cur_admin; |
| 5260 | |
| 5261 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
| 5262 | s->last_change = now.tv_sec; |
| 5263 | if (s->proxy->lbprm.set_server_status_down) |
| 5264 | s->proxy->lbprm.set_server_status_down(s); |
| 5265 | |
| 5266 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5267 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5268 | |
| 5269 | /* we might have streams queued on this server and waiting for |
| 5270 | * a connection. Those which are redispatchable will be queued |
| 5271 | * to another server or to the proxy itself. |
| 5272 | */ |
| 5273 | xferred = pendconn_redistribute(s); |
| 5274 | |
| 5275 | tmptrash = alloc_trash_chunk(); |
| 5276 | if (tmptrash) { |
| 5277 | chunk_printf(tmptrash, |
| 5278 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5279 | s->proxy->id, s->id); |
| 5280 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5281 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5282 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5283 | |
| 5284 | /* we don't send an alert if the server was previously paused */ |
| 5285 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5286 | send_log(s->proxy, log_level, "%s.\n", |
| 5287 | tmptrash->area); |
| 5288 | send_email_alert(s, log_level, "%s", |
| 5289 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5290 | free_trash_chunk(tmptrash); |
| 5291 | tmptrash = NULL; |
| 5292 | } |
| 5293 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5294 | set_backend_down(s->proxy); |
| 5295 | |
| 5296 | s->counters.down_trans++; |
| 5297 | } |
| 5298 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
| 5299 | s->last_change = now.tv_sec; |
| 5300 | if (s->proxy->lbprm.set_server_status_down) |
| 5301 | s->proxy->lbprm.set_server_status_down(s); |
| 5302 | |
| 5303 | /* we might have streams queued on this server and waiting for |
| 5304 | * a connection. Those which are redispatchable will be queued |
| 5305 | * to another server or to the proxy itself. |
| 5306 | */ |
| 5307 | xferred = pendconn_redistribute(s); |
| 5308 | |
| 5309 | tmptrash = alloc_trash_chunk(); |
| 5310 | if (tmptrash) { |
| 5311 | chunk_printf(tmptrash, |
| 5312 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5313 | s->proxy->id, s->id); |
| 5314 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5315 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5316 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5317 | ha_warning("%s.\n", tmptrash->area); |
| 5318 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5319 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5320 | free_trash_chunk(tmptrash); |
| 5321 | tmptrash = NULL; |
| 5322 | } |
| 5323 | |
| 5324 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5325 | set_backend_down(s->proxy); |
| 5326 | } |
| 5327 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5328 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
| 5329 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5330 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5331 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5332 | s->proxy->last_change = now.tv_sec; |
| 5333 | } |
| 5334 | |
Amaury Denoyelle | fe2bf09 | 2020-10-29 15:59:04 +0100 | [diff] [blame] | 5335 | if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5336 | s->down_time += now.tv_sec - s->last_change; |
| 5337 | |
| 5338 | s->last_change = now.tv_sec; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5339 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5340 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5341 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5342 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5343 | /* now propagate the status change to any LB algorithms */ |
| 5344 | if (px->lbprm.update_server_eweight) |
| 5345 | px->lbprm.update_server_eweight(s); |
| 5346 | else if (srv_willbe_usable(s)) { |
| 5347 | if (px->lbprm.set_server_status_up) |
| 5348 | px->lbprm.set_server_status_up(s); |
| 5349 | } |
| 5350 | else { |
| 5351 | if (px->lbprm.set_server_status_down) |
| 5352 | px->lbprm.set_server_status_down(s); |
| 5353 | } |
| 5354 | |
| 5355 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5356 | * and it's not a backup server and its effective weight is > 0, |
| 5357 | * then it can accept new connections, so we shut down all streams |
| 5358 | * on all backup servers. |
| 5359 | */ |
| 5360 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5361 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5362 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5363 | |
| 5364 | /* check if we can handle some connections queued at the proxy. We |
| 5365 | * will take as many as we can handle. |
| 5366 | */ |
| 5367 | xferred = pendconn_grab_from_px(s); |
| 5368 | |
| 5369 | tmptrash = alloc_trash_chunk(); |
| 5370 | if (tmptrash) { |
| 5371 | chunk_printf(tmptrash, |
| 5372 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5373 | s->proxy->id, s->id); |
| 5374 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5375 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5376 | ha_warning("%s.\n", tmptrash->area); |
| 5377 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5378 | tmptrash->area); |
| 5379 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5380 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5381 | free_trash_chunk(tmptrash); |
| 5382 | tmptrash = NULL; |
| 5383 | } |
| 5384 | |
| 5385 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5386 | set_backend_down(s->proxy); |
| 5387 | } |
| 5388 | else if (s->cur_eweight != s->next_eweight) { |
| 5389 | /* now propagate the status change to any LB algorithms */ |
| 5390 | if (px->lbprm.update_server_eweight) |
| 5391 | px->lbprm.update_server_eweight(s); |
| 5392 | else if (srv_willbe_usable(s)) { |
| 5393 | if (px->lbprm.set_server_status_up) |
| 5394 | px->lbprm.set_server_status_up(s); |
| 5395 | } |
| 5396 | else { |
| 5397 | if (px->lbprm.set_server_status_down) |
| 5398 | px->lbprm.set_server_status_down(s); |
| 5399 | } |
| 5400 | |
| 5401 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5402 | set_backend_down(s->proxy); |
| 5403 | } |
| 5404 | |
| 5405 | s->next_admin = next_admin; |
| 5406 | } |
| 5407 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5408 | /* reset operational state change */ |
| 5409 | *s->op_st_chg.reason = 0; |
| 5410 | s->op_st_chg.status = s->op_st_chg.code = -1; |
| 5411 | s->op_st_chg.duration = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5412 | |
| 5413 | /* Now we try to apply pending admin changes */ |
| 5414 | |
| 5415 | /* Maintenance must also disable health checks */ |
| 5416 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5417 | if (s->check.state & CHK_ST_ENABLED) { |
| 5418 | s->check.state |= CHK_ST_PAUSED; |
| 5419 | check->health = 0; |
| 5420 | } |
| 5421 | |
| 5422 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5423 | tmptrash = alloc_trash_chunk(); |
| 5424 | if (tmptrash) { |
| 5425 | chunk_printf(tmptrash, |
| 5426 | "%sServer %s/%s was DOWN and now enters maintenance%s%s%s", |
| 5427 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5428 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5429 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5430 | srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5431 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5432 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5433 | ha_warning("%s.\n", tmptrash->area); |
| 5434 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5435 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5436 | } |
| 5437 | free_trash_chunk(tmptrash); |
| 5438 | tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5439 | } |
Emeric Brun | 8f29829 | 2017-12-06 16:47:17 +0100 | [diff] [blame] | 5440 | /* commit new admin status */ |
| 5441 | |
| 5442 | s->cur_admin = s->next_admin; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5443 | } |
| 5444 | else { /* server was still running */ |
| 5445 | check->health = 0; /* failure */ |
| 5446 | s->last_change = now.tv_sec; |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5447 | |
| 5448 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5449 | if (s->proxy->lbprm.set_server_status_down) |
| 5450 | s->proxy->lbprm.set_server_status_down(s); |
| 5451 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5452 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5453 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5454 | |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5455 | /* force connection cleanup on the given server */ |
| 5456 | srv_cleanup_connections(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5457 | /* we might have streams queued on this server and waiting for |
| 5458 | * a connection. Those which are redispatchable will be queued |
| 5459 | * to another server or to the proxy itself. |
| 5460 | */ |
| 5461 | xferred = pendconn_redistribute(s); |
| 5462 | |
| 5463 | tmptrash = alloc_trash_chunk(); |
| 5464 | if (tmptrash) { |
| 5465 | chunk_printf(tmptrash, |
| 5466 | "%sServer %s/%s is going DOWN for maintenance%s%s%s", |
| 5467 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5468 | s->proxy->id, s->id, |
| 5469 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5470 | |
| 5471 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
| 5472 | |
| 5473 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5474 | ha_warning("%s.\n", tmptrash->area); |
| 5475 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5476 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5477 | } |
| 5478 | free_trash_chunk(tmptrash); |
| 5479 | tmptrash = NULL; |
| 5480 | } |
| 5481 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5482 | set_backend_down(s->proxy); |
| 5483 | |
| 5484 | s->counters.down_trans++; |
| 5485 | } |
| 5486 | } |
| 5487 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5488 | /* OK here we're leaving maintenance, we have many things to check, |
| 5489 | * because the server might possibly be coming back up depending on |
| 5490 | * its state. In practice, leaving maintenance means that we should |
| 5491 | * immediately turn to UP (more or less the slowstart) under the |
| 5492 | * following conditions : |
| 5493 | * - server is neither checked nor tracked |
| 5494 | * - server tracks another server which is not checked |
| 5495 | * - server tracks another server which is already up |
| 5496 | * Which sums up as something simpler : |
| 5497 | * "either the tracking server is up or the server's checks are disabled |
| 5498 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5499 | * case associated to the stopping state which can be inherited. Note |
| 5500 | * that the server might still be in drain mode, which is naturally dealt |
| 5501 | * with by the lower level functions. |
| 5502 | */ |
| 5503 | |
| 5504 | if (s->check.state & CHK_ST_ENABLED) { |
| 5505 | s->check.state &= ~CHK_ST_PAUSED; |
| 5506 | check->health = check->rise; /* start OK but check immediately */ |
| 5507 | } |
| 5508 | |
| 5509 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5510 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5511 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5512 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5513 | s->next_state = SRV_ST_STOPPING; |
| 5514 | } |
| 5515 | else { |
Willy Tarreau | d332f13 | 2021-08-04 19:35:13 +0200 | [diff] [blame] | 5516 | s->last_change = now.tv_sec; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5517 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5518 | if (s->slowstart > 0) { |
| 5519 | if (s->warmup) |
| 5520 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5521 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5522 | else |
| 5523 | s->next_state = SRV_ST_RUNNING; |
| 5524 | } |
| 5525 | |
| 5526 | } |
| 5527 | |
| 5528 | tmptrash = alloc_trash_chunk(); |
| 5529 | if (tmptrash) { |
| 5530 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5531 | chunk_printf(tmptrash, |
| 5532 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5533 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5534 | s->proxy->id, s->id, |
| 5535 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5536 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5537 | } |
| 5538 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5539 | chunk_printf(tmptrash, |
| 5540 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5541 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5542 | s->proxy->id, s->id, s->hostname, |
| 5543 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5544 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5545 | } |
| 5546 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5547 | chunk_printf(tmptrash, |
| 5548 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5549 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5550 | s->proxy->id, s->id, |
| 5551 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5552 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5553 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5554 | ha_warning("%s.\n", tmptrash->area); |
| 5555 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5556 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5557 | free_trash_chunk(tmptrash); |
| 5558 | tmptrash = NULL; |
| 5559 | } |
| 5560 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5561 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5562 | /* now propagate the status change to any LB algorithms */ |
| 5563 | if (px->lbprm.update_server_eweight) |
| 5564 | px->lbprm.update_server_eweight(s); |
| 5565 | else if (srv_willbe_usable(s)) { |
| 5566 | if (px->lbprm.set_server_status_up) |
| 5567 | px->lbprm.set_server_status_up(s); |
| 5568 | } |
| 5569 | else { |
| 5570 | if (px->lbprm.set_server_status_down) |
| 5571 | px->lbprm.set_server_status_down(s); |
| 5572 | } |
| 5573 | |
| 5574 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5575 | set_backend_down(s->proxy); |
Willy Tarreau | d332f13 | 2021-08-04 19:35:13 +0200 | [diff] [blame] | 5576 | else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) |
| 5577 | s->proxy->last_change = now.tv_sec; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5578 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5579 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5580 | * and it's not a backup server and its effective weight is > 0, |
| 5581 | * then it can accept new connections, so we shut down all streams |
| 5582 | * on all backup servers. |
| 5583 | */ |
| 5584 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5585 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5586 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5587 | |
| 5588 | /* check if we can handle some connections queued at the proxy. We |
| 5589 | * will take as many as we can handle. |
| 5590 | */ |
| 5591 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5592 | } |
| 5593 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5594 | /* remaining in maintenance mode, let's inform precisely about the |
| 5595 | * situation. |
| 5596 | */ |
| 5597 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5598 | tmptrash = alloc_trash_chunk(); |
| 5599 | if (tmptrash) { |
| 5600 | chunk_printf(tmptrash, |
| 5601 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5602 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5603 | s->proxy->id, s->id); |
| 5604 | |
| 5605 | if (s->track) /* normally it's mandatory here */ |
| 5606 | chunk_appendf(tmptrash, " via %s/%s", |
| 5607 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5608 | ha_warning("%s.\n", tmptrash->area); |
| 5609 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5610 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5611 | free_trash_chunk(tmptrash); |
| 5612 | tmptrash = NULL; |
| 5613 | } |
| 5614 | } |
| 5615 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5616 | tmptrash = alloc_trash_chunk(); |
| 5617 | if (tmptrash) { |
| 5618 | chunk_printf(tmptrash, |
| 5619 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5620 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5621 | s->proxy->id, s->id, s->hostname); |
| 5622 | |
| 5623 | if (s->track) /* normally it's mandatory here */ |
| 5624 | chunk_appendf(tmptrash, " via %s/%s", |
| 5625 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5626 | ha_warning("%s.\n", tmptrash->area); |
| 5627 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5628 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5629 | free_trash_chunk(tmptrash); |
| 5630 | tmptrash = NULL; |
| 5631 | } |
| 5632 | } |
| 5633 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5634 | tmptrash = alloc_trash_chunk(); |
| 5635 | if (tmptrash) { |
| 5636 | chunk_printf(tmptrash, |
| 5637 | "%sServer %s/%s remains in forced maintenance", |
| 5638 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5639 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5640 | ha_warning("%s.\n", tmptrash->area); |
| 5641 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5642 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5643 | free_trash_chunk(tmptrash); |
| 5644 | tmptrash = NULL; |
| 5645 | } |
| 5646 | } |
| 5647 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
| 5648 | |
| 5649 | s->cur_admin = s->next_admin; |
| 5650 | } |
| 5651 | |
| 5652 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5653 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5654 | /* drain state is applied only if not yet in maint */ |
| 5655 | |
| 5656 | s->last_change = now.tv_sec; |
| 5657 | if (px->lbprm.set_server_status_down) |
| 5658 | px->lbprm.set_server_status_down(s); |
| 5659 | |
| 5660 | /* we might have streams queued on this server and waiting for |
| 5661 | * a connection. Those which are redispatchable will be queued |
| 5662 | * to another server or to the proxy itself. |
| 5663 | */ |
| 5664 | xferred = pendconn_redistribute(s); |
| 5665 | |
| 5666 | tmptrash = alloc_trash_chunk(); |
| 5667 | if (tmptrash) { |
| 5668 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s", |
| 5669 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5670 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5671 | |
| 5672 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
| 5673 | |
| 5674 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5675 | ha_warning("%s.\n", tmptrash->area); |
| 5676 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5677 | tmptrash->area); |
| 5678 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5679 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5680 | } |
| 5681 | free_trash_chunk(tmptrash); |
| 5682 | tmptrash = NULL; |
| 5683 | } |
| 5684 | |
| 5685 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5686 | set_backend_down(s->proxy); |
| 5687 | } |
| 5688 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5689 | /* OK completely leaving drain mode */ |
| 5690 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5691 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5692 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5693 | s->proxy->last_change = now.tv_sec; |
| 5694 | } |
| 5695 | |
| 5696 | if (s->last_change < now.tv_sec) // ignore negative times |
| 5697 | s->down_time += now.tv_sec - s->last_change; |
| 5698 | s->last_change = now.tv_sec; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5699 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5700 | |
| 5701 | tmptrash = alloc_trash_chunk(); |
| 5702 | if (tmptrash) { |
| 5703 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5704 | chunk_printf(tmptrash, |
| 5705 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5706 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5707 | s->proxy->id, s->id, |
| 5708 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5709 | } |
| 5710 | else { |
| 5711 | chunk_printf(tmptrash, |
| 5712 | "%sServer %s/%s is %s (leaving drain)", |
| 5713 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5714 | s->proxy->id, s->id, |
| 5715 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5716 | if (s->track) /* normally it's mandatory here */ |
| 5717 | chunk_appendf(tmptrash, " via %s/%s", |
| 5718 | s->track->proxy->id, s->track->id); |
| 5719 | } |
| 5720 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5721 | ha_warning("%s.\n", tmptrash->area); |
| 5722 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5723 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5724 | free_trash_chunk(tmptrash); |
| 5725 | tmptrash = NULL; |
| 5726 | } |
| 5727 | |
| 5728 | /* now propagate the status change to any LB algorithms */ |
| 5729 | if (px->lbprm.update_server_eweight) |
| 5730 | px->lbprm.update_server_eweight(s); |
| 5731 | else if (srv_willbe_usable(s)) { |
| 5732 | if (px->lbprm.set_server_status_up) |
| 5733 | px->lbprm.set_server_status_up(s); |
| 5734 | } |
| 5735 | else { |
| 5736 | if (px->lbprm.set_server_status_down) |
| 5737 | px->lbprm.set_server_status_down(s); |
| 5738 | } |
| 5739 | } |
| 5740 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5741 | /* remaining in drain mode after removing one of its flags */ |
| 5742 | |
| 5743 | tmptrash = alloc_trash_chunk(); |
| 5744 | if (tmptrash) { |
| 5745 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5746 | chunk_printf(tmptrash, |
| 5747 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
| 5748 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5749 | s->proxy->id, s->id); |
| 5750 | |
| 5751 | if (s->track) /* normally it's mandatory here */ |
| 5752 | chunk_appendf(tmptrash, " via %s/%s", |
| 5753 | s->track->proxy->id, s->track->id); |
| 5754 | } |
| 5755 | else { |
| 5756 | chunk_printf(tmptrash, |
| 5757 | "%sServer %s/%s remains in forced drain mode", |
| 5758 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5759 | s->proxy->id, s->id); |
| 5760 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5761 | ha_warning("%s.\n", tmptrash->area); |
| 5762 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5763 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5764 | free_trash_chunk(tmptrash); |
| 5765 | tmptrash = NULL; |
| 5766 | } |
| 5767 | |
| 5768 | /* commit new admin status */ |
| 5769 | |
| 5770 | s->cur_admin = s->next_admin; |
| 5771 | } |
| 5772 | } |
| 5773 | |
| 5774 | /* Re-set log strings to empty */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5775 | *s->adm_st_chg_cause = 0; |
| 5776 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5777 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5778 | struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state) |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5779 | { |
| 5780 | struct connection *conn; |
| 5781 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5782 | while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns, |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5783 | struct connection *, toremove_list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5784 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | return task; |
| 5788 | } |
| 5789 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5790 | /* Move toremove_nb connections from idle_tree to toremove_list, -1 means |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5791 | * moving them all. |
| 5792 | * Returns the number of connections moved. |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5793 | * |
| 5794 | * Must be called with idle_conns_lock held. |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5795 | */ |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5796 | static int srv_migrate_conns_to_remove(struct eb_root *idle_tree, struct mt_list *toremove_list, int toremove_nb) |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5797 | { |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5798 | struct eb_node *node, *next; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5799 | struct conn_hash_node *hash_node; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5800 | int i = 0; |
| 5801 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5802 | node = eb_first(idle_tree); |
| 5803 | while (node) { |
| 5804 | next = eb_next(node); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5805 | if (toremove_nb != -1 && i >= toremove_nb) |
| 5806 | break; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5807 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5808 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5809 | eb_delete(node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5810 | MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5811 | i++; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5812 | |
| 5813 | node = next; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5814 | } |
| 5815 | return i; |
| 5816 | } |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5817 | /* cleanup connections for a given server |
| 5818 | * might be useful when going on forced maintenance or live changing ip/port |
| 5819 | */ |
William Dauchy | 707ad32 | 2020-05-04 13:52:40 +0200 | [diff] [blame] | 5820 | static void srv_cleanup_connections(struct server *srv) |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5821 | { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5822 | int did_remove; |
| 5823 | int i; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5824 | |
Amaury Denoyelle | 10d5c31 | 2021-01-06 14:28:51 +0100 | [diff] [blame] | 5825 | /* nothing to do if pool-max-conn is null */ |
| 5826 | if (!srv->max_idle_conns) |
| 5827 | return; |
| 5828 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5829 | /* check all threads starting with ours */ |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5830 | for (i = tid;;) { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5831 | did_remove = 0; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5832 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5833 | if (srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, -1) > 0) |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5834 | did_remove = 1; |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5835 | if (srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, -1) > 0) |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5836 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5837 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5838 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5839 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5840 | |
| 5841 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 5842 | break; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5843 | } |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | /* removes an idle conn after updating the server idle conns counters */ |
| 5847 | void srv_release_conn(struct server *srv, struct connection *conn) |
| 5848 | { |
| 5849 | if (conn->flags & CO_FL_LIST_MASK) { |
| 5850 | /* The connection is currently in the server's idle list, so tell it |
| 5851 | * there's one less connection available in that list. |
| 5852 | */ |
| 5853 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5854 | _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb); |
| 5855 | _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]); |
| 5856 | } |
| 5857 | else { |
| 5858 | /* The connection is not private and not in any server's idle |
| 5859 | * list, so decrement the current number of used connections |
| 5860 | */ |
| 5861 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5862 | } |
| 5863 | |
| 5864 | /* Remove the connection from any tree (safe, idle or available) */ |
| 5865 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5866 | conn_delete_from_tree(&conn->hash_node->node); |
| 5867 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5868 | } |
| 5869 | |
| 5870 | /* retrieve a connection from its <hash> in <tree> |
| 5871 | * returns NULL if no connection found |
| 5872 | */ |
| 5873 | struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash) |
| 5874 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5875 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5876 | struct connection *conn = NULL; |
| 5877 | struct conn_hash_node *hash_node = NULL; |
| 5878 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5879 | node = eb64_lookup(tree, hash); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5880 | if (node) { |
| 5881 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 5882 | conn = hash_node->conn; |
| 5883 | } |
| 5884 | |
| 5885 | return conn; |
| 5886 | } |
| 5887 | |
| 5888 | /* retrieve the next connection sharing the same hash as <conn> |
| 5889 | * returns NULL if no connection found |
| 5890 | */ |
| 5891 | struct connection *srv_lookup_conn_next(struct connection *conn) |
| 5892 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5893 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5894 | struct connection *next_conn = NULL; |
| 5895 | struct conn_hash_node *hash_node = NULL; |
| 5896 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5897 | node = eb64_next_dup(&conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5898 | if (node) { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5899 | hash_node = eb64_entry(node, struct conn_hash_node, node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5900 | next_conn = hash_node->conn; |
| 5901 | } |
| 5902 | |
| 5903 | return next_conn; |
| 5904 | } |
| 5905 | |
| 5906 | /* This adds an idle connection to the server's list if the connection is |
| 5907 | * reusable, not held by any owner anymore, but still has available streams. |
| 5908 | */ |
| 5909 | int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe) |
| 5910 | { |
| 5911 | /* we try to keep the connection in the server's idle list |
| 5912 | * if we don't have too many FD in use, and if the number of |
| 5913 | * idle+current conns is lower than what was observed before |
| 5914 | * last purge, or if we already don't have idle conns for the |
| 5915 | * current thread and we don't exceed last count by global.nbthread. |
| 5916 | */ |
| 5917 | if (!(conn->flags & CO_FL_PRIVATE) && |
| 5918 | srv && srv->pool_purge_delay > 0 && |
| 5919 | ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) && |
| 5920 | ha_used_fds < global.tune.pool_high_count && |
| 5921 | (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) && |
| 5922 | ((eb_is_empty(&srv->per_thr[tid].safe_conns) && |
| 5923 | (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) || |
| 5924 | (ha_used_fds < global.tune.pool_low_count && |
| 5925 | (srv->curr_used_conns + srv->curr_idle_conns <= |
| 5926 | MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) && |
| 5927 | !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) { |
| 5928 | int retadd; |
| 5929 | |
| 5930 | retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1); |
| 5931 | if (retadd > srv->max_idle_conns) { |
| 5932 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5933 | return 0; |
| 5934 | } |
| 5935 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5936 | |
| 5937 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5938 | conn_delete_from_tree(&conn->hash_node->node); |
| 5939 | |
| 5940 | if (is_safe) { |
| 5941 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5942 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5943 | _HA_ATOMIC_INC(&srv->curr_safe_nb); |
| 5944 | } else { |
| 5945 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5946 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5947 | _HA_ATOMIC_INC(&srv->curr_idle_nb); |
| 5948 | } |
| 5949 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5950 | _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]); |
| 5951 | |
| 5952 | __ha_barrier_full(); |
| 5953 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 5954 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5955 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 5956 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 5957 | now_ms); |
| 5958 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 5959 | if (!task_in_wq(idle_conn_task) && ! |
| 5960 | task_in_rq(idle_conn_task)) { |
| 5961 | task_schedule(idle_conn_task, |
| 5962 | srv->idle_node.key); |
| 5963 | } |
| 5964 | |
| 5965 | } |
| 5966 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5967 | } |
| 5968 | return 1; |
| 5969 | } |
| 5970 | return 0; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5971 | } |
| 5972 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5973 | struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state) |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5974 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5975 | struct server *srv; |
| 5976 | struct eb32_node *eb; |
| 5977 | int i; |
| 5978 | unsigned int next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5979 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 5980 | next_wakeup = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5981 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5982 | while (1) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 5983 | int exceed_conns; |
| 5984 | int to_kill; |
| 5985 | int curr_idle; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5986 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5987 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 5988 | if (!eb) { |
| 5989 | /* we might have reached the end of the tree, typically because |
| 5990 | * <now_ms> is in the first half and we're first scanning the last |
| 5991 | * half. Let's loop back to the beginning of the tree now. |
| 5992 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5993 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5994 | eb = eb32_first(&idle_conn_srv); |
| 5995 | if (likely(!eb)) |
| 5996 | break; |
| 5997 | } |
| 5998 | if (tick_is_lt(now_ms, eb->key)) { |
| 5999 | /* timer not expired yet, revisit it later */ |
| 6000 | next_wakeup = eb->key; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6001 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6002 | } |
| 6003 | srv = eb32_entry(eb, struct server, idle_node); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6004 | |
| 6005 | /* Calculate how many idle connections we want to kill : |
| 6006 | * we want to remove half the difference between the total |
| 6007 | * of established connections (used or idle) and the max |
| 6008 | * number of used connections. |
| 6009 | */ |
| 6010 | curr_idle = srv->curr_idle_conns; |
| 6011 | if (curr_idle == 0) |
| 6012 | goto remove; |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6013 | exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6014 | exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1); |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6015 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6016 | srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2; |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6017 | if (srv->est_need_conns < srv->max_used_conns) |
| 6018 | srv->est_need_conns = srv->max_used_conns; |
| 6019 | |
Willy Tarreau | 9dc231a | 2022-11-21 14:32:33 +0100 | [diff] [blame] | 6020 | HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6021 | |
Willy Tarreau | 18ed789 | 2020-07-02 19:05:30 +0200 | [diff] [blame] | 6022 | if (exceed_conns <= 0) |
| 6023 | goto remove; |
| 6024 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6025 | /* check all threads starting with ours */ |
| 6026 | for (i = tid;;) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6027 | int max_conn; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6028 | int j; |
| 6029 | int did_remove = 0; |
| 6030 | |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6031 | max_conn = (exceed_conns * srv->curr_idle_thr[i]) / |
| 6032 | curr_idle + 1; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6033 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6034 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6035 | j = srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, max_conn); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6036 | if (j > 0) |
| 6037 | did_remove = 1; |
| 6038 | if (max_conn - j > 0 && |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6039 | srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, max_conn - j) > 0) |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6040 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6041 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6042 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6043 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 6044 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6045 | |
| 6046 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 6047 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6048 | } |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6049 | remove: |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6050 | eb32_delete(&srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6051 | |
| 6052 | if (srv->curr_idle_conns) { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6053 | /* There are still more idle connections, add the |
| 6054 | * server back in the tree. |
| 6055 | */ |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6056 | srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6057 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6058 | next_wakeup = tick_first(next_wakeup, srv->idle_node.key); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6059 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6060 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6061 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6062 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6063 | task->expire = next_wakeup; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6064 | return task; |
| 6065 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6066 | |
Amaury Denoyelle | 3109ccf | 2021-04-29 17:30:05 +0200 | [diff] [blame] | 6067 | /* Close remaining idle connections. This functions is designed to be run on |
| 6068 | * process shutdown. This guarantees a proper socket shutdown to avoid |
| 6069 | * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is |
| 6070 | * bypassed. |
| 6071 | * |
| 6072 | * This function is not thread-safe so it must only be called via a global |
| 6073 | * deinit function. |
| 6074 | */ |
| 6075 | static void srv_close_idle_conns(struct server *srv) |
| 6076 | { |
| 6077 | struct eb_root **cleaned_tree; |
| 6078 | int i; |
| 6079 | |
| 6080 | for (i = 0; i < global.nbthread; ++i) { |
| 6081 | struct eb_root *conn_trees[] = { |
| 6082 | &srv->per_thr[i].idle_conns, |
| 6083 | &srv->per_thr[i].safe_conns, |
| 6084 | &srv->per_thr[i].avail_conns, |
| 6085 | NULL |
| 6086 | }; |
| 6087 | |
| 6088 | for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) { |
| 6089 | while (!eb_is_empty(*cleaned_tree)) { |
| 6090 | struct ebmb_node *node = ebmb_first(*cleaned_tree); |
| 6091 | struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 6092 | struct connection *conn = conn_hash_node->conn; |
| 6093 | |
| 6094 | if (conn->ctrl->ctrl_close) |
| 6095 | conn->ctrl->ctrl_close(conn); |
| 6096 | ebmb_delete(node); |
| 6097 | } |
| 6098 | } |
| 6099 | } |
| 6100 | } |
| 6101 | |
| 6102 | REGISTER_SERVER_DEINIT(srv_close_idle_conns); |
| 6103 | |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6104 | /* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */ |
| 6105 | static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 6106 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6107 | char **err) |
| 6108 | { |
| 6109 | if (too_many_args(1, args, err, NULL)) |
| 6110 | return -1; |
| 6111 | |
| 6112 | if (strcmp(args[1], "on") == 0) |
| 6113 | global.tune.options |= GTUNE_IDLE_POOL_SHARED; |
| 6114 | else if (strcmp(args[1], "off") == 0) |
| 6115 | global.tune.options &= ~GTUNE_IDLE_POOL_SHARED; |
| 6116 | else { |
| 6117 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 6118 | return -1; |
| 6119 | } |
| 6120 | return 0; |
| 6121 | } |
| 6122 | |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6123 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 6124 | static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 6125 | const struct proxy *defpx, const char *file, int line, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6126 | char **err) |
| 6127 | { |
| 6128 | int arg = -1; |
| 6129 | |
| 6130 | if (too_many_args(1, args, err, NULL)) |
| 6131 | return -1; |
| 6132 | |
| 6133 | if (*(args[1]) != 0) |
| 6134 | arg = atoi(args[1]); |
| 6135 | |
| 6136 | if (arg < 0 || arg > 100) { |
| 6137 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 6138 | return -1; |
| 6139 | } |
| 6140 | |
| 6141 | if (args[0][10] == 'h') |
| 6142 | global.tune.pool_high_ratio = arg; |
| 6143 | else |
| 6144 | global.tune.pool_low_ratio = arg; |
| 6145 | return 0; |
| 6146 | } |
| 6147 | |
| 6148 | /* config keyword parsers */ |
| 6149 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6150 | { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared }, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6151 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6152 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6153 | { 0, NULL, NULL } |
| 6154 | }}; |
| 6155 | |
| 6156 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 6157 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 6158 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6159 | * Local variables: |
| 6160 | * c-indent-level: 8 |
| 6161 | * c-basic-offset: 8 |
| 6162 | * End: |
| 6163 | */ |