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 | |
Aurelien DARRAGON | 7d541a9 | 2022-12-07 12:17:24 +0100 | [diff] [blame] | 127 | if ((check->state & (CHK_ST_CONFIGURED|CHK_ST_FASTINTER)) == CHK_ST_CONFIGURED && |
| 128 | (check->health == check->rise + check->fall - 1)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 129 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 130 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 131 | if ((s->next_state == SRV_ST_STOPPED) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 132 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 133 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 134 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 137 | /* |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 138 | * Use this to publish EVENT_HDL_SUB_SERVER family type event |
| 139 | * from srv facility |
| 140 | * Event will be published in both global subscription list and |
| 141 | * server dedicated subscription list |
| 142 | * server ptr must be valid |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 143 | * must be called with srv lock or under thread_isolate |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 144 | */ |
| 145 | static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate) |
| 146 | { |
| 147 | struct event_hdl_cb_data_server cb_data; |
| 148 | |
| 149 | /* safe data assignments */ |
| 150 | cb_data.safe.puid = srv->puid; |
| 151 | cb_data.safe.rid = srv->rid; |
| 152 | cb_data.safe.flags = srv->flags; |
| 153 | snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id); |
| 154 | if (srv->proxy) |
| 155 | snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id); |
| 156 | /* unsafe data assignments */ |
| 157 | cb_data.unsafe.ptr = srv; |
| 158 | cb_data.unsafe.thread_isolate = thread_isolate; |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 159 | cb_data.unsafe.srv_lock = !thread_isolate; |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 160 | /* publish in server dedicated sub list */ |
| 161 | event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data)); |
| 162 | /* publish in global subscription list */ |
| 163 | event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data)); |
| 164 | } |
| 165 | |
| 166 | /* |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 167 | * Check that we did not get a hash collision. |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 168 | * Unlikely, but it can happen. The server's proxy must be at least |
| 169 | * read-locked. |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 170 | */ |
| 171 | static inline void srv_check_for_dup_dyncookie(struct server *s) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 172 | { |
| 173 | struct proxy *p = s->proxy; |
| 174 | struct server *tmpserv; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 175 | |
| 176 | for (tmpserv = p->srv; tmpserv != NULL; |
| 177 | tmpserv = tmpserv->next) { |
| 178 | if (tmpserv == s) |
| 179 | continue; |
| 180 | if (tmpserv->next_admin & SRV_ADMF_FMAINT) |
| 181 | continue; |
| 182 | if (tmpserv->cookie && |
| 183 | strcmp(tmpserv->cookie, s->cookie) == 0) { |
| 184 | ha_warning("We generated two equal cookies for two different servers.\n" |
| 185 | "Please change the secret key for '%s'.\n", |
| 186 | s->proxy->id); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 192 | /* |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 193 | * 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] | 194 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 195 | void srv_set_dyncookie(struct server *s) |
| 196 | { |
| 197 | struct proxy *p = s->proxy; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 198 | char *tmpbuf; |
| 199 | unsigned long long hash_value; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 200 | size_t key_len; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 201 | size_t buffer_len; |
| 202 | int addr_len; |
| 203 | int port; |
| 204 | |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 205 | HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 206 | |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 207 | if ((s->flags & SRV_F_COOKIESET) || |
| 208 | !(s->proxy->ck_opts & PR_CK_DYNAMIC) || |
| 209 | s->proxy->dyncookie_key == NULL) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 210 | goto out; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 211 | key_len = strlen(p->dyncookie_key); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 212 | |
| 213 | if (s->addr.ss_family != AF_INET && |
| 214 | s->addr.ss_family != AF_INET6) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 215 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 216 | /* |
| 217 | * Buffer to calculate the cookie value. |
| 218 | * The buffer contains the secret key + the server IP address |
| 219 | * + the TCP port. |
| 220 | */ |
| 221 | addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16; |
| 222 | /* |
| 223 | * The TCP port should use only 2 bytes, but is stored in |
| 224 | * an unsigned int in struct server, so let's use 4, to be |
| 225 | * on the safe side. |
| 226 | */ |
| 227 | buffer_len = key_len + addr_len + 4; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 228 | tmpbuf = trash.area; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 229 | memcpy(tmpbuf, p->dyncookie_key, key_len); |
| 230 | memcpy(&(tmpbuf[key_len]), |
| 231 | s->addr.ss_family == AF_INET ? |
| 232 | (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr : |
| 233 | (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr), |
| 234 | addr_len); |
| 235 | /* |
| 236 | * Make sure it's the same across all the load balancers, |
| 237 | * no matter their endianness. |
| 238 | */ |
| 239 | port = htonl(s->svc_port); |
| 240 | memcpy(&tmpbuf[key_len + addr_len], &port, 4); |
| 241 | hash_value = XXH64(tmpbuf, buffer_len, 0); |
| 242 | memprintf(&s->cookie, "%016llx", hash_value); |
| 243 | if (!s->cookie) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 244 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 245 | s->cklen = 16; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 246 | |
| 247 | /* Don't bother checking if the dyncookie is duplicated if |
| 248 | * the server is marked as "disabled", maybe it doesn't have |
| 249 | * its real IP yet, but just a place holder. |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 250 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 251 | if (!(s->next_admin & SRV_ADMF_FMAINT)) |
| 252 | srv_check_for_dup_dyncookie(s); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 253 | out: |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 254 | HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 257 | /* Returns true if it's possible to reuse an idle connection from server <srv> |
| 258 | * for a websocket stream. This is the case if server is configured to use the |
| 259 | * same protocol for both HTTP and websocket streams. This depends on the value |
| 260 | * of "proto", "alpn" and "ws" keywords. |
| 261 | */ |
| 262 | int srv_check_reuse_ws(struct server *srv) |
| 263 | { |
| 264 | if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) { |
| 265 | /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used |
| 266 | * for mux selection. |
| 267 | */ |
| 268 | const struct ist srv_mux = srv->mux_proto ? |
| 269 | srv->mux_proto->token : IST_NULL; |
| 270 | |
| 271 | switch (srv->ws) { |
| 272 | /* "auto" means use the same protocol : reuse is possible. */ |
| 273 | case SRV_WS_AUTO: |
| 274 | return 1; |
| 275 | |
| 276 | /* "h2" means use h2 for websocket : reuse is possible if |
| 277 | * server mux is h2. |
| 278 | */ |
| 279 | case SRV_WS_H2: |
| 280 | if (srv->mux_proto && isteq(srv_mux, ist("h2"))) |
| 281 | return 1; |
| 282 | break; |
| 283 | |
| 284 | /* "h1" means use h1 for websocket : reuse is possible if |
| 285 | * server mux is h1. |
| 286 | */ |
| 287 | case SRV_WS_H1: |
| 288 | if (!srv->mux_proto || isteq(srv_mux, ist("h1"))) |
| 289 | return 1; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | else { |
| 294 | /* ALPN selection. |
| 295 | * Based on the assumption that only "h2" and "http/1.1" token |
| 296 | * are used on server ALPN. |
| 297 | */ |
| 298 | const struct ist alpn = ist2(srv->ssl_ctx.alpn_str, |
| 299 | srv->ssl_ctx.alpn_len); |
| 300 | |
| 301 | switch (srv->ws) { |
| 302 | case SRV_WS_AUTO: |
| 303 | /* for auto mode, consider reuse as possible if the |
| 304 | * server uses a single protocol ALPN |
| 305 | */ |
| 306 | if (!istchr(alpn, ',')) |
| 307 | return 1; |
| 308 | break; |
| 309 | |
| 310 | case SRV_WS_H2: |
| 311 | return isteq(alpn, ist("\x02h2")); |
| 312 | |
| 313 | case SRV_WS_H1: |
| 314 | return isteq(alpn, ist("\x08http/1.1")); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* Return the proto to used for a websocket stream on <srv> without ALPN. NULL |
| 322 | * is a valid value indicating to use the fallback mux. |
| 323 | */ |
| 324 | const struct mux_ops *srv_get_ws_proto(struct server *srv) |
| 325 | { |
| 326 | const struct mux_proto_list *mux = NULL; |
| 327 | |
| 328 | switch (srv->ws) { |
| 329 | case SRV_WS_AUTO: |
| 330 | mux = srv->mux_proto; |
| 331 | break; |
| 332 | |
| 333 | case SRV_WS_H1: |
| 334 | mux = get_mux_proto(ist("h1")); |
| 335 | break; |
| 336 | |
| 337 | case SRV_WS_H2: |
| 338 | mux = get_mux_proto(ist("h2")); |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | return mux ? mux->mux : NULL; |
| 343 | } |
| 344 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 345 | /* |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 346 | * Must be called with the server lock held. The server is first removed from |
| 347 | * the proxy tree if it was already attached. If <reattach> is true, the server |
| 348 | * will then be attached in the proxy tree. The proxy lock is held to |
| 349 | * manipulate the tree. |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 350 | */ |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 351 | static void srv_set_addr_desc(struct server *s, int reattach) |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 352 | { |
| 353 | struct proxy *p = s->proxy; |
| 354 | char *key; |
| 355 | |
| 356 | key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS); |
| 357 | |
| 358 | if (s->addr_node.key) { |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 359 | if (key && strcmp(key, s->addr_node.key) == 0) { |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 360 | free(key); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 365 | ebpt_delete(&s->addr_node); |
| 366 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 367 | |
| 368 | free(s->addr_node.key); |
| 369 | } |
| 370 | |
| 371 | s->addr_node.key = key; |
| 372 | |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 373 | if (reattach) { |
| 374 | if (s->addr_node.key) { |
| 375 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 376 | ebis_insert(&p->used_server_addr, &s->addr_node); |
| 377 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 378 | } |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 379 | } |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 383 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 384 | * parsing sessions. |
| 385 | */ |
| 386 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 387 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 388 | LIST_APPEND(&srv_keywords.list, &kwl->list); |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 392 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 393 | * find one with a valid ->parse() function. This way it is possible to declare |
| 394 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 395 | * if some options are met. Note that if the requested keyword contains an |
| 396 | * opening parenthesis, everything from this point is ignored. |
| 397 | */ |
| 398 | struct srv_kw *srv_find_kw(const char *kw) |
| 399 | { |
| 400 | int index; |
| 401 | const char *kwend; |
| 402 | struct srv_kw_list *kwl; |
| 403 | struct srv_kw *ret = NULL; |
| 404 | |
| 405 | kwend = strchr(kw, '('); |
| 406 | if (!kwend) |
| 407 | kwend = kw + strlen(kw); |
| 408 | |
| 409 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 410 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 411 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 412 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 413 | if (kwl->kw[index].parse) |
| 414 | return &kwl->kw[index]; /* found it !*/ |
| 415 | else |
| 416 | ret = &kwl->kw[index]; /* may be OK */ |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 424 | * unsupported keywords are only dumped if their supported form was not |
| 425 | * found. |
| 426 | */ |
| 427 | void srv_dump_kws(char **out) |
| 428 | { |
| 429 | struct srv_kw_list *kwl; |
| 430 | int index; |
| 431 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 432 | if (!out) |
| 433 | return; |
| 434 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 435 | *out = NULL; |
| 436 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 437 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 438 | if (kwl->kw[index].parse || |
| 439 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 440 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 441 | kwl->scope, |
| 442 | kwl->kw[index].kw, |
| 443 | kwl->kw[index].skip ? " <arg>" : "", |
| 444 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 445 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 446 | } |
| 447 | } |
| 448 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Try to find in srv_keyword the word that looks closest to <word> by counting |
| 452 | * transitions between letters, digits and other characters. Will return the |
| 453 | * best matching word if found, otherwise NULL. An optional array of extra |
| 454 | * words to compare may be passed in <extra>, but it must then be terminated |
| 455 | * by a NULL entry. If unused it may be NULL. |
| 456 | */ |
| 457 | static const char *srv_find_best_kw(const char *word) |
| 458 | { |
| 459 | uint8_t word_sig[1024]; |
| 460 | uint8_t list_sig[1024]; |
| 461 | const struct srv_kw_list *kwl; |
| 462 | const char *best_ptr = NULL; |
| 463 | int dist, best_dist = INT_MAX; |
| 464 | const char **extra; |
| 465 | int index; |
| 466 | |
| 467 | make_word_fingerprint(word_sig, word); |
| 468 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 469 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 470 | make_word_fingerprint(list_sig, kwl->kw[index].kw); |
| 471 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 472 | if (dist < best_dist) { |
| 473 | best_dist = dist; |
| 474 | best_ptr = kwl->kw[index].kw; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 479 | for (extra = extra_kw_list; *extra; extra++) { |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 480 | make_word_fingerprint(list_sig, *extra); |
| 481 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 482 | if (dist < best_dist) { |
| 483 | best_dist = dist; |
| 484 | best_ptr = *extra; |
| 485 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr))) |
| 489 | best_ptr = NULL; |
| 490 | |
| 491 | return best_ptr; |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 492 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 493 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 494 | /* Parse the "backup" server keyword */ |
| 495 | static int srv_parse_backup(char **args, int *cur_arg, |
| 496 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 497 | { |
| 498 | newsrv->flags |= SRV_F_BACKUP; |
| 499 | return 0; |
| 500 | } |
| 501 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 502 | |
Frédéric Lécaille | 9d1b95b | 2017-03-15 09:13:33 +0100 | [diff] [blame] | 503 | /* Parse the "cookie" server keyword */ |
| 504 | static int srv_parse_cookie(char **args, int *cur_arg, |
| 505 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 506 | { |
| 507 | char *arg; |
| 508 | |
| 509 | arg = args[*cur_arg + 1]; |
| 510 | if (!*arg) { |
| 511 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 512 | return ERR_ALERT | ERR_FATAL; |
| 513 | } |
| 514 | |
| 515 | free(newsrv->cookie); |
| 516 | newsrv->cookie = strdup(arg); |
| 517 | newsrv->cklen = strlen(arg); |
| 518 | newsrv->flags |= SRV_F_COOKIESET; |
| 519 | return 0; |
| 520 | } |
| 521 | |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 522 | /* Parse the "disabled" server keyword */ |
| 523 | static int srv_parse_disabled(char **args, int *cur_arg, |
| 524 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 525 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 526 | newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 527 | newsrv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 528 | newsrv->check.state |= CHK_ST_PAUSED; |
| 529 | newsrv->check.health = 0; |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* Parse the "enabled" server keyword */ |
| 534 | static int srv_parse_enabled(char **args, int *cur_arg, |
| 535 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 536 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 537 | newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT; |
| 538 | newsrv->next_state = SRV_ST_RUNNING; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 539 | newsrv->check.state &= ~CHK_ST_PAUSED; |
| 540 | newsrv->check.health = newsrv->check.rise; |
| 541 | return 0; |
| 542 | } |
| 543 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 544 | /* Parse the "error-limit" server keyword */ |
| 545 | static int srv_parse_error_limit(char **args, int *cur_arg, |
| 546 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 547 | { |
| 548 | if (!*args[*cur_arg + 1]) { |
| 549 | memprintf(err, "'%s' expects an integer argument.", |
| 550 | args[*cur_arg]); |
| 551 | return ERR_ALERT | ERR_FATAL; |
| 552 | } |
| 553 | |
| 554 | newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]); |
| 555 | |
| 556 | if (newsrv->consecutive_errors_limit <= 0) { |
| 557 | memprintf(err, "%s has to be > 0.", |
| 558 | args[*cur_arg]); |
| 559 | return ERR_ALERT | ERR_FATAL; |
| 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
Amaury Denoyelle | f9d5957 | 2021-10-18 14:40:29 +0200 | [diff] [blame] | 565 | /* Parse the "ws" keyword */ |
| 566 | static int srv_parse_ws(char **args, int *cur_arg, |
| 567 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 568 | { |
| 569 | if (!args[*cur_arg + 1]) { |
| 570 | memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]); |
| 571 | return ERR_ALERT | ERR_FATAL; |
| 572 | } |
| 573 | |
| 574 | if (strcmp(args[*cur_arg + 1], "h1") == 0) { |
| 575 | newsrv->ws = SRV_WS_H1; |
| 576 | } |
| 577 | else if (strcmp(args[*cur_arg + 1], "h2") == 0) { |
| 578 | newsrv->ws = SRV_WS_H2; |
| 579 | } |
| 580 | else if (strcmp(args[*cur_arg + 1], "auto") == 0) { |
| 581 | newsrv->ws = SRV_WS_AUTO; |
| 582 | } |
| 583 | else { |
| 584 | memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]); |
| 585 | return ERR_ALERT | ERR_FATAL; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 592 | /* Parse the "init-addr" server keyword */ |
| 593 | static int srv_parse_init_addr(char **args, int *cur_arg, |
| 594 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 595 | { |
| 596 | char *p, *end; |
| 597 | int done; |
| 598 | struct sockaddr_storage sa; |
| 599 | |
| 600 | newsrv->init_addr_methods = 0; |
| 601 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 602 | |
| 603 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 604 | /* cut on next comma */ |
| 605 | for (end = p; *end && *end != ','; end++); |
| 606 | if (*end) |
| 607 | *(end++) = 0; |
| 608 | |
| 609 | memset(&sa, 0, sizeof(sa)); |
| 610 | if (strcmp(p, "libc") == 0) { |
| 611 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 612 | } |
| 613 | else if (strcmp(p, "last") == 0) { |
| 614 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 615 | } |
| 616 | else if (strcmp(p, "none") == 0) { |
| 617 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 618 | } |
| 619 | else if (str2ip2(p, &sa, 0)) { |
| 620 | if (is_addr(&newsrv->init_addr)) { |
| 621 | memprintf(err, "'%s' : initial address already specified, cannot add '%s'.", |
| 622 | args[*cur_arg], p); |
| 623 | return ERR_ALERT | ERR_FATAL; |
| 624 | } |
| 625 | newsrv->init_addr = sa; |
| 626 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 627 | } |
| 628 | else { |
| 629 | memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.", |
| 630 | args[*cur_arg], p); |
| 631 | return ERR_ALERT | ERR_FATAL; |
| 632 | } |
| 633 | if (!done) { |
| 634 | memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'", |
| 635 | args[*cur_arg], p); |
| 636 | return ERR_ALERT | ERR_FATAL; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* Parse the "log-proto" server keyword */ |
| 644 | static int srv_parse_log_proto(char **args, int *cur_arg, |
| 645 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 646 | { |
| 647 | if (strcmp(args[*cur_arg + 1], "legacy") == 0) |
| 648 | newsrv->log_proto = SRV_LOG_PROTO_LEGACY; |
| 649 | else if (strcmp(args[*cur_arg + 1], "octet-count") == 0) |
| 650 | newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING; |
| 651 | else { |
| 652 | memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'", |
| 653 | args[*cur_arg], args[*cur_arg + 1]); |
| 654 | return ERR_ALERT | ERR_FATAL; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /* Parse the "maxconn" server keyword */ |
| 661 | static int srv_parse_maxconn(char **args, int *cur_arg, |
| 662 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 663 | { |
| 664 | newsrv->maxconn = atol(args[*cur_arg + 1]); |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | /* Parse the "maxqueue" server keyword */ |
| 669 | static int srv_parse_maxqueue(char **args, int *cur_arg, |
| 670 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 671 | { |
| 672 | newsrv->maxqueue = atol(args[*cur_arg + 1]); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /* Parse the "minconn" server keyword */ |
| 677 | static int srv_parse_minconn(char **args, int *cur_arg, |
| 678 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 679 | { |
| 680 | newsrv->minconn = atol(args[*cur_arg + 1]); |
| 681 | return 0; |
| 682 | } |
| 683 | |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 684 | static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 685 | { |
| 686 | char *arg; |
| 687 | |
| 688 | arg = args[*cur_arg + 1]; |
| 689 | if (!*arg) { |
| 690 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 691 | return ERR_ALERT | ERR_FATAL; |
| 692 | } |
| 693 | newsrv->max_reuse = atoi(arg); |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 698 | 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] | 699 | { |
| 700 | const char *res; |
| 701 | char *arg; |
| 702 | unsigned int time; |
| 703 | |
| 704 | arg = args[*cur_arg + 1]; |
| 705 | if (!*arg) { |
| 706 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 707 | return ERR_ALERT | ERR_FATAL; |
| 708 | } |
| 709 | res = parse_time_err(arg, &time, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 710 | if (res == PARSE_TIME_OVER) { |
| 711 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 712 | args[*cur_arg+1], args[*cur_arg]); |
| 713 | return ERR_ALERT | ERR_FATAL; |
| 714 | } |
| 715 | else if (res == PARSE_TIME_UNDER) { |
| 716 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 717 | args[*cur_arg+1], args[*cur_arg]); |
| 718 | return ERR_ALERT | ERR_FATAL; |
| 719 | } |
| 720 | else if (res) { |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 721 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", |
| 722 | *res, args[*cur_arg]); |
| 723 | return ERR_ALERT | ERR_FATAL; |
| 724 | } |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 725 | newsrv->pool_purge_delay = time; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 730 | static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 731 | { |
| 732 | char *arg; |
| 733 | |
| 734 | arg = args[*cur_arg + 1]; |
| 735 | if (!*arg) { |
| 736 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 737 | return ERR_ALERT | ERR_FATAL; |
| 738 | } |
| 739 | |
| 740 | newsrv->low_idle_conns = atoi(arg); |
| 741 | return 0; |
| 742 | } |
| 743 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 744 | static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 745 | { |
| 746 | char *arg; |
| 747 | |
| 748 | arg = args[*cur_arg + 1]; |
| 749 | if (!*arg) { |
| 750 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 751 | return ERR_ALERT | ERR_FATAL; |
| 752 | } |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 753 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 754 | newsrv->max_idle_conns = atoi(arg); |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 755 | if ((int)newsrv->max_idle_conns < -1) { |
| 756 | memprintf(err, "'%s' must be >= -1", args[*cur_arg]); |
| 757 | return ERR_ALERT | ERR_FATAL; |
| 758 | } |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 763 | /* parse the "id" server keyword */ |
| 764 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 765 | { |
| 766 | struct eb32_node *node; |
| 767 | |
| 768 | if (!*args[*cur_arg + 1]) { |
| 769 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 770 | return ERR_ALERT | ERR_FATAL; |
| 771 | } |
| 772 | |
| 773 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 774 | newsrv->conf.id.key = newsrv->puid; |
| 775 | |
| 776 | if (newsrv->puid <= 0) { |
| 777 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 778 | return ERR_ALERT | ERR_FATAL; |
| 779 | } |
| 780 | |
| 781 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 782 | if (node) { |
| 783 | struct server *target = container_of(node, struct server, conf.id); |
| 784 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 785 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 786 | target->id); |
| 787 | return ERR_ALERT | ERR_FATAL; |
| 788 | } |
| 789 | |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 790 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 794 | /* Parse the "namespace" server keyword */ |
| 795 | static int srv_parse_namespace(char **args, int *cur_arg, |
| 796 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 797 | { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 798 | #ifdef USE_NS |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 799 | char *arg; |
| 800 | |
| 801 | arg = args[*cur_arg + 1]; |
| 802 | if (!*arg) { |
| 803 | memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]); |
| 804 | return ERR_ALERT | ERR_FATAL; |
| 805 | } |
| 806 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 807 | if (strcmp(arg, "*") == 0) { |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 808 | /* Use the namespace associated with the connection (if present). */ |
| 809 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * As this parser may be called several times for the same 'default-server' |
| 815 | * object, or for a new 'server' instance deriving from a 'default-server' |
| 816 | * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it. |
| 817 | */ |
| 818 | newsrv->flags &= ~SRV_F_USE_NS_FROM_PP; |
| 819 | |
| 820 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 821 | if (!newsrv->netns) |
| 822 | newsrv->netns = netns_store_insert(arg); |
| 823 | |
| 824 | if (!newsrv->netns) { |
| 825 | memprintf(err, "Cannot open namespace '%s'", arg); |
| 826 | return ERR_ALERT | ERR_FATAL; |
| 827 | } |
| 828 | |
| 829 | return 0; |
| 830 | #else |
| 831 | memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]); |
| 832 | return ERR_ALERT | ERR_FATAL; |
| 833 | #endif |
| 834 | } |
| 835 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 836 | /* Parse the "no-backup" server keyword */ |
| 837 | static int srv_parse_no_backup(char **args, int *cur_arg, |
| 838 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 839 | { |
| 840 | newsrv->flags &= ~SRV_F_BACKUP; |
| 841 | return 0; |
| 842 | } |
| 843 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 844 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 845 | /* Disable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | srv->pp_opts &= ~flags; |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /* Parse the "no-send-proxy" server keyword */ |
| 853 | static int srv_parse_no_send_proxy(char **args, int *cur_arg, |
| 854 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 855 | { |
| 856 | return srv_disable_pp_flags(newsrv, SRV_PP_V1); |
| 857 | } |
| 858 | |
| 859 | /* Parse the "no-send-proxy-v2" server keyword */ |
| 860 | static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg, |
| 861 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 862 | { |
| 863 | return srv_disable_pp_flags(newsrv, SRV_PP_V2); |
| 864 | } |
| 865 | |
Frédéric Lécaille | 36d1565 | 2022-10-17 14:58:19 +0200 | [diff] [blame] | 866 | /* Parse the "shard" server keyword */ |
| 867 | static int srv_parse_shard(char **args, int *cur_arg, |
| 868 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 869 | { |
| 870 | newsrv->shard = atol(args[*cur_arg + 1]); |
| 871 | return 0; |
| 872 | } |
| 873 | |
Frédéric Lécaille | 1b9423d | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 874 | /* Parse the "no-tfo" server keyword */ |
| 875 | static int srv_parse_no_tfo(char **args, int *cur_arg, |
| 876 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 877 | { |
| 878 | newsrv->flags &= ~SRV_F_FASTOPEN; |
| 879 | return 0; |
| 880 | } |
| 881 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 882 | /* Parse the "non-stick" server keyword */ |
| 883 | static int srv_parse_non_stick(char **args, int *cur_arg, |
| 884 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 885 | { |
| 886 | newsrv->flags |= SRV_F_NON_STICK; |
| 887 | return 0; |
| 888 | } |
| 889 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 890 | /* Enable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 891 | 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] | 892 | { |
| 893 | srv->pp_opts |= flags; |
| 894 | return 0; |
| 895 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 896 | /* parse the "proto" server keyword */ |
| 897 | static int srv_parse_proto(char **args, int *cur_arg, |
| 898 | struct proxy *px, struct server *newsrv, char **err) |
| 899 | { |
| 900 | struct ist proto; |
| 901 | |
| 902 | if (!*args[*cur_arg + 1]) { |
| 903 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 904 | return ERR_ALERT | ERR_FATAL; |
| 905 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 906 | proto = ist(args[*cur_arg + 1]); |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 907 | newsrv->mux_proto = get_mux_proto(proto); |
| 908 | if (!newsrv->mux_proto) { |
| 909 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 910 | return ERR_ALERT | ERR_FATAL; |
| 911 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 912 | return 0; |
| 913 | } |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 914 | |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 915 | /* parse the "proxy-v2-options" */ |
| 916 | static int srv_parse_proxy_v2_options(char **args, int *cur_arg, |
| 917 | struct proxy *px, struct server *newsrv, char **err) |
| 918 | { |
| 919 | char *p, *n; |
| 920 | for (p = args[*cur_arg+1]; p; p = n) { |
| 921 | n = strchr(p, ','); |
| 922 | if (n) |
| 923 | *n++ = '\0'; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 924 | if (strcmp(p, "ssl") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 925 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 926 | } else if (strcmp(p, "cert-cn") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 927 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 928 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 929 | } else if (strcmp(p, "cert-key") == 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_KEY_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 932 | } else if (strcmp(p, "cert-sig") == 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_SIG_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 935 | } else if (strcmp(p, "ssl-cipher") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 936 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 937 | newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 938 | } else if (strcmp(p, "authority") == 0) { |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 939 | newsrv->pp_opts |= SRV_PP_V2_AUTHORITY; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 940 | } else if (strcmp(p, "crc32c") == 0) { |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 941 | newsrv->pp_opts |= SRV_PP_V2_CRC32C; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 942 | } else if (strcmp(p, "unique-id") == 0) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 943 | newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID; |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 944 | } else |
| 945 | goto fail; |
| 946 | } |
| 947 | return 0; |
| 948 | fail: |
| 949 | if (err) |
| 950 | memprintf(err, "'%s' : proxy v2 option not implemented", p); |
| 951 | return ERR_ALERT | ERR_FATAL; |
| 952 | } |
| 953 | |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 954 | /* Parse the "observe" server keyword */ |
| 955 | static int srv_parse_observe(char **args, int *cur_arg, |
| 956 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 957 | { |
| 958 | char *arg; |
| 959 | |
| 960 | arg = args[*cur_arg + 1]; |
| 961 | if (!*arg) { |
| 962 | memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]); |
| 963 | return ERR_ALERT | ERR_FATAL; |
| 964 | } |
| 965 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 966 | if (strcmp(arg, "none") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 967 | newsrv->observe = HANA_OBS_NONE; |
| 968 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 969 | else if (strcmp(arg, "layer4") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 970 | newsrv->observe = HANA_OBS_LAYER4; |
| 971 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 972 | else if (strcmp(arg, "layer7") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 973 | if (curproxy->mode != PR_MODE_HTTP) { |
| 974 | memprintf(err, "'%s' can only be used in http proxies.\n", arg); |
| 975 | return ERR_ALERT; |
| 976 | } |
| 977 | newsrv->observe = HANA_OBS_LAYER7; |
| 978 | } |
| 979 | else { |
| 980 | memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' " |
| 981 | "but got '%s'\n", args[*cur_arg], arg); |
| 982 | return ERR_ALERT | ERR_FATAL; |
| 983 | } |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 988 | /* Parse the "on-error" server keyword */ |
| 989 | static int srv_parse_on_error(char **args, int *cur_arg, |
| 990 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 991 | { |
| 992 | if (strcmp(args[*cur_arg + 1], "fastinter") == 0) |
| 993 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 994 | else if (strcmp(args[*cur_arg + 1], "fail-check") == 0) |
| 995 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 996 | else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0) |
| 997 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 998 | else if (strcmp(args[*cur_arg + 1], "mark-down") == 0) |
| 999 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 1000 | else { |
| 1001 | memprintf(err, "'%s' expects one of 'fastinter', " |
| 1002 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'", |
| 1003 | args[*cur_arg], args[*cur_arg + 1]); |
| 1004 | return ERR_ALERT | ERR_FATAL; |
| 1005 | } |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /* Parse the "on-marked-down" server keyword */ |
| 1011 | static int srv_parse_on_marked_down(char **args, int *cur_arg, |
| 1012 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1013 | { |
| 1014 | if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0) |
| 1015 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1016 | else { |
| 1017 | memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'", |
| 1018 | args[*cur_arg], args[*cur_arg + 1]); |
| 1019 | return ERR_ALERT | ERR_FATAL; |
| 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | /* Parse the "on-marked-up" server keyword */ |
| 1026 | static int srv_parse_on_marked_up(char **args, int *cur_arg, |
| 1027 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1028 | { |
| 1029 | if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0) |
| 1030 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1031 | else { |
| 1032 | memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'", |
| 1033 | args[*cur_arg], args[*cur_arg + 1]); |
| 1034 | return ERR_ALERT | ERR_FATAL; |
| 1035 | } |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 1040 | /* Parse the "redir" server keyword */ |
| 1041 | static int srv_parse_redir(char **args, int *cur_arg, |
| 1042 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1043 | { |
| 1044 | char *arg; |
| 1045 | |
| 1046 | arg = args[*cur_arg + 1]; |
| 1047 | if (!*arg) { |
| 1048 | memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]); |
| 1049 | return ERR_ALERT | ERR_FATAL; |
| 1050 | } |
| 1051 | |
| 1052 | free(newsrv->rdr_pfx); |
| 1053 | newsrv->rdr_pfx = strdup(arg); |
| 1054 | newsrv->rdr_len = strlen(arg); |
| 1055 | |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1059 | /* Parse the "resolvers" server keyword */ |
| 1060 | static int srv_parse_resolvers(char **args, int *cur_arg, |
| 1061 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1062 | { |
| 1063 | free(newsrv->resolvers_id); |
| 1064 | newsrv->resolvers_id = strdup(args[*cur_arg + 1]); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | /* Parse the "resolve-net" server keyword */ |
| 1069 | static int srv_parse_resolve_net(char **args, int *cur_arg, |
| 1070 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1071 | { |
| 1072 | char *p, *e; |
| 1073 | unsigned char mask; |
| 1074 | struct resolv_options *opt; |
| 1075 | |
| 1076 | if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') { |
| 1077 | memprintf(err, "'%s' expects a list of networks.", |
| 1078 | args[*cur_arg]); |
| 1079 | return ERR_ALERT | ERR_FATAL; |
| 1080 | } |
| 1081 | |
| 1082 | opt = &newsrv->resolv_opts; |
| 1083 | |
| 1084 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 1085 | * string network in in_addr or in6_addr. |
| 1086 | */ |
| 1087 | p = args[*cur_arg + 1]; |
| 1088 | e = p; |
| 1089 | while (*p != '\0') { |
| 1090 | /* If no room available, return error. */ |
| 1091 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
| 1092 | memprintf(err, "'%s' exceed %d networks.", |
| 1093 | args[*cur_arg], SRV_MAX_PREF_NET); |
| 1094 | return ERR_ALERT | ERR_FATAL; |
| 1095 | } |
| 1096 | /* look for end or comma. */ |
| 1097 | while (*e != ',' && *e != '\0') |
| 1098 | e++; |
| 1099 | if (*e == ',') { |
| 1100 | *e = '\0'; |
| 1101 | e++; |
| 1102 | } |
| 1103 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 1104 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 1105 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 1106 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 1107 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 1108 | &mask)) { |
| 1109 | /* Try to convert input string from ipv6 network. */ |
| 1110 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 1111 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 1112 | } else { |
| 1113 | /* All network conversions fail, return error. */ |
| 1114 | memprintf(err, "'%s' invalid network '%s'.", |
| 1115 | args[*cur_arg], p); |
| 1116 | return ERR_ALERT | ERR_FATAL; |
| 1117 | } |
| 1118 | opt->pref_net_nb++; |
| 1119 | p = e; |
| 1120 | } |
| 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | /* Parse the "resolve-opts" server keyword */ |
| 1126 | static int srv_parse_resolve_opts(char **args, int *cur_arg, |
| 1127 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1128 | { |
| 1129 | char *p, *end; |
| 1130 | |
| 1131 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 1132 | /* cut on next comma */ |
| 1133 | for (end = p; *end && *end != ','; end++); |
| 1134 | if (*end) |
| 1135 | *(end++) = 0; |
| 1136 | |
| 1137 | if (strcmp(p, "allow-dup-ip") == 0) { |
| 1138 | newsrv->resolv_opts.accept_duplicate_ip = 1; |
| 1139 | } |
| 1140 | else if (strcmp(p, "ignore-weight") == 0) { |
| 1141 | newsrv->resolv_opts.ignore_weight = 1; |
| 1142 | } |
| 1143 | else if (strcmp(p, "prevent-dup-ip") == 0) { |
| 1144 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
| 1145 | } |
| 1146 | else { |
| 1147 | memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.", |
| 1148 | args[*cur_arg], p); |
| 1149 | return ERR_ALERT | ERR_FATAL; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* Parse the "resolve-prefer" server keyword */ |
| 1157 | static int srv_parse_resolve_prefer(char **args, int *cur_arg, |
| 1158 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1159 | { |
| 1160 | if (strcmp(args[*cur_arg + 1], "ipv4") == 0) |
| 1161 | newsrv->resolv_opts.family_prio = AF_INET; |
| 1162 | else if (strcmp(args[*cur_arg + 1], "ipv6") == 0) |
| 1163 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 1164 | else { |
| 1165 | memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.", |
| 1166 | args[*cur_arg]); |
| 1167 | return ERR_ALERT | ERR_FATAL; |
| 1168 | } |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1173 | /* Parse the "send-proxy" server keyword */ |
| 1174 | static int srv_parse_send_proxy(char **args, int *cur_arg, |
| 1175 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1176 | { |
| 1177 | return srv_enable_pp_flags(newsrv, SRV_PP_V1); |
| 1178 | } |
| 1179 | |
| 1180 | /* Parse the "send-proxy-v2" server keyword */ |
| 1181 | static int srv_parse_send_proxy_v2(char **args, int *cur_arg, |
| 1182 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1183 | { |
| 1184 | return srv_enable_pp_flags(newsrv, SRV_PP_V2); |
| 1185 | } |
| 1186 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1187 | /* Parse the "slowstart" server keyword */ |
| 1188 | static int srv_parse_slowstart(char **args, int *cur_arg, |
| 1189 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1190 | { |
| 1191 | /* slowstart is stored in seconds */ |
| 1192 | unsigned int val; |
| 1193 | const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS); |
| 1194 | |
| 1195 | if (time_err == PARSE_TIME_OVER) { |
| 1196 | memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1197 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1198 | return ERR_ALERT | ERR_FATAL; |
| 1199 | } |
| 1200 | else if (time_err == PARSE_TIME_UNDER) { |
| 1201 | memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1202 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1203 | return ERR_ALERT | ERR_FATAL; |
| 1204 | } |
| 1205 | else if (time_err) { |
| 1206 | memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.", |
| 1207 | *time_err, newsrv->id); |
| 1208 | return ERR_ALERT | ERR_FATAL; |
| 1209 | } |
| 1210 | newsrv->slowstart = (val + 999) / 1000; |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1214 | |
| 1215 | /* Parse the "source" server keyword */ |
| 1216 | static int srv_parse_source(char **args, int *cur_arg, |
| 1217 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1218 | { |
| 1219 | char *errmsg; |
| 1220 | int port_low, port_high; |
| 1221 | struct sockaddr_storage *sk; |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1222 | |
| 1223 | errmsg = NULL; |
| 1224 | |
| 1225 | if (!*args[*cur_arg + 1]) { |
| 1226 | memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, " |
| 1227 | "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface"); |
| 1228 | goto err; |
| 1229 | } |
| 1230 | |
| 1231 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1232 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1233 | &errmsg, NULL, NULL, |
| 1234 | 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] | 1235 | if (!sk) { |
| 1236 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1237 | goto err; |
| 1238 | } |
| 1239 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1240 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1241 | newsrv->conn_src.source_addr = *sk; |
| 1242 | |
| 1243 | if (port_low != port_high) { |
| 1244 | int i; |
| 1245 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1246 | 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] | 1247 | if (!newsrv->conn_src.sport_range) { |
| 1248 | ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]); |
| 1249 | goto err; |
| 1250 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1251 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1252 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1253 | } |
| 1254 | |
| 1255 | *cur_arg += 2; |
| 1256 | while (*(args[*cur_arg])) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1257 | 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] | 1258 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 1259 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1260 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 1261 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1262 | goto err; |
| 1263 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1264 | if (strcmp(args[*cur_arg + 1], "client") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1265 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1266 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1267 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1268 | else if (strcmp(args[*cur_arg + 1], "clientip") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1269 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1270 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1271 | } |
| 1272 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 1273 | char *name, *end; |
| 1274 | |
| 1275 | name = args[*cur_arg + 1] + 7; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1276 | while (isspace((unsigned char)*name)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1277 | name++; |
| 1278 | |
| 1279 | end = name; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1280 | while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')') |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1281 | end++; |
| 1282 | |
| 1283 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1284 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1285 | free(newsrv->conn_src.bind_hdr_name); |
| 1286 | 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] | 1287 | if (!newsrv->conn_src.bind_hdr_name) { |
| 1288 | ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]); |
| 1289 | goto err; |
| 1290 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1291 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1292 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1293 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 1294 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1295 | |
| 1296 | /* now look for an occurrence number */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1297 | while (isspace((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1298 | end++; |
| 1299 | if (*end == ',') { |
| 1300 | end++; |
| 1301 | name = end; |
| 1302 | if (*end == '-') |
| 1303 | end++; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1304 | while (isdigit((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1305 | end++; |
| 1306 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 1307 | } |
| 1308 | |
| 1309 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1310 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 1311 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1312 | goto err; |
| 1313 | } |
| 1314 | } |
| 1315 | else { |
| 1316 | struct sockaddr_storage *sk; |
| 1317 | int port1, port2; |
| 1318 | |
| 1319 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1320 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, |
| 1321 | &errmsg, NULL, NULL, |
| 1322 | 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] | 1323 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1324 | 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] | 1325 | goto err; |
| 1326 | } |
| 1327 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1328 | newsrv->conn_src.tproxy_addr = *sk; |
| 1329 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1330 | } |
| 1331 | global.last_checks |= LSTCHK_NETADM; |
| 1332 | *cur_arg += 2; |
| 1333 | continue; |
| 1334 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1335 | 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] | 1336 | goto err; |
| 1337 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 1338 | } /* "usesrc" */ |
| 1339 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1340 | 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] | 1341 | #ifdef SO_BINDTODEVICE |
| 1342 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1343 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1344 | goto err; |
| 1345 | } |
| 1346 | free(newsrv->conn_src.iface_name); |
| 1347 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 1348 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1349 | global.last_checks |= LSTCHK_NETADM; |
| 1350 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1351 | 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] | 1352 | goto err; |
| 1353 | #endif |
| 1354 | *cur_arg += 2; |
| 1355 | continue; |
| 1356 | } |
| 1357 | /* this keyword in not an option of "source" */ |
| 1358 | break; |
| 1359 | } /* while */ |
| 1360 | |
| 1361 | return 0; |
| 1362 | |
| 1363 | err: |
| 1364 | free(errmsg); |
| 1365 | return ERR_ALERT | ERR_FATAL; |
| 1366 | } |
| 1367 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1368 | /* Parse the "stick" server keyword */ |
| 1369 | static int srv_parse_stick(char **args, int *cur_arg, |
| 1370 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1371 | { |
| 1372 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1376 | /* Parse the "track" server keyword */ |
| 1377 | static int srv_parse_track(char **args, int *cur_arg, |
| 1378 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1379 | { |
| 1380 | char *arg; |
| 1381 | |
| 1382 | arg = args[*cur_arg + 1]; |
| 1383 | if (!*arg) { |
| 1384 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 1385 | return ERR_ALERT | ERR_FATAL; |
| 1386 | } |
| 1387 | |
| 1388 | free(newsrv->trackit); |
| 1389 | newsrv->trackit = strdup(arg); |
| 1390 | |
| 1391 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /* Parse the "socks4" server keyword */ |
| 1395 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 1396 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1397 | { |
| 1398 | char *errmsg; |
| 1399 | int port_low, port_high; |
| 1400 | struct sockaddr_storage *sk; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1401 | |
| 1402 | errmsg = NULL; |
| 1403 | |
| 1404 | if (!*args[*cur_arg + 1]) { |
| 1405 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 1406 | goto err; |
| 1407 | } |
| 1408 | |
| 1409 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1410 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1411 | &errmsg, NULL, NULL, |
| 1412 | 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] | 1413 | if (!sk) { |
| 1414 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1415 | goto err; |
| 1416 | } |
| 1417 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1418 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 1419 | newsrv->socks4_addr = *sk; |
| 1420 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1421 | return 0; |
| 1422 | |
| 1423 | err: |
| 1424 | free(errmsg); |
| 1425 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1428 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 1429 | /* parse the "tfo" server keyword */ |
| 1430 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1431 | { |
| 1432 | newsrv->flags |= SRV_F_FASTOPEN; |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1436 | /* parse the "usesrc" server keyword */ |
| 1437 | static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1438 | { |
| 1439 | memprintf(err, "'%s' only allowed after a '%s' statement.", |
| 1440 | "usesrc", "source"); |
| 1441 | return ERR_ALERT | ERR_FATAL; |
| 1442 | } |
| 1443 | |
| 1444 | /* parse the "weight" server keyword */ |
| 1445 | static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1446 | { |
| 1447 | int w; |
| 1448 | |
| 1449 | w = atol(args[*cur_arg + 1]); |
| 1450 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1451 | memprintf(err, "weight of server %s is not within 0 and %d (%d).", |
| 1452 | newsrv->id, SRV_UWGHT_MAX, w); |
| 1453 | return ERR_ALERT | ERR_FATAL; |
| 1454 | } |
| 1455 | newsrv->uweight = newsrv->iweight = w; |
| 1456 | |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1460 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1461 | * 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] | 1462 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1463 | * |
| 1464 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1465 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1466 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1467 | { |
Willy Tarreau | 751153e | 2021-02-17 13:33:24 +0100 | [diff] [blame] | 1468 | struct stream *stream; |
| 1469 | struct mt_list *elt1, elt2; |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1470 | int thr; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1471 | |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1472 | for (thr = 0; thr < global.nbthread; thr++) |
| 1473 | mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2) |
| 1474 | if (stream->srv_conn == srv) |
| 1475 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /* 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] | 1479 | * 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] | 1480 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1481 | * |
| 1482 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1483 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1484 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1485 | { |
| 1486 | struct server *srv; |
| 1487 | |
| 1488 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1489 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1490 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1493 | /* Appends some information to a message string related to a server going UP or |
| 1494 | * DOWN. If both <forced> and <reason> are null and the server tracks another |
| 1495 | * 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] | 1496 | * If <check> is non-null, an entire string describing the check result will be |
| 1497 | * appended after a comma and a space (eg: to report some information from the |
| 1498 | * check that changed the state). In the other case, the string will be built |
| 1499 | * using the check results stored into the struct server if present. |
| 1500 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1501 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1502 | * |
| 1503 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1504 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1505 | void srv_append_status(struct buffer *msg, struct server *s, |
| 1506 | struct check *check, int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1507 | { |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1508 | short status = s->op_st_chg.status; |
| 1509 | short code = s->op_st_chg.code; |
| 1510 | long duration = s->op_st_chg.duration; |
| 1511 | char *desc = s->op_st_chg.reason; |
| 1512 | |
| 1513 | if (check) { |
| 1514 | status = check->status; |
| 1515 | code = check->code; |
| 1516 | duration = check->duration; |
| 1517 | desc = check->desc; |
| 1518 | } |
| 1519 | |
| 1520 | if (status != -1) { |
| 1521 | chunk_appendf(msg, ", reason: %s", get_check_status_description(status)); |
| 1522 | |
| 1523 | if (status >= HCHK_STATUS_L57DATA) |
| 1524 | chunk_appendf(msg, ", code: %d", code); |
| 1525 | |
| 1526 | if (desc && *desc) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1527 | struct buffer src; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1528 | |
| 1529 | chunk_appendf(msg, ", info: \""); |
| 1530 | |
| 1531 | chunk_initlen(&src, desc, 0, strlen(desc)); |
| 1532 | chunk_asciiencode(msg, &src, '"'); |
| 1533 | |
| 1534 | chunk_appendf(msg, "\""); |
| 1535 | } |
| 1536 | |
| 1537 | if (duration >= 0) |
| 1538 | chunk_appendf(msg, ", check duration: %ldms", duration); |
| 1539 | } |
| 1540 | else if (desc && *desc) { |
| 1541 | chunk_appendf(msg, ", %s", desc); |
| 1542 | } |
| 1543 | else if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1544 | 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] | 1545 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1546 | |
| 1547 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1548 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1549 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1550 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1551 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1552 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1553 | s->cur_sess, xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1554 | else |
| 1555 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1556 | " %d sessions requeued, %d total in queue", |
| 1557 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1558 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1559 | xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1563 | /* Marks server <s> down, regardless of its checks' statuses. The server is |
| 1564 | * registered in a list to postpone the counting of the remaining servers on |
| 1565 | * the proxy and transfers queued streams whenever possible to other servers at |
| 1566 | * a sync point. Maintenance servers are ignored. It stores the <reason> if |
| 1567 | * non-null as the reason for going down or the available data from the check |
| 1568 | * struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1569 | * |
| 1570 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1571 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1572 | 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] | 1573 | { |
| 1574 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1575 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1576 | 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] | 1577 | return; |
| 1578 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1579 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1580 | *s->op_st_chg.reason = 0; |
| 1581 | s->op_st_chg.status = -1; |
| 1582 | if (reason) { |
| 1583 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1584 | } |
| 1585 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1586 | 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] | 1587 | s->op_st_chg.code = check->code; |
| 1588 | s->op_st_chg.status = check->status; |
| 1589 | s->op_st_chg.duration = check->duration; |
| 1590 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1591 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1592 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1593 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1594 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1595 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1596 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1597 | srv_set_stopped(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1598 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1599 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1602 | /* 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] | 1603 | * in maintenance. The server is registered in a list to postpone the counting |
| 1604 | * of the remaining servers on the proxy and tries to grab requests from the |
| 1605 | * proxy at a sync point. Maintenance servers are ignored. It stores the |
| 1606 | * <reason> if non-null as the reason for going down or the available data |
| 1607 | * from the check struct to recompute this reason later. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1608 | * |
| 1609 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1610 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1611 | 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] | 1612 | { |
| 1613 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1614 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1615 | if (s->cur_admin & SRV_ADMF_MAINT) |
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 | 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] | 1619 | return; |
| 1620 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1621 | s->next_state = SRV_ST_STARTING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1622 | *s->op_st_chg.reason = 0; |
| 1623 | s->op_st_chg.status = -1; |
| 1624 | if (reason) { |
| 1625 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1626 | } |
| 1627 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1628 | 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] | 1629 | s->op_st_chg.code = check->code; |
| 1630 | s->op_st_chg.status = check->status; |
| 1631 | s->op_st_chg.duration = check->duration; |
| 1632 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1633 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1634 | if (s->slowstart <= 0) |
| 1635 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1636 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1637 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1638 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1639 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1640 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1641 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1642 | srv_set_running(srv, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1643 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1644 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1647 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1648 | * isn't in maintenance. The server is registered in a list to postpone the |
| 1649 | * counting of the remaining servers on the proxy and tries to grab requests |
| 1650 | * from the proxy. Maintenance servers are ignored. It stores the |
| 1651 | * <reason> if non-null as the reason for going down or the available data |
| 1652 | * from the check struct to recompute this reason later. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1653 | * up. Note that it makes use of the trash to build the log strings, so <reason> |
| 1654 | * must not be placed there. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1655 | * |
| 1656 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1657 | */ |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1658 | 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] | 1659 | { |
| 1660 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1661 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1662 | if (s->cur_admin & SRV_ADMF_MAINT) |
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 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1666 | return; |
| 1667 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1668 | s->next_state = SRV_ST_STOPPING; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1669 | *s->op_st_chg.reason = 0; |
| 1670 | s->op_st_chg.status = -1; |
| 1671 | if (reason) { |
| 1672 | strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason)); |
| 1673 | } |
| 1674 | else if (check) { |
Willy Tarreau | 358847f | 2017-11-20 21:33:21 +0100 | [diff] [blame] | 1675 | 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] | 1676 | s->op_st_chg.code = check->code; |
| 1677 | s->op_st_chg.status = check->status; |
| 1678 | s->op_st_chg.duration = check->duration; |
| 1679 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1680 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1681 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1682 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1683 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1684 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1685 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1686 | srv_set_stopping(srv, NULL, NULL); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1687 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1688 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1689 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1690 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1691 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1692 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1693 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1694 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1695 | * 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] | 1696 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1697 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1698 | * |
| 1699 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1700 | */ |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1701 | 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] | 1702 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1703 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1704 | |
| 1705 | if (!mode) |
| 1706 | return; |
| 1707 | |
| 1708 | /* 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] | 1709 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1710 | return; |
| 1711 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1712 | s->next_admin |= mode; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1713 | if (cause) |
| 1714 | strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause)); |
| 1715 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1716 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1717 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1718 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1719 | /* 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] | 1720 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1721 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1722 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1723 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1724 | /* compute the inherited flag to propagate */ |
| 1725 | if (mode & SRV_ADMF_MAINT) |
| 1726 | mode = SRV_ADMF_IMAINT; |
| 1727 | else if (mode & SRV_ADMF_DRAIN) |
| 1728 | mode = SRV_ADMF_IDRAIN; |
| 1729 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1730 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1731 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1732 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1733 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1734 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1737 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1738 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1739 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1740 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1741 | * 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] | 1742 | * |
| 1743 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1744 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1745 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1746 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1747 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1748 | |
| 1749 | if (!mode) |
| 1750 | return; |
| 1751 | |
| 1752 | /* 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] | 1753 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1754 | return; |
| 1755 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1756 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1757 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1758 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1759 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1760 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1761 | /* 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] | 1762 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1763 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1764 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1766 | if (mode & SRV_ADMF_MAINT) |
| 1767 | mode = SRV_ADMF_IMAINT; |
| 1768 | else if (mode & SRV_ADMF_DRAIN) |
| 1769 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1770 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1771 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1772 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1773 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1774 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1775 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1778 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1779 | * upon startup so that inherited states are correct. |
| 1780 | */ |
| 1781 | static void srv_propagate_admin_state(struct server *srv) |
| 1782 | { |
| 1783 | struct server *srv2; |
| 1784 | |
| 1785 | if (!srv->trackers) |
| 1786 | return; |
| 1787 | |
| 1788 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1789 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1790 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1791 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1792 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1793 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1794 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1795 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1800 | * Only servers *not* tracking another one are considered, because other |
| 1801 | * ones will be handled when the server they track is visited. |
| 1802 | */ |
| 1803 | void srv_compute_all_admin_states(struct proxy *px) |
| 1804 | { |
| 1805 | struct server *srv; |
| 1806 | |
| 1807 | for (srv = px->srv; srv; srv = srv->next) { |
| 1808 | if (srv->track) |
| 1809 | continue; |
| 1810 | srv_propagate_admin_state(srv); |
| 1811 | } |
| 1812 | } |
| 1813 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1814 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1815 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1816 | * all code contributors. |
| 1817 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1818 | * the config parser can report an appropriate error when a known keyword was |
| 1819 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1820 | * 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] | 1821 | */ |
| 1822 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1823 | { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1824 | { "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] | 1825 | { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */ |
| 1826 | { "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] | 1827 | { "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] | 1828 | { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1829 | { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1830 | { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */ |
| 1831 | { "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] | 1832 | { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */ |
| 1833 | { "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] | 1834 | { "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] | 1835 | { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1836 | { "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] | 1837 | { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */ |
| 1838 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1839 | { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */ |
| 1840 | { "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] | 1841 | { "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] | 1842 | { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */ |
| 1843 | { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */ |
| 1844 | { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */ |
| 1845 | { "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] | 1846 | { "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 */ |
| 1847 | { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */ |
| 1848 | { "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] | 1849 | { "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] | 1850 | { "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] | 1851 | { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */ |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 1852 | { "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] | 1853 | { "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] | 1854 | { "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] | 1855 | { "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] | 1856 | { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1857 | { "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] | 1858 | { "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] | 1859 | { "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] | 1860 | { "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] | 1861 | { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1862 | { "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] | 1863 | { "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] | 1864 | { "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] | 1865 | { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */ |
| 1866 | { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */ |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1867 | { NULL, NULL, 0 }, |
| 1868 | }}; |
| 1869 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1870 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1871 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1872 | /* 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] | 1873 | * 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] | 1874 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1875 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1876 | * |
| 1877 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1878 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1879 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1880 | { |
| 1881 | struct proxy *px = sv->proxy; |
| 1882 | unsigned w; |
| 1883 | |
| 1884 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 1885 | /* go to full throttle if the slowstart interval is reached */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1886 | if (sv->next_state == SRV_ST_STARTING) |
| 1887 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1891 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1892 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1893 | 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] | 1894 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 1895 | else |
| 1896 | w = px->lbprm.wdiv; |
| 1897 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1898 | 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] | 1899 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1900 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1901 | if (must_update) |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1902 | srv_update_status(sv); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1905 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1906 | * Parses weight_str and configures sv accordingly. |
| 1907 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1908 | * |
| 1909 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1910 | */ |
| 1911 | const char *server_parse_weight_change_request(struct server *sv, |
| 1912 | const char *weight_str) |
| 1913 | { |
| 1914 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1915 | long int w; |
| 1916 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1917 | |
| 1918 | px = sv->proxy; |
| 1919 | |
| 1920 | /* if the weight is terminated with '%', it is set relative to |
| 1921 | * the initial weight, otherwise it is absolute. |
| 1922 | */ |
| 1923 | if (!*weight_str) |
| 1924 | return "Require <weight> or <weight%>.\n"; |
| 1925 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1926 | w = strtol(weight_str, &end, 10); |
| 1927 | if (end == weight_str) |
| 1928 | return "Empty weight string empty or preceded by garbage"; |
| 1929 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1930 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1931 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1932 | /* Avoid integer overflow */ |
| 1933 | if (w > 25600) |
| 1934 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1935 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1936 | if (w > 256) |
| 1937 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1938 | } |
| 1939 | else if (w < 0 || w > 256) |
| 1940 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1941 | else if (end[0] != '\0') |
| 1942 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1943 | |
| 1944 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 1945 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 1946 | |
| 1947 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1948 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1949 | |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1953 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1954 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 1955 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1956 | * Returns: |
| 1957 | * - error string on error |
| 1958 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1959 | * |
| 1960 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1961 | */ |
| 1962 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1963 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1964 | { |
| 1965 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 1966 | |
| 1967 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1968 | srv_update_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1969 | return NULL; |
| 1970 | } |
| 1971 | if (inet_pton(AF_INET, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1972 | srv_update_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1973 | return NULL; |
| 1974 | } |
| 1975 | |
| 1976 | return "Could not understand IP address format.\n"; |
| 1977 | } |
| 1978 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1979 | /* |
| 1980 | * Must be called with the server lock held. |
| 1981 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1982 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 1983 | const char *maxconn_str) |
| 1984 | { |
| 1985 | long int v; |
| 1986 | char *end; |
| 1987 | |
| 1988 | if (!*maxconn_str) |
| 1989 | return "Require <maxconn>.\n"; |
| 1990 | |
| 1991 | v = strtol(maxconn_str, &end, 10); |
| 1992 | if (end == maxconn_str) |
| 1993 | return "maxconn string empty or preceded by garbage"; |
| 1994 | else if (end[0] != '\0') |
| 1995 | return "Trailing garbage in maxconn string"; |
| 1996 | |
| 1997 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 1998 | sv->maxconn = sv->minconn = v; |
| 1999 | } else { // dynamic maxconn |
| 2000 | sv->maxconn = v; |
| 2001 | } |
| 2002 | |
| 2003 | if (may_dequeue_tasks(sv, sv->proxy)) |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 2004 | process_srv_queue(sv); |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 2005 | |
| 2006 | return NULL; |
| 2007 | } |
| 2008 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2009 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 2010 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2011 | { |
| 2012 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2013 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2014 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2015 | NULL, |
| 2016 | }; |
| 2017 | |
| 2018 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 2019 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2020 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2021 | 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] | 2022 | } |
| 2023 | |
William Lallemand | 0d05867 | 2022-03-16 15:44:42 +0100 | [diff] [blame] | 2024 | 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] | 2025 | { |
| 2026 | struct sample_expr *expr; |
| 2027 | |
| 2028 | 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] | 2029 | if (!expr) { |
| 2030 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 2031 | return ERR_ALERT | ERR_FATAL; |
| 2032 | } |
| 2033 | |
| 2034 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 2035 | memprintf(err, "error detected while parsing sni expression : " |
| 2036 | " fetch method '%s' extracts information from '%s', " |
Amaury Denoyelle | c008a63 | 2021-05-28 11:01:22 +0200 | [diff] [blame] | 2037 | "none of which is available here.", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2038 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2039 | return ERR_ALERT | ERR_FATAL; |
| 2040 | } |
| 2041 | |
| 2042 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 2043 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2044 | newsrv->ssl_ctx.sni = expr; |
| 2045 | |
| 2046 | return 0; |
| 2047 | } |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2048 | |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2049 | 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] | 2050 | { |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2051 | char *msg = "error encountered while processing "; |
| 2052 | char *quote = "'"; |
| 2053 | char *token = args[cur_arg]; |
| 2054 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2055 | if (err && *err) { |
| 2056 | indent_msg(err, 2); |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2057 | msg = *err; |
| 2058 | quote = ""; |
| 2059 | token = ""; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2060 | } |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2061 | |
| 2062 | if (err_code & ERR_WARN && !(err_code & ERR_ALERT)) |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2063 | 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] | 2064 | else |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2065 | 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] | 2066 | } |
| 2067 | |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2068 | 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] | 2069 | { |
| 2070 | int range_sz; |
| 2071 | |
| 2072 | range_sz = src->conn_src.sport_range->size; |
| 2073 | if (range_sz > 0) { |
| 2074 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 2075 | if (srv->conn_src.sport_range != NULL) { |
| 2076 | int i; |
| 2077 | |
| 2078 | for (i = 0; i < range_sz; i++) { |
| 2079 | srv->conn_src.sport_range->ports[i] = |
| 2080 | src->conn_src.sport_range->ports[i]; |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | /* |
| 2087 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 2088 | */ |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2089 | 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] | 2090 | { |
| 2091 | srv->conn_src.opts = src->conn_src.opts; |
| 2092 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 2093 | |
| 2094 | /* Source port range copy. */ |
| 2095 | if (src->conn_src.sport_range != NULL) |
| 2096 | srv_conn_src_sport_range_cpy(srv, src); |
| 2097 | |
| 2098 | #ifdef CONFIG_HAP_TRANSPARENT |
| 2099 | if (src->conn_src.bind_hdr_name != NULL) { |
| 2100 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 2101 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 2102 | } |
| 2103 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 2104 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 2105 | #endif |
| 2106 | if (src->conn_src.iface_name != NULL) |
| 2107 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
| 2108 | } |
| 2109 | |
| 2110 | /* |
| 2111 | * Copy <src> server SSL settings to <srv> server allocating |
| 2112 | * everything needed. |
| 2113 | */ |
| 2114 | #if defined(USE_OPENSSL) |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2115 | 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] | 2116 | { |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2117 | /* <src> is the current proxy's default server and SSL is enabled */ |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2118 | BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */ |
| 2119 | |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2120 | if (src == &srv->proxy->defsrv && src->use_ssl == 1) |
| 2121 | srv->flags |= SRV_F_DEFSRV_USE_SSL; |
| 2122 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2123 | if (src->ssl_ctx.ca_file != NULL) |
| 2124 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 2125 | if (src->ssl_ctx.crl_file != NULL) |
| 2126 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2127 | if (src->ssl_ctx.client_crt != NULL) |
| 2128 | 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] | 2129 | |
| 2130 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 2131 | |
Remi Tricot-Le Breton | 0498fa4 | 2021-07-13 18:28:22 +0200 | [diff] [blame] | 2132 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2133 | if (src->ssl_ctx.verify_host != NULL) |
| 2134 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 2135 | if (src->ssl_ctx.ciphers != NULL) |
| 2136 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 2e8d52f | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 2137 | if (src->ssl_ctx.options) |
| 2138 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 2139 | if (src->ssl_ctx.methods.flags) |
| 2140 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 2141 | if (src->ssl_ctx.methods.min) |
| 2142 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 2143 | if (src->ssl_ctx.methods.max) |
| 2144 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 2145 | |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 2146 | if (src->ssl_ctx.ciphersuites != NULL) |
| 2147 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2148 | if (src->sni_expr != NULL) |
| 2149 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2150 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2151 | if (src->ssl_ctx.alpn_str) { |
| 2152 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 2153 | if (srv->ssl_ctx.alpn_str) { |
| 2154 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 2155 | src->ssl_ctx.alpn_len); |
| 2156 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 2157 | } |
| 2158 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2159 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2160 | if (src->ssl_ctx.npn_str) { |
| 2161 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 2162 | if (srv->ssl_ctx.npn_str) { |
| 2163 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 2164 | src->ssl_ctx.npn_len); |
| 2165 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 2166 | } |
| 2167 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2168 | } |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2169 | |
| 2170 | /* Activate ssl on server <s>. |
| 2171 | * do nothing if there is no change to apply |
| 2172 | * |
| 2173 | * Must be called with the server lock held. |
| 2174 | */ |
| 2175 | void srv_set_ssl(struct server *s, int use_ssl) |
| 2176 | { |
| 2177 | if (s->use_ssl == use_ssl) |
| 2178 | return; |
| 2179 | |
| 2180 | s->use_ssl = use_ssl; |
| 2181 | if (s->use_ssl) |
| 2182 | s->xprt = xprt_get(XPRT_SSL); |
| 2183 | else |
William Dauchy | a087f87 | 2022-01-06 16:57:15 +0100 | [diff] [blame] | 2184 | s->xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | #endif /* USE_OPENSSL */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2188 | |
| 2189 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2190 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2191 | * 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] | 2192 | * 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] | 2193 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2194 | 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] | 2195 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2196 | char *hostname_dn; |
| 2197 | int hostname_len, hostname_dn_len; |
| 2198 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2199 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2200 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2201 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2202 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2203 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 2204 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 2205 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2206 | if (hostname_dn_len == -1) |
| 2207 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2208 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2209 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2210 | free(srv->hostname); |
| 2211 | free(srv->hostname_dn); |
| 2212 | srv->hostname = strdup(hostname); |
| 2213 | srv->hostname_dn = strdup(hostname_dn); |
| 2214 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2215 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2216 | goto err; |
| 2217 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2218 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2219 | |
| 2220 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2221 | ha_free(&srv->hostname); |
| 2222 | ha_free(&srv->hostname_dn); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2223 | return -1; |
| 2224 | } |
| 2225 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2226 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2227 | * Copy <src> server settings to <srv> server allocating |
| 2228 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2229 | * This function is not supposed to be called at any time, but only |
| 2230 | * during server settings parsing or during server allocations from |
| 2231 | * a server template, and just after having calloc()'ed a new server. |
| 2232 | * So, <src> may only be a default server (when parsing server settings) |
| 2233 | * or a server template (during server allocations from a server template). |
| 2234 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 2235 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2236 | */ |
Christopher Faulet | b32cb9b | 2022-08-03 11:28:08 +0200 | [diff] [blame] | 2237 | 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] | 2238 | { |
| 2239 | /* Connection source settings copy */ |
| 2240 | srv_conn_src_cpy(srv, src); |
| 2241 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2242 | if (srv_tmpl) { |
| 2243 | srv->addr = src->addr; |
| 2244 | srv->svc_port = src->svc_port; |
| 2245 | } |
| 2246 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2247 | srv->pp_opts = src->pp_opts; |
| 2248 | if (src->rdr_pfx != NULL) { |
| 2249 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 2250 | srv->rdr_len = src->rdr_len; |
| 2251 | } |
| 2252 | if (src->cookie != NULL) { |
| 2253 | srv->cookie = strdup(src->cookie); |
| 2254 | srv->cklen = src->cklen; |
| 2255 | } |
| 2256 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | 2444108 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 2257 | srv->check.addr = src->check.addr; |
| 2258 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2259 | srv->check.use_ssl = src->check.use_ssl; |
| 2260 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 2261 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 2262 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 2263 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2264 | /* Note: 'flags' field has potentially been already initialized. */ |
| 2265 | srv->flags |= src->flags; |
| 2266 | srv->do_check = src->do_check; |
| 2267 | srv->do_agent = src->do_agent; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2268 | srv->check.inter = src->check.inter; |
| 2269 | srv->check.fastinter = src->check.fastinter; |
| 2270 | srv->check.downinter = src->check.downinter; |
| 2271 | srv->agent.use_ssl = src->agent.use_ssl; |
| 2272 | srv->agent.port = src->agent.port; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2273 | |
| 2274 | if (src->agent.tcpcheck_rules) { |
| 2275 | srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules)); |
| 2276 | if (srv->agent.tcpcheck_rules) { |
| 2277 | srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags; |
| 2278 | srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list; |
| 2279 | LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars); |
| 2280 | dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars, |
| 2281 | &src->agent.tcpcheck_rules->preset_vars); |
| 2282 | } |
| 2283 | } |
| 2284 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2285 | srv->agent.inter = src->agent.inter; |
| 2286 | srv->agent.fastinter = src->agent.fastinter; |
| 2287 | srv->agent.downinter = src->agent.downinter; |
| 2288 | srv->maxqueue = src->maxqueue; |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 2289 | srv->ws = src->ws; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2290 | srv->minconn = src->minconn; |
| 2291 | srv->maxconn = src->maxconn; |
| 2292 | srv->slowstart = src->slowstart; |
| 2293 | srv->observe = src->observe; |
| 2294 | srv->onerror = src->onerror; |
| 2295 | srv->onmarkeddown = src->onmarkeddown; |
| 2296 | srv->onmarkedup = src->onmarkedup; |
| 2297 | if (src->trackit != NULL) |
| 2298 | srv->trackit = strdup(src->trackit); |
| 2299 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 2300 | srv->uweight = srv->iweight = src->iweight; |
| 2301 | |
| 2302 | srv->check.send_proxy = src->check.send_proxy; |
| 2303 | /* health: up, but will fall down at first failure */ |
| 2304 | srv->check.rise = srv->check.health = src->check.rise; |
| 2305 | srv->check.fall = src->check.fall; |
| 2306 | |
| 2307 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2308 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 2309 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 2310 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2311 | srv->check.state |= CHK_ST_PAUSED; |
| 2312 | srv->check.health = 0; |
| 2313 | } |
| 2314 | |
| 2315 | /* health: up but will fall down at first failure */ |
| 2316 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 2317 | srv->agent.fall = src->agent.fall; |
| 2318 | |
| 2319 | if (src->resolvers_id != NULL) |
| 2320 | srv->resolvers_id = strdup(src->resolvers_id); |
Emeric Brun | 21fbeed | 2020-12-23 18:01:04 +0100 | [diff] [blame] | 2321 | srv->resolv_opts.family_prio = src->resolv_opts.family_prio; |
| 2322 | srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip; |
| 2323 | srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight; |
| 2324 | if (srv->resolv_opts.family_prio == AF_UNSPEC) |
| 2325 | srv->resolv_opts.family_prio = AF_INET6; |
| 2326 | memcpy(srv->resolv_opts.pref_net, |
| 2327 | src->resolv_opts.pref_net, |
| 2328 | sizeof srv->resolv_opts.pref_net); |
| 2329 | 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] | 2330 | |
| 2331 | srv->init_addr_methods = src->init_addr_methods; |
| 2332 | srv->init_addr = src->init_addr; |
| 2333 | #if defined(USE_OPENSSL) |
| 2334 | srv_ssl_settings_cpy(srv, src); |
| 2335 | #endif |
| 2336 | #ifdef TCP_USER_TIMEOUT |
| 2337 | srv->tcp_ut = src->tcp_ut; |
| 2338 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2339 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 2340 | srv->pool_purge_delay = src->pool_purge_delay; |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 2341 | srv->low_idle_conns = src->low_idle_conns; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 2342 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 2343 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2344 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2345 | if (srv_tmpl) |
| 2346 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2347 | |
| 2348 | srv->check.via_socks4 = src->check.via_socks4; |
| 2349 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2350 | } |
| 2351 | |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 2352 | /* allocate a server and attach it to the global servers_list. Returns |
| 2353 | * the server on success, otherwise NULL. |
| 2354 | */ |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 2355 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2356 | { |
| 2357 | struct server *srv; |
| 2358 | |
| 2359 | srv = calloc(1, sizeof *srv); |
| 2360 | if (!srv) |
| 2361 | return NULL; |
| 2362 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2363 | srv_take(srv); |
| 2364 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2365 | srv->obj_type = OBJ_TYPE_SERVER; |
| 2366 | srv->proxy = proxy; |
Willy Tarreau | cdc83e0 | 2021-06-23 16:11:02 +0200 | [diff] [blame] | 2367 | queue_init(&srv->queue, proxy, srv); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2368 | LIST_APPEND(&servers_list, &srv->global_list); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2369 | LIST_INIT(&srv->srv_rec_item); |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 2370 | LIST_INIT(&srv->ip_rec_item); |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2371 | MT_LIST_INIT(&srv->prev_deleted); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2372 | event_hdl_sub_list_init(&srv->e_subs); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 2373 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2374 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2375 | srv->last_change = now.tv_sec; |
| 2376 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2377 | srv->check.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2378 | srv->check.status = HCHK_STATUS_INI; |
| 2379 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 2380 | srv->check.proxy = proxy; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 2381 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2382 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2383 | srv->agent.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2384 | srv->agent.status = HCHK_STATUS_INI; |
| 2385 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 2386 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2387 | 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] | 2388 | #if defined(USE_QUIC) |
| 2389 | srv->cids = EB_ROOT_UNIQUE; |
| 2390 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2391 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2392 | srv->extra_counters = NULL; |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 2393 | #ifdef USE_OPENSSL |
| 2394 | HA_RWLOCK_INIT(&srv->ssl_ctx.lock); |
| 2395 | #endif |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2396 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2397 | /* please don't put default server settings here, they are set in |
Willy Tarreau | 144289b | 2021-02-12 08:19:01 +0100 | [diff] [blame] | 2398 | * proxy_preset_defaults(). |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2399 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2400 | return srv; |
| 2401 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2402 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2403 | /* Increment the server refcount. */ |
| 2404 | void srv_take(struct server *srv) |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2405 | { |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2406 | HA_ATOMIC_INC(&srv->refcount); |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2407 | } |
| 2408 | |
Amaury Denoyelle | 13f2e2c | 2021-08-09 15:08:54 +0200 | [diff] [blame] | 2409 | /* Deallocate a server <srv> and its member. <srv> must be allocated. For |
| 2410 | * dynamic servers, its refcount is decremented first. The free operations are |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2411 | * conducted only if the refcount is nul. |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2412 | * |
| 2413 | * 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] | 2414 | * when calling srv_drop on the list of servers. |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2415 | */ |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2416 | struct server *srv_drop(struct server *srv) |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2417 | { |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2418 | struct server *next = NULL; |
| 2419 | |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2420 | if (!srv) |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2421 | goto end; |
| 2422 | |
| 2423 | next = srv->next; |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2424 | |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2425 | /* For dynamic servers, decrement the reference counter. Only free the |
| 2426 | * server when reaching zero. |
| 2427 | */ |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2428 | if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1)) |
| 2429 | goto end; |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2430 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2431 | /* make sure we are removed from our 'next->prev_deleted' list |
| 2432 | * This doesn't require full thread isolation as we're using mt lists |
| 2433 | * However this could easily be turned into regular list if required |
| 2434 | * (with the proper use of thread isolation) |
| 2435 | */ |
| 2436 | MT_LIST_DELETE(&srv->prev_deleted); |
| 2437 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2438 | task_destroy(srv->warmup); |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 2439 | task_destroy(srv->srvrq_check); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2440 | |
| 2441 | free(srv->id); |
| 2442 | free(srv->cookie); |
| 2443 | free(srv->hostname); |
| 2444 | free(srv->hostname_dn); |
| 2445 | free((char*)srv->conf.file); |
| 2446 | free(srv->per_thr); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 2447 | free(srv->per_tgrp); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2448 | free(srv->curr_idle_thr); |
| 2449 | free(srv->resolvers_id); |
| 2450 | free(srv->addr_node.key); |
Amaury Denoyelle | fb24794 | 2021-04-20 16:48:22 +0200 | [diff] [blame] | 2451 | free(srv->lb_nodes); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2452 | |
Aurelien DARRAGON | b5ee8be | 2023-03-09 14:28:00 +0100 | [diff] [blame] | 2453 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2454 | xprt_get(XPRT_SSL)->destroy_srv(srv); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2455 | HA_SPIN_DESTROY(&srv->lock); |
| 2456 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2457 | LIST_DELETE(&srv->global_list); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2458 | event_hdl_sub_list_destroy(&srv->e_subs); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2459 | |
| 2460 | EXTRA_COUNTERS_FREE(srv->extra_counters); |
| 2461 | |
Tim Duesterhus | 025b93e | 2021-11-04 21:03:52 +0100 | [diff] [blame] | 2462 | ha_free(&srv); |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2463 | |
| 2464 | end: |
| 2465 | return next; |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2466 | } |
| 2467 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 2468 | /* Remove a server <srv> from a tracking list if <srv> is tracking another |
| 2469 | * server. No special care is taken if <srv> is tracked itself by another one : |
| 2470 | * this situation should be avoided by the caller. |
| 2471 | * |
| 2472 | * Not thread-safe. |
| 2473 | */ |
| 2474 | static void release_server_track(struct server *srv) |
| 2475 | { |
| 2476 | struct server *strack = srv->track; |
| 2477 | struct server **base; |
| 2478 | |
| 2479 | if (!strack) |
| 2480 | return; |
| 2481 | |
| 2482 | for (base = &strack->trackers; *base; base = &((*base)->tracknext)) { |
| 2483 | if (*base == srv) { |
| 2484 | *base = srv->tracknext; |
| 2485 | return; |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /* srv not found on the tracking list, this should never happen */ |
| 2490 | BUG_ON(!*base); |
| 2491 | } |
| 2492 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2493 | /* |
| 2494 | * Parse as much as possible such a range string argument: low[-high] |
| 2495 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2496 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2497 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2498 | * Returns 0 if succeeded, -1 if not. |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2499 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2500 | static int _srv_parse_tmpl_range(struct server *srv, const char *arg, |
| 2501 | int *nb_low, int *nb_high) |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2502 | { |
| 2503 | char *nb_high_arg; |
| 2504 | |
| 2505 | *nb_high = 0; |
| 2506 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2507 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2508 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2509 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2510 | *nb_high_arg++ = '\0'; |
| 2511 | *nb_high = atoi(nb_high_arg); |
| 2512 | } |
| 2513 | else { |
| 2514 | *nb_high += *nb_low; |
| 2515 | *nb_low = 1; |
| 2516 | } |
| 2517 | |
| 2518 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 2519 | return -1; |
| 2520 | |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2524 | /* Parse as much as possible such a range string argument: low[-high] |
| 2525 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2526 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2527 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2528 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2529 | * initialize a new server on startup. |
| 2530 | * |
| 2531 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2532 | * Returns 0 if succeeded, -1 if not. |
| 2533 | */ |
| 2534 | static inline void _srv_parse_set_id_from_prefix(struct server *srv, |
| 2535 | const char *prefix, int nb) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2536 | { |
| 2537 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 2538 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2539 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2540 | } |
| 2541 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2542 | /* 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] | 2543 | * Note that a server template is a special server with |
| 2544 | * a few different parameters than a server which has |
| 2545 | * been parsed mostly the same way as a server. |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2546 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2547 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2548 | * initialize a new server on startup. |
| 2549 | * |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2550 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2551 | * 'srv' template included. |
| 2552 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2553 | 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] | 2554 | { |
| 2555 | int i; |
| 2556 | struct server *newsrv; |
| 2557 | |
| 2558 | 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] | 2559 | newsrv = new_server(px); |
| 2560 | if (!newsrv) |
| 2561 | goto err; |
| 2562 | |
Christopher Faulet | 75bef00 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 2563 | newsrv->conf.file = strdup(srv->conf.file); |
| 2564 | newsrv->conf.line = srv->conf.line; |
| 2565 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2566 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2567 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2568 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2569 | if (newsrv->sni_expr) { |
| 2570 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 2571 | if (!newsrv->ssl_ctx.sni) |
| 2572 | goto err; |
| 2573 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2574 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2575 | /* append to list of servers available to receive an hostname */ |
Emeric Brun | caef19e | 2021-06-14 10:02:18 +0200 | [diff] [blame] | 2576 | if (newsrv->srvrq) |
| 2577 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2578 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2579 | /* Set this new server ID. */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2580 | _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] | 2581 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2582 | /* Linked backwards first. This will be restablished after parsing. */ |
| 2583 | newsrv->next = px->srv; |
| 2584 | px->srv = newsrv; |
| 2585 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2586 | _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] | 2587 | |
| 2588 | return i - srv->tmpl_info.nb_low; |
| 2589 | |
| 2590 | err: |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2591 | _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] | 2592 | if (newsrv) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2593 | release_sample_expr(newsrv->ssl_ctx.sni); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2594 | free_check(&newsrv->agent); |
| 2595 | free_check(&newsrv->check); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2596 | LIST_DELETE(&newsrv->global_list); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2597 | } |
| 2598 | free(newsrv); |
| 2599 | return i - srv->tmpl_info.nb_low; |
| 2600 | } |
| 2601 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2602 | /* Allocate a new server pointed by <srv> and try to parse the first arguments |
| 2603 | * in <args> as an address for a server or an address-range for a template or |
| 2604 | * nothing for a default-server. <cur_arg> is incremented to the next argument. |
| 2605 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2606 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2607 | * initialize a new server on startup. |
| 2608 | * |
| 2609 | * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case |
| 2610 | * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated, |
| 2611 | * <srv> will be set to NULL. |
| 2612 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2613 | static int _srv_parse_init(struct server **srv, char **args, int *cur_arg, |
| 2614 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2615 | int parse_flags) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2616 | { |
| 2617 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2618 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2619 | int err_code = 0; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2620 | char *fqdn = NULL; |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2621 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2622 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2623 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2624 | *srv = NULL; |
| 2625 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2626 | /* There is no mandatory first arguments for default server. */ |
| 2627 | if (parse_flags & SRV_PARSE_PARSE_ADDR) { |
| 2628 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2629 | if (!*args[3]) { |
| 2630 | /* 'server-template' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2631 | ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n", |
| 2632 | args[0]); |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2633 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2634 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2637 | err = invalid_prefix_char(args[1]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2638 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2639 | else { |
| 2640 | if (!*args[2]) { |
| 2641 | /* 'server' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2642 | ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2643 | args[0]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2644 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2645 | goto out; |
| 2646 | } |
| 2647 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2648 | err = invalid_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2649 | } |
| 2650 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2651 | if (err) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2652 | ha_alert("character '%c' is not permitted in %s %s '%s'.\n", |
| 2653 | *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2654 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2655 | goto out; |
| 2656 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2657 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2658 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2659 | *cur_arg = 2; |
| 2660 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2661 | /* Parse server-template <nb | range> arg. */ |
| 2662 | 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] | 2663 | ha_alert("Wrong %s number or range arg '%s'.\n", |
| 2664 | args[0], args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2665 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2666 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2667 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2668 | (*cur_arg)++; |
| 2669 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2670 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2671 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2672 | struct sockaddr_storage *sk; |
| 2673 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2674 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2675 | *srv = newsrv = new_server(curproxy); |
| 2676 | if (!newsrv) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2677 | ha_alert("out of memory.\n"); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2678 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2679 | goto out; |
| 2680 | } |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2681 | register_parsing_obj(&newsrv->obj_type); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2682 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2683 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2684 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2685 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2686 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2687 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2688 | if (parse_flags & SRV_PARSE_DYNAMIC) |
| 2689 | newsrv->flags |= SRV_F_DYNAMIC; |
| 2690 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2691 | /* Note: for a server template, its id is its prefix. |
| 2692 | * This is a temporary id which will be used for server allocations to come |
| 2693 | * after parsing. |
| 2694 | */ |
| 2695 | if (!(parse_flags & SRV_PARSE_TEMPLATE)) |
| 2696 | newsrv->id = strdup(args[1]); |
| 2697 | else |
| 2698 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2699 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 2700 | /* revision defaults to 0 */ |
| 2701 | newsrv->rid = 0; |
| 2702 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2703 | /* several ways to check the port component : |
| 2704 | * - IP => port=+0, relative (IPv4 only) |
| 2705 | * - IP: => port=+0, relative |
| 2706 | * - IP:N => port=N, absolute |
| 2707 | * - IP:+N => port=+N, relative |
| 2708 | * - IP:-N => port=-N, relative |
| 2709 | */ |
| 2710 | if (!(parse_flags & SRV_PARSE_PARSE_ADDR)) |
| 2711 | goto skip_addr; |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2712 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2713 | sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2714 | &errmsg, NULL, &fqdn, |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2715 | (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); |
| 2716 | if (!sk) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2717 | ha_alert("%s\n", errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2718 | err_code |= ERR_ALERT | ERR_FATAL; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2719 | ha_free(&errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2720 | goto out; |
| 2721 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2722 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2723 | if (!port1 || !port2) { |
| 2724 | /* no port specified, +offset, -offset */ |
| 2725 | newsrv->flags |= SRV_F_MAPPORTS; |
| 2726 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2727 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2728 | /* save hostname and create associated name resolution */ |
| 2729 | if (fqdn) { |
| 2730 | if (fqdn[0] == '_') { /* SRV record */ |
| 2731 | /* Check if a SRV request already exists, and if not, create it */ |
| 2732 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2733 | newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn); |
| 2734 | if (newsrv->srvrq == NULL) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2735 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2736 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2737 | } |
Christopher Faulet | 81ba74a | 2021-06-29 20:52:35 +0200 | [diff] [blame] | 2738 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2739 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2740 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2741 | ha_alert("Can't create DNS resolution for server '%s'\n", |
| 2742 | newsrv->id); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2743 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2744 | goto out; |
| 2745 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2746 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2747 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2748 | newsrv->addr = *sk; |
| 2749 | newsrv->svc_port = port; |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 2750 | /* |
| 2751 | * we don't need to lock the server here, because |
| 2752 | * we are in the process of initializing. |
| 2753 | * |
| 2754 | * Note that the server is not attached into the proxy tree if |
| 2755 | * this is a dynamic server. |
| 2756 | */ |
| 2757 | srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC)); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2758 | |
Willy Tarreau | 14e7f29 | 2021-10-27 17:41:07 +0200 | [diff] [blame] | 2759 | if (!newsrv->srvrq && !newsrv->hostname && |
| 2760 | !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2761 | ha_alert("Unknown protocol family %d '%s'\n", |
| 2762 | newsrv->addr.ss_family, args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2763 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2764 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2765 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2766 | |
| 2767 | (*cur_arg)++; |
| 2768 | skip_addr: |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2769 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2770 | /* Copy default server settings to new server */ |
| 2771 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
| 2772 | } else { |
| 2773 | /* Initialize dynamic server weight to 1 */ |
| 2774 | newsrv->uweight = newsrv->iweight = 1; |
| 2775 | |
| 2776 | /* A dynamic server is disabled on startup */ |
| 2777 | newsrv->next_admin = SRV_ADMF_FMAINT; |
| 2778 | newsrv->next_state = SRV_ST_STOPPED; |
| 2779 | server_recalc_eweight(newsrv, 0); |
Amaury Denoyelle | fca1817 | 2021-07-22 16:03:36 +0200 | [diff] [blame] | 2780 | |
| 2781 | /* Set default values for checks */ |
| 2782 | newsrv->check.inter = DEF_CHKINTR; |
| 2783 | newsrv->check.rise = DEF_RISETIME; |
| 2784 | newsrv->check.fall = DEF_FALLTIME; |
| 2785 | |
| 2786 | newsrv->agent.inter = DEF_CHKINTR; |
| 2787 | newsrv->agent.rise = DEF_AGENT_RISETIME; |
| 2788 | newsrv->agent.fall = DEF_AGENT_FALLTIME; |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2789 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2790 | HA_SPIN_INIT(&newsrv->lock); |
| 2791 | } |
| 2792 | else { |
| 2793 | *srv = newsrv = &curproxy->defsrv; |
| 2794 | *cur_arg = 1; |
| 2795 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 2796 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2797 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2798 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2799 | free(fqdn); |
| 2800 | return 0; |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2801 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2802 | out: |
| 2803 | free(fqdn); |
| 2804 | return err_code; |
| 2805 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2806 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2807 | /* Parse the server keyword in <args>. |
| 2808 | * <cur_arg> is incremented beyond the keyword optional value. Note that this |
| 2809 | * might not be the case if an error is reported. |
| 2810 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2811 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2812 | * initialize a new server on startup. |
| 2813 | * |
| 2814 | * A mask of errors is returned. ERR_FATAL is set if the parsing should be |
| 2815 | * interrupted. |
| 2816 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2817 | static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg, |
| 2818 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2819 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2820 | { |
| 2821 | int err_code = 0; |
| 2822 | struct srv_kw *kw; |
| 2823 | const char *best; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2824 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2825 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2826 | kw = srv_find_kw(args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2827 | if (!kw) { |
| 2828 | best = srv_find_best_kw(args[*cur_arg]); |
| 2829 | if (best) |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2830 | ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n", |
| 2831 | args[*cur_arg], best, |
| 2832 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2833 | " Hint: no address was expected for this server."); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2834 | else |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2835 | ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg], |
| 2836 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2837 | " Hint: no address was expected for this server."); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2838 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2839 | return ERR_ALERT | ERR_FATAL; |
| 2840 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2841 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2842 | if (!kw->parse) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2843 | ha_alert("'%s' option is not implemented in this version (check build options)\n", |
| 2844 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2845 | err_code = ERR_ALERT | ERR_FATAL; |
| 2846 | goto out; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2847 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2848 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2849 | if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2850 | ha_alert("'%s' option is not accepted in default-server sections\n", |
| 2851 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2852 | err_code = ERR_ALERT; |
| 2853 | goto out; |
| 2854 | } |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2855 | else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2856 | ha_alert("'%s' option is not accepted for dynamic server\n", |
| 2857 | args[*cur_arg]); |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2858 | err_code |= ERR_ALERT; |
| 2859 | goto out; |
| 2860 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2861 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2862 | err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg); |
| 2863 | if (err_code) { |
| 2864 | display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg); |
| 2865 | free(errmsg); |
| 2866 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2867 | |
| 2868 | out: |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2869 | if (kw->skip != -1) |
| 2870 | *cur_arg += 1 + kw->skip; |
| 2871 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2872 | return err_code; |
| 2873 | } |
| 2874 | |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2875 | /* This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2876 | * initialize a new server on startup. |
| 2877 | */ |
| 2878 | static int _srv_parse_sni_expr_init(char **args, int cur_arg, |
| 2879 | struct server *srv, struct proxy *proxy, |
| 2880 | char **errmsg) |
| 2881 | { |
| 2882 | int ret; |
| 2883 | |
| 2884 | if (!srv->sni_expr) |
| 2885 | return 0; |
| 2886 | |
| 2887 | ret = server_parse_sni_expr(srv, proxy, errmsg); |
| 2888 | if (!ret) |
| 2889 | return 0; |
| 2890 | |
| 2891 | return ret; |
| 2892 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2893 | |
| 2894 | /* Server initializations finalization. |
| 2895 | * Initialize health check, agent check and SNI expression if enabled. |
| 2896 | * Must not be called for a default server instance. |
| 2897 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2898 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2899 | * initialize a new server on startup. |
| 2900 | */ |
| 2901 | static int _srv_parse_finalize(char **args, int cur_arg, |
| 2902 | struct server *srv, struct proxy *px, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2903 | int parse_flags) |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2904 | { |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2905 | int ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2906 | char *errmsg = NULL; |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2907 | |
| 2908 | if (srv->do_check && srv->trackit) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2909 | 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] | 2910 | return ERR_ALERT | ERR_FATAL; |
| 2911 | } |
| 2912 | |
| 2913 | if (srv->do_agent && !srv->agent.port) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2914 | ha_alert("server %s does not have agent port. Agent check has been disabled.\n", |
| 2915 | srv->id); |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2916 | return ERR_ALERT | ERR_FATAL; |
| 2917 | } |
| 2918 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2919 | if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) { |
| 2920 | if (errmsg) { |
| 2921 | ha_alert("%s\n", errmsg); |
| 2922 | free(errmsg); |
| 2923 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2924 | return ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2925 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2926 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2927 | /* A dynamic server is disabled on startup. It must not be counted as |
| 2928 | * an active backend entry. |
| 2929 | */ |
| 2930 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2931 | if (srv->flags & SRV_F_BACKUP) |
| 2932 | px->srv_bck++; |
| 2933 | else |
| 2934 | px->srv_act++; |
| 2935 | } |
| 2936 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2937 | srv_lb_commit_status(srv); |
| 2938 | |
| 2939 | return 0; |
| 2940 | } |
| 2941 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2942 | int parse_server(const char *file, int linenum, char **args, |
| 2943 | struct proxy *curproxy, const struct proxy *defproxy, |
| 2944 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2945 | { |
| 2946 | struct server *newsrv = NULL; |
| 2947 | int err_code = 0; |
| 2948 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2949 | int cur_arg; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2950 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2951 | set_usermsgs_ctx(file, linenum, NULL); |
| 2952 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2953 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) { |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2954 | ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2955 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2956 | goto out; |
| 2957 | } |
| 2958 | else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) { |
| 2959 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2960 | goto out; |
| 2961 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2962 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2963 | if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) == |
| 2964 | (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) { |
| 2965 | if (!*args[2]) |
| 2966 | return 0; |
| 2967 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2968 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2969 | err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2970 | parse_flags); |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 2971 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2972 | /* the servers are linked backwards first */ |
| 2973 | if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2974 | newsrv->next = curproxy->srv; |
| 2975 | curproxy->srv = newsrv; |
| 2976 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2977 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2978 | if (err_code & ERR_CODE) |
| 2979 | goto out; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2980 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2981 | newsrv->conf.file = strdup(file); |
| 2982 | newsrv->conf.line = linenum; |
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 | while (*args[cur_arg]) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2985 | err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2986 | parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2987 | if (err_code & ERR_FATAL) |
| 2988 | goto out; |
| 2989 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2990 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2991 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2992 | err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2993 | if (err_code & ERR_FATAL) |
| 2994 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2995 | } |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 2996 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2997 | if (parse_flags & SRV_PARSE_TEMPLATE) |
| 2998 | _srv_parse_tmpl_init(newsrv, curproxy); |
| 2999 | |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 3000 | /* If the server id is fixed, insert it in the proxy used_id tree. |
| 3001 | * This is needed to detect a later duplicate id via srv_parse_id. |
| 3002 | * |
| 3003 | * If no is specified, a dynamic one is generated in |
| 3004 | * check_config_validity. |
| 3005 | */ |
| 3006 | if (newsrv->flags & SRV_F_FORCED_ID) |
| 3007 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 3008 | |
Amaury Denoyelle | 24abb0c | 2021-05-07 15:13:51 +0200 | [diff] [blame] | 3009 | HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight, |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3010 | "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] | 3011 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3012 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3013 | return 0; |
| 3014 | |
| 3015 | out: |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3016 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3017 | return err_code; |
| 3018 | } |
| 3019 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 3020 | /* Returns a pointer to the first server matching either id <id>. |
| 3021 | * NULL is returned if no match is found. |
| 3022 | * the lookup is performed in the backend <bk> |
| 3023 | */ |
| 3024 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 3025 | { |
| 3026 | struct eb32_node *eb32; |
| 3027 | struct server *curserver; |
| 3028 | |
| 3029 | if (!bk || (id ==0)) |
| 3030 | return NULL; |
| 3031 | |
| 3032 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3033 | if (!(bk->cap & PR_CAP_BE)) |
| 3034 | return NULL; |
| 3035 | |
| 3036 | curserver = NULL; |
| 3037 | |
| 3038 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 3039 | if (eb32) |
| 3040 | curserver = container_of(eb32, struct server, conf.id); |
| 3041 | |
| 3042 | return curserver; |
| 3043 | } |
| 3044 | |
| 3045 | /* Returns a pointer to the first server matching either name <name>, or id |
| 3046 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 3047 | * the lookup is performed in the backend <bk> |
| 3048 | */ |
| 3049 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 3050 | { |
| 3051 | struct server *curserver; |
| 3052 | |
| 3053 | if (!bk || !name) |
| 3054 | return NULL; |
| 3055 | |
| 3056 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3057 | if (!(bk->cap & PR_CAP_BE)) |
| 3058 | return NULL; |
| 3059 | |
| 3060 | curserver = NULL; |
| 3061 | if (*name == '#') { |
| 3062 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 3063 | if (curserver) |
| 3064 | return curserver; |
| 3065 | } |
| 3066 | else { |
| 3067 | curserver = bk->srv; |
| 3068 | |
| 3069 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 3070 | curserver = curserver->next; |
| 3071 | |
| 3072 | if (curserver) |
| 3073 | return curserver; |
| 3074 | } |
| 3075 | |
| 3076 | return NULL; |
| 3077 | } |
| 3078 | |
| 3079 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 3080 | { |
| 3081 | struct server *byname; |
| 3082 | struct server *byid; |
| 3083 | |
| 3084 | if (!name && !id) |
| 3085 | return NULL; |
| 3086 | |
| 3087 | if (diff) |
| 3088 | *diff = 0; |
| 3089 | |
| 3090 | byname = byid = NULL; |
| 3091 | |
| 3092 | if (name) { |
| 3093 | byname = server_find_by_name(bk, name); |
| 3094 | if (byname && (!id || byname->puid == id)) |
| 3095 | return byname; |
| 3096 | } |
| 3097 | |
| 3098 | /* remaining possibilities : |
| 3099 | * - name not set |
| 3100 | * - name set but not found |
| 3101 | * - name found but ID doesn't match |
| 3102 | */ |
| 3103 | if (id) { |
| 3104 | byid = server_find_by_id(bk, id); |
| 3105 | if (byid) { |
| 3106 | if (byname) { |
| 3107 | /* use id only if forced by configuration */ |
| 3108 | if (byid->flags & SRV_F_FORCED_ID) { |
| 3109 | if (diff) |
| 3110 | *diff |= 2; |
| 3111 | return byid; |
| 3112 | } |
| 3113 | else { |
| 3114 | if (diff) |
| 3115 | *diff |= 1; |
| 3116 | return byname; |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | /* remaining possibilities: |
| 3121 | * - name not set |
| 3122 | * - name set but not found |
| 3123 | */ |
| 3124 | if (name && diff) |
| 3125 | *diff |= 2; |
| 3126 | return byid; |
| 3127 | } |
| 3128 | |
| 3129 | /* id bot found */ |
| 3130 | if (byname) { |
| 3131 | if (diff) |
| 3132 | *diff |= 1; |
| 3133 | return byname; |
| 3134 | } |
| 3135 | } |
| 3136 | |
| 3137 | return NULL; |
| 3138 | } |
| 3139 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3140 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3141 | * update a server's current IP address. |
| 3142 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3143 | * ip is in network format. |
| 3144 | * updater is a string which contains an information about the requester of the update. |
| 3145 | * updater is used if not NULL. |
| 3146 | * |
| 3147 | * 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] | 3148 | * |
| 3149 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3150 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3151 | 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] | 3152 | { |
Christopher Faulet | d6c6b5f | 2020-09-08 10:27:24 +0200 | [diff] [blame] | 3153 | /* save the new IP family & address if necessary */ |
| 3154 | switch (ip_sin_family) { |
| 3155 | case AF_INET: |
| 3156 | if (s->addr.ss_family == ip_sin_family && |
| 3157 | !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4)) |
| 3158 | return 0; |
| 3159 | break; |
| 3160 | case AF_INET6: |
| 3161 | if (s->addr.ss_family == ip_sin_family && |
| 3162 | !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16)) |
| 3163 | return 0; |
| 3164 | break; |
| 3165 | }; |
| 3166 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3167 | /* generates a log line and a warning on stderr */ |
| 3168 | if (1) { |
| 3169 | /* book enough space for both IPv4 and IPv6 */ |
| 3170 | char oldip[INET6_ADDRSTRLEN]; |
| 3171 | char newip[INET6_ADDRSTRLEN]; |
| 3172 | |
| 3173 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3174 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3175 | |
| 3176 | /* copy old IP address in a string */ |
| 3177 | switch (s->addr.ss_family) { |
| 3178 | case AF_INET: |
| 3179 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3180 | break; |
| 3181 | case AF_INET6: |
| 3182 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3183 | break; |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3184 | default: |
Willy Tarreau | fc458ec | 2023-04-07 18:11:39 +0200 | [diff] [blame] | 3185 | strlcpy2(oldip, "(none)", sizeof(oldip)); |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3186 | break; |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3187 | }; |
| 3188 | |
| 3189 | /* copy new IP address in a string */ |
| 3190 | switch (ip_sin_family) { |
| 3191 | case AF_INET: |
| 3192 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3193 | break; |
| 3194 | case AF_INET6: |
| 3195 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3196 | break; |
| 3197 | }; |
| 3198 | |
| 3199 | /* save log line into a buffer */ |
| 3200 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3201 | s->proxy->id, s->id, oldip, newip, updater); |
| 3202 | |
| 3203 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3204 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3205 | |
| 3206 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3207 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | /* save the new IP family */ |
| 3211 | s->addr.ss_family = ip_sin_family; |
| 3212 | /* save the new IP address */ |
| 3213 | switch (ip_sin_family) { |
| 3214 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3215 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3216 | break; |
| 3217 | case AF_INET6: |
| 3218 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3219 | break; |
| 3220 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3221 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3222 | srv_set_addr_desc(s, 1); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3223 | |
| 3224 | return 0; |
| 3225 | } |
| 3226 | |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 3227 | /* update agent health check address and port |
| 3228 | * addr can be ip4/ip6 or a hostname |
| 3229 | * if one error occurs, don't apply anything |
| 3230 | * must be called with the server lock held. |
| 3231 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3232 | 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] | 3233 | { |
| 3234 | struct sockaddr_storage sk; |
| 3235 | struct buffer *msg; |
| 3236 | int new_port; |
| 3237 | |
| 3238 | msg = get_trash_chunk(); |
| 3239 | chunk_reset(msg); |
| 3240 | |
| 3241 | if (!(s->agent.state & CHK_ST_ENABLED)) { |
| 3242 | chunk_strcat(msg, "agent checks are not enabled on this server"); |
| 3243 | goto out; |
| 3244 | } |
| 3245 | if (addr) { |
| 3246 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3247 | if (str2ip(addr, &sk) == NULL) { |
| 3248 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3249 | goto out; |
| 3250 | } |
| 3251 | } |
| 3252 | if (port) { |
| 3253 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3254 | chunk_appendf(msg, "provided port is not an integer"); |
| 3255 | goto out; |
| 3256 | } |
| 3257 | if (new_port < 0 || new_port > 65535) { |
| 3258 | chunk_appendf(msg, "provided port is invalid"); |
| 3259 | goto out; |
| 3260 | } |
| 3261 | } |
| 3262 | out: |
| 3263 | if (msg->data) |
| 3264 | return msg->area; |
| 3265 | else { |
| 3266 | if (addr) |
| 3267 | set_srv_agent_addr(s, &sk); |
| 3268 | if (port) |
| 3269 | set_srv_agent_port(s, new_port); |
| 3270 | } |
| 3271 | return NULL; |
| 3272 | } |
| 3273 | |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 3274 | /* update server health check address and port |
| 3275 | * addr must be ip4 or ip6, it won't be resolved |
| 3276 | * if one error occurs, don't apply anything |
| 3277 | * must be called with the server lock held. |
| 3278 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3279 | 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] | 3280 | { |
| 3281 | struct sockaddr_storage sk; |
| 3282 | struct buffer *msg; |
| 3283 | int new_port; |
| 3284 | |
| 3285 | msg = get_trash_chunk(); |
| 3286 | chunk_reset(msg); |
| 3287 | |
| 3288 | if (!(s->check.state & CHK_ST_ENABLED)) { |
| 3289 | chunk_strcat(msg, "health checks are not enabled on this server"); |
| 3290 | goto out; |
| 3291 | } |
| 3292 | if (addr) { |
| 3293 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3294 | if (str2ip2(addr, &sk, 0) == NULL) { |
| 3295 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3296 | goto out; |
| 3297 | } |
| 3298 | } |
| 3299 | if (port) { |
| 3300 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3301 | chunk_appendf(msg, "provided port is not an integer"); |
| 3302 | goto out; |
| 3303 | } |
| 3304 | if (new_port < 0 || new_port > 65535) { |
| 3305 | chunk_appendf(msg, "provided port is invalid"); |
| 3306 | goto out; |
| 3307 | } |
| 3308 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 3309 | if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) { |
| 3310 | chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use"); |
| 3311 | goto out; |
| 3312 | } |
| 3313 | } |
| 3314 | out: |
| 3315 | if (msg->data) |
| 3316 | return msg->area; |
| 3317 | else { |
| 3318 | if (addr) |
| 3319 | s->check.addr = sk; |
| 3320 | if (port) |
| 3321 | s->check.port = new_port; |
| 3322 | } |
| 3323 | return NULL; |
| 3324 | } |
| 3325 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3326 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3327 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3328 | * |
| 3329 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3330 | * returned by the function. |
| 3331 | * |
| 3332 | * The function first does the following, in that order: |
| 3333 | * - validates the new addr and/or port |
| 3334 | * - checks if an update is required (new IP or port is different than current ones) |
| 3335 | * - checks the update is allowed: |
| 3336 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3337 | * - allow all changes if no CHECKS are configured |
| 3338 | * - if CHECK is configured: |
| 3339 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3340 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3341 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3342 | * |
| 3343 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3344 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3345 | 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] | 3346 | { |
| 3347 | struct sockaddr_storage sa; |
| 3348 | int ret, port_change_required; |
| 3349 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3350 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3351 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3352 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3353 | |
| 3354 | msg = get_trash_chunk(); |
| 3355 | chunk_reset(msg); |
| 3356 | |
| 3357 | if (addr) { |
| 3358 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3359 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3360 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3361 | goto out; |
| 3362 | } |
| 3363 | |
| 3364 | /* changes are allowed on AF_INET* families only */ |
| 3365 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3366 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3367 | goto out; |
| 3368 | } |
| 3369 | |
| 3370 | /* collecting data currently setup */ |
| 3371 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3372 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3373 | /* changes are allowed on AF_INET* families only */ |
| 3374 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3375 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3376 | goto out; |
| 3377 | } |
| 3378 | |
| 3379 | /* applying ADDR changes if required and allowed |
| 3380 | * ipcmp returns 0 when both ADDR are the same |
| 3381 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3382 | if (ipcmp(&s->addr, &sa, 0) == 0) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3383 | chunk_appendf(msg, "no need to change the addr"); |
| 3384 | goto port; |
| 3385 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3386 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3387 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3388 | |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3389 | /* update report for caller */ |
| 3390 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3391 | } |
| 3392 | |
| 3393 | port: |
| 3394 | if (port) { |
| 3395 | char sign = '\0'; |
| 3396 | char *endptr; |
| 3397 | |
| 3398 | if (addr) |
| 3399 | chunk_appendf(msg, ", "); |
| 3400 | |
| 3401 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3402 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3403 | |
| 3404 | /* check if PORT change is required */ |
| 3405 | port_change_required = 0; |
| 3406 | |
| 3407 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3408 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3409 | new_port = strtol(port, &endptr, 10); |
| 3410 | if ((errno != 0) || (port == endptr)) { |
| 3411 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3412 | goto out; |
| 3413 | } |
| 3414 | |
| 3415 | /* check if caller triggers a port mapped or offset */ |
| 3416 | if (sign == '-' || (sign == '+')) { |
| 3417 | /* check if server currently uses port map */ |
| 3418 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3419 | /* switch from fixed port to port map mandatorily triggers |
| 3420 | * a port change */ |
| 3421 | port_change_required = 1; |
| 3422 | /* check is configured |
| 3423 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3424 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3425 | * to port mapping */ |
William Dauchy | 69f118d | 2021-02-03 22:30:07 +0100 | [diff] [blame] | 3426 | if (!s->check.port) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3427 | 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."); |
| 3428 | goto out; |
| 3429 | } |
| 3430 | } |
| 3431 | /* we're already using port maps */ |
| 3432 | else { |
| 3433 | port_change_required = current_port != new_port; |
| 3434 | } |
| 3435 | } |
| 3436 | /* fixed port */ |
| 3437 | else { |
| 3438 | port_change_required = current_port != new_port; |
| 3439 | } |
| 3440 | |
| 3441 | /* applying PORT changes if required and update response message */ |
| 3442 | if (port_change_required) { |
| 3443 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3444 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3445 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3446 | |
| 3447 | /* prepare message */ |
| 3448 | chunk_appendf(msg, "port changed from '"); |
| 3449 | if (s->flags & SRV_F_MAPPORTS) |
| 3450 | chunk_appendf(msg, "+"); |
| 3451 | chunk_appendf(msg, "%d' to '", current_port); |
| 3452 | |
| 3453 | if (sign == '-') { |
| 3454 | s->flags |= SRV_F_MAPPORTS; |
| 3455 | chunk_appendf(msg, "%c", sign); |
| 3456 | /* just use for result output */ |
| 3457 | new_port = -new_port; |
| 3458 | } |
| 3459 | else if (sign == '+') { |
| 3460 | s->flags |= SRV_F_MAPPORTS; |
| 3461 | chunk_appendf(msg, "%c", sign); |
| 3462 | } |
| 3463 | else { |
| 3464 | s->flags &= ~SRV_F_MAPPORTS; |
| 3465 | } |
| 3466 | |
| 3467 | chunk_appendf(msg, "%d'", new_port); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3468 | } |
| 3469 | else { |
| 3470 | chunk_appendf(msg, "no need to change the port"); |
| 3471 | } |
| 3472 | } |
| 3473 | |
| 3474 | out: |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3475 | if (changed) { |
| 3476 | /* force connection cleanup on the given server */ |
| 3477 | srv_cleanup_connections(s); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3478 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3479 | srv_set_addr_desc(s, 1); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3480 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3481 | if (updater) |
| 3482 | chunk_appendf(msg, " by '%s'", updater); |
| 3483 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3484 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3487 | /* |
| 3488 | * update server status based on result of SRV resolution |
| 3489 | * returns: |
| 3490 | * 0 if server status is updated |
| 3491 | * 1 if server status has not changed |
| 3492 | * |
| 3493 | * Must be called with the server lock held. |
| 3494 | */ |
| 3495 | int srvrq_update_srv_status(struct server *s, int has_no_ip) |
| 3496 | { |
| 3497 | if (!s->srvrq) |
| 3498 | return 1; |
| 3499 | |
| 3500 | /* since this server has an IP, it can go back in production */ |
| 3501 | if (has_no_ip == 0) { |
| 3502 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3503 | return 1; |
| 3504 | } |
| 3505 | |
| 3506 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3507 | return 1; |
| 3508 | |
| 3509 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record"); |
| 3510 | return 0; |
| 3511 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3512 | |
| 3513 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3514 | * update server status based on result of name resolution |
| 3515 | * returns: |
| 3516 | * 0 if server status is updated |
| 3517 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3518 | * |
| 3519 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3520 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3521 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3522 | { |
Emeric Brun | 750fe79 | 2020-12-23 16:51:12 +0100 | [diff] [blame] | 3523 | struct resolvers *resolvers = s->resolvers; |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3524 | struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3525 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3526 | |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3527 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3528 | if (resolution == NULL) |
| 3529 | return srvrq_update_srv_status(s, has_no_ip); |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3530 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3531 | switch (resolution->status) { |
| 3532 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3533 | /* status when HAProxy has just (re)started. |
| 3534 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3535 | break; |
| 3536 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3537 | case RSLV_STATUS_VALID: |
| 3538 | /* |
| 3539 | * resume health checks |
| 3540 | * server will be turned back on if health check is safe |
| 3541 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3542 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3543 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3544 | return 1; |
| 3545 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, |
| 3546 | "No IP for server "); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3547 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3548 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3549 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3550 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3551 | return 1; |
| 3552 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3553 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3554 | s->proxy->id, s->id); |
| 3555 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3556 | ha_warning("%s.\n", trash.area); |
| 3557 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3558 | return 0; |
| 3559 | |
| 3560 | case RSLV_STATUS_NX: |
| 3561 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3562 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3563 | if (!tick_is_expired(exp, now_ms)) |
| 3564 | break; |
| 3565 | |
| 3566 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3567 | return 1; |
| 3568 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status"); |
| 3569 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3570 | |
| 3571 | case RSLV_STATUS_TIMEOUT: |
| 3572 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3573 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3574 | if (!tick_is_expired(exp, now_ms)) |
| 3575 | break; |
| 3576 | |
| 3577 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3578 | return 1; |
| 3579 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status"); |
| 3580 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3581 | |
| 3582 | case RSLV_STATUS_REFUSED: |
| 3583 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3584 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3585 | if (!tick_is_expired(exp, now_ms)) |
| 3586 | break; |
| 3587 | |
| 3588 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3589 | return 1; |
| 3590 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status"); |
| 3591 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3592 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3593 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3594 | /* stop server if resolution failed for a long enough period */ |
| 3595 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3596 | if (!tick_is_expired(exp, now_ms)) |
| 3597 | break; |
| 3598 | |
| 3599 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3600 | return 1; |
| 3601 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error"); |
| 3602 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | return 1; |
| 3606 | } |
| 3607 | |
| 3608 | /* |
| 3609 | * Server Name Resolution valid response callback |
| 3610 | * It expects: |
| 3611 | * - <nameserver>: the name server which answered the valid response |
| 3612 | * - <response>: buffer containing a valid DNS response |
| 3613 | * - <response_len>: size of <response> |
| 3614 | * It performs the following actions: |
| 3615 | * - ignore response if current ip found and server family not met |
| 3616 | * - update with first new ip found if family is met and current IP is not found |
| 3617 | * returns: |
| 3618 | * 0 on error |
| 3619 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3620 | * |
| 3621 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3622 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3623 | int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3624 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3625 | struct server *s = NULL; |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3626 | struct resolv_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3627 | void *serverip, *firstip; |
| 3628 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3629 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3630 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3631 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3632 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3633 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3634 | if (!s) |
| 3635 | return 1; |
| 3636 | |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3637 | if (s->srvrq) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3638 | /* If DNS resolution is disabled ignore it. |
| 3639 | * This is the case if the server was associated to |
| 3640 | * a SRV record and this record is now expired. |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3641 | */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3642 | if (s->flags & SRV_F_NO_RESOLUTION) |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3643 | return 1; |
| 3644 | } |
| 3645 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3646 | resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3647 | if (!resolution) |
| 3648 | return 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3649 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3650 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3651 | firstip = NULL; /* pointer to the first valid response found */ |
| 3652 | /* it will be used as the new IP if a change is required */ |
| 3653 | firstip_sin_family = AF_UNSPEC; |
| 3654 | serverip = NULL; /* current server IP address */ |
| 3655 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3656 | /* initializing server IP pointer */ |
| 3657 | server_sin_family = s->addr.ss_family; |
| 3658 | switch (server_sin_family) { |
| 3659 | case AF_INET: |
| 3660 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3661 | break; |
| 3662 | |
| 3663 | case AF_INET6: |
| 3664 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3665 | break; |
| 3666 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3667 | case AF_UNSPEC: |
| 3668 | break; |
| 3669 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3670 | default: |
| 3671 | goto invalid; |
| 3672 | } |
| 3673 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3674 | ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts, |
| 3675 | serverip, server_sin_family, &firstip, |
| 3676 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3677 | |
| 3678 | switch (ret) { |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3679 | case RSLV_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3680 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3681 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3682 | case RSLV_UPD_SRVIP_NOT_FOUND: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3683 | goto save_ip; |
| 3684 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3685 | case RSLV_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3686 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3687 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3688 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3689 | case RSLV_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3690 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3691 | resolution->status = RSLV_STATUS_OTHER; |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3692 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3693 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3694 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3695 | default: |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3696 | has_no_ip = 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3697 | goto invalid; |
| 3698 | |
| 3699 | } |
| 3700 | |
| 3701 | save_ip: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3702 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3703 | counters->app.resolver.update++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3704 | /* save the first ip we found */ |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3705 | chunk_printf(chk, "%s/%s", counters->pid, counters->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3706 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3707 | else |
| 3708 | chunk_printf(chk, "DNS cache"); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3709 | srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3710 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3711 | update_status: |
Christopher Faulet | a8ce497 | 2021-06-24 15:26:03 +0200 | [diff] [blame] | 3712 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3713 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3714 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3715 | |
| 3716 | invalid: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3717 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3718 | counters->app.resolver.invalid++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3719 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 3720 | } |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3721 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3722 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3723 | return 0; |
| 3724 | } |
| 3725 | |
| 3726 | /* |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3727 | * SRV record error management callback |
| 3728 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3729 | * 0 if we can trash answser items. |
| 3730 | * 1 when safely ignored and we must kept answer items |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3731 | * |
| 3732 | * Grabs the server's lock. |
| 3733 | */ |
| 3734 | int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code) |
| 3735 | { |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3736 | struct resolv_srvrq *srvrq; |
| 3737 | struct resolv_resolution *res; |
| 3738 | struct resolvers *resolvers; |
| 3739 | int exp; |
| 3740 | |
| 3741 | /* SRV records */ |
| 3742 | srvrq = objt_resolv_srvrq(requester->owner); |
| 3743 | if (!srvrq) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3744 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3745 | |
| 3746 | resolvers = srvrq->resolvers; |
| 3747 | res = requester->resolution; |
| 3748 | |
| 3749 | switch (res->status) { |
| 3750 | |
| 3751 | case RSLV_STATUS_NX: |
| 3752 | /* stop server if resolution is NX for a long enough period */ |
| 3753 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 3754 | if (!tick_is_expired(exp, now_ms)) |
| 3755 | return 1; |
| 3756 | break; |
| 3757 | |
| 3758 | case RSLV_STATUS_TIMEOUT: |
| 3759 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 3760 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 3761 | if (!tick_is_expired(exp, now_ms)) |
| 3762 | return 1; |
| 3763 | break; |
| 3764 | |
| 3765 | case RSLV_STATUS_REFUSED: |
| 3766 | /* stop server if resolution is REFUSED for a long enough period */ |
| 3767 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 3768 | if (!tick_is_expired(exp, now_ms)) |
| 3769 | return 1; |
| 3770 | break; |
| 3771 | |
| 3772 | default: |
| 3773 | /* stop server if resolution failed for a long enough period */ |
| 3774 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 3775 | if (!tick_is_expired(exp, now_ms)) |
| 3776 | return 1; |
| 3777 | } |
| 3778 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3779 | /* Remove any associated server ref */ |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3780 | resolv_detach_from_resolution_answer_items(res, requester); |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3781 | |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3782 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3786 | * Server Name Resolution error management callback |
| 3787 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3788 | * 0 if we can trash answser items. |
| 3789 | * 1 when safely ignored and we must kept answer items |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3790 | * |
| 3791 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3792 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3793 | int snr_resolution_error_cb(struct resolv_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3794 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3795 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3796 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3797 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3798 | if (!s) |
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_LOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3802 | if (!snr_update_srv_status(s, 1)) { |
Christopher Faulet | 5130c21 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 3803 | memset(&s->addr, 0, sizeof(s->addr)); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3804 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3805 | resolv_detach_from_resolution_answer_items(requester->resolution, requester); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3806 | return 0; |
| 3807 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3808 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3809 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3810 | return 1; |
| 3811 | } |
| 3812 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3813 | /* |
| 3814 | * 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] | 3815 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3816 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 3817 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3818 | * |
| 3819 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3820 | */ |
| 3821 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 3822 | { |
| 3823 | struct server *tmpsrv; |
| 3824 | struct proxy *be; |
| 3825 | |
| 3826 | if (!srv) |
| 3827 | return NULL; |
| 3828 | |
| 3829 | be = srv->proxy; |
| 3830 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3831 | /* we found the current server is the same, ignore it */ |
| 3832 | if (srv == tmpsrv) |
| 3833 | continue; |
| 3834 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3835 | /* We want to compare the IP in the record with the IP of the servers in the |
| 3836 | * same backend, only if: |
| 3837 | * * DNS resolution is enabled on the server |
| 3838 | * * the hostname used for the resolution by our server is the same than the |
| 3839 | * one used for the server found in the backend |
| 3840 | * * the server found in the backend is not our current server |
| 3841 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3842 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3843 | if ((tmpsrv->hostname_dn == NULL) || |
| 3844 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3845 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3846 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3847 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3848 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3849 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3850 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3851 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3852 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3853 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3854 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3855 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3856 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3857 | /* At this point, we have 2 different servers using the same DNS hostname |
| 3858 | * for their respective resolution. |
| 3859 | */ |
| 3860 | if (*ip_family == tmpsrv->addr.ss_family && |
| 3861 | ((tmpsrv->addr.ss_family == AF_INET && |
| 3862 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 3863 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 3864 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3865 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3866 | return tmpsrv; |
| 3867 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3868 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3871 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3872 | return NULL; |
| 3873 | } |
| 3874 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3875 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 3876 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 3877 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 3878 | * not NULL, is filled up. |
| 3879 | */ |
| 3880 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 3881 | { |
| 3882 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3883 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 3884 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3885 | return 1; |
| 3886 | } |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3890 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 3891 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3892 | * |
| 3893 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3894 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3895 | 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] | 3896 | { |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3897 | struct resolv_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3898 | char *hostname_dn; |
| 3899 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3900 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 3901 | /* Note that the server lock is already held. */ |
| 3902 | if (!srv->resolvers) |
| 3903 | return -1; |
| 3904 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3905 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3906 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3907 | /* run time DNS/SRV resolution was not active for this server |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3908 | * and we can't enable it at run time for now. |
| 3909 | */ |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3910 | if (!srv->resolv_requester && !srv->srvrq) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3911 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3912 | |
| 3913 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3914 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3915 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 3916 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3917 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3918 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3919 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3920 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3921 | resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3922 | if (resolution && |
| 3923 | resolution->hostname_dn && |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3924 | resolution->hostname_dn_len == hostname_dn_len && |
| 3925 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3926 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3927 | |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3928 | resolv_unlink_resolution(srv->resolv_requester); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3929 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3930 | free(srv->hostname); |
| 3931 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3932 | srv->hostname = strdup(hostname); |
| 3933 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3934 | srv->hostname_dn_len = hostname_dn_len; |
| 3935 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3936 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3937 | |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 3938 | if (srv->flags & SRV_F_NO_RESOLUTION) |
| 3939 | goto end; |
| 3940 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3941 | if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3942 | goto err; |
| 3943 | |
| 3944 | end: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3945 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3946 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3947 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3948 | |
| 3949 | err: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3950 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3951 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3952 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3953 | } |
| 3954 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3955 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 3956 | * from the state file. This is suited for initial address configuration. |
| 3957 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 3958 | * *err_code, if not NULL, is filled up. |
| 3959 | */ |
| 3960 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 3961 | { |
| 3962 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 3963 | if (err_code) |
| 3964 | *err_code |= ERR_WARN; |
| 3965 | return 1; |
| 3966 | } |
| 3967 | return 0; |
| 3968 | } |
| 3969 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3970 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 3971 | static int srv_iterate_initaddr(struct server *srv) |
| 3972 | { |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3973 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3974 | int return_code = 0; |
| 3975 | int err_code; |
| 3976 | unsigned int methods; |
| 3977 | |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3978 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 3979 | if (!name && srv->srvrq) |
| 3980 | name = srv->srvrq->name; |
| 3981 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3982 | methods = srv->init_addr_methods; |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3983 | if (!methods) { |
| 3984 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3985 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 3986 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3987 | if (srv->resolvers_id) { |
| 3988 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 3989 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 3990 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3991 | } |
| 3992 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 3993 | /* "-dr" : always append "none" so that server addresses resolution |
| 3994 | * failures are silently ignored, this is convenient to validate some |
| 3995 | * configs out of their environment. |
| 3996 | */ |
| 3997 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 3998 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 3999 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4000 | while (methods) { |
| 4001 | err_code = 0; |
| 4002 | switch (srv_get_next_initaddr(&methods)) { |
| 4003 | case SRV_IADDR_LAST: |
| 4004 | if (!srv->lastaddr) |
| 4005 | continue; |
| 4006 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4007 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4008 | return_code |= err_code; |
| 4009 | break; |
| 4010 | |
| 4011 | case SRV_IADDR_LIBC: |
| 4012 | if (!srv->hostname) |
| 4013 | continue; |
| 4014 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4015 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4016 | return_code |= err_code; |
| 4017 | break; |
| 4018 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4019 | case SRV_IADDR_NONE: |
| 4020 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4021 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4022 | ha_warning("could not resolve address '%s', disabling server.\n", |
| 4023 | name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4024 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4025 | return return_code; |
| 4026 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4027 | case SRV_IADDR_IP: |
| 4028 | ipcpy(&srv->init_addr, &srv->addr); |
| 4029 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4030 | ha_warning("could not resolve address '%s', falling back to configured address.\n", |
| 4031 | name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4032 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4033 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4034 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4035 | default: /* unhandled method */ |
| 4036 | break; |
| 4037 | } |
| 4038 | } |
| 4039 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4040 | if (!return_code) |
| 4041 | ha_alert("no method found to resolve address '%s'.\n", name); |
| 4042 | else |
| 4043 | ha_alert("could not resolve address '%s'.\n", name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4044 | |
| 4045 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4046 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4047 | out: |
| 4048 | srv_set_dyncookie(srv); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 4049 | srv_set_addr_desc(srv, 1); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4050 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4051 | } |
| 4052 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4053 | /* |
| 4054 | * This function parses all backends and all servers within each backend |
| 4055 | * and performs servers' addr resolution based on information provided by: |
| 4056 | * - configuration file |
| 4057 | * - server-state file (states provided by an 'old' haproxy process) |
| 4058 | * |
| 4059 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4060 | */ |
| 4061 | int srv_init_addr(void) |
| 4062 | { |
| 4063 | struct proxy *curproxy; |
| 4064 | int return_code = 0; |
| 4065 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4066 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4067 | while (curproxy) { |
| 4068 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4069 | |
| 4070 | /* servers are in backend only */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4071 | 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] | 4072 | goto srv_init_addr_next; |
| 4073 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4074 | for (srv = curproxy->srv; srv; srv = srv->next) { |
| 4075 | set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4076 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4077 | return_code |= srv_iterate_initaddr(srv); |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4078 | reset_usermsgs_ctx(); |
| 4079 | } |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4080 | |
| 4081 | srv_init_addr_next: |
| 4082 | curproxy = curproxy->next; |
| 4083 | } |
| 4084 | |
| 4085 | return return_code; |
| 4086 | } |
| 4087 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4088 | /* |
| 4089 | * Must be called with the server lock held. |
| 4090 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4091 | 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] | 4092 | { |
| 4093 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4094 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4095 | |
| 4096 | msg = get_trash_chunk(); |
| 4097 | chunk_reset(msg); |
| 4098 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4099 | if (server->hostname && strcmp(fqdn, server->hostname) == 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4100 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4101 | goto out; |
| 4102 | } |
| 4103 | |
| 4104 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4105 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4106 | goto out; |
| 4107 | } |
| 4108 | |
| 4109 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4110 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4111 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4112 | if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4113 | chunk_reset(msg); |
| 4114 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4115 | server->proxy->id, server->id); |
| 4116 | goto out; |
| 4117 | } |
| 4118 | |
| 4119 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4120 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4121 | |
| 4122 | out: |
| 4123 | if (updater) |
| 4124 | chunk_appendf(msg, " by '%s'", updater); |
| 4125 | chunk_appendf(msg, "\n"); |
| 4126 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4127 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4131 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4132 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4133 | * 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] | 4134 | * used for CLI commands requiring a server name. |
| 4135 | * Important: the <arg> is modified to remove the '/'. |
| 4136 | */ |
| 4137 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4138 | { |
| 4139 | struct proxy *px; |
| 4140 | struct server *sv; |
| 4141 | char *line; |
| 4142 | |
| 4143 | /* split "backend/server" and make <line> point to server */ |
| 4144 | for (line = arg; *line; line++) |
| 4145 | if (*line == '/') { |
| 4146 | *line++ = '\0'; |
| 4147 | break; |
| 4148 | } |
| 4149 | |
| 4150 | if (!*line || !*arg) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4151 | cli_err(appctx, "Require 'backend/server'.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4152 | return NULL; |
| 4153 | } |
| 4154 | |
| 4155 | if (!get_backend_server(arg, line, &px, &sv)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4156 | cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4157 | return NULL; |
| 4158 | } |
| 4159 | |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4160 | if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4161 | cli_err(appctx, "Proxy is disabled.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4162 | return NULL; |
| 4163 | } |
| 4164 | |
| 4165 | return sv; |
| 4166 | } |
| 4167 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4168 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4169 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4170 | 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] | 4171 | { |
| 4172 | struct server *sv; |
| 4173 | const char *warning; |
| 4174 | |
| 4175 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4176 | return 1; |
| 4177 | |
| 4178 | sv = cli_find_server(appctx, args[2]); |
| 4179 | if (!sv) |
| 4180 | return 1; |
| 4181 | |
| 4182 | if (strcmp(args[3], "weight") == 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 | warning = server_parse_weight_change_request(sv, args[4]); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4185 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4186 | if (warning) |
| 4187 | cli_err(appctx, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4188 | } |
| 4189 | else if (strcmp(args[3], "state") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4190 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4191 | if (strcmp(args[4], "ready") == 0) |
| 4192 | srv_adm_set_ready(sv); |
| 4193 | else if (strcmp(args[4], "drain") == 0) |
| 4194 | srv_adm_set_drain(sv); |
| 4195 | else if (strcmp(args[4], "maint") == 0) |
| 4196 | srv_adm_set_maint(sv); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4197 | else |
| 4198 | 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] | 4199 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4200 | } |
| 4201 | else if (strcmp(args[3], "health") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4202 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4203 | if (sv->track) |
| 4204 | cli_err(appctx, "cannot change health on a tracking server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4205 | else if (strcmp(args[4], "up") == 0) { |
| 4206 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4207 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4208 | } |
| 4209 | else if (strcmp(args[4], "stopping") == 0) { |
| 4210 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4211 | srv_set_stopping(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4212 | } |
| 4213 | else if (strcmp(args[4], "down") == 0) { |
| 4214 | sv->check.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4215 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4216 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4217 | else |
| 4218 | 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] | 4219 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4220 | } |
| 4221 | else if (strcmp(args[3], "agent") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4222 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4223 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4224 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4225 | else if (strcmp(args[4], "up") == 0) { |
| 4226 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4227 | srv_set_running(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4228 | } |
| 4229 | else if (strcmp(args[4], "down") == 0) { |
| 4230 | sv->agent.health = 0; |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 4231 | srv_set_stopped(sv, "changed from CLI", NULL); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4232 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4233 | else |
| 4234 | cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4235 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4236 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4237 | else if (strcmp(args[3], "agent-addr") == 0) { |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4238 | char *addr = NULL; |
| 4239 | char *port = NULL; |
| 4240 | if (strlen(args[4]) == 0) { |
| 4241 | cli_err(appctx, "set server <b>/<s> agent-addr requires" |
| 4242 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4243 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4244 | } |
| 4245 | addr = args[4]; |
| 4246 | if (strcmp(args[5], "port") == 0) |
| 4247 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4248 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4249 | warning = srv_update_agent_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4250 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4251 | if (warning) |
| 4252 | cli_msg(appctx, LOG_WARNING, warning); |
| 4253 | } |
| 4254 | else if (strcmp(args[3], "agent-port") == 0) { |
| 4255 | char *port = NULL; |
| 4256 | if (strlen(args[4]) == 0) { |
| 4257 | cli_err(appctx, "set server <b>/<s> agent-port requires" |
| 4258 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4259 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4260 | } |
| 4261 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4262 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4263 | warning = srv_update_agent_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4264 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4265 | if (warning) |
| 4266 | cli_msg(appctx, LOG_WARNING, warning); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4267 | } |
| 4268 | else if (strcmp(args[3], "agent-send") == 0) { |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4269 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4270 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4271 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
| 4272 | else { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4273 | if (!set_srv_agent_send(sv, args[4])) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4274 | cli_err(appctx, "cannot allocate memory for new string.\n"); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4275 | } |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4276 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4277 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4278 | else if (strcmp(args[3], "check-addr") == 0) { |
| 4279 | char *addr = NULL; |
| 4280 | char *port = NULL; |
| 4281 | if (strlen(args[4]) == 0) { |
| 4282 | cli_err(appctx, "set server <b>/<s> check-addr requires" |
| 4283 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4284 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4285 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4286 | addr = args[4]; |
| 4287 | if (strcmp(args[5], "port") == 0) |
| 4288 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4289 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4290 | warning = srv_update_check_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4291 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4292 | if (warning) |
| 4293 | cli_msg(appctx, LOG_WARNING, warning); |
| 4294 | } |
| 4295 | else if (strcmp(args[3], "check-port") == 0) { |
| 4296 | char *port = NULL; |
| 4297 | if (strlen(args[4]) == 0) { |
| 4298 | cli_err(appctx, "set server <b>/<s> check-port requires" |
| 4299 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4300 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4301 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4302 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4303 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4304 | warning = srv_update_check_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4305 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4306 | if (warning) |
| 4307 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4308 | } |
| 4309 | else if (strcmp(args[3], "addr") == 0) { |
| 4310 | char *addr = NULL; |
| 4311 | char *port = NULL; |
| 4312 | if (strlen(args[4]) == 0) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4313 | 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] | 4314 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4315 | } |
| 4316 | else { |
| 4317 | addr = args[4]; |
| 4318 | } |
| 4319 | if (strcmp(args[5], "port") == 0) { |
| 4320 | port = args[6]; |
| 4321 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4322 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4323 | warning = srv_update_addr_port(sv, addr, port, "stats socket command"); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4324 | if (warning) |
| 4325 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4326 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4327 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4328 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4329 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4330 | if (!*args[4]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4331 | cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4332 | goto out; |
| 4333 | } |
| 4334 | if (!sv->resolvers) { |
| 4335 | cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n"); |
| 4336 | goto out; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4337 | } |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4338 | if (sv->srvrq) { |
| 4339 | 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] | 4340 | goto out; |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4341 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4342 | HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock); |
| 4343 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4344 | /* ensure runtime resolver will process this new fqdn */ |
| 4345 | if (sv->flags & SRV_F_NO_RESOLUTION) { |
| 4346 | sv->flags &= ~SRV_F_NO_RESOLUTION; |
| 4347 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4348 | warning = srv_update_fqdn(sv, args[4], "stats socket command", 1); |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4349 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4350 | HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4351 | if (warning) |
| 4352 | cli_msg(appctx, LOG_WARNING, warning); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4353 | } |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4354 | else if (strcmp(args[3], "ssl") == 0) { |
| 4355 | #ifdef USE_OPENSSL |
Amaury Denoyelle | b89d3d3 | 2021-05-19 15:00:54 +0200 | [diff] [blame] | 4356 | if (sv->flags & SRV_F_DYNAMIC) { |
| 4357 | cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n"); |
| 4358 | goto out; |
| 4359 | } |
| 4360 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4361 | if (sv->ssl_ctx.ctx == NULL) { |
| 4362 | cli_err(appctx, "'set server <srv> ssl' cannot be set. " |
| 4363 | " default-server should define ssl settings\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4364 | goto out; |
| 4365 | } |
| 4366 | |
| 4367 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4368 | if (strcmp(args[4], "on") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4369 | srv_set_ssl(sv, 1); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4370 | } else if (strcmp(args[4], "off") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4371 | srv_set_ssl(sv, 0); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4372 | } else { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4373 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4374 | cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4375 | goto out; |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4376 | } |
| 4377 | srv_cleanup_connections(sv); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4378 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4379 | cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n"); |
| 4380 | #else |
| 4381 | cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n"); |
| 4382 | #endif |
| 4383 | } else { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4384 | cli_err(appctx, |
William Dauchy | 3f4ec7d | 2021-02-15 17:22:16 +0100 | [diff] [blame] | 4385 | "usage: set server <backend>/<server> " |
| 4386 | "addr | agent | agent-addr | agent-port | agent-send | " |
| 4387 | "check-addr | check-port | fqdn | health | ssl | " |
| 4388 | "state | weight\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4389 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4390 | out: |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4391 | return 1; |
| 4392 | } |
| 4393 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4394 | 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] | 4395 | { |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4396 | struct proxy *px; |
| 4397 | struct server *sv; |
| 4398 | char *line; |
| 4399 | |
| 4400 | |
| 4401 | /* split "backend/server" and make <line> point to server */ |
| 4402 | for (line = args[2]; *line; line++) |
| 4403 | if (*line == '/') { |
| 4404 | *line++ = '\0'; |
| 4405 | break; |
| 4406 | } |
| 4407 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4408 | if (!*line) |
| 4409 | return cli_err(appctx, "Require 'backend/server'.\n"); |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4410 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4411 | if (!get_backend_server(args[2], line, &px, &sv)) |
| 4412 | 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] | 4413 | |
| 4414 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4415 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4416 | sv->iweight); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 4417 | if (applet_putstr(appctx, trash.area) == -1) |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4418 | return 0; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4419 | return 1; |
| 4420 | } |
| 4421 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4422 | /* Parse a "set weight" command. |
| 4423 | * |
| 4424 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4425 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4426 | 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] | 4427 | { |
| 4428 | struct server *sv; |
| 4429 | const char *warning; |
| 4430 | |
| 4431 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4432 | return 1; |
| 4433 | |
| 4434 | sv = cli_find_server(appctx, args[2]); |
| 4435 | if (!sv) |
| 4436 | return 1; |
| 4437 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4438 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4439 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4440 | warning = server_parse_weight_change_request(sv, args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4441 | if (warning) |
| 4442 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4443 | |
| 4444 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4445 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4446 | return 1; |
| 4447 | } |
| 4448 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4449 | /* parse a "set maxconn server" command. It always returns 1. |
| 4450 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4451 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4452 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4453 | 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] | 4454 | { |
| 4455 | struct server *sv; |
| 4456 | const char *warning; |
| 4457 | |
| 4458 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4459 | return 1; |
| 4460 | |
| 4461 | sv = cli_find_server(appctx, args[3]); |
| 4462 | if (!sv) |
| 4463 | return 1; |
| 4464 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4465 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4466 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4467 | warning = server_parse_maxconn_change_request(sv, args[4]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4468 | if (warning) |
| 4469 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4470 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4471 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4472 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4473 | return 1; |
| 4474 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4475 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4476 | /* parse a "disable agent" command. It always returns 1. |
| 4477 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4478 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4479 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4480 | 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] | 4481 | { |
| 4482 | struct server *sv; |
| 4483 | |
| 4484 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4485 | return 1; |
| 4486 | |
| 4487 | sv = cli_find_server(appctx, args[2]); |
| 4488 | if (!sv) |
| 4489 | return 1; |
| 4490 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4491 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4492 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4493 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4494 | return 1; |
| 4495 | } |
| 4496 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4497 | /* parse a "disable health" command. It always returns 1. |
| 4498 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4499 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4500 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4501 | 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] | 4502 | { |
| 4503 | struct server *sv; |
| 4504 | |
| 4505 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4506 | return 1; |
| 4507 | |
| 4508 | sv = cli_find_server(appctx, args[2]); |
| 4509 | if (!sv) |
| 4510 | return 1; |
| 4511 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4512 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4513 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4514 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4515 | return 1; |
| 4516 | } |
| 4517 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4518 | /* parse a "disable server" command. It always returns 1. |
| 4519 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4520 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4521 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4522 | 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] | 4523 | { |
| 4524 | struct server *sv; |
| 4525 | |
| 4526 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4527 | return 1; |
| 4528 | |
| 4529 | sv = cli_find_server(appctx, args[2]); |
| 4530 | if (!sv) |
| 4531 | return 1; |
| 4532 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4533 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4534 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4535 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4536 | return 1; |
| 4537 | } |
| 4538 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4539 | /* parse a "enable agent" command. It always returns 1. |
| 4540 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4541 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4542 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4543 | 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] | 4544 | { |
| 4545 | struct server *sv; |
| 4546 | |
| 4547 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4548 | return 1; |
| 4549 | |
| 4550 | sv = cli_find_server(appctx, args[2]); |
| 4551 | if (!sv) |
| 4552 | return 1; |
| 4553 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4554 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) |
| 4555 | 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] | 4556 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4557 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4558 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4559 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4560 | return 1; |
| 4561 | } |
| 4562 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4563 | /* parse a "enable health" command. It always returns 1. |
| 4564 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4565 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4566 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4567 | 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] | 4568 | { |
| 4569 | struct server *sv; |
| 4570 | |
| 4571 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4572 | return 1; |
| 4573 | |
| 4574 | sv = cli_find_server(appctx, args[2]); |
| 4575 | if (!sv) |
| 4576 | return 1; |
| 4577 | |
Amaury Denoyelle | 0f456d5 | 2021-09-21 10:29:09 +0200 | [diff] [blame] | 4578 | if (!(sv->check.state & CHK_ST_CONFIGURED)) |
| 4579 | return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n"); |
| 4580 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4581 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4582 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4583 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4584 | return 1; |
| 4585 | } |
| 4586 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4587 | /* parse a "enable server" command. It always returns 1. |
| 4588 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4589 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4590 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4591 | 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] | 4592 | { |
| 4593 | struct server *sv; |
| 4594 | |
| 4595 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4596 | return 1; |
| 4597 | |
| 4598 | sv = cli_find_server(appctx, args[2]); |
| 4599 | if (!sv) |
| 4600 | return 1; |
| 4601 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4602 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4603 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4604 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4605 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4606 | sv->cookie) |
| 4607 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4608 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4609 | return 1; |
| 4610 | } |
| 4611 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4612 | /* Allocates data structure related to load balancing for the server <sv>. It |
| 4613 | * is only required for dynamic servers. |
| 4614 | * |
| 4615 | * At the moment, the server lock is not used as this function is only called |
| 4616 | * for a dynamic server not yet registered. |
| 4617 | * |
| 4618 | * Returns 1 on success, 0 on allocation failure. |
| 4619 | */ |
| 4620 | static int srv_alloc_lb(struct server *sv, struct proxy *be) |
| 4621 | { |
| 4622 | int node; |
| 4623 | |
| 4624 | sv->lb_tree = (sv->flags & SRV_F_BACKUP) ? |
| 4625 | &be->lbprm.chash.bck : &be->lbprm.chash.act; |
| 4626 | sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE; |
| 4627 | sv->lb_nodes_now = 0; |
| 4628 | |
Willy Tarreau | dcb121f | 2021-04-20 11:37:45 +0200 | [diff] [blame] | 4629 | if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) || |
| 4630 | ((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] | 4631 | sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes)); |
| 4632 | |
| 4633 | if (!sv->lb_nodes) |
| 4634 | return 0; |
| 4635 | |
| 4636 | for (node = 0; node < sv->lb_nodes_tot; node++) { |
| 4637 | sv->lb_nodes[node].server = sv; |
| 4638 | sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | return 1; |
| 4643 | } |
| 4644 | |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4645 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 4646 | * reached, the task automatically stops. Note that any server status change |
| 4647 | * must have updated s->last_change accordingly. |
| 4648 | */ |
| 4649 | static struct task *server_warmup(struct task *t, void *context, unsigned int state) |
| 4650 | { |
| 4651 | struct server *s = context; |
| 4652 | |
| 4653 | /* by default, plan on stopping the task */ |
| 4654 | t->expire = TICK_ETERNITY; |
| 4655 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 4656 | (s->next_state != SRV_ST_STARTING)) |
| 4657 | return t; |
| 4658 | |
| 4659 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4660 | |
| 4661 | /* recalculate the weights and update the state */ |
| 4662 | server_recalc_eweight(s, 1); |
| 4663 | |
| 4664 | /* probably that we can refill this server with a bit more connections */ |
| 4665 | pendconn_grab_from_px(s); |
| 4666 | |
| 4667 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4668 | |
| 4669 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 4670 | * whichever is greater, resulting in small 5% steps. |
| 4671 | */ |
| 4672 | if (s->next_state == SRV_ST_STARTING) |
| 4673 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 4674 | return t; |
| 4675 | } |
| 4676 | |
| 4677 | /* Allocate the slowstart task if the server is configured with a slowstart |
| 4678 | * timer. If server next_state is SRV_ST_STARTING, the task is scheduled. |
| 4679 | * |
| 4680 | * Returns 0 on success else non-zero. |
| 4681 | */ |
| 4682 | static int init_srv_slowstart(struct server *srv) |
| 4683 | { |
| 4684 | struct task *t; |
| 4685 | |
| 4686 | if (srv->slowstart) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4687 | if ((t = task_new_anywhere()) == NULL) { |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4688 | ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id); |
| 4689 | return ERR_ALERT | ERR_FATAL; |
| 4690 | } |
| 4691 | |
| 4692 | /* We need a warmup task that will be called when the server |
| 4693 | * state switches from down to up. |
| 4694 | */ |
| 4695 | srv->warmup = t; |
| 4696 | t->process = server_warmup; |
| 4697 | t->context = srv; |
| 4698 | |
| 4699 | /* server can be in this state only because of */ |
| 4700 | if (srv->next_state == SRV_ST_STARTING) { |
| 4701 | task_schedule(srv->warmup, |
| 4702 | tick_add(now_ms, |
| 4703 | MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20))); |
| 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | return ERR_NONE; |
| 4708 | } |
| 4709 | REGISTER_POST_SERVER_CHECK(init_srv_slowstart); |
| 4710 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4711 | /* Memory allocation and initialization of the per_thr field. |
| 4712 | * Returns 0 if the field has been successfully initialized, -1 on failure. |
| 4713 | */ |
| 4714 | int srv_init_per_thr(struct server *srv) |
| 4715 | { |
| 4716 | int i; |
| 4717 | |
| 4718 | srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr)); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 4719 | srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp)); |
| 4720 | if (!srv->per_thr || !srv->per_tgrp) |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4721 | return -1; |
| 4722 | |
| 4723 | for (i = 0; i < global.nbthread; i++) { |
| 4724 | srv->per_thr[i].idle_conns = EB_ROOT; |
| 4725 | srv->per_thr[i].safe_conns = EB_ROOT; |
| 4726 | srv->per_thr[i].avail_conns = EB_ROOT; |
| 4727 | MT_LIST_INIT(&srv->per_thr[i].streams); |
| 4728 | } |
| 4729 | |
| 4730 | return 0; |
| 4731 | } |
| 4732 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4733 | /* Parse a "add server" command |
| 4734 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4735 | */ |
| 4736 | static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4737 | { |
| 4738 | struct proxy *be; |
| 4739 | struct server *srv; |
| 4740 | char *be_name, *sv_name; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4741 | int errcode, argc; |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4742 | int next_id; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4743 | const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR; |
| 4744 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4745 | usermsgs_clr("CLI"); |
| 4746 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4747 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4748 | return 1; |
| 4749 | |
| 4750 | ++args; |
| 4751 | |
| 4752 | sv_name = be_name = args[1]; |
| 4753 | /* split backend/server arg */ |
| 4754 | while (*sv_name && *(++sv_name)) { |
| 4755 | if (*sv_name == '/') { |
| 4756 | *sv_name = '\0'; |
| 4757 | ++sv_name; |
| 4758 | break; |
| 4759 | } |
| 4760 | } |
| 4761 | |
| 4762 | if (!*sv_name) |
| 4763 | return cli_err(appctx, "Require 'backend/server'."); |
| 4764 | |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4765 | be = proxy_be_by_name(be_name); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4766 | if (!be) |
| 4767 | return cli_err(appctx, "No such backend."); |
| 4768 | |
| 4769 | if (!(be->lbprm.algo & BE_LB_PROP_DYN)) { |
Amaury Denoyelle | eafd701 | 2021-04-29 14:59:42 +0200 | [diff] [blame] | 4770 | 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] | 4771 | return 1; |
| 4772 | } |
| 4773 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4774 | /* At this point, some operations might not be thread-safe anymore. This |
| 4775 | * might be the case for parsing handlers which were designed to run |
| 4776 | * only at the starting stage on single-thread mode. |
| 4777 | * |
| 4778 | * Activate thread isolation to ensure thread-safety. |
| 4779 | */ |
| 4780 | thread_isolate(); |
| 4781 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4782 | args[1] = sv_name; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4783 | errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags); |
| 4784 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4785 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4786 | |
| 4787 | while (*args[argc]) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4788 | errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4789 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4790 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4791 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4792 | } |
| 4793 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4794 | errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags); |
| 4795 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4796 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4797 | |
Amaury Denoyelle | efbf35c | 2021-06-10 17:34:10 +0200 | [diff] [blame] | 4798 | /* A dynamic server does not currently support resolution. |
| 4799 | * |
| 4800 | * Initialize it explicitly to the "none" method to ensure no |
| 4801 | * resolution will ever be executed. |
| 4802 | */ |
| 4803 | srv->init_addr_methods = SRV_IADDR_NONE; |
| 4804 | |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4805 | if (srv->mux_proto) { |
| 4806 | 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] | 4807 | ha_alert("MUX protocol is not usable for server.\n"); |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4808 | goto out; |
| 4809 | } |
| 4810 | } |
| 4811 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4812 | if (srv_init_per_thr(srv) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4813 | ha_alert("failed to allocate per-thread lists for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4814 | goto out; |
| 4815 | } |
| 4816 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4817 | if (srv->max_idle_conns != 0) { |
| 4818 | srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr)); |
| 4819 | if (!srv->curr_idle_thr) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4820 | ha_alert("failed to allocate counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4821 | goto out; |
| 4822 | } |
| 4823 | } |
| 4824 | |
| 4825 | if (!srv_alloc_lb(srv, be)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4826 | ha_alert("Failed to initialize load-balancing data.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4827 | goto out; |
| 4828 | } |
| 4829 | |
| 4830 | if (!stats_allocate_proxy_counters_internal(&srv->extra_counters, |
| 4831 | COUNTERS_SV, |
| 4832 | STATS_PX_CAP_SRV)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4833 | ha_alert("failed to allocate extra counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4834 | goto out; |
| 4835 | } |
| 4836 | |
Aurelien DARRAGON | 86207e7 | 2023-02-08 11:55:08 +0100 | [diff] [blame] | 4837 | /* ensure minconn/maxconn consistency */ |
| 4838 | srv_minmax_conn_apply(srv); |
| 4839 | |
Amaury Denoyelle | 79b90e8 | 2021-09-20 15:15:19 +0200 | [diff] [blame] | 4840 | if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) || |
| 4841 | srv->check.use_ssl == 1) { |
Amaury Denoyelle | 34897d2 | 2021-05-19 09:49:41 +0200 | [diff] [blame] | 4842 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 4843 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) |
| 4844 | goto out; |
| 4845 | } |
| 4846 | } |
| 4847 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4848 | if (srv->trackit) { |
| 4849 | if (srv_apply_track(srv, be)) |
| 4850 | goto out; |
| 4851 | } |
| 4852 | |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4853 | /* Init check/agent if configured. The check is manually disabled |
| 4854 | * because a dynamic server is started in a disable state. It must be |
| 4855 | * manually activated via a "enable health/agent" command. |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4856 | */ |
| 4857 | if (srv->do_check) { |
| 4858 | if (init_srv_check(srv)) |
| 4859 | goto out; |
| 4860 | |
| 4861 | srv->check.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4862 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4863 | |
| 4864 | if (srv->do_agent) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4865 | if (init_srv_agent_check(srv)) |
| 4866 | goto out; |
| 4867 | |
| 4868 | srv->agent.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4869 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4870 | |
Amaury Denoyelle | cd8a6f2 | 2021-09-21 11:51:54 +0200 | [diff] [blame] | 4871 | /* Init slowstart if needed. */ |
| 4872 | if (init_srv_slowstart(srv)) |
| 4873 | goto out; |
| 4874 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4875 | /* Attach the server to the end of the proxy linked list. Note that this |
| 4876 | * operation is not thread-safe so this is executed under thread |
| 4877 | * isolation. |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4878 | * |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4879 | * 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] | 4880 | */ |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4881 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4882 | /* TODO use a double-linked list for px->srv */ |
| 4883 | if (be->srv) { |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4884 | struct server *next = be->srv; |
| 4885 | |
| 4886 | while (1) { |
| 4887 | /* check for duplicate server */ |
Tim Duesterhus | c5aa113 | 2021-10-16 17:48:15 +0200 | [diff] [blame] | 4888 | if (strcmp(srv->id, next->id) == 0) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4889 | 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] | 4890 | goto out; |
| 4891 | } |
| 4892 | |
| 4893 | if (!next->next) |
| 4894 | break; |
| 4895 | |
| 4896 | next = next->next; |
| 4897 | } |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4898 | |
| 4899 | next->next = srv; |
| 4900 | } |
| 4901 | else { |
| 4902 | srv->next = be->srv; |
| 4903 | be->srv = srv; |
| 4904 | } |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4905 | |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4906 | /* generate the server id if not manually specified */ |
| 4907 | if (!srv->puid) { |
| 4908 | next_id = get_next_id(&be->conf.used_server_id, 1); |
| 4909 | if (!next_id) { |
| 4910 | ha_alert("Cannot attach server : no id left in proxy\n"); |
| 4911 | goto out; |
| 4912 | } |
| 4913 | |
| 4914 | srv->conf.id.key = srv->puid = next_id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4915 | } |
Christopher Faulet | 70f8948 | 2021-12-07 18:49:44 +0100 | [diff] [blame] | 4916 | srv->conf.name.key = srv->id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4917 | |
| 4918 | /* insert the server in the backend trees */ |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 4919 | eb32_insert(&be->conf.used_server_id, &srv->conf.id); |
| 4920 | ebis_insert(&be->conf.used_server_name, &srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 4921 | /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */ |
| 4922 | if (srv->addr_node.key) |
| 4923 | ebis_insert(&be->used_server_addr, &srv->addr_node); |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4924 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 4925 | /* check if LSB bit (odd bit) is set for reuse_cnt */ |
| 4926 | if (srv_id_reuse_cnt & 1) { |
| 4927 | /* cnt must be increased */ |
| 4928 | srv_id_reuse_cnt++; |
| 4929 | } |
| 4930 | /* srv_id_reuse_cnt is always even at this stage, divide by 2 to |
| 4931 | * save some space |
| 4932 | * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt)) |
| 4933 | */ |
| 4934 | srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0; |
| 4935 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 4936 | /* adding server cannot fail when we reach this: |
| 4937 | * publishing EVENT_HDL_SUB_SERVER_ADD |
| 4938 | */ |
| 4939 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1); |
| 4940 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4941 | thread_release(); |
| 4942 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4943 | /* Start the check task. The server must be fully initialized. |
| 4944 | * |
| 4945 | * <srvpos> and <nbcheck> parameters are set to 1 as there should be no |
| 4946 | * need to randomly spread the task interval for dynamic servers. |
| 4947 | */ |
| 4948 | if (srv->check.state & CHK_ST_CONFIGURED) { |
| 4949 | if (!start_check_task(&srv->check, 0, 1, 1)) |
| 4950 | ha_alert("System might be unstable, consider to execute a reload"); |
| 4951 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4952 | if (srv->agent.state & CHK_ST_CONFIGURED) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4953 | if (!start_check_task(&srv->agent, 0, 1, 1)) |
| 4954 | ha_alert("System might be unstable, consider to execute a reload"); |
| 4955 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4956 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4957 | ha_notice("New server registered.\n"); |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 4958 | cli_umsg(appctx, LOG_INFO); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4959 | |
| 4960 | return 0; |
| 4961 | |
| 4962 | out: |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 4963 | if (srv) { |
| 4964 | if (srv->track) |
| 4965 | release_server_track(srv); |
| 4966 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4967 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 4968 | free_check(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4969 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4970 | free_check(&srv->agent); |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4971 | |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 4972 | /* remove the server from the proxy linked list */ |
| 4973 | if (be->srv == srv) { |
| 4974 | be->srv = srv->next; |
| 4975 | } |
| 4976 | else { |
| 4977 | struct server *prev; |
| 4978 | for (prev = be->srv; prev && prev->next != srv; prev = prev->next) |
| 4979 | ; |
| 4980 | if (prev) |
| 4981 | prev->next = srv->next; |
| 4982 | } |
| 4983 | |
| 4984 | } |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 4985 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4986 | thread_release(); |
| 4987 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4988 | if (!usermsgs_empty()) |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 4989 | cli_umsgerr(appctx); |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4990 | |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 4991 | if (srv) |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 4992 | srv_drop(srv); |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4993 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4994 | return 1; |
| 4995 | } |
| 4996 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 4997 | /* Parse a "del server" command |
| 4998 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4999 | */ |
| 5000 | static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 5001 | { |
| 5002 | struct proxy *be; |
| 5003 | struct server *srv; |
| 5004 | char *be_name, *sv_name; |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5005 | struct server *prev_del; |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5006 | |
| 5007 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 5008 | return 1; |
| 5009 | |
| 5010 | ++args; |
| 5011 | |
| 5012 | sv_name = be_name = args[1]; |
| 5013 | /* split backend/server arg */ |
| 5014 | while (*sv_name && *(++sv_name)) { |
| 5015 | if (*sv_name == '/') { |
| 5016 | *sv_name = '\0'; |
| 5017 | ++sv_name; |
| 5018 | break; |
| 5019 | } |
| 5020 | } |
| 5021 | |
| 5022 | if (!*sv_name) |
| 5023 | return cli_err(appctx, "Require 'backend/server'."); |
| 5024 | |
| 5025 | /* 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] | 5026 | * requires thread isolation. In addition, any place referencing the |
| 5027 | * server about to be deleted would be unsafe after our operation, so |
| 5028 | * we must be certain to be alone so that no other thread has even |
| 5029 | * started to grab a temporary reference to this server. |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5030 | */ |
Willy Tarreau | ba3ab79 | 2021-08-04 14:42:37 +0200 | [diff] [blame] | 5031 | thread_isolate_full(); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5032 | |
| 5033 | get_backend_server(be_name, sv_name, &be, &srv); |
| 5034 | if (!be) { |
| 5035 | cli_err(appctx, "No such backend."); |
| 5036 | goto out; |
| 5037 | } |
| 5038 | |
| 5039 | if (!srv) { |
| 5040 | cli_err(appctx, "No such server."); |
| 5041 | goto out; |
| 5042 | } |
| 5043 | |
Amaury Denoyelle | 14c3c5c | 2021-08-23 14:10:51 +0200 | [diff] [blame] | 5044 | if (srv->flags & SRV_F_NON_PURGEABLE) { |
| 5045 | 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] | 5046 | goto out; |
| 5047 | } |
| 5048 | |
| 5049 | /* Only servers in maintenance can be deleted. This ensures that the |
| 5050 | * server is not present anymore in the lb structures (through |
| 5051 | * lbprm.set_server_status_down). |
| 5052 | */ |
| 5053 | if (!(srv->cur_admin & SRV_ADMF_MAINT)) { |
| 5054 | cli_err(appctx, "Only servers in maintenance mode can be deleted."); |
| 5055 | goto out; |
| 5056 | } |
| 5057 | |
| 5058 | /* Ensure that there is no active/idle/pending connection on the server. |
| 5059 | * |
| 5060 | * TODO idle connections should not prevent server deletion. A proper |
| 5061 | * cleanup function should be implemented to be used here. |
| 5062 | */ |
| 5063 | if (srv->cur_sess || srv->curr_idle_conns || |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 5064 | !eb_is_empty(&srv->queue.head)) { |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5065 | cli_err(appctx, "Server still has connections attached to it, cannot remove it."); |
| 5066 | goto out; |
| 5067 | } |
| 5068 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 5069 | /* removing cannot fail anymore when we reach this: |
| 5070 | * publishing EVENT_HDL_SUB_SERVER_DEL |
| 5071 | */ |
| 5072 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1); |
| 5073 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5074 | /* remove srv from tracking list */ |
| 5075 | if (srv->track) |
| 5076 | release_server_track(srv); |
| 5077 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5078 | /* stop the check task if running */ |
| 5079 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5080 | check_purge(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5081 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5082 | check_purge(&srv->agent); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5083 | |
| 5084 | /* detach the server from the proxy linked list |
| 5085 | * The proxy servers list is currently not protected by a lock, so this |
| 5086 | * requires thread_isolate/release. |
| 5087 | */ |
| 5088 | |
| 5089 | /* be->srv cannot be empty since we have already found the server with |
| 5090 | * get_backend_server */ |
| 5091 | BUG_ON(!be->srv); |
| 5092 | if (be->srv == srv) { |
| 5093 | be->srv = srv->next; |
| 5094 | } |
| 5095 | else { |
| 5096 | struct server *next; |
Amaury Denoyelle | d6b4b6d | 2021-04-21 11:50:26 +0200 | [diff] [blame] | 5097 | for (next = be->srv; srv != next->next; next = next->next) { |
| 5098 | /* srv cannot be not found since we have already found |
| 5099 | * it with get_backend_server */ |
| 5100 | BUG_ON(!next); |
| 5101 | } |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5102 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5103 | next->next = srv->next; |
| 5104 | } |
| 5105 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5106 | /* Some deleted servers could still point to us using their 'next', |
| 5107 | * update them as needed |
| 5108 | * Please note the small race between the POP and APPEND, although in |
| 5109 | * this situation this is not an issue as we are under full thread |
| 5110 | * isolation |
| 5111 | */ |
| 5112 | while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) { |
| 5113 | /* update its 'next' ptr */ |
| 5114 | prev_del->next = srv->next; |
| 5115 | if (srv->next) { |
| 5116 | /* now it is our 'next' responsibility */ |
| 5117 | MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted); |
| 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | /* we ourselves need to inform our 'next' that we will still point it */ |
| 5122 | if (srv->next) |
| 5123 | MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted); |
| 5124 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5125 | /* remove srv from addr_node tree */ |
Amaury Denoyelle | 82d7f77 | 2021-06-09 16:00:43 +0200 | [diff] [blame] | 5126 | eb32_delete(&srv->conf.id); |
| 5127 | ebpt_delete(&srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5128 | if (srv->addr_node.key) |
| 5129 | ebpt_delete(&srv->addr_node); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5130 | |
| 5131 | /* remove srv from idle_node tree for idle conn cleanup */ |
| 5132 | eb32_delete(&srv->idle_node); |
| 5133 | |
Aurelien DARRAGON | 75b9d1c | 2023-01-24 14:40:01 +0100 | [diff] [blame] | 5134 | /* flag the server as deleted |
| 5135 | * (despite the server being removed from primary server list, |
| 5136 | * one could still access the server data from a valid ptr) |
| 5137 | * Deleted flag helps detecting when a server is in transient removal |
| 5138 | * state. |
| 5139 | * ie: removed from the list but not yet freed/purged from memory. |
| 5140 | */ |
| 5141 | srv->flags |= SRV_F_DELETED; |
| 5142 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5143 | /* set LSB bit (odd bit) for reuse_cnt */ |
| 5144 | srv_id_reuse_cnt |= 1; |
| 5145 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5146 | thread_release(); |
| 5147 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5148 | ha_notice("Server deleted.\n"); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5149 | srv_drop(srv); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5150 | |
| 5151 | cli_msg(appctx, LOG_INFO, "Server deleted."); |
| 5152 | |
| 5153 | return 0; |
| 5154 | |
| 5155 | out: |
| 5156 | thread_release(); |
| 5157 | |
| 5158 | return 1; |
| 5159 | } |
| 5160 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 5161 | /* register cli keywords */ |
| 5162 | static struct cli_kw_list cli_kws = {{ },{ |
Amaury Denoyelle | c755efd | 2021-08-03 18:05:37 +0200 | [diff] [blame] | 5163 | { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL }, |
| 5164 | { { "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] | 5165 | { { "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] | 5166 | { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL }, |
| 5167 | { { "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] | 5168 | { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL }, |
| 5169 | { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL }, |
| 5170 | { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server }, |
| 5171 | { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight }, |
| 5172 | { { "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] | 5173 | { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL }, |
| 5174 | { { "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] | 5175 | {{},} |
| 5176 | }}; |
| 5177 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5178 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 5179 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5180 | /* Prepare a server <srv> to track check status of another one. <srv>.<trackit> |
| 5181 | * field is used to retrieve the identifier of the tracked server, either with |
| 5182 | * the format "proxy/server" or just "server". <curproxy> must point to the |
| 5183 | * backend owning <srv>; if no proxy is specified in <trackit>, it will be used |
| 5184 | * to find the tracked server. |
| 5185 | * |
| 5186 | * Returns 0 if the server track has been activated else non-zero. |
| 5187 | * |
| 5188 | * Not thread-safe. |
| 5189 | */ |
| 5190 | int srv_apply_track(struct server *srv, struct proxy *curproxy) |
| 5191 | { |
| 5192 | struct proxy *px; |
| 5193 | struct server *strack, *loop; |
| 5194 | char *pname, *sname; |
| 5195 | |
| 5196 | if (!srv->trackit) |
| 5197 | return 1; |
| 5198 | |
| 5199 | pname = srv->trackit; |
| 5200 | sname = strrchr(pname, '/'); |
| 5201 | |
| 5202 | if (sname) { |
| 5203 | *sname++ = '\0'; |
| 5204 | } |
| 5205 | else { |
| 5206 | sname = pname; |
| 5207 | pname = NULL; |
| 5208 | } |
| 5209 | |
| 5210 | if (pname) { |
| 5211 | px = proxy_be_by_name(pname); |
| 5212 | if (!px) { |
| 5213 | ha_alert("unable to find required proxy '%s' for tracking.\n", |
| 5214 | pname); |
| 5215 | return 1; |
| 5216 | } |
| 5217 | } |
| 5218 | else { |
| 5219 | px = curproxy; |
| 5220 | } |
| 5221 | |
| 5222 | strack = findserver(px, sname); |
| 5223 | if (!strack) { |
| 5224 | ha_alert("unable to find required server '%s' for tracking.\n", |
| 5225 | sname); |
| 5226 | return 1; |
| 5227 | } |
| 5228 | |
Amaury Denoyelle | 79f68be | 2021-07-13 10:35:50 +0200 | [diff] [blame] | 5229 | if (strack->flags & SRV_F_DYNAMIC) { |
| 5230 | ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n", |
| 5231 | px->id, strack->id); |
| 5232 | return 1; |
| 5233 | } |
| 5234 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5235 | if (!strack->do_check && !strack->do_agent && !strack->track && |
| 5236 | !strack->trackit) { |
| 5237 | ha_alert("unable to use %s/%s for " |
| 5238 | "tracking as it does not have any check nor agent enabled.\n", |
| 5239 | px->id, strack->id); |
| 5240 | return 1; |
| 5241 | } |
| 5242 | |
| 5243 | for (loop = strack->track; loop && loop != srv; loop = loop->track) |
| 5244 | ; |
| 5245 | |
| 5246 | if (srv == strack || loop) { |
| 5247 | ha_alert("unable to track %s/%s as it " |
| 5248 | "belongs to a tracking chain looping back to %s/%s.\n", |
| 5249 | px->id, strack->id, px->id, |
| 5250 | srv == strack ? strack->id : loop->id); |
| 5251 | return 1; |
| 5252 | } |
| 5253 | |
| 5254 | if (curproxy != px && |
| 5255 | (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) { |
| 5256 | ha_alert("unable to use %s/%s for" |
| 5257 | "tracking: disable-on-404 option inconsistency.\n", |
| 5258 | px->id, strack->id); |
| 5259 | return 1; |
| 5260 | } |
| 5261 | |
| 5262 | srv->track = strack; |
| 5263 | srv->tracknext = strack->trackers; |
| 5264 | strack->trackers = srv; |
Amaury Denoyelle | 0626961 | 2021-08-23 14:05:07 +0200 | [diff] [blame] | 5265 | strack->flags |= SRV_F_NON_PURGEABLE; |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5266 | |
| 5267 | ha_free(&srv->trackit); |
| 5268 | |
| 5269 | return 0; |
| 5270 | } |
| 5271 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5272 | /* |
Aurelien DARRAGON | 0ddf052 | 2023-03-27 11:57:28 +0200 | [diff] [blame^] | 5273 | * This function applies server's status changes. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5274 | * |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5275 | * Must be called with the server lock held. This may also be called at init |
| 5276 | * time as the result of parsing the state file, in which case no lock will be |
| 5277 | * held, and the server's warmup task can be null. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5278 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5279 | static void srv_update_status(struct server *s) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5280 | { |
| 5281 | struct check *check = &s->check; |
| 5282 | int xferred; |
| 5283 | struct proxy *px = s->proxy; |
| 5284 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 5285 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 5286 | int log_level; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5287 | struct buffer *tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5288 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5289 | /* If currently main is not set we try to apply pending state changes */ |
| 5290 | if (!(s->cur_admin & SRV_ADMF_MAINT)) { |
| 5291 | int next_admin; |
| 5292 | |
| 5293 | /* Backup next admin */ |
| 5294 | next_admin = s->next_admin; |
| 5295 | |
| 5296 | /* restore current admin state */ |
| 5297 | s->next_admin = s->cur_admin; |
| 5298 | |
| 5299 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 5300 | /* no maintenance + server DOWN: publish event SERVER DOWN */ |
| 5301 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0); |
| 5302 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5303 | s->last_change = now.tv_sec; |
| 5304 | if (s->proxy->lbprm.set_server_status_down) |
| 5305 | s->proxy->lbprm.set_server_status_down(s); |
| 5306 | |
| 5307 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5308 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5309 | |
| 5310 | /* we might have streams queued on this server and waiting for |
| 5311 | * a connection. Those which are redispatchable will be queued |
| 5312 | * to another server or to the proxy itself. |
| 5313 | */ |
| 5314 | xferred = pendconn_redistribute(s); |
| 5315 | |
| 5316 | tmptrash = alloc_trash_chunk(); |
| 5317 | if (tmptrash) { |
| 5318 | chunk_printf(tmptrash, |
| 5319 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5320 | s->proxy->id, s->id); |
| 5321 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5322 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5323 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5324 | |
| 5325 | /* we don't send an alert if the server was previously paused */ |
| 5326 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5327 | send_log(s->proxy, log_level, "%s.\n", |
| 5328 | tmptrash->area); |
| 5329 | send_email_alert(s, log_level, "%s", |
| 5330 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5331 | free_trash_chunk(tmptrash); |
| 5332 | tmptrash = NULL; |
| 5333 | } |
| 5334 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5335 | set_backend_down(s->proxy); |
| 5336 | |
| 5337 | s->counters.down_trans++; |
| 5338 | } |
| 5339 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
| 5340 | s->last_change = now.tv_sec; |
| 5341 | if (s->proxy->lbprm.set_server_status_down) |
| 5342 | s->proxy->lbprm.set_server_status_down(s); |
| 5343 | |
| 5344 | /* we might have streams queued on this server and waiting for |
| 5345 | * a connection. Those which are redispatchable will be queued |
| 5346 | * to another server or to the proxy itself. |
| 5347 | */ |
| 5348 | xferred = pendconn_redistribute(s); |
| 5349 | |
| 5350 | tmptrash = alloc_trash_chunk(); |
| 5351 | if (tmptrash) { |
| 5352 | chunk_printf(tmptrash, |
| 5353 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5354 | s->proxy->id, s->id); |
| 5355 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5356 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5357 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5358 | ha_warning("%s.\n", tmptrash->area); |
| 5359 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5360 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5361 | free_trash_chunk(tmptrash); |
| 5362 | tmptrash = NULL; |
| 5363 | } |
| 5364 | |
| 5365 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5366 | set_backend_down(s->proxy); |
| 5367 | } |
| 5368 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5369 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 5370 | /* no maintenance + server going UP: publish event SERVER UP */ |
| 5371 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0); |
| 5372 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5373 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5374 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5375 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5376 | s->proxy->last_change = now.tv_sec; |
| 5377 | } |
| 5378 | |
Amaury Denoyelle | fe2bf09 | 2020-10-29 15:59:04 +0100 | [diff] [blame] | 5379 | 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] | 5380 | s->down_time += now.tv_sec - s->last_change; |
| 5381 | |
| 5382 | s->last_change = now.tv_sec; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5383 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5384 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5385 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5386 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5387 | /* now propagate the status change to any LB algorithms */ |
| 5388 | if (px->lbprm.update_server_eweight) |
| 5389 | px->lbprm.update_server_eweight(s); |
| 5390 | else if (srv_willbe_usable(s)) { |
| 5391 | if (px->lbprm.set_server_status_up) |
| 5392 | px->lbprm.set_server_status_up(s); |
| 5393 | } |
| 5394 | else { |
| 5395 | if (px->lbprm.set_server_status_down) |
| 5396 | px->lbprm.set_server_status_down(s); |
| 5397 | } |
| 5398 | |
| 5399 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5400 | * and it's not a backup server and its effective weight is > 0, |
| 5401 | * then it can accept new connections, so we shut down all streams |
| 5402 | * on all backup servers. |
| 5403 | */ |
| 5404 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5405 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5406 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5407 | |
| 5408 | /* check if we can handle some connections queued at the proxy. We |
| 5409 | * will take as many as we can handle. |
| 5410 | */ |
| 5411 | xferred = pendconn_grab_from_px(s); |
| 5412 | |
| 5413 | tmptrash = alloc_trash_chunk(); |
| 5414 | if (tmptrash) { |
| 5415 | chunk_printf(tmptrash, |
| 5416 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5417 | s->proxy->id, s->id); |
| 5418 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5419 | srv_append_status(tmptrash, s, NULL, xferred, 0); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5420 | ha_warning("%s.\n", tmptrash->area); |
| 5421 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5422 | tmptrash->area); |
| 5423 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5424 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5425 | free_trash_chunk(tmptrash); |
| 5426 | tmptrash = NULL; |
| 5427 | } |
| 5428 | |
| 5429 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5430 | set_backend_down(s->proxy); |
| 5431 | } |
| 5432 | else if (s->cur_eweight != s->next_eweight) { |
| 5433 | /* now propagate the status change to any LB algorithms */ |
| 5434 | if (px->lbprm.update_server_eweight) |
| 5435 | px->lbprm.update_server_eweight(s); |
| 5436 | else if (srv_willbe_usable(s)) { |
| 5437 | if (px->lbprm.set_server_status_up) |
| 5438 | px->lbprm.set_server_status_up(s); |
| 5439 | } |
| 5440 | else { |
| 5441 | if (px->lbprm.set_server_status_down) |
| 5442 | px->lbprm.set_server_status_down(s); |
| 5443 | } |
| 5444 | |
| 5445 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5446 | set_backend_down(s->proxy); |
| 5447 | } |
| 5448 | |
| 5449 | s->next_admin = next_admin; |
| 5450 | } |
| 5451 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 5452 | /* reset operational state change */ |
| 5453 | *s->op_st_chg.reason = 0; |
| 5454 | s->op_st_chg.status = s->op_st_chg.code = -1; |
| 5455 | s->op_st_chg.duration = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5456 | |
| 5457 | /* Now we try to apply pending admin changes */ |
| 5458 | |
| 5459 | /* Maintenance must also disable health checks */ |
| 5460 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5461 | if (s->check.state & CHK_ST_ENABLED) { |
| 5462 | s->check.state |= CHK_ST_PAUSED; |
| 5463 | check->health = 0; |
| 5464 | } |
| 5465 | |
| 5466 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5467 | tmptrash = alloc_trash_chunk(); |
| 5468 | if (tmptrash) { |
| 5469 | chunk_printf(tmptrash, |
| 5470 | "%sServer %s/%s was DOWN and now enters maintenance%s%s%s", |
| 5471 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5472 | *(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] | 5473 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5474 | 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] | 5475 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5476 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5477 | ha_warning("%s.\n", tmptrash->area); |
| 5478 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5479 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5480 | } |
| 5481 | free_trash_chunk(tmptrash); |
| 5482 | tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5483 | } |
Emeric Brun | 8f29829 | 2017-12-06 16:47:17 +0100 | [diff] [blame] | 5484 | /* commit new admin status */ |
| 5485 | |
| 5486 | s->cur_admin = s->next_admin; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5487 | } |
| 5488 | else { /* server was still running */ |
| 5489 | check->health = 0; /* failure */ |
| 5490 | s->last_change = now.tv_sec; |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5491 | |
| 5492 | s->next_state = SRV_ST_STOPPED; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5493 | if (s->proxy->lbprm.set_server_status_down) |
| 5494 | s->proxy->lbprm.set_server_status_down(s); |
| 5495 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5496 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5497 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5498 | |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 5499 | /* maintenance on previously running server: publish event SERVER DOWN */ |
| 5500 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0); |
| 5501 | |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5502 | /* force connection cleanup on the given server */ |
| 5503 | srv_cleanup_connections(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5504 | /* we might have streams queued on this server and waiting for |
| 5505 | * a connection. Those which are redispatchable will be queued |
| 5506 | * to another server or to the proxy itself. |
| 5507 | */ |
| 5508 | xferred = pendconn_redistribute(s); |
| 5509 | |
| 5510 | tmptrash = alloc_trash_chunk(); |
| 5511 | if (tmptrash) { |
| 5512 | chunk_printf(tmptrash, |
| 5513 | "%sServer %s/%s is going DOWN for maintenance%s%s%s", |
| 5514 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5515 | s->proxy->id, s->id, |
| 5516 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5517 | |
| 5518 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
| 5519 | |
| 5520 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5521 | ha_warning("%s.\n", tmptrash->area); |
| 5522 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5523 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5524 | } |
| 5525 | free_trash_chunk(tmptrash); |
| 5526 | tmptrash = NULL; |
| 5527 | } |
| 5528 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5529 | set_backend_down(s->proxy); |
| 5530 | |
| 5531 | s->counters.down_trans++; |
| 5532 | } |
| 5533 | } |
| 5534 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5535 | /* OK here we're leaving maintenance, we have many things to check, |
| 5536 | * because the server might possibly be coming back up depending on |
| 5537 | * its state. In practice, leaving maintenance means that we should |
| 5538 | * immediately turn to UP (more or less the slowstart) under the |
| 5539 | * following conditions : |
| 5540 | * - server is neither checked nor tracked |
| 5541 | * - server tracks another server which is not checked |
| 5542 | * - server tracks another server which is already up |
| 5543 | * Which sums up as something simpler : |
| 5544 | * "either the tracking server is up or the server's checks are disabled |
| 5545 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5546 | * case associated to the stopping state which can be inherited. Note |
| 5547 | * that the server might still be in drain mode, which is naturally dealt |
| 5548 | * with by the lower level functions. |
| 5549 | */ |
| 5550 | |
| 5551 | if (s->check.state & CHK_ST_ENABLED) { |
| 5552 | s->check.state &= ~CHK_ST_PAUSED; |
| 5553 | check->health = check->rise; /* start OK but check immediately */ |
| 5554 | } |
| 5555 | |
| 5556 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5557 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5558 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5559 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5560 | s->next_state = SRV_ST_STOPPING; |
| 5561 | } |
| 5562 | else { |
Willy Tarreau | d332f13 | 2021-08-04 19:35:13 +0200 | [diff] [blame] | 5563 | s->last_change = now.tv_sec; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5564 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5565 | if (s->slowstart > 0) { |
| 5566 | if (s->warmup) |
| 5567 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5568 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5569 | else |
| 5570 | s->next_state = SRV_ST_RUNNING; |
| 5571 | } |
| 5572 | |
| 5573 | } |
| 5574 | |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 5575 | /* ignore if server stays down when leaving maintenance mode */ |
| 5576 | if (s->next_state != SRV_ST_STOPPED) { |
| 5577 | /* leaving maintenance + server UP: publish event SERVER UP */ |
| 5578 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0); |
| 5579 | } |
| 5580 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5581 | tmptrash = alloc_trash_chunk(); |
| 5582 | if (tmptrash) { |
| 5583 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5584 | chunk_printf(tmptrash, |
| 5585 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5586 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5587 | s->proxy->id, s->id, |
| 5588 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5589 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5590 | } |
| 5591 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5592 | chunk_printf(tmptrash, |
| 5593 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5594 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5595 | s->proxy->id, s->id, s->hostname, |
| 5596 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5597 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5598 | } |
| 5599 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5600 | chunk_printf(tmptrash, |
| 5601 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5602 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5603 | s->proxy->id, s->id, |
| 5604 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5605 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5606 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5607 | ha_warning("%s.\n", tmptrash->area); |
| 5608 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5609 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5610 | free_trash_chunk(tmptrash); |
| 5611 | tmptrash = NULL; |
| 5612 | } |
| 5613 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5614 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5615 | /* now propagate the status change to any LB algorithms */ |
| 5616 | if (px->lbprm.update_server_eweight) |
| 5617 | px->lbprm.update_server_eweight(s); |
| 5618 | else if (srv_willbe_usable(s)) { |
| 5619 | if (px->lbprm.set_server_status_up) |
| 5620 | px->lbprm.set_server_status_up(s); |
| 5621 | } |
| 5622 | else { |
| 5623 | if (px->lbprm.set_server_status_down) |
| 5624 | px->lbprm.set_server_status_down(s); |
| 5625 | } |
| 5626 | |
| 5627 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5628 | set_backend_down(s->proxy); |
Willy Tarreau | d332f13 | 2021-08-04 19:35:13 +0200 | [diff] [blame] | 5629 | else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) |
| 5630 | s->proxy->last_change = now.tv_sec; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5631 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5632 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5633 | * and it's not a backup server and its effective weight is > 0, |
| 5634 | * then it can accept new connections, so we shut down all streams |
| 5635 | * on all backup servers. |
| 5636 | */ |
| 5637 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5638 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5639 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5640 | |
| 5641 | /* check if we can handle some connections queued at the proxy. We |
| 5642 | * will take as many as we can handle. |
| 5643 | */ |
| 5644 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5645 | } |
| 5646 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5647 | /* remaining in maintenance mode, let's inform precisely about the |
| 5648 | * situation. |
| 5649 | */ |
| 5650 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5651 | tmptrash = alloc_trash_chunk(); |
| 5652 | if (tmptrash) { |
| 5653 | chunk_printf(tmptrash, |
| 5654 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5655 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5656 | s->proxy->id, s->id); |
| 5657 | |
| 5658 | if (s->track) /* normally it's mandatory here */ |
| 5659 | chunk_appendf(tmptrash, " via %s/%s", |
| 5660 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5661 | ha_warning("%s.\n", tmptrash->area); |
| 5662 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5663 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5664 | free_trash_chunk(tmptrash); |
| 5665 | tmptrash = NULL; |
| 5666 | } |
| 5667 | } |
| 5668 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5669 | tmptrash = alloc_trash_chunk(); |
| 5670 | if (tmptrash) { |
| 5671 | chunk_printf(tmptrash, |
| 5672 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5673 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5674 | s->proxy->id, s->id, s->hostname); |
| 5675 | |
| 5676 | if (s->track) /* normally it's mandatory here */ |
| 5677 | chunk_appendf(tmptrash, " via %s/%s", |
| 5678 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5679 | ha_warning("%s.\n", tmptrash->area); |
| 5680 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5681 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5682 | free_trash_chunk(tmptrash); |
| 5683 | tmptrash = NULL; |
| 5684 | } |
| 5685 | } |
| 5686 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5687 | tmptrash = alloc_trash_chunk(); |
| 5688 | if (tmptrash) { |
| 5689 | chunk_printf(tmptrash, |
| 5690 | "%sServer %s/%s remains in forced maintenance", |
| 5691 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5692 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5693 | ha_warning("%s.\n", tmptrash->area); |
| 5694 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5695 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5696 | free_trash_chunk(tmptrash); |
| 5697 | tmptrash = NULL; |
| 5698 | } |
| 5699 | } |
| 5700 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
| 5701 | |
| 5702 | s->cur_admin = s->next_admin; |
| 5703 | } |
| 5704 | |
| 5705 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5706 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5707 | /* drain state is applied only if not yet in maint */ |
| 5708 | |
| 5709 | s->last_change = now.tv_sec; |
| 5710 | if (px->lbprm.set_server_status_down) |
| 5711 | px->lbprm.set_server_status_down(s); |
| 5712 | |
| 5713 | /* we might have streams queued on this server and waiting for |
| 5714 | * a connection. Those which are redispatchable will be queued |
| 5715 | * to another server or to the proxy itself. |
| 5716 | */ |
| 5717 | xferred = pendconn_redistribute(s); |
| 5718 | |
| 5719 | tmptrash = alloc_trash_chunk(); |
| 5720 | if (tmptrash) { |
| 5721 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s", |
| 5722 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id, |
| 5723 | *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : ""); |
| 5724 | |
| 5725 | srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
| 5726 | |
| 5727 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5728 | ha_warning("%s.\n", tmptrash->area); |
| 5729 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5730 | tmptrash->area); |
| 5731 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5732 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5733 | } |
| 5734 | free_trash_chunk(tmptrash); |
| 5735 | tmptrash = NULL; |
| 5736 | } |
| 5737 | |
| 5738 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5739 | set_backend_down(s->proxy); |
| 5740 | } |
| 5741 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5742 | /* OK completely leaving drain mode */ |
| 5743 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5744 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5745 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5746 | s->proxy->last_change = now.tv_sec; |
| 5747 | } |
| 5748 | |
| 5749 | if (s->last_change < now.tv_sec) // ignore negative times |
| 5750 | s->down_time += now.tv_sec - s->last_change; |
| 5751 | s->last_change = now.tv_sec; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5752 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5753 | |
| 5754 | tmptrash = alloc_trash_chunk(); |
| 5755 | if (tmptrash) { |
| 5756 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5757 | chunk_printf(tmptrash, |
| 5758 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5759 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5760 | s->proxy->id, s->id, |
| 5761 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5762 | } |
| 5763 | else { |
| 5764 | chunk_printf(tmptrash, |
| 5765 | "%sServer %s/%s is %s (leaving drain)", |
| 5766 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5767 | s->proxy->id, s->id, |
| 5768 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5769 | if (s->track) /* normally it's mandatory here */ |
| 5770 | chunk_appendf(tmptrash, " via %s/%s", |
| 5771 | s->track->proxy->id, s->track->id); |
| 5772 | } |
| 5773 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5774 | ha_warning("%s.\n", tmptrash->area); |
| 5775 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5776 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5777 | free_trash_chunk(tmptrash); |
| 5778 | tmptrash = NULL; |
| 5779 | } |
| 5780 | |
| 5781 | /* now propagate the status change to any LB algorithms */ |
| 5782 | if (px->lbprm.update_server_eweight) |
| 5783 | px->lbprm.update_server_eweight(s); |
| 5784 | else if (srv_willbe_usable(s)) { |
| 5785 | if (px->lbprm.set_server_status_up) |
| 5786 | px->lbprm.set_server_status_up(s); |
| 5787 | } |
| 5788 | else { |
| 5789 | if (px->lbprm.set_server_status_down) |
| 5790 | px->lbprm.set_server_status_down(s); |
| 5791 | } |
| 5792 | } |
| 5793 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5794 | /* remaining in drain mode after removing one of its flags */ |
| 5795 | |
| 5796 | tmptrash = alloc_trash_chunk(); |
| 5797 | if (tmptrash) { |
| 5798 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5799 | chunk_printf(tmptrash, |
| 5800 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
| 5801 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5802 | s->proxy->id, s->id); |
| 5803 | |
| 5804 | if (s->track) /* normally it's mandatory here */ |
| 5805 | chunk_appendf(tmptrash, " via %s/%s", |
| 5806 | s->track->proxy->id, s->track->id); |
| 5807 | } |
| 5808 | else { |
| 5809 | chunk_printf(tmptrash, |
| 5810 | "%sServer %s/%s remains in forced drain mode", |
| 5811 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5812 | s->proxy->id, s->id); |
| 5813 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5814 | ha_warning("%s.\n", tmptrash->area); |
| 5815 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5816 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5817 | free_trash_chunk(tmptrash); |
| 5818 | tmptrash = NULL; |
| 5819 | } |
| 5820 | |
| 5821 | /* commit new admin status */ |
| 5822 | |
| 5823 | s->cur_admin = s->next_admin; |
| 5824 | } |
| 5825 | } |
| 5826 | |
| 5827 | /* Re-set log strings to empty */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5828 | *s->adm_st_chg_cause = 0; |
| 5829 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5830 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5831 | 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] | 5832 | { |
| 5833 | struct connection *conn; |
| 5834 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5835 | while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns, |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5836 | struct connection *, toremove_list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5837 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | return task; |
| 5841 | } |
| 5842 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5843 | /* Move toremove_nb connections from idle_tree to toremove_list, -1 means |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5844 | * moving them all. |
| 5845 | * Returns the number of connections moved. |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5846 | * |
| 5847 | * Must be called with idle_conns_lock held. |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5848 | */ |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5849 | 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] | 5850 | { |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5851 | struct eb_node *node, *next; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5852 | struct conn_hash_node *hash_node; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5853 | int i = 0; |
| 5854 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5855 | node = eb_first(idle_tree); |
| 5856 | while (node) { |
| 5857 | next = eb_next(node); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5858 | if (toremove_nb != -1 && i >= toremove_nb) |
| 5859 | break; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5860 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5861 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5862 | eb_delete(node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5863 | MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5864 | i++; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5865 | |
| 5866 | node = next; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5867 | } |
| 5868 | return i; |
| 5869 | } |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5870 | /* cleanup connections for a given server |
| 5871 | * might be useful when going on forced maintenance or live changing ip/port |
| 5872 | */ |
William Dauchy | 707ad32 | 2020-05-04 13:52:40 +0200 | [diff] [blame] | 5873 | static void srv_cleanup_connections(struct server *srv) |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5874 | { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5875 | int did_remove; |
| 5876 | int i; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5877 | |
Amaury Denoyelle | 10d5c31 | 2021-01-06 14:28:51 +0100 | [diff] [blame] | 5878 | /* nothing to do if pool-max-conn is null */ |
| 5879 | if (!srv->max_idle_conns) |
| 5880 | return; |
| 5881 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5882 | /* check all threads starting with ours */ |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5883 | for (i = tid;;) { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5884 | did_remove = 0; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5885 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5886 | 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] | 5887 | did_remove = 1; |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5888 | 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] | 5889 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5890 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5891 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5892 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5893 | |
| 5894 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 5895 | break; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5896 | } |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5897 | } |
| 5898 | |
| 5899 | /* removes an idle conn after updating the server idle conns counters */ |
| 5900 | void srv_release_conn(struct server *srv, struct connection *conn) |
| 5901 | { |
| 5902 | if (conn->flags & CO_FL_LIST_MASK) { |
| 5903 | /* The connection is currently in the server's idle list, so tell it |
| 5904 | * there's one less connection available in that list. |
| 5905 | */ |
| 5906 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5907 | _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb); |
| 5908 | _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]); |
| 5909 | } |
| 5910 | else { |
| 5911 | /* The connection is not private and not in any server's idle |
| 5912 | * list, so decrement the current number of used connections |
| 5913 | */ |
| 5914 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5915 | } |
| 5916 | |
| 5917 | /* Remove the connection from any tree (safe, idle or available) */ |
| 5918 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5919 | conn_delete_from_tree(&conn->hash_node->node); |
Christopher Faulet | 5e1b0e7 | 2023-02-28 15:39:38 +0100 | [diff] [blame] | 5920 | conn->flags &= ~CO_FL_LIST_MASK; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5921 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5922 | } |
| 5923 | |
| 5924 | /* retrieve a connection from its <hash> in <tree> |
| 5925 | * returns NULL if no connection found |
| 5926 | */ |
| 5927 | struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash) |
| 5928 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5929 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5930 | struct connection *conn = NULL; |
| 5931 | struct conn_hash_node *hash_node = NULL; |
| 5932 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5933 | node = eb64_lookup(tree, hash); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5934 | if (node) { |
| 5935 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 5936 | conn = hash_node->conn; |
| 5937 | } |
| 5938 | |
| 5939 | return conn; |
| 5940 | } |
| 5941 | |
| 5942 | /* retrieve the next connection sharing the same hash as <conn> |
| 5943 | * returns NULL if no connection found |
| 5944 | */ |
| 5945 | struct connection *srv_lookup_conn_next(struct connection *conn) |
| 5946 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5947 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5948 | struct connection *next_conn = NULL; |
| 5949 | struct conn_hash_node *hash_node = NULL; |
| 5950 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5951 | node = eb64_next_dup(&conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5952 | if (node) { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5953 | hash_node = eb64_entry(node, struct conn_hash_node, node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5954 | next_conn = hash_node->conn; |
| 5955 | } |
| 5956 | |
| 5957 | return next_conn; |
| 5958 | } |
| 5959 | |
| 5960 | /* This adds an idle connection to the server's list if the connection is |
| 5961 | * reusable, not held by any owner anymore, but still has available streams. |
| 5962 | */ |
| 5963 | int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe) |
| 5964 | { |
| 5965 | /* we try to keep the connection in the server's idle list |
| 5966 | * if we don't have too many FD in use, and if the number of |
| 5967 | * idle+current conns is lower than what was observed before |
| 5968 | * last purge, or if we already don't have idle conns for the |
| 5969 | * current thread and we don't exceed last count by global.nbthread. |
| 5970 | */ |
| 5971 | if (!(conn->flags & CO_FL_PRIVATE) && |
| 5972 | srv && srv->pool_purge_delay > 0 && |
| 5973 | ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) && |
| 5974 | ha_used_fds < global.tune.pool_high_count && |
| 5975 | (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) && |
| 5976 | ((eb_is_empty(&srv->per_thr[tid].safe_conns) && |
| 5977 | (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) || |
| 5978 | (ha_used_fds < global.tune.pool_low_count && |
| 5979 | (srv->curr_used_conns + srv->curr_idle_conns <= |
| 5980 | MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) && |
| 5981 | !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) { |
| 5982 | int retadd; |
| 5983 | |
| 5984 | retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1); |
| 5985 | if (retadd > srv->max_idle_conns) { |
| 5986 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5987 | return 0; |
| 5988 | } |
| 5989 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5990 | |
| 5991 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5992 | conn_delete_from_tree(&conn->hash_node->node); |
| 5993 | |
| 5994 | if (is_safe) { |
| 5995 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5996 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5997 | _HA_ATOMIC_INC(&srv->curr_safe_nb); |
| 5998 | } else { |
| 5999 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6000 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6001 | _HA_ATOMIC_INC(&srv->curr_idle_nb); |
| 6002 | } |
| 6003 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6004 | _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]); |
| 6005 | |
| 6006 | __ha_barrier_full(); |
| 6007 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 6008 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6009 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 6010 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 6011 | now_ms); |
| 6012 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 6013 | if (!task_in_wq(idle_conn_task) && ! |
| 6014 | task_in_rq(idle_conn_task)) { |
| 6015 | task_schedule(idle_conn_task, |
| 6016 | srv->idle_node.key); |
| 6017 | } |
| 6018 | |
| 6019 | } |
| 6020 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6021 | } |
| 6022 | return 1; |
| 6023 | } |
| 6024 | return 0; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 6025 | } |
| 6026 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6027 | 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] | 6028 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6029 | struct server *srv; |
| 6030 | struct eb32_node *eb; |
| 6031 | int i; |
| 6032 | unsigned int next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 6033 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6034 | next_wakeup = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6035 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6036 | while (1) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6037 | int exceed_conns; |
| 6038 | int to_kill; |
| 6039 | int curr_idle; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 6040 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6041 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 6042 | if (!eb) { |
| 6043 | /* we might have reached the end of the tree, typically because |
| 6044 | * <now_ms> is in the first half and we're first scanning the last |
| 6045 | * half. Let's loop back to the beginning of the tree now. |
| 6046 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6047 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6048 | eb = eb32_first(&idle_conn_srv); |
| 6049 | if (likely(!eb)) |
| 6050 | break; |
| 6051 | } |
| 6052 | if (tick_is_lt(now_ms, eb->key)) { |
| 6053 | /* timer not expired yet, revisit it later */ |
| 6054 | next_wakeup = eb->key; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6055 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6056 | } |
| 6057 | srv = eb32_entry(eb, struct server, idle_node); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6058 | |
| 6059 | /* Calculate how many idle connections we want to kill : |
| 6060 | * we want to remove half the difference between the total |
| 6061 | * of established connections (used or idle) and the max |
| 6062 | * number of used connections. |
| 6063 | */ |
| 6064 | curr_idle = srv->curr_idle_conns; |
| 6065 | if (curr_idle == 0) |
| 6066 | goto remove; |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6067 | 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] | 6068 | exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1); |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6069 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6070 | 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] | 6071 | if (srv->est_need_conns < srv->max_used_conns) |
| 6072 | srv->est_need_conns = srv->max_used_conns; |
| 6073 | |
Willy Tarreau | 9dc231a | 2022-11-21 14:32:33 +0100 | [diff] [blame] | 6074 | HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6075 | |
Willy Tarreau | 18ed789 | 2020-07-02 19:05:30 +0200 | [diff] [blame] | 6076 | if (exceed_conns <= 0) |
| 6077 | goto remove; |
| 6078 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6079 | /* check all threads starting with ours */ |
| 6080 | for (i = tid;;) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6081 | int max_conn; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6082 | int j; |
| 6083 | int did_remove = 0; |
| 6084 | |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6085 | max_conn = (exceed_conns * srv->curr_idle_thr[i]) / |
| 6086 | curr_idle + 1; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6087 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6088 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6089 | 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] | 6090 | if (j > 0) |
| 6091 | did_remove = 1; |
| 6092 | if (max_conn - j > 0 && |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6093 | 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] | 6094 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6095 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6096 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6097 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 6098 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6099 | |
| 6100 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 6101 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6102 | } |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6103 | remove: |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6104 | eb32_delete(&srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6105 | |
| 6106 | if (srv->curr_idle_conns) { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6107 | /* There are still more idle connections, add the |
| 6108 | * server back in the tree. |
| 6109 | */ |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6110 | srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6111 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6112 | next_wakeup = tick_first(next_wakeup, srv->idle_node.key); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6113 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6114 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6115 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6116 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6117 | task->expire = next_wakeup; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6118 | return task; |
| 6119 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6120 | |
Amaury Denoyelle | 3109ccf | 2021-04-29 17:30:05 +0200 | [diff] [blame] | 6121 | /* Close remaining idle connections. This functions is designed to be run on |
| 6122 | * process shutdown. This guarantees a proper socket shutdown to avoid |
| 6123 | * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is |
| 6124 | * bypassed. |
| 6125 | * |
| 6126 | * This function is not thread-safe so it must only be called via a global |
| 6127 | * deinit function. |
| 6128 | */ |
| 6129 | static void srv_close_idle_conns(struct server *srv) |
| 6130 | { |
| 6131 | struct eb_root **cleaned_tree; |
| 6132 | int i; |
| 6133 | |
| 6134 | for (i = 0; i < global.nbthread; ++i) { |
| 6135 | struct eb_root *conn_trees[] = { |
| 6136 | &srv->per_thr[i].idle_conns, |
| 6137 | &srv->per_thr[i].safe_conns, |
| 6138 | &srv->per_thr[i].avail_conns, |
| 6139 | NULL |
| 6140 | }; |
| 6141 | |
| 6142 | for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) { |
| 6143 | while (!eb_is_empty(*cleaned_tree)) { |
| 6144 | struct ebmb_node *node = ebmb_first(*cleaned_tree); |
| 6145 | struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 6146 | struct connection *conn = conn_hash_node->conn; |
| 6147 | |
| 6148 | if (conn->ctrl->ctrl_close) |
| 6149 | conn->ctrl->ctrl_close(conn); |
| 6150 | ebmb_delete(node); |
| 6151 | } |
| 6152 | } |
| 6153 | } |
| 6154 | } |
| 6155 | |
| 6156 | REGISTER_SERVER_DEINIT(srv_close_idle_conns); |
| 6157 | |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6158 | /* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */ |
| 6159 | 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] | 6160 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6161 | char **err) |
| 6162 | { |
| 6163 | if (too_many_args(1, args, err, NULL)) |
| 6164 | return -1; |
| 6165 | |
| 6166 | if (strcmp(args[1], "on") == 0) |
| 6167 | global.tune.options |= GTUNE_IDLE_POOL_SHARED; |
| 6168 | else if (strcmp(args[1], "off") == 0) |
| 6169 | global.tune.options &= ~GTUNE_IDLE_POOL_SHARED; |
| 6170 | else { |
| 6171 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 6172 | return -1; |
| 6173 | } |
| 6174 | return 0; |
| 6175 | } |
| 6176 | |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6177 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 6178 | 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] | 6179 | const struct proxy *defpx, const char *file, int line, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6180 | char **err) |
| 6181 | { |
| 6182 | int arg = -1; |
| 6183 | |
| 6184 | if (too_many_args(1, args, err, NULL)) |
| 6185 | return -1; |
| 6186 | |
| 6187 | if (*(args[1]) != 0) |
| 6188 | arg = atoi(args[1]); |
| 6189 | |
| 6190 | if (arg < 0 || arg > 100) { |
| 6191 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 6192 | return -1; |
| 6193 | } |
| 6194 | |
| 6195 | if (args[0][10] == 'h') |
| 6196 | global.tune.pool_high_ratio = arg; |
| 6197 | else |
| 6198 | global.tune.pool_low_ratio = arg; |
| 6199 | return 0; |
| 6200 | } |
| 6201 | |
| 6202 | /* config keyword parsers */ |
| 6203 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6204 | { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared }, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6205 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6206 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6207 | { 0, NULL, NULL } |
| 6208 | }}; |
| 6209 | |
| 6210 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 6211 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 6212 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6213 | * Local variables: |
| 6214 | * c-indent-level: 8 |
| 6215 | * c-basic-offset: 8 |
| 6216 | * End: |
| 6217 | */ |