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 | |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 107 | static const char *srv_adm_st_chg_cause_str[] = { |
| 108 | [SRV_ADM_STCHGC_NONE] = "", |
| 109 | [SRV_ADM_STCHGC_DNS_NOENT] = "entry removed from SRV record", |
| 110 | [SRV_ADM_STCHGC_DNS_NOIP] = "No IP for server ", |
| 111 | [SRV_ADM_STCHGC_DNS_NX] = "DNS NX status", |
| 112 | [SRV_ADM_STCHGC_DNS_TIMEOUT] = "DNS timeout status", |
| 113 | [SRV_ADM_STCHGC_DNS_REFUSED] = "DNS refused status", |
| 114 | [SRV_ADM_STCHGC_DNS_UNSPEC] = "unspecified DNS error", |
| 115 | [SRV_ADM_STCHGC_STATS_DISABLE] = "'disable' on stats page", |
| 116 | [SRV_ADM_STCHGC_STATS_STOP] = "'stop' on stats page" |
| 117 | }; |
| 118 | |
| 119 | const char *srv_adm_st_chg_cause(enum srv_adm_st_chg_cause cause) |
| 120 | { |
| 121 | return srv_adm_st_chg_cause_str[cause]; |
| 122 | } |
| 123 | |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 124 | static const char *srv_op_st_chg_cause_str[] = { |
| 125 | [SRV_OP_STCHGC_NONE] = "", |
| 126 | [SRV_OP_STCHGC_HEALTH] = "", |
| 127 | [SRV_OP_STCHGC_AGENT] = "", |
| 128 | [SRV_OP_STCHGC_CLI] = "changed from CLI", |
| 129 | [SRV_OP_STCHGC_LUA] = "changed from Lua script", |
| 130 | [SRV_OP_STCHGC_STATS_WEB] = "changed from Web interface", |
| 131 | [SRV_OP_STCHGC_STATEFILE] = "changed from server-state after a reload" |
| 132 | }; |
| 133 | |
| 134 | const char *srv_op_st_chg_cause(enum srv_op_st_chg_cause cause) |
| 135 | { |
| 136 | return srv_op_st_chg_cause_str[cause]; |
| 137 | } |
| 138 | |
Simon Horman | a360844 | 2013-11-01 16:46:15 +0900 | [diff] [blame] | 139 | int srv_downtime(const struct server *s) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 140 | { |
Amaury Denoyelle | e6ba791 | 2020-10-29 15:59:05 +0100 | [diff] [blame] | 141 | 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] | 142 | return s->down_time; |
| 143 | |
| 144 | return now.tv_sec - s->last_change + s->down_time; |
| 145 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 147 | int srv_lastsession(const struct server *s) |
| 148 | { |
| 149 | if (s->counters.last_sess) |
| 150 | return now.tv_sec - s->counters.last_sess; |
| 151 | |
| 152 | return -1; |
| 153 | } |
| 154 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 155 | int srv_getinter(const struct check *check) |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 156 | { |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 157 | const struct server *s = check->server; |
| 158 | |
Aurelien DARRAGON | 7d541a9 | 2022-12-07 12:17:24 +0100 | [diff] [blame] | 159 | if ((check->state & (CHK_ST_CONFIGURED|CHK_ST_FASTINTER)) == CHK_ST_CONFIGURED && |
| 160 | (check->health == check->rise + check->fall - 1)) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 161 | return check->inter; |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 162 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 163 | if ((s->next_state == SRV_ST_STOPPED) && check->health == 0) |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 164 | return (check->downinter)?(check->downinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 165 | |
Simon Horman | 4a74143 | 2013-02-23 15:35:38 +0900 | [diff] [blame] | 166 | return (check->fastinter)?(check->fastinter):(check->inter); |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 169 | /* |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 170 | * Use this to publish EVENT_HDL_SUB_SERVER family type event |
| 171 | * from srv facility |
| 172 | * Event will be published in both global subscription list and |
| 173 | * server dedicated subscription list |
| 174 | * server ptr must be valid |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 175 | * must be called with srv lock or under thread_isolate |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 176 | */ |
| 177 | static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate) |
| 178 | { |
| 179 | struct event_hdl_cb_data_server cb_data; |
| 180 | |
| 181 | /* safe data assignments */ |
| 182 | cb_data.safe.puid = srv->puid; |
| 183 | cb_data.safe.rid = srv->rid; |
| 184 | cb_data.safe.flags = srv->flags; |
| 185 | snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id); |
Aurelien DARRAGON | d714213 | 2023-03-22 17:35:47 +0100 | [diff] [blame] | 186 | cb_data.safe.proxy_name[0] = '\0'; |
| 187 | cb_data.safe.proxy_uuid = -1; /* default value */ |
| 188 | if (srv->proxy) { |
| 189 | cb_data.safe.proxy_uuid = srv->proxy->uuid; |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 190 | snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id); |
Aurelien DARRAGON | d714213 | 2023-03-22 17:35:47 +0100 | [diff] [blame] | 191 | } |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 192 | /* unsafe data assignments */ |
| 193 | cb_data.unsafe.ptr = srv; |
| 194 | cb_data.unsafe.thread_isolate = thread_isolate; |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 195 | cb_data.unsafe.srv_lock = !thread_isolate; |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 196 | /* publish in server dedicated sub list */ |
| 197 | event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data)); |
| 198 | /* publish in global subscription list */ |
| 199 | event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data)); |
| 200 | } |
| 201 | |
| 202 | /* |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 203 | * Check that we did not get a hash collision. |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 204 | * Unlikely, but it can happen. The server's proxy must be at least |
| 205 | * read-locked. |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 206 | */ |
| 207 | static inline void srv_check_for_dup_dyncookie(struct server *s) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 208 | { |
| 209 | struct proxy *p = s->proxy; |
| 210 | struct server *tmpserv; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 211 | |
| 212 | for (tmpserv = p->srv; tmpserv != NULL; |
| 213 | tmpserv = tmpserv->next) { |
| 214 | if (tmpserv == s) |
| 215 | continue; |
| 216 | if (tmpserv->next_admin & SRV_ADMF_FMAINT) |
| 217 | continue; |
| 218 | if (tmpserv->cookie && |
| 219 | strcmp(tmpserv->cookie, s->cookie) == 0) { |
| 220 | ha_warning("We generated two equal cookies for two different servers.\n" |
| 221 | "Please change the secret key for '%s'.\n", |
| 222 | s->proxy->id); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 228 | /* |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 229 | * 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] | 230 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 231 | void srv_set_dyncookie(struct server *s) |
| 232 | { |
| 233 | struct proxy *p = s->proxy; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 234 | char *tmpbuf; |
| 235 | unsigned long long hash_value; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 236 | size_t key_len; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 237 | size_t buffer_len; |
| 238 | int addr_len; |
| 239 | int port; |
| 240 | |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 241 | HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 242 | |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 243 | if ((s->flags & SRV_F_COOKIESET) || |
| 244 | !(s->proxy->ck_opts & PR_CK_DYNAMIC) || |
| 245 | s->proxy->dyncookie_key == NULL) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 246 | goto out; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 247 | key_len = strlen(p->dyncookie_key); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 248 | |
| 249 | if (s->addr.ss_family != AF_INET && |
| 250 | s->addr.ss_family != AF_INET6) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 251 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Buffer to calculate the cookie value. |
| 254 | * The buffer contains the secret key + the server IP address |
| 255 | * + the TCP port. |
| 256 | */ |
| 257 | addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16; |
| 258 | /* |
| 259 | * The TCP port should use only 2 bytes, but is stored in |
| 260 | * an unsigned int in struct server, so let's use 4, to be |
| 261 | * on the safe side. |
| 262 | */ |
| 263 | buffer_len = key_len + addr_len + 4; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 264 | tmpbuf = trash.area; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 265 | memcpy(tmpbuf, p->dyncookie_key, key_len); |
| 266 | memcpy(&(tmpbuf[key_len]), |
| 267 | s->addr.ss_family == AF_INET ? |
| 268 | (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr : |
| 269 | (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr), |
| 270 | addr_len); |
| 271 | /* |
| 272 | * Make sure it's the same across all the load balancers, |
| 273 | * no matter their endianness. |
| 274 | */ |
| 275 | port = htonl(s->svc_port); |
| 276 | memcpy(&tmpbuf[key_len + addr_len], &port, 4); |
| 277 | hash_value = XXH64(tmpbuf, buffer_len, 0); |
| 278 | memprintf(&s->cookie, "%016llx", hash_value); |
| 279 | if (!s->cookie) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 280 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 281 | s->cklen = 16; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 282 | |
| 283 | /* Don't bother checking if the dyncookie is duplicated if |
| 284 | * the server is marked as "disabled", maybe it doesn't have |
| 285 | * its real IP yet, but just a place holder. |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 286 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 287 | if (!(s->next_admin & SRV_ADMF_FMAINT)) |
| 288 | srv_check_for_dup_dyncookie(s); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 289 | out: |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 290 | HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 293 | /* Returns true if it's possible to reuse an idle connection from server <srv> |
| 294 | * for a websocket stream. This is the case if server is configured to use the |
| 295 | * same protocol for both HTTP and websocket streams. This depends on the value |
| 296 | * of "proto", "alpn" and "ws" keywords. |
| 297 | */ |
| 298 | int srv_check_reuse_ws(struct server *srv) |
| 299 | { |
| 300 | if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) { |
| 301 | /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used |
| 302 | * for mux selection. |
| 303 | */ |
| 304 | const struct ist srv_mux = srv->mux_proto ? |
| 305 | srv->mux_proto->token : IST_NULL; |
| 306 | |
| 307 | switch (srv->ws) { |
| 308 | /* "auto" means use the same protocol : reuse is possible. */ |
| 309 | case SRV_WS_AUTO: |
| 310 | return 1; |
| 311 | |
| 312 | /* "h2" means use h2 for websocket : reuse is possible if |
| 313 | * server mux is h2. |
| 314 | */ |
| 315 | case SRV_WS_H2: |
| 316 | if (srv->mux_proto && isteq(srv_mux, ist("h2"))) |
| 317 | return 1; |
| 318 | break; |
| 319 | |
| 320 | /* "h1" means use h1 for websocket : reuse is possible if |
| 321 | * server mux is h1. |
| 322 | */ |
| 323 | case SRV_WS_H1: |
| 324 | if (!srv->mux_proto || isteq(srv_mux, ist("h1"))) |
| 325 | return 1; |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | else { |
| 330 | /* ALPN selection. |
| 331 | * Based on the assumption that only "h2" and "http/1.1" token |
| 332 | * are used on server ALPN. |
| 333 | */ |
| 334 | const struct ist alpn = ist2(srv->ssl_ctx.alpn_str, |
| 335 | srv->ssl_ctx.alpn_len); |
| 336 | |
| 337 | switch (srv->ws) { |
| 338 | case SRV_WS_AUTO: |
| 339 | /* for auto mode, consider reuse as possible if the |
| 340 | * server uses a single protocol ALPN |
| 341 | */ |
| 342 | if (!istchr(alpn, ',')) |
| 343 | return 1; |
| 344 | break; |
| 345 | |
| 346 | case SRV_WS_H2: |
| 347 | return isteq(alpn, ist("\x02h2")); |
| 348 | |
| 349 | case SRV_WS_H1: |
| 350 | return isteq(alpn, ist("\x08http/1.1")); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /* Return the proto to used for a websocket stream on <srv> without ALPN. NULL |
| 358 | * is a valid value indicating to use the fallback mux. |
| 359 | */ |
| 360 | const struct mux_ops *srv_get_ws_proto(struct server *srv) |
| 361 | { |
| 362 | const struct mux_proto_list *mux = NULL; |
| 363 | |
| 364 | switch (srv->ws) { |
| 365 | case SRV_WS_AUTO: |
| 366 | mux = srv->mux_proto; |
| 367 | break; |
| 368 | |
| 369 | case SRV_WS_H1: |
| 370 | mux = get_mux_proto(ist("h1")); |
| 371 | break; |
| 372 | |
| 373 | case SRV_WS_H2: |
| 374 | mux = get_mux_proto(ist("h2")); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | return mux ? mux->mux : NULL; |
| 379 | } |
| 380 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 381 | /* |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 382 | * Must be called with the server lock held. The server is first removed from |
| 383 | * the proxy tree if it was already attached. If <reattach> is true, the server |
| 384 | * will then be attached in the proxy tree. The proxy lock is held to |
| 385 | * manipulate the tree. |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 386 | */ |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 387 | static void srv_set_addr_desc(struct server *s, int reattach) |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 388 | { |
| 389 | struct proxy *p = s->proxy; |
| 390 | char *key; |
| 391 | |
| 392 | key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS); |
| 393 | |
| 394 | if (s->addr_node.key) { |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 395 | if (key && strcmp(key, s->addr_node.key) == 0) { |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 396 | free(key); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 401 | ebpt_delete(&s->addr_node); |
| 402 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 403 | |
| 404 | free(s->addr_node.key); |
| 405 | } |
| 406 | |
| 407 | s->addr_node.key = key; |
| 408 | |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 409 | if (reattach) { |
| 410 | if (s->addr_node.key) { |
| 411 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 412 | ebis_insert(&p->used_server_addr, &s->addr_node); |
| 413 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 414 | } |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 415 | } |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 419 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 420 | * parsing sessions. |
| 421 | */ |
| 422 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 423 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 424 | LIST_APPEND(&srv_keywords.list, &kwl->list); |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 428 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 429 | * find one with a valid ->parse() function. This way it is possible to declare |
| 430 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 431 | * if some options are met. Note that if the requested keyword contains an |
| 432 | * opening parenthesis, everything from this point is ignored. |
| 433 | */ |
| 434 | struct srv_kw *srv_find_kw(const char *kw) |
| 435 | { |
| 436 | int index; |
| 437 | const char *kwend; |
| 438 | struct srv_kw_list *kwl; |
| 439 | struct srv_kw *ret = NULL; |
| 440 | |
| 441 | kwend = strchr(kw, '('); |
| 442 | if (!kwend) |
| 443 | kwend = kw + strlen(kw); |
| 444 | |
| 445 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 446 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 447 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 448 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 449 | if (kwl->kw[index].parse) |
| 450 | return &kwl->kw[index]; /* found it !*/ |
| 451 | else |
| 452 | ret = &kwl->kw[index]; /* may be OK */ |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 460 | * unsupported keywords are only dumped if their supported form was not |
| 461 | * found. |
| 462 | */ |
| 463 | void srv_dump_kws(char **out) |
| 464 | { |
| 465 | struct srv_kw_list *kwl; |
| 466 | int index; |
| 467 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 468 | if (!out) |
| 469 | return; |
| 470 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 471 | *out = NULL; |
| 472 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 473 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 474 | if (kwl->kw[index].parse || |
| 475 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 476 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 477 | kwl->scope, |
| 478 | kwl->kw[index].kw, |
| 479 | kwl->kw[index].skip ? " <arg>" : "", |
| 480 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 481 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 482 | } |
| 483 | } |
| 484 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Try to find in srv_keyword the word that looks closest to <word> by counting |
| 488 | * transitions between letters, digits and other characters. Will return the |
| 489 | * best matching word if found, otherwise NULL. An optional array of extra |
| 490 | * words to compare may be passed in <extra>, but it must then be terminated |
| 491 | * by a NULL entry. If unused it may be NULL. |
| 492 | */ |
| 493 | static const char *srv_find_best_kw(const char *word) |
| 494 | { |
| 495 | uint8_t word_sig[1024]; |
| 496 | uint8_t list_sig[1024]; |
| 497 | const struct srv_kw_list *kwl; |
| 498 | const char *best_ptr = NULL; |
| 499 | int dist, best_dist = INT_MAX; |
| 500 | const char **extra; |
| 501 | int index; |
| 502 | |
| 503 | make_word_fingerprint(word_sig, word); |
| 504 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 505 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 506 | make_word_fingerprint(list_sig, kwl->kw[index].kw); |
| 507 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 508 | if (dist < best_dist) { |
| 509 | best_dist = dist; |
| 510 | best_ptr = kwl->kw[index].kw; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 515 | for (extra = extra_kw_list; *extra; extra++) { |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 516 | make_word_fingerprint(list_sig, *extra); |
| 517 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 518 | if (dist < best_dist) { |
| 519 | best_dist = dist; |
| 520 | best_ptr = *extra; |
| 521 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr))) |
| 525 | best_ptr = NULL; |
| 526 | |
| 527 | return best_ptr; |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 528 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 529 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 530 | /* Parse the "backup" server keyword */ |
| 531 | static int srv_parse_backup(char **args, int *cur_arg, |
| 532 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 533 | { |
| 534 | newsrv->flags |= SRV_F_BACKUP; |
| 535 | return 0; |
| 536 | } |
| 537 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 538 | |
Frédéric Lécaille | 9d1b95b | 2017-03-15 09:13:33 +0100 | [diff] [blame] | 539 | /* Parse the "cookie" server keyword */ |
| 540 | static int srv_parse_cookie(char **args, int *cur_arg, |
| 541 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 542 | { |
| 543 | char *arg; |
| 544 | |
| 545 | arg = args[*cur_arg + 1]; |
| 546 | if (!*arg) { |
| 547 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 548 | return ERR_ALERT | ERR_FATAL; |
| 549 | } |
| 550 | |
| 551 | free(newsrv->cookie); |
| 552 | newsrv->cookie = strdup(arg); |
| 553 | newsrv->cklen = strlen(arg); |
| 554 | newsrv->flags |= SRV_F_COOKIESET; |
| 555 | return 0; |
| 556 | } |
| 557 | |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 558 | /* Parse the "disabled" server keyword */ |
| 559 | static int srv_parse_disabled(char **args, int *cur_arg, |
| 560 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 561 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 562 | newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 563 | newsrv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 564 | newsrv->check.state |= CHK_ST_PAUSED; |
| 565 | newsrv->check.health = 0; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | /* Parse the "enabled" server keyword */ |
| 570 | static int srv_parse_enabled(char **args, int *cur_arg, |
| 571 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 572 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 573 | newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT; |
| 574 | newsrv->next_state = SRV_ST_RUNNING; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 575 | newsrv->check.state &= ~CHK_ST_PAUSED; |
| 576 | newsrv->check.health = newsrv->check.rise; |
| 577 | return 0; |
| 578 | } |
| 579 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 580 | /* Parse the "error-limit" server keyword */ |
| 581 | static int srv_parse_error_limit(char **args, int *cur_arg, |
| 582 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 583 | { |
| 584 | if (!*args[*cur_arg + 1]) { |
| 585 | memprintf(err, "'%s' expects an integer argument.", |
| 586 | args[*cur_arg]); |
| 587 | return ERR_ALERT | ERR_FATAL; |
| 588 | } |
| 589 | |
| 590 | newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]); |
| 591 | |
| 592 | if (newsrv->consecutive_errors_limit <= 0) { |
| 593 | memprintf(err, "%s has to be > 0.", |
| 594 | args[*cur_arg]); |
| 595 | return ERR_ALERT | ERR_FATAL; |
| 596 | } |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Amaury Denoyelle | f9d5957 | 2021-10-18 14:40:29 +0200 | [diff] [blame] | 601 | /* Parse the "ws" keyword */ |
| 602 | static int srv_parse_ws(char **args, int *cur_arg, |
| 603 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 604 | { |
| 605 | if (!args[*cur_arg + 1]) { |
| 606 | memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]); |
| 607 | return ERR_ALERT | ERR_FATAL; |
| 608 | } |
| 609 | |
| 610 | if (strcmp(args[*cur_arg + 1], "h1") == 0) { |
| 611 | newsrv->ws = SRV_WS_H1; |
| 612 | } |
| 613 | else if (strcmp(args[*cur_arg + 1], "h2") == 0) { |
| 614 | newsrv->ws = SRV_WS_H2; |
| 615 | } |
| 616 | else if (strcmp(args[*cur_arg + 1], "auto") == 0) { |
| 617 | newsrv->ws = SRV_WS_AUTO; |
| 618 | } |
| 619 | else { |
| 620 | memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]); |
| 621 | return ERR_ALERT | ERR_FATAL; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 628 | /* Parse the "init-addr" server keyword */ |
| 629 | static int srv_parse_init_addr(char **args, int *cur_arg, |
| 630 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 631 | { |
| 632 | char *p, *end; |
| 633 | int done; |
| 634 | struct sockaddr_storage sa; |
| 635 | |
| 636 | newsrv->init_addr_methods = 0; |
| 637 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 638 | |
| 639 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 640 | /* cut on next comma */ |
| 641 | for (end = p; *end && *end != ','; end++); |
| 642 | if (*end) |
| 643 | *(end++) = 0; |
| 644 | |
| 645 | memset(&sa, 0, sizeof(sa)); |
| 646 | if (strcmp(p, "libc") == 0) { |
| 647 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 648 | } |
| 649 | else if (strcmp(p, "last") == 0) { |
| 650 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 651 | } |
| 652 | else if (strcmp(p, "none") == 0) { |
| 653 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 654 | } |
| 655 | else if (str2ip2(p, &sa, 0)) { |
| 656 | if (is_addr(&newsrv->init_addr)) { |
| 657 | memprintf(err, "'%s' : initial address already specified, cannot add '%s'.", |
| 658 | args[*cur_arg], p); |
| 659 | return ERR_ALERT | ERR_FATAL; |
| 660 | } |
| 661 | newsrv->init_addr = sa; |
| 662 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 663 | } |
| 664 | else { |
| 665 | memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.", |
| 666 | args[*cur_arg], p); |
| 667 | return ERR_ALERT | ERR_FATAL; |
| 668 | } |
| 669 | if (!done) { |
| 670 | memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'", |
| 671 | args[*cur_arg], p); |
| 672 | return ERR_ALERT | ERR_FATAL; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* Parse the "log-proto" server keyword */ |
| 680 | static int srv_parse_log_proto(char **args, int *cur_arg, |
| 681 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 682 | { |
| 683 | if (strcmp(args[*cur_arg + 1], "legacy") == 0) |
| 684 | newsrv->log_proto = SRV_LOG_PROTO_LEGACY; |
| 685 | else if (strcmp(args[*cur_arg + 1], "octet-count") == 0) |
| 686 | newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING; |
| 687 | else { |
| 688 | memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'", |
| 689 | args[*cur_arg], args[*cur_arg + 1]); |
| 690 | return ERR_ALERT | ERR_FATAL; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | /* Parse the "maxconn" server keyword */ |
| 697 | static int srv_parse_maxconn(char **args, int *cur_arg, |
| 698 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 699 | { |
| 700 | newsrv->maxconn = atol(args[*cur_arg + 1]); |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | /* Parse the "maxqueue" server keyword */ |
| 705 | static int srv_parse_maxqueue(char **args, int *cur_arg, |
| 706 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 707 | { |
| 708 | newsrv->maxqueue = atol(args[*cur_arg + 1]); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | /* Parse the "minconn" server keyword */ |
| 713 | static int srv_parse_minconn(char **args, int *cur_arg, |
| 714 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 715 | { |
| 716 | newsrv->minconn = atol(args[*cur_arg + 1]); |
| 717 | return 0; |
| 718 | } |
| 719 | |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 720 | static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 721 | { |
| 722 | char *arg; |
| 723 | |
| 724 | arg = args[*cur_arg + 1]; |
| 725 | if (!*arg) { |
| 726 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 727 | return ERR_ALERT | ERR_FATAL; |
| 728 | } |
| 729 | newsrv->max_reuse = atoi(arg); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 734 | 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] | 735 | { |
| 736 | const char *res; |
| 737 | char *arg; |
| 738 | unsigned int time; |
| 739 | |
| 740 | arg = args[*cur_arg + 1]; |
| 741 | if (!*arg) { |
| 742 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 743 | return ERR_ALERT | ERR_FATAL; |
| 744 | } |
| 745 | res = parse_time_err(arg, &time, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 746 | if (res == PARSE_TIME_OVER) { |
| 747 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 748 | args[*cur_arg+1], args[*cur_arg]); |
| 749 | return ERR_ALERT | ERR_FATAL; |
| 750 | } |
| 751 | else if (res == PARSE_TIME_UNDER) { |
| 752 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 753 | args[*cur_arg+1], args[*cur_arg]); |
| 754 | return ERR_ALERT | ERR_FATAL; |
| 755 | } |
| 756 | else if (res) { |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 757 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", |
| 758 | *res, args[*cur_arg]); |
| 759 | return ERR_ALERT | ERR_FATAL; |
| 760 | } |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 761 | newsrv->pool_purge_delay = time; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 766 | static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 767 | { |
| 768 | char *arg; |
| 769 | |
| 770 | arg = args[*cur_arg + 1]; |
| 771 | if (!*arg) { |
| 772 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 773 | return ERR_ALERT | ERR_FATAL; |
| 774 | } |
| 775 | |
| 776 | newsrv->low_idle_conns = atoi(arg); |
| 777 | return 0; |
| 778 | } |
| 779 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 780 | static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 781 | { |
| 782 | char *arg; |
| 783 | |
| 784 | arg = args[*cur_arg + 1]; |
| 785 | if (!*arg) { |
| 786 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 787 | return ERR_ALERT | ERR_FATAL; |
| 788 | } |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 789 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 790 | newsrv->max_idle_conns = atoi(arg); |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 791 | if ((int)newsrv->max_idle_conns < -1) { |
| 792 | memprintf(err, "'%s' must be >= -1", args[*cur_arg]); |
| 793 | return ERR_ALERT | ERR_FATAL; |
| 794 | } |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 799 | /* parse the "id" server keyword */ |
| 800 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 801 | { |
| 802 | struct eb32_node *node; |
| 803 | |
| 804 | if (!*args[*cur_arg + 1]) { |
| 805 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 806 | return ERR_ALERT | ERR_FATAL; |
| 807 | } |
| 808 | |
| 809 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 810 | newsrv->conf.id.key = newsrv->puid; |
| 811 | |
| 812 | if (newsrv->puid <= 0) { |
| 813 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 814 | return ERR_ALERT | ERR_FATAL; |
| 815 | } |
| 816 | |
| 817 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 818 | if (node) { |
| 819 | struct server *target = container_of(node, struct server, conf.id); |
| 820 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 821 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 822 | target->id); |
| 823 | return ERR_ALERT | ERR_FATAL; |
| 824 | } |
| 825 | |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 826 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 830 | /* Parse the "namespace" server keyword */ |
| 831 | static int srv_parse_namespace(char **args, int *cur_arg, |
| 832 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 833 | { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 834 | #ifdef USE_NS |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 835 | char *arg; |
| 836 | |
| 837 | arg = args[*cur_arg + 1]; |
| 838 | if (!*arg) { |
| 839 | memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]); |
| 840 | return ERR_ALERT | ERR_FATAL; |
| 841 | } |
| 842 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 843 | if (strcmp(arg, "*") == 0) { |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 844 | /* Use the namespace associated with the connection (if present). */ |
| 845 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * As this parser may be called several times for the same 'default-server' |
| 851 | * object, or for a new 'server' instance deriving from a 'default-server' |
| 852 | * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it. |
| 853 | */ |
| 854 | newsrv->flags &= ~SRV_F_USE_NS_FROM_PP; |
| 855 | |
| 856 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 857 | if (!newsrv->netns) |
| 858 | newsrv->netns = netns_store_insert(arg); |
| 859 | |
| 860 | if (!newsrv->netns) { |
| 861 | memprintf(err, "Cannot open namespace '%s'", arg); |
| 862 | return ERR_ALERT | ERR_FATAL; |
| 863 | } |
| 864 | |
| 865 | return 0; |
| 866 | #else |
| 867 | memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]); |
| 868 | return ERR_ALERT | ERR_FATAL; |
| 869 | #endif |
| 870 | } |
| 871 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 872 | /* Parse the "no-backup" server keyword */ |
| 873 | static int srv_parse_no_backup(char **args, int *cur_arg, |
| 874 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 875 | { |
| 876 | newsrv->flags &= ~SRV_F_BACKUP; |
| 877 | return 0; |
| 878 | } |
| 879 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 880 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 881 | /* Disable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 882 | 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] | 883 | { |
| 884 | srv->pp_opts &= ~flags; |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | /* Parse the "no-send-proxy" server keyword */ |
| 889 | static int srv_parse_no_send_proxy(char **args, int *cur_arg, |
| 890 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 891 | { |
| 892 | return srv_disable_pp_flags(newsrv, SRV_PP_V1); |
| 893 | } |
| 894 | |
| 895 | /* Parse the "no-send-proxy-v2" server keyword */ |
| 896 | static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg, |
| 897 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 898 | { |
| 899 | return srv_disable_pp_flags(newsrv, SRV_PP_V2); |
| 900 | } |
| 901 | |
Frédéric Lécaille | 36d1565 | 2022-10-17 14:58:19 +0200 | [diff] [blame] | 902 | /* Parse the "shard" server keyword */ |
| 903 | static int srv_parse_shard(char **args, int *cur_arg, |
| 904 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 905 | { |
| 906 | newsrv->shard = atol(args[*cur_arg + 1]); |
| 907 | return 0; |
| 908 | } |
| 909 | |
Frédéric Lécaille | 1b9423d | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 910 | /* Parse the "no-tfo" server keyword */ |
| 911 | static int srv_parse_no_tfo(char **args, int *cur_arg, |
| 912 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 913 | { |
| 914 | newsrv->flags &= ~SRV_F_FASTOPEN; |
| 915 | return 0; |
| 916 | } |
| 917 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 918 | /* Parse the "non-stick" server keyword */ |
| 919 | static int srv_parse_non_stick(char **args, int *cur_arg, |
| 920 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 921 | { |
| 922 | newsrv->flags |= SRV_F_NON_STICK; |
| 923 | return 0; |
| 924 | } |
| 925 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 926 | /* Enable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 927 | 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] | 928 | { |
| 929 | srv->pp_opts |= flags; |
| 930 | return 0; |
| 931 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 932 | /* parse the "proto" server keyword */ |
| 933 | static int srv_parse_proto(char **args, int *cur_arg, |
| 934 | struct proxy *px, struct server *newsrv, char **err) |
| 935 | { |
| 936 | struct ist proto; |
| 937 | |
| 938 | if (!*args[*cur_arg + 1]) { |
| 939 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 940 | return ERR_ALERT | ERR_FATAL; |
| 941 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 942 | proto = ist(args[*cur_arg + 1]); |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 943 | newsrv->mux_proto = get_mux_proto(proto); |
| 944 | if (!newsrv->mux_proto) { |
| 945 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 946 | return ERR_ALERT | ERR_FATAL; |
| 947 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 948 | return 0; |
| 949 | } |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 950 | |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 951 | /* parse the "proxy-v2-options" */ |
| 952 | static int srv_parse_proxy_v2_options(char **args, int *cur_arg, |
| 953 | struct proxy *px, struct server *newsrv, char **err) |
| 954 | { |
| 955 | char *p, *n; |
| 956 | for (p = args[*cur_arg+1]; p; p = n) { |
| 957 | n = strchr(p, ','); |
| 958 | if (n) |
| 959 | *n++ = '\0'; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 960 | if (strcmp(p, "ssl") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 961 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 962 | } else if (strcmp(p, "cert-cn") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 963 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 964 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 965 | } else if (strcmp(p, "cert-key") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 966 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 967 | newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 968 | } else if (strcmp(p, "cert-sig") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 969 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 970 | newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 971 | } else if (strcmp(p, "ssl-cipher") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 972 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 973 | newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 974 | } else if (strcmp(p, "authority") == 0) { |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 975 | newsrv->pp_opts |= SRV_PP_V2_AUTHORITY; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 976 | } else if (strcmp(p, "crc32c") == 0) { |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 977 | newsrv->pp_opts |= SRV_PP_V2_CRC32C; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 978 | } else if (strcmp(p, "unique-id") == 0) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 979 | newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID; |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 980 | } else |
| 981 | goto fail; |
| 982 | } |
| 983 | return 0; |
| 984 | fail: |
| 985 | if (err) |
| 986 | memprintf(err, "'%s' : proxy v2 option not implemented", p); |
| 987 | return ERR_ALERT | ERR_FATAL; |
| 988 | } |
| 989 | |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 990 | /* Parse the "observe" server keyword */ |
| 991 | static int srv_parse_observe(char **args, int *cur_arg, |
| 992 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 993 | { |
| 994 | char *arg; |
| 995 | |
| 996 | arg = args[*cur_arg + 1]; |
| 997 | if (!*arg) { |
| 998 | memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]); |
| 999 | return ERR_ALERT | ERR_FATAL; |
| 1000 | } |
| 1001 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1002 | if (strcmp(arg, "none") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1003 | newsrv->observe = HANA_OBS_NONE; |
| 1004 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1005 | else if (strcmp(arg, "layer4") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1006 | newsrv->observe = HANA_OBS_LAYER4; |
| 1007 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1008 | else if (strcmp(arg, "layer7") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1009 | if (curproxy->mode != PR_MODE_HTTP) { |
| 1010 | memprintf(err, "'%s' can only be used in http proxies.\n", arg); |
| 1011 | return ERR_ALERT; |
| 1012 | } |
| 1013 | newsrv->observe = HANA_OBS_LAYER7; |
| 1014 | } |
| 1015 | else { |
| 1016 | memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' " |
| 1017 | "but got '%s'\n", args[*cur_arg], arg); |
| 1018 | return ERR_ALERT | ERR_FATAL; |
| 1019 | } |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1024 | /* Parse the "on-error" server keyword */ |
| 1025 | static int srv_parse_on_error(char **args, int *cur_arg, |
| 1026 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1027 | { |
| 1028 | if (strcmp(args[*cur_arg + 1], "fastinter") == 0) |
| 1029 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 1030 | else if (strcmp(args[*cur_arg + 1], "fail-check") == 0) |
| 1031 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 1032 | else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0) |
| 1033 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 1034 | else if (strcmp(args[*cur_arg + 1], "mark-down") == 0) |
| 1035 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 1036 | else { |
| 1037 | memprintf(err, "'%s' expects one of 'fastinter', " |
| 1038 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'", |
| 1039 | args[*cur_arg], args[*cur_arg + 1]); |
| 1040 | return ERR_ALERT | ERR_FATAL; |
| 1041 | } |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | /* Parse the "on-marked-down" server keyword */ |
| 1047 | static int srv_parse_on_marked_down(char **args, int *cur_arg, |
| 1048 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1049 | { |
| 1050 | if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0) |
| 1051 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1052 | else { |
| 1053 | memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'", |
| 1054 | args[*cur_arg], args[*cur_arg + 1]); |
| 1055 | return ERR_ALERT | ERR_FATAL; |
| 1056 | } |
| 1057 | |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | /* Parse the "on-marked-up" server keyword */ |
| 1062 | static int srv_parse_on_marked_up(char **args, int *cur_arg, |
| 1063 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1064 | { |
| 1065 | if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0) |
| 1066 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1067 | else { |
| 1068 | memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'", |
| 1069 | args[*cur_arg], args[*cur_arg + 1]); |
| 1070 | return ERR_ALERT | ERR_FATAL; |
| 1071 | } |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 1076 | /* Parse the "redir" server keyword */ |
| 1077 | static int srv_parse_redir(char **args, int *cur_arg, |
| 1078 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1079 | { |
| 1080 | char *arg; |
| 1081 | |
| 1082 | arg = args[*cur_arg + 1]; |
| 1083 | if (!*arg) { |
| 1084 | memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]); |
| 1085 | return ERR_ALERT | ERR_FATAL; |
| 1086 | } |
| 1087 | |
| 1088 | free(newsrv->rdr_pfx); |
| 1089 | newsrv->rdr_pfx = strdup(arg); |
| 1090 | newsrv->rdr_len = strlen(arg); |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1095 | /* Parse the "resolvers" server keyword */ |
| 1096 | static int srv_parse_resolvers(char **args, int *cur_arg, |
| 1097 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1098 | { |
| 1099 | free(newsrv->resolvers_id); |
| 1100 | newsrv->resolvers_id = strdup(args[*cur_arg + 1]); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | /* Parse the "resolve-net" server keyword */ |
| 1105 | static int srv_parse_resolve_net(char **args, int *cur_arg, |
| 1106 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1107 | { |
| 1108 | char *p, *e; |
| 1109 | unsigned char mask; |
| 1110 | struct resolv_options *opt; |
| 1111 | |
| 1112 | if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') { |
| 1113 | memprintf(err, "'%s' expects a list of networks.", |
| 1114 | args[*cur_arg]); |
| 1115 | return ERR_ALERT | ERR_FATAL; |
| 1116 | } |
| 1117 | |
| 1118 | opt = &newsrv->resolv_opts; |
| 1119 | |
| 1120 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 1121 | * string network in in_addr or in6_addr. |
| 1122 | */ |
| 1123 | p = args[*cur_arg + 1]; |
| 1124 | e = p; |
| 1125 | while (*p != '\0') { |
| 1126 | /* If no room available, return error. */ |
| 1127 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
| 1128 | memprintf(err, "'%s' exceed %d networks.", |
| 1129 | args[*cur_arg], SRV_MAX_PREF_NET); |
| 1130 | return ERR_ALERT | ERR_FATAL; |
| 1131 | } |
| 1132 | /* look for end or comma. */ |
| 1133 | while (*e != ',' && *e != '\0') |
| 1134 | e++; |
| 1135 | if (*e == ',') { |
| 1136 | *e = '\0'; |
| 1137 | e++; |
| 1138 | } |
| 1139 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 1140 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 1141 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 1142 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 1143 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 1144 | &mask)) { |
| 1145 | /* Try to convert input string from ipv6 network. */ |
| 1146 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 1147 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 1148 | } else { |
| 1149 | /* All network conversions fail, return error. */ |
| 1150 | memprintf(err, "'%s' invalid network '%s'.", |
| 1151 | args[*cur_arg], p); |
| 1152 | return ERR_ALERT | ERR_FATAL; |
| 1153 | } |
| 1154 | opt->pref_net_nb++; |
| 1155 | p = e; |
| 1156 | } |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | /* Parse the "resolve-opts" server keyword */ |
| 1162 | static int srv_parse_resolve_opts(char **args, int *cur_arg, |
| 1163 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1164 | { |
| 1165 | char *p, *end; |
| 1166 | |
| 1167 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 1168 | /* cut on next comma */ |
| 1169 | for (end = p; *end && *end != ','; end++); |
| 1170 | if (*end) |
| 1171 | *(end++) = 0; |
| 1172 | |
| 1173 | if (strcmp(p, "allow-dup-ip") == 0) { |
| 1174 | newsrv->resolv_opts.accept_duplicate_ip = 1; |
| 1175 | } |
| 1176 | else if (strcmp(p, "ignore-weight") == 0) { |
| 1177 | newsrv->resolv_opts.ignore_weight = 1; |
| 1178 | } |
| 1179 | else if (strcmp(p, "prevent-dup-ip") == 0) { |
| 1180 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
| 1181 | } |
| 1182 | else { |
| 1183 | memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.", |
| 1184 | args[*cur_arg], p); |
| 1185 | return ERR_ALERT | ERR_FATAL; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | /* Parse the "resolve-prefer" server keyword */ |
| 1193 | static int srv_parse_resolve_prefer(char **args, int *cur_arg, |
| 1194 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1195 | { |
| 1196 | if (strcmp(args[*cur_arg + 1], "ipv4") == 0) |
| 1197 | newsrv->resolv_opts.family_prio = AF_INET; |
| 1198 | else if (strcmp(args[*cur_arg + 1], "ipv6") == 0) |
| 1199 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 1200 | else { |
| 1201 | memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.", |
| 1202 | args[*cur_arg]); |
| 1203 | return ERR_ALERT | ERR_FATAL; |
| 1204 | } |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1209 | /* Parse the "send-proxy" server keyword */ |
| 1210 | static int srv_parse_send_proxy(char **args, int *cur_arg, |
| 1211 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1212 | { |
| 1213 | return srv_enable_pp_flags(newsrv, SRV_PP_V1); |
| 1214 | } |
| 1215 | |
| 1216 | /* Parse the "send-proxy-v2" server keyword */ |
| 1217 | static int srv_parse_send_proxy_v2(char **args, int *cur_arg, |
| 1218 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1219 | { |
| 1220 | return srv_enable_pp_flags(newsrv, SRV_PP_V2); |
| 1221 | } |
| 1222 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1223 | /* Parse the "slowstart" server keyword */ |
| 1224 | static int srv_parse_slowstart(char **args, int *cur_arg, |
| 1225 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1226 | { |
| 1227 | /* slowstart is stored in seconds */ |
| 1228 | unsigned int val; |
| 1229 | const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS); |
| 1230 | |
| 1231 | if (time_err == PARSE_TIME_OVER) { |
| 1232 | memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1233 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1234 | return ERR_ALERT | ERR_FATAL; |
| 1235 | } |
| 1236 | else if (time_err == PARSE_TIME_UNDER) { |
| 1237 | memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1238 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1239 | return ERR_ALERT | ERR_FATAL; |
| 1240 | } |
| 1241 | else if (time_err) { |
| 1242 | memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.", |
| 1243 | *time_err, newsrv->id); |
| 1244 | return ERR_ALERT | ERR_FATAL; |
| 1245 | } |
| 1246 | newsrv->slowstart = (val + 999) / 1000; |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1250 | |
| 1251 | /* Parse the "source" server keyword */ |
| 1252 | static int srv_parse_source(char **args, int *cur_arg, |
| 1253 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1254 | { |
| 1255 | char *errmsg; |
| 1256 | int port_low, port_high; |
| 1257 | struct sockaddr_storage *sk; |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1258 | |
| 1259 | errmsg = NULL; |
| 1260 | |
| 1261 | if (!*args[*cur_arg + 1]) { |
| 1262 | memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, " |
| 1263 | "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface"); |
| 1264 | goto err; |
| 1265 | } |
| 1266 | |
| 1267 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1268 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1269 | &errmsg, NULL, NULL, |
| 1270 | 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] | 1271 | if (!sk) { |
| 1272 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1273 | goto err; |
| 1274 | } |
| 1275 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1276 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1277 | newsrv->conn_src.source_addr = *sk; |
| 1278 | |
| 1279 | if (port_low != port_high) { |
| 1280 | int i; |
| 1281 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1282 | 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] | 1283 | if (!newsrv->conn_src.sport_range) { |
| 1284 | ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]); |
| 1285 | goto err; |
| 1286 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1287 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1288 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1289 | } |
| 1290 | |
| 1291 | *cur_arg += 2; |
| 1292 | while (*(args[*cur_arg])) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1293 | 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] | 1294 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 1295 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1296 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 1297 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1298 | goto err; |
| 1299 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1300 | if (strcmp(args[*cur_arg + 1], "client") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1301 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1302 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1303 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1304 | else if (strcmp(args[*cur_arg + 1], "clientip") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1305 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1306 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1307 | } |
| 1308 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 1309 | char *name, *end; |
| 1310 | |
| 1311 | name = args[*cur_arg + 1] + 7; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1312 | while (isspace((unsigned char)*name)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1313 | name++; |
| 1314 | |
| 1315 | end = name; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1316 | while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')') |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1317 | end++; |
| 1318 | |
| 1319 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1320 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1321 | free(newsrv->conn_src.bind_hdr_name); |
| 1322 | 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] | 1323 | if (!newsrv->conn_src.bind_hdr_name) { |
| 1324 | ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]); |
| 1325 | goto err; |
| 1326 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1327 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1328 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1329 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 1330 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1331 | |
| 1332 | /* now look for an occurrence number */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1333 | while (isspace((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1334 | end++; |
| 1335 | if (*end == ',') { |
| 1336 | end++; |
| 1337 | name = end; |
| 1338 | if (*end == '-') |
| 1339 | end++; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1340 | while (isdigit((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1341 | end++; |
| 1342 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 1343 | } |
| 1344 | |
| 1345 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1346 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 1347 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1348 | goto err; |
| 1349 | } |
| 1350 | } |
| 1351 | else { |
| 1352 | struct sockaddr_storage *sk; |
| 1353 | int port1, port2; |
| 1354 | |
| 1355 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1356 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, |
| 1357 | &errmsg, NULL, NULL, |
| 1358 | 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] | 1359 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1360 | 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] | 1361 | goto err; |
| 1362 | } |
| 1363 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1364 | newsrv->conn_src.tproxy_addr = *sk; |
| 1365 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1366 | } |
| 1367 | global.last_checks |= LSTCHK_NETADM; |
| 1368 | *cur_arg += 2; |
| 1369 | continue; |
| 1370 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1371 | 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] | 1372 | goto err; |
| 1373 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 1374 | } /* "usesrc" */ |
| 1375 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1376 | 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] | 1377 | #ifdef SO_BINDTODEVICE |
| 1378 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1379 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1380 | goto err; |
| 1381 | } |
| 1382 | free(newsrv->conn_src.iface_name); |
| 1383 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 1384 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1385 | global.last_checks |= LSTCHK_NETADM; |
| 1386 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1387 | 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] | 1388 | goto err; |
| 1389 | #endif |
| 1390 | *cur_arg += 2; |
| 1391 | continue; |
| 1392 | } |
| 1393 | /* this keyword in not an option of "source" */ |
| 1394 | break; |
| 1395 | } /* while */ |
| 1396 | |
| 1397 | return 0; |
| 1398 | |
| 1399 | err: |
| 1400 | free(errmsg); |
| 1401 | return ERR_ALERT | ERR_FATAL; |
| 1402 | } |
| 1403 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1404 | /* Parse the "stick" server keyword */ |
| 1405 | static int srv_parse_stick(char **args, int *cur_arg, |
| 1406 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1407 | { |
| 1408 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1412 | /* Parse the "track" server keyword */ |
| 1413 | static int srv_parse_track(char **args, int *cur_arg, |
| 1414 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1415 | { |
| 1416 | char *arg; |
| 1417 | |
| 1418 | arg = args[*cur_arg + 1]; |
| 1419 | if (!*arg) { |
| 1420 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 1421 | return ERR_ALERT | ERR_FATAL; |
| 1422 | } |
| 1423 | |
| 1424 | free(newsrv->trackit); |
| 1425 | newsrv->trackit = strdup(arg); |
| 1426 | |
| 1427 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /* Parse the "socks4" server keyword */ |
| 1431 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 1432 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1433 | { |
| 1434 | char *errmsg; |
| 1435 | int port_low, port_high; |
| 1436 | struct sockaddr_storage *sk; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1437 | |
| 1438 | errmsg = NULL; |
| 1439 | |
| 1440 | if (!*args[*cur_arg + 1]) { |
| 1441 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 1442 | goto err; |
| 1443 | } |
| 1444 | |
| 1445 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1446 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1447 | &errmsg, NULL, NULL, |
| 1448 | 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] | 1449 | if (!sk) { |
| 1450 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1451 | goto err; |
| 1452 | } |
| 1453 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1454 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 1455 | newsrv->socks4_addr = *sk; |
| 1456 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1457 | return 0; |
| 1458 | |
| 1459 | err: |
| 1460 | free(errmsg); |
| 1461 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1462 | } |
| 1463 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1464 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 1465 | /* parse the "tfo" server keyword */ |
| 1466 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1467 | { |
| 1468 | newsrv->flags |= SRV_F_FASTOPEN; |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1472 | /* parse the "usesrc" server keyword */ |
| 1473 | static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1474 | { |
| 1475 | memprintf(err, "'%s' only allowed after a '%s' statement.", |
| 1476 | "usesrc", "source"); |
| 1477 | return ERR_ALERT | ERR_FATAL; |
| 1478 | } |
| 1479 | |
| 1480 | /* parse the "weight" server keyword */ |
| 1481 | static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1482 | { |
| 1483 | int w; |
| 1484 | |
| 1485 | w = atol(args[*cur_arg + 1]); |
| 1486 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1487 | memprintf(err, "weight of server %s is not within 0 and %d (%d).", |
| 1488 | newsrv->id, SRV_UWGHT_MAX, w); |
| 1489 | return ERR_ALERT | ERR_FATAL; |
| 1490 | } |
| 1491 | newsrv->uweight = newsrv->iweight = w; |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1496 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1497 | * 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] | 1498 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1499 | * |
| 1500 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1501 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1502 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1503 | { |
Willy Tarreau | 751153e | 2021-02-17 13:33:24 +0100 | [diff] [blame] | 1504 | struct stream *stream; |
| 1505 | struct mt_list *elt1, elt2; |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1506 | int thr; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1507 | |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1508 | for (thr = 0; thr < global.nbthread; thr++) |
| 1509 | mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2) |
| 1510 | if (stream->srv_conn == srv) |
| 1511 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* 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] | 1515 | * 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] | 1516 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1517 | * |
| 1518 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1519 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1520 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1521 | { |
| 1522 | struct server *srv; |
| 1523 | |
| 1524 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1525 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1526 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1529 | static void srv_append_op_chg_cause(struct buffer *msg, struct server *s) |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1530 | { |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1531 | switch (s->op_st_chg_cause) { |
| 1532 | case SRV_OP_STCHGC_NONE: |
| 1533 | break; /* do nothing */ |
| 1534 | case SRV_OP_STCHGC_HEALTH: |
| 1535 | check_append_info(msg, &s->check); |
| 1536 | break; |
| 1537 | case SRV_OP_STCHGC_AGENT: |
| 1538 | check_append_info(msg, &s->agent); |
| 1539 | break; |
| 1540 | default: |
| 1541 | chunk_appendf(msg, ", %s", srv_op_st_chg_cause(s->op_st_chg_cause)); |
| 1542 | break; |
| 1543 | } |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | static void srv_append_adm_chg_cause(struct buffer *msg, struct server *s) |
| 1547 | { |
| 1548 | if (s->adm_st_chg_cause) |
| 1549 | chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(s->adm_st_chg_cause)); |
| 1550 | } |
| 1551 | |
| 1552 | /* Appends some information to a message string related to a server tracking |
| 1553 | * or requeued connections info. |
| 1554 | * |
| 1555 | * If <forced> is null and the server tracks another one, a "via" |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1556 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1557 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1558 | * |
| 1559 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1560 | */ |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1561 | static void srv_append_more(struct buffer *msg, struct server *s, |
| 1562 | int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1563 | { |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1564 | if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1565 | 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] | 1566 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1567 | |
| 1568 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1569 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1570 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1571 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1572 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1573 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1574 | s->cur_sess, xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1575 | else |
| 1576 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1577 | " %d sessions requeued, %d total in queue", |
| 1578 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1579 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1580 | xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1584 | /* Marks server <s> down, regardless of its checks' statuses. The server is |
| 1585 | * registered in a list to postpone the counting of the remaining servers on |
| 1586 | * the proxy and transfers queued streams whenever possible to other servers at |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1587 | * a sync point. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1588 | * |
| 1589 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1590 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1591 | void srv_set_stopped(struct server *s, enum srv_op_st_chg_cause cause) |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1592 | { |
| 1593 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1594 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1595 | 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] | 1596 | return; |
| 1597 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1598 | s->next_state = SRV_ST_STOPPED; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1599 | s->op_st_chg_cause = cause; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1600 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1601 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1602 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1603 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1604 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1605 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1606 | srv_set_stopped(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1607 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1608 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1609 | } |
| 1610 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1611 | /* 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] | 1612 | * in maintenance. The server is registered in a list to postpone the counting |
| 1613 | * of the remaining servers on the proxy and tries to grab requests from the |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1614 | * proxy at a sync point. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1615 | * |
| 1616 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1617 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1618 | void srv_set_running(struct server *s, enum srv_op_st_chg_cause cause) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1619 | { |
| 1620 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1621 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1622 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1623 | return; |
| 1624 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1625 | 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] | 1626 | return; |
| 1627 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1628 | s->next_state = SRV_ST_STARTING; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1629 | s->op_st_chg_cause = cause; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1630 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1631 | if (s->slowstart <= 0) |
| 1632 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1633 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1634 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1635 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1636 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1637 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1638 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1639 | srv_set_running(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1640 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1641 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1644 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1645 | * isn't in maintenance. The server is registered in a list to postpone the |
| 1646 | * counting of the remaining servers on the proxy and tries to grab requests |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1647 | * from the proxy. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1648 | * |
| 1649 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1650 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1651 | void srv_set_stopping(struct server *s, enum srv_op_st_chg_cause cause) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1652 | { |
| 1653 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1654 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1655 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1656 | return; |
| 1657 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1658 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1659 | return; |
| 1660 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1661 | s->next_state = SRV_ST_STOPPING; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1662 | s->op_st_chg_cause = cause; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1663 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1664 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1665 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1666 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1667 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1668 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1669 | srv_set_stopping(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1670 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1671 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1672 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1673 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1674 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1675 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1676 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1677 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1678 | * 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] | 1679 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1680 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1681 | * |
| 1682 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1683 | */ |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1684 | void srv_set_admin_flag(struct server *s, enum srv_admin mode, enum srv_adm_st_chg_cause cause) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1685 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1686 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1687 | |
| 1688 | if (!mode) |
| 1689 | return; |
| 1690 | |
| 1691 | /* 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] | 1692 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1693 | return; |
| 1694 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1695 | s->next_admin |= mode; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1696 | s->adm_st_chg_cause = cause; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1697 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1698 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1699 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1700 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1701 | /* 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] | 1702 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1703 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1704 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1705 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1706 | /* compute the inherited flag to propagate */ |
| 1707 | if (mode & SRV_ADMF_MAINT) |
| 1708 | mode = SRV_ADMF_IMAINT; |
| 1709 | else if (mode & SRV_ADMF_DRAIN) |
| 1710 | mode = SRV_ADMF_IDRAIN; |
| 1711 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1712 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1713 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1714 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1715 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1716 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1717 | } |
| 1718 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1719 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1720 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1721 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1722 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1723 | * 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] | 1724 | * |
| 1725 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1726 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1727 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1728 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1729 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1730 | |
| 1731 | if (!mode) |
| 1732 | return; |
| 1733 | |
| 1734 | /* 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] | 1735 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1736 | return; |
| 1737 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1738 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1739 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1740 | /* propagate changes */ |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1741 | srv_update_status(s); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1742 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1743 | /* 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] | 1744 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1745 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1746 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1747 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1748 | if (mode & SRV_ADMF_MAINT) |
| 1749 | mode = SRV_ADMF_IMAINT; |
| 1750 | else if (mode & SRV_ADMF_DRAIN) |
| 1751 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1752 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1753 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1754 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1755 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1756 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1757 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1760 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1761 | * upon startup so that inherited states are correct. |
| 1762 | */ |
| 1763 | static void srv_propagate_admin_state(struct server *srv) |
| 1764 | { |
| 1765 | struct server *srv2; |
| 1766 | |
| 1767 | if (!srv->trackers) |
| 1768 | return; |
| 1769 | |
| 1770 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1771 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1772 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1773 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1774 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1775 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1776 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1777 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1782 | * Only servers *not* tracking another one are considered, because other |
| 1783 | * ones will be handled when the server they track is visited. |
| 1784 | */ |
| 1785 | void srv_compute_all_admin_states(struct proxy *px) |
| 1786 | { |
| 1787 | struct server *srv; |
| 1788 | |
| 1789 | for (srv = px->srv; srv; srv = srv->next) { |
| 1790 | if (srv->track) |
| 1791 | continue; |
| 1792 | srv_propagate_admin_state(srv); |
| 1793 | } |
| 1794 | } |
| 1795 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1796 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1797 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1798 | * all code contributors. |
| 1799 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1800 | * the config parser can report an appropriate error when a known keyword was |
| 1801 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1802 | * 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] | 1803 | */ |
| 1804 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1805 | { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1806 | { "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] | 1807 | { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */ |
| 1808 | { "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] | 1809 | { "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] | 1810 | { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1811 | { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1812 | { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */ |
| 1813 | { "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] | 1814 | { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */ |
| 1815 | { "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] | 1816 | { "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] | 1817 | { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1818 | { "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] | 1819 | { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */ |
| 1820 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1821 | { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */ |
| 1822 | { "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] | 1823 | { "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] | 1824 | { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */ |
| 1825 | { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */ |
| 1826 | { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */ |
| 1827 | { "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] | 1828 | { "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 */ |
| 1829 | { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */ |
| 1830 | { "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] | 1831 | { "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] | 1832 | { "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] | 1833 | { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */ |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 1834 | { "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] | 1835 | { "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] | 1836 | { "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] | 1837 | { "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] | 1838 | { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1839 | { "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] | 1840 | { "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] | 1841 | { "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] | 1842 | { "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] | 1843 | { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1844 | { "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] | 1845 | { "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] | 1846 | { "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] | 1847 | { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */ |
| 1848 | { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */ |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1849 | { NULL, NULL, 0 }, |
| 1850 | }}; |
| 1851 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1852 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1853 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1854 | /* 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] | 1855 | * 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] | 1856 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1857 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1858 | * |
| 1859 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1860 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1861 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1862 | { |
| 1863 | struct proxy *px = sv->proxy; |
| 1864 | unsigned w; |
| 1865 | |
| 1866 | if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { |
| 1867 | /* go to full throttle if the slowstart interval is reached */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1868 | if (sv->next_state == SRV_ST_STARTING) |
| 1869 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1873 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1874 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1875 | 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] | 1876 | w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; |
| 1877 | else |
| 1878 | w = px->lbprm.wdiv; |
| 1879 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1880 | 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] | 1881 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1882 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1883 | if (must_update) |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1884 | srv_update_status(sv); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1887 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1888 | * Parses weight_str and configures sv accordingly. |
| 1889 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1890 | * |
| 1891 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1892 | */ |
| 1893 | const char *server_parse_weight_change_request(struct server *sv, |
| 1894 | const char *weight_str) |
| 1895 | { |
| 1896 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1897 | long int w; |
| 1898 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1899 | |
| 1900 | px = sv->proxy; |
| 1901 | |
| 1902 | /* if the weight is terminated with '%', it is set relative to |
| 1903 | * the initial weight, otherwise it is absolute. |
| 1904 | */ |
| 1905 | if (!*weight_str) |
| 1906 | return "Require <weight> or <weight%>.\n"; |
| 1907 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1908 | w = strtol(weight_str, &end, 10); |
| 1909 | if (end == weight_str) |
| 1910 | return "Empty weight string empty or preceded by garbage"; |
| 1911 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1912 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1913 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1914 | /* Avoid integer overflow */ |
| 1915 | if (w > 25600) |
| 1916 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1917 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 1918 | if (w > 256) |
| 1919 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1920 | } |
| 1921 | else if (w < 0 || w > 256) |
| 1922 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1923 | else if (end[0] != '\0') |
| 1924 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1925 | |
| 1926 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 1927 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 1928 | |
| 1929 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1930 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1931 | |
| 1932 | return NULL; |
| 1933 | } |
| 1934 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1935 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1936 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 1937 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1938 | * Returns: |
| 1939 | * - error string on error |
| 1940 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1941 | * |
| 1942 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1943 | */ |
| 1944 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 1945 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1946 | { |
| 1947 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 1948 | |
| 1949 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1950 | srv_update_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1951 | return NULL; |
| 1952 | } |
| 1953 | if (inet_pton(AF_INET, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 1954 | srv_update_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 1955 | return NULL; |
| 1956 | } |
| 1957 | |
| 1958 | return "Could not understand IP address format.\n"; |
| 1959 | } |
| 1960 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1961 | /* |
| 1962 | * Must be called with the server lock held. |
| 1963 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1964 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 1965 | const char *maxconn_str) |
| 1966 | { |
| 1967 | long int v; |
| 1968 | char *end; |
| 1969 | |
| 1970 | if (!*maxconn_str) |
| 1971 | return "Require <maxconn>.\n"; |
| 1972 | |
| 1973 | v = strtol(maxconn_str, &end, 10); |
| 1974 | if (end == maxconn_str) |
| 1975 | return "maxconn string empty or preceded by garbage"; |
| 1976 | else if (end[0] != '\0') |
| 1977 | return "Trailing garbage in maxconn string"; |
| 1978 | |
| 1979 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 1980 | sv->maxconn = sv->minconn = v; |
| 1981 | } else { // dynamic maxconn |
| 1982 | sv->maxconn = v; |
| 1983 | } |
| 1984 | |
| 1985 | if (may_dequeue_tasks(sv, sv->proxy)) |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 1986 | process_srv_queue(sv); |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 1987 | |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1991 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 1992 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1993 | { |
| 1994 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1995 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 1996 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 1997 | NULL, |
| 1998 | }; |
| 1999 | |
| 2000 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 2001 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2002 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2003 | 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] | 2004 | } |
| 2005 | |
William Lallemand | 0d05867 | 2022-03-16 15:44:42 +0100 | [diff] [blame] | 2006 | 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] | 2007 | { |
| 2008 | struct sample_expr *expr; |
| 2009 | |
| 2010 | 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] | 2011 | if (!expr) { |
| 2012 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 2013 | return ERR_ALERT | ERR_FATAL; |
| 2014 | } |
| 2015 | |
| 2016 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 2017 | memprintf(err, "error detected while parsing sni expression : " |
| 2018 | " fetch method '%s' extracts information from '%s', " |
Amaury Denoyelle | c008a63 | 2021-05-28 11:01:22 +0200 | [diff] [blame] | 2019 | "none of which is available here.", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2020 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2021 | return ERR_ALERT | ERR_FATAL; |
| 2022 | } |
| 2023 | |
| 2024 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 2025 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2026 | newsrv->ssl_ctx.sni = expr; |
| 2027 | |
| 2028 | return 0; |
| 2029 | } |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2030 | |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2031 | 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] | 2032 | { |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2033 | char *msg = "error encountered while processing "; |
| 2034 | char *quote = "'"; |
| 2035 | char *token = args[cur_arg]; |
| 2036 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2037 | if (err && *err) { |
| 2038 | indent_msg(err, 2); |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2039 | msg = *err; |
| 2040 | quote = ""; |
| 2041 | token = ""; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2042 | } |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2043 | |
| 2044 | if (err_code & ERR_WARN && !(err_code & ERR_ALERT)) |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2045 | 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] | 2046 | else |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2047 | 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] | 2048 | } |
| 2049 | |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2050 | 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] | 2051 | { |
| 2052 | int range_sz; |
| 2053 | |
| 2054 | range_sz = src->conn_src.sport_range->size; |
| 2055 | if (range_sz > 0) { |
| 2056 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 2057 | if (srv->conn_src.sport_range != NULL) { |
| 2058 | int i; |
| 2059 | |
| 2060 | for (i = 0; i < range_sz; i++) { |
| 2061 | srv->conn_src.sport_range->ports[i] = |
| 2062 | src->conn_src.sport_range->ports[i]; |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 2070 | */ |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2071 | 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] | 2072 | { |
| 2073 | srv->conn_src.opts = src->conn_src.opts; |
| 2074 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 2075 | |
| 2076 | /* Source port range copy. */ |
| 2077 | if (src->conn_src.sport_range != NULL) |
| 2078 | srv_conn_src_sport_range_cpy(srv, src); |
| 2079 | |
| 2080 | #ifdef CONFIG_HAP_TRANSPARENT |
| 2081 | if (src->conn_src.bind_hdr_name != NULL) { |
| 2082 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 2083 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 2084 | } |
| 2085 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 2086 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 2087 | #endif |
| 2088 | if (src->conn_src.iface_name != NULL) |
| 2089 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
| 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * Copy <src> server SSL settings to <srv> server allocating |
| 2094 | * everything needed. |
| 2095 | */ |
| 2096 | #if defined(USE_OPENSSL) |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2097 | 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] | 2098 | { |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2099 | /* <src> is the current proxy's default server and SSL is enabled */ |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2100 | BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */ |
| 2101 | |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2102 | if (src == &srv->proxy->defsrv && src->use_ssl == 1) |
| 2103 | srv->flags |= SRV_F_DEFSRV_USE_SSL; |
| 2104 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2105 | if (src->ssl_ctx.ca_file != NULL) |
| 2106 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 2107 | if (src->ssl_ctx.crl_file != NULL) |
| 2108 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2109 | if (src->ssl_ctx.client_crt != NULL) |
| 2110 | 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] | 2111 | |
| 2112 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 2113 | |
Remi Tricot-Le Breton | 0498fa4 | 2021-07-13 18:28:22 +0200 | [diff] [blame] | 2114 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2115 | if (src->ssl_ctx.verify_host != NULL) |
| 2116 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 2117 | if (src->ssl_ctx.ciphers != NULL) |
| 2118 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 2e8d52f | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 2119 | if (src->ssl_ctx.options) |
| 2120 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 2121 | if (src->ssl_ctx.methods.flags) |
| 2122 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 2123 | if (src->ssl_ctx.methods.min) |
| 2124 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 2125 | if (src->ssl_ctx.methods.max) |
| 2126 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 2127 | |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 2128 | if (src->ssl_ctx.ciphersuites != NULL) |
| 2129 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2130 | if (src->sni_expr != NULL) |
| 2131 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2132 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2133 | if (src->ssl_ctx.alpn_str) { |
| 2134 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 2135 | if (srv->ssl_ctx.alpn_str) { |
| 2136 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 2137 | src->ssl_ctx.alpn_len); |
| 2138 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 2139 | } |
| 2140 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2141 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2142 | if (src->ssl_ctx.npn_str) { |
| 2143 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 2144 | if (srv->ssl_ctx.npn_str) { |
| 2145 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 2146 | src->ssl_ctx.npn_len); |
| 2147 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 2148 | } |
| 2149 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2150 | } |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2151 | |
| 2152 | /* Activate ssl on server <s>. |
| 2153 | * do nothing if there is no change to apply |
| 2154 | * |
| 2155 | * Must be called with the server lock held. |
| 2156 | */ |
| 2157 | void srv_set_ssl(struct server *s, int use_ssl) |
| 2158 | { |
| 2159 | if (s->use_ssl == use_ssl) |
| 2160 | return; |
| 2161 | |
| 2162 | s->use_ssl = use_ssl; |
| 2163 | if (s->use_ssl) |
| 2164 | s->xprt = xprt_get(XPRT_SSL); |
| 2165 | else |
William Dauchy | a087f87 | 2022-01-06 16:57:15 +0100 | [diff] [blame] | 2166 | s->xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | #endif /* USE_OPENSSL */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2170 | |
| 2171 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2172 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2173 | * 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] | 2174 | * 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] | 2175 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2176 | 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] | 2177 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2178 | char *hostname_dn; |
| 2179 | int hostname_len, hostname_dn_len; |
| 2180 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2181 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2182 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2183 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2184 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2185 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 2186 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 2187 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2188 | if (hostname_dn_len == -1) |
| 2189 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2190 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2191 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2192 | free(srv->hostname); |
| 2193 | free(srv->hostname_dn); |
| 2194 | srv->hostname = strdup(hostname); |
| 2195 | srv->hostname_dn = strdup(hostname_dn); |
| 2196 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2197 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2198 | goto err; |
| 2199 | |
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 | |
| 2202 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2203 | ha_free(&srv->hostname); |
| 2204 | ha_free(&srv->hostname_dn); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2205 | return -1; |
| 2206 | } |
| 2207 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2208 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2209 | * Copy <src> server settings to <srv> server allocating |
| 2210 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2211 | * This function is not supposed to be called at any time, but only |
| 2212 | * during server settings parsing or during server allocations from |
| 2213 | * a server template, and just after having calloc()'ed a new server. |
| 2214 | * So, <src> may only be a default server (when parsing server settings) |
| 2215 | * or a server template (during server allocations from a server template). |
| 2216 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 2217 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2218 | */ |
Christopher Faulet | b32cb9b | 2022-08-03 11:28:08 +0200 | [diff] [blame] | 2219 | 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] | 2220 | { |
| 2221 | /* Connection source settings copy */ |
| 2222 | srv_conn_src_cpy(srv, src); |
| 2223 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2224 | if (srv_tmpl) { |
| 2225 | srv->addr = src->addr; |
| 2226 | srv->svc_port = src->svc_port; |
| 2227 | } |
| 2228 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2229 | srv->pp_opts = src->pp_opts; |
| 2230 | if (src->rdr_pfx != NULL) { |
| 2231 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 2232 | srv->rdr_len = src->rdr_len; |
| 2233 | } |
| 2234 | if (src->cookie != NULL) { |
| 2235 | srv->cookie = strdup(src->cookie); |
| 2236 | srv->cklen = src->cklen; |
| 2237 | } |
| 2238 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | 2444108 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 2239 | srv->check.addr = src->check.addr; |
| 2240 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2241 | srv->check.use_ssl = src->check.use_ssl; |
| 2242 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 2243 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 2244 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 2245 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2246 | /* Note: 'flags' field has potentially been already initialized. */ |
| 2247 | srv->flags |= src->flags; |
| 2248 | srv->do_check = src->do_check; |
| 2249 | srv->do_agent = src->do_agent; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2250 | srv->check.inter = src->check.inter; |
| 2251 | srv->check.fastinter = src->check.fastinter; |
| 2252 | srv->check.downinter = src->check.downinter; |
| 2253 | srv->agent.use_ssl = src->agent.use_ssl; |
| 2254 | srv->agent.port = src->agent.port; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2255 | |
| 2256 | if (src->agent.tcpcheck_rules) { |
| 2257 | srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules)); |
| 2258 | if (srv->agent.tcpcheck_rules) { |
| 2259 | srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags; |
| 2260 | srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list; |
| 2261 | LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars); |
| 2262 | dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars, |
| 2263 | &src->agent.tcpcheck_rules->preset_vars); |
| 2264 | } |
| 2265 | } |
| 2266 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2267 | srv->agent.inter = src->agent.inter; |
| 2268 | srv->agent.fastinter = src->agent.fastinter; |
| 2269 | srv->agent.downinter = src->agent.downinter; |
| 2270 | srv->maxqueue = src->maxqueue; |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 2271 | srv->ws = src->ws; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2272 | srv->minconn = src->minconn; |
| 2273 | srv->maxconn = src->maxconn; |
| 2274 | srv->slowstart = src->slowstart; |
| 2275 | srv->observe = src->observe; |
| 2276 | srv->onerror = src->onerror; |
| 2277 | srv->onmarkeddown = src->onmarkeddown; |
| 2278 | srv->onmarkedup = src->onmarkedup; |
| 2279 | if (src->trackit != NULL) |
| 2280 | srv->trackit = strdup(src->trackit); |
| 2281 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 2282 | srv->uweight = srv->iweight = src->iweight; |
| 2283 | |
| 2284 | srv->check.send_proxy = src->check.send_proxy; |
| 2285 | /* health: up, but will fall down at first failure */ |
| 2286 | srv->check.rise = srv->check.health = src->check.rise; |
| 2287 | srv->check.fall = src->check.fall; |
| 2288 | |
| 2289 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2290 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 2291 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 2292 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2293 | srv->check.state |= CHK_ST_PAUSED; |
| 2294 | srv->check.health = 0; |
| 2295 | } |
| 2296 | |
| 2297 | /* health: up but will fall down at first failure */ |
| 2298 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 2299 | srv->agent.fall = src->agent.fall; |
| 2300 | |
| 2301 | if (src->resolvers_id != NULL) |
| 2302 | srv->resolvers_id = strdup(src->resolvers_id); |
Emeric Brun | 21fbeed | 2020-12-23 18:01:04 +0100 | [diff] [blame] | 2303 | srv->resolv_opts.family_prio = src->resolv_opts.family_prio; |
| 2304 | srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip; |
| 2305 | srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight; |
| 2306 | if (srv->resolv_opts.family_prio == AF_UNSPEC) |
| 2307 | srv->resolv_opts.family_prio = AF_INET6; |
| 2308 | memcpy(srv->resolv_opts.pref_net, |
| 2309 | src->resolv_opts.pref_net, |
| 2310 | sizeof srv->resolv_opts.pref_net); |
| 2311 | 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] | 2312 | |
| 2313 | srv->init_addr_methods = src->init_addr_methods; |
| 2314 | srv->init_addr = src->init_addr; |
| 2315 | #if defined(USE_OPENSSL) |
| 2316 | srv_ssl_settings_cpy(srv, src); |
| 2317 | #endif |
| 2318 | #ifdef TCP_USER_TIMEOUT |
| 2319 | srv->tcp_ut = src->tcp_ut; |
| 2320 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2321 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 2322 | srv->pool_purge_delay = src->pool_purge_delay; |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 2323 | srv->low_idle_conns = src->low_idle_conns; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 2324 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 2325 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2326 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2327 | if (srv_tmpl) |
| 2328 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2329 | |
| 2330 | srv->check.via_socks4 = src->check.via_socks4; |
| 2331 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2332 | } |
| 2333 | |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 2334 | /* allocate a server and attach it to the global servers_list. Returns |
| 2335 | * the server on success, otherwise NULL. |
| 2336 | */ |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 2337 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2338 | { |
| 2339 | struct server *srv; |
| 2340 | |
| 2341 | srv = calloc(1, sizeof *srv); |
| 2342 | if (!srv) |
| 2343 | return NULL; |
| 2344 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2345 | srv_take(srv); |
| 2346 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2347 | srv->obj_type = OBJ_TYPE_SERVER; |
| 2348 | srv->proxy = proxy; |
Willy Tarreau | cdc83e0 | 2021-06-23 16:11:02 +0200 | [diff] [blame] | 2349 | queue_init(&srv->queue, proxy, srv); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2350 | LIST_APPEND(&servers_list, &srv->global_list); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2351 | LIST_INIT(&srv->srv_rec_item); |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 2352 | LIST_INIT(&srv->ip_rec_item); |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2353 | MT_LIST_INIT(&srv->prev_deleted); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2354 | event_hdl_sub_list_init(&srv->e_subs); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 2355 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2356 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2357 | srv->last_change = now.tv_sec; |
| 2358 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2359 | srv->check.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2360 | srv->check.status = HCHK_STATUS_INI; |
| 2361 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 2362 | srv->check.proxy = proxy; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 2363 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2364 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2365 | srv->agent.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2366 | srv->agent.status = HCHK_STATUS_INI; |
| 2367 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 2368 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2369 | 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] | 2370 | #if defined(USE_QUIC) |
| 2371 | srv->cids = EB_ROOT_UNIQUE; |
| 2372 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2373 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2374 | srv->extra_counters = NULL; |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 2375 | #ifdef USE_OPENSSL |
| 2376 | HA_RWLOCK_INIT(&srv->ssl_ctx.lock); |
| 2377 | #endif |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2378 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2379 | /* please don't put default server settings here, they are set in |
Willy Tarreau | 144289b | 2021-02-12 08:19:01 +0100 | [diff] [blame] | 2380 | * proxy_preset_defaults(). |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2381 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2382 | return srv; |
| 2383 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2384 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2385 | /* Increment the server refcount. */ |
| 2386 | void srv_take(struct server *srv) |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2387 | { |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2388 | HA_ATOMIC_INC(&srv->refcount); |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2389 | } |
| 2390 | |
Amaury Denoyelle | 13f2e2c | 2021-08-09 15:08:54 +0200 | [diff] [blame] | 2391 | /* Deallocate a server <srv> and its member. <srv> must be allocated. For |
| 2392 | * dynamic servers, its refcount is decremented first. The free operations are |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2393 | * conducted only if the refcount is nul. |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2394 | * |
| 2395 | * 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] | 2396 | * when calling srv_drop on the list of servers. |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2397 | */ |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2398 | struct server *srv_drop(struct server *srv) |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2399 | { |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2400 | struct server *next = NULL; |
| 2401 | |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2402 | if (!srv) |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2403 | goto end; |
| 2404 | |
| 2405 | next = srv->next; |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2406 | |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2407 | /* For dynamic servers, decrement the reference counter. Only free the |
| 2408 | * server when reaching zero. |
| 2409 | */ |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2410 | if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1)) |
| 2411 | goto end; |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2412 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2413 | /* make sure we are removed from our 'next->prev_deleted' list |
| 2414 | * This doesn't require full thread isolation as we're using mt lists |
| 2415 | * However this could easily be turned into regular list if required |
| 2416 | * (with the proper use of thread isolation) |
| 2417 | */ |
| 2418 | MT_LIST_DELETE(&srv->prev_deleted); |
| 2419 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2420 | task_destroy(srv->warmup); |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 2421 | task_destroy(srv->srvrq_check); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2422 | |
| 2423 | free(srv->id); |
| 2424 | free(srv->cookie); |
| 2425 | free(srv->hostname); |
| 2426 | free(srv->hostname_dn); |
| 2427 | free((char*)srv->conf.file); |
| 2428 | free(srv->per_thr); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 2429 | free(srv->per_tgrp); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2430 | free(srv->curr_idle_thr); |
| 2431 | free(srv->resolvers_id); |
| 2432 | free(srv->addr_node.key); |
Amaury Denoyelle | fb24794 | 2021-04-20 16:48:22 +0200 | [diff] [blame] | 2433 | free(srv->lb_nodes); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2434 | |
Aurelien DARRAGON | b5ee8be | 2023-03-09 14:28:00 +0100 | [diff] [blame] | 2435 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2436 | xprt_get(XPRT_SSL)->destroy_srv(srv); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2437 | HA_SPIN_DESTROY(&srv->lock); |
| 2438 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2439 | LIST_DELETE(&srv->global_list); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2440 | event_hdl_sub_list_destroy(&srv->e_subs); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2441 | |
| 2442 | EXTRA_COUNTERS_FREE(srv->extra_counters); |
| 2443 | |
Tim Duesterhus | 025b93e | 2021-11-04 21:03:52 +0100 | [diff] [blame] | 2444 | ha_free(&srv); |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2445 | |
| 2446 | end: |
| 2447 | return next; |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 2450 | /* Remove a server <srv> from a tracking list if <srv> is tracking another |
| 2451 | * server. No special care is taken if <srv> is tracked itself by another one : |
| 2452 | * this situation should be avoided by the caller. |
| 2453 | * |
| 2454 | * Not thread-safe. |
| 2455 | */ |
| 2456 | static void release_server_track(struct server *srv) |
| 2457 | { |
| 2458 | struct server *strack = srv->track; |
| 2459 | struct server **base; |
| 2460 | |
| 2461 | if (!strack) |
| 2462 | return; |
| 2463 | |
| 2464 | for (base = &strack->trackers; *base; base = &((*base)->tracknext)) { |
| 2465 | if (*base == srv) { |
| 2466 | *base = srv->tracknext; |
| 2467 | return; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | /* srv not found on the tracking list, this should never happen */ |
| 2472 | BUG_ON(!*base); |
| 2473 | } |
| 2474 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2475 | /* |
| 2476 | * Parse as much as possible such a range string argument: low[-high] |
| 2477 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2478 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2479 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2480 | * Returns 0 if succeeded, -1 if not. |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2481 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2482 | static int _srv_parse_tmpl_range(struct server *srv, const char *arg, |
| 2483 | int *nb_low, int *nb_high) |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2484 | { |
| 2485 | char *nb_high_arg; |
| 2486 | |
| 2487 | *nb_high = 0; |
| 2488 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2489 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2490 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2491 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2492 | *nb_high_arg++ = '\0'; |
| 2493 | *nb_high = atoi(nb_high_arg); |
| 2494 | } |
| 2495 | else { |
| 2496 | *nb_high += *nb_low; |
| 2497 | *nb_low = 1; |
| 2498 | } |
| 2499 | |
| 2500 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 2501 | return -1; |
| 2502 | |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2506 | /* Parse as much as possible such a range string argument: low[-high] |
| 2507 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2508 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2509 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2510 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2511 | * initialize a new server on startup. |
| 2512 | * |
| 2513 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2514 | * Returns 0 if succeeded, -1 if not. |
| 2515 | */ |
| 2516 | static inline void _srv_parse_set_id_from_prefix(struct server *srv, |
| 2517 | const char *prefix, int nb) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2518 | { |
| 2519 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 2520 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2521 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2524 | /* 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] | 2525 | * Note that a server template is a special server with |
| 2526 | * a few different parameters than a server which has |
| 2527 | * been parsed mostly the same way as a server. |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2528 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2529 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2530 | * initialize a new server on startup. |
| 2531 | * |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2532 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2533 | * 'srv' template included. |
| 2534 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2535 | 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] | 2536 | { |
| 2537 | int i; |
| 2538 | struct server *newsrv; |
| 2539 | |
| 2540 | 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] | 2541 | newsrv = new_server(px); |
| 2542 | if (!newsrv) |
| 2543 | goto err; |
| 2544 | |
Christopher Faulet | 75bef00 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 2545 | newsrv->conf.file = strdup(srv->conf.file); |
| 2546 | newsrv->conf.line = srv->conf.line; |
| 2547 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2548 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2549 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2550 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2551 | if (newsrv->sni_expr) { |
| 2552 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 2553 | if (!newsrv->ssl_ctx.sni) |
| 2554 | goto err; |
| 2555 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2556 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2557 | /* append to list of servers available to receive an hostname */ |
Emeric Brun | caef19e | 2021-06-14 10:02:18 +0200 | [diff] [blame] | 2558 | if (newsrv->srvrq) |
| 2559 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2560 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2561 | /* Set this new server ID. */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2562 | _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] | 2563 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2564 | /* Linked backwards first. This will be restablished after parsing. */ |
| 2565 | newsrv->next = px->srv; |
| 2566 | px->srv = newsrv; |
| 2567 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2568 | _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] | 2569 | |
| 2570 | return i - srv->tmpl_info.nb_low; |
| 2571 | |
| 2572 | err: |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2573 | _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] | 2574 | if (newsrv) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2575 | release_sample_expr(newsrv->ssl_ctx.sni); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2576 | free_check(&newsrv->agent); |
| 2577 | free_check(&newsrv->check); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2578 | LIST_DELETE(&newsrv->global_list); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2579 | } |
| 2580 | free(newsrv); |
| 2581 | return i - srv->tmpl_info.nb_low; |
| 2582 | } |
| 2583 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2584 | /* Allocate a new server pointed by <srv> and try to parse the first arguments |
| 2585 | * in <args> as an address for a server or an address-range for a template or |
| 2586 | * nothing for a default-server. <cur_arg> is incremented to the next argument. |
| 2587 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2588 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2589 | * initialize a new server on startup. |
| 2590 | * |
| 2591 | * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case |
| 2592 | * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated, |
| 2593 | * <srv> will be set to NULL. |
| 2594 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2595 | static int _srv_parse_init(struct server **srv, char **args, int *cur_arg, |
| 2596 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2597 | int parse_flags) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2598 | { |
| 2599 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2600 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2601 | int err_code = 0; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2602 | char *fqdn = NULL; |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2603 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2604 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2605 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2606 | *srv = NULL; |
| 2607 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2608 | /* There is no mandatory first arguments for default server. */ |
| 2609 | if (parse_flags & SRV_PARSE_PARSE_ADDR) { |
| 2610 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2611 | if (!*args[3]) { |
| 2612 | /* 'server-template' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2613 | ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n", |
| 2614 | args[0]); |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2615 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2616 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2619 | err = invalid_prefix_char(args[1]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2620 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2621 | else { |
| 2622 | if (!*args[2]) { |
| 2623 | /* 'server' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2624 | ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2625 | args[0]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2626 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2627 | goto out; |
| 2628 | } |
| 2629 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2630 | err = invalid_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2633 | if (err) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2634 | ha_alert("character '%c' is not permitted in %s %s '%s'.\n", |
| 2635 | *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2636 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2637 | goto out; |
| 2638 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2639 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2640 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2641 | *cur_arg = 2; |
| 2642 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2643 | /* Parse server-template <nb | range> arg. */ |
| 2644 | 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] | 2645 | ha_alert("Wrong %s number or range arg '%s'.\n", |
| 2646 | args[0], args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2647 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2648 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2649 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2650 | (*cur_arg)++; |
| 2651 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2652 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2653 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2654 | struct sockaddr_storage *sk; |
| 2655 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2656 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2657 | *srv = newsrv = new_server(curproxy); |
| 2658 | if (!newsrv) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2659 | ha_alert("out of memory.\n"); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2660 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2661 | goto out; |
| 2662 | } |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2663 | register_parsing_obj(&newsrv->obj_type); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2664 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2665 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2666 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2667 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2668 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2669 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2670 | if (parse_flags & SRV_PARSE_DYNAMIC) |
| 2671 | newsrv->flags |= SRV_F_DYNAMIC; |
| 2672 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2673 | /* Note: for a server template, its id is its prefix. |
| 2674 | * This is a temporary id which will be used for server allocations to come |
| 2675 | * after parsing. |
| 2676 | */ |
| 2677 | if (!(parse_flags & SRV_PARSE_TEMPLATE)) |
| 2678 | newsrv->id = strdup(args[1]); |
| 2679 | else |
| 2680 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2681 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 2682 | /* revision defaults to 0 */ |
| 2683 | newsrv->rid = 0; |
| 2684 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2685 | /* several ways to check the port component : |
| 2686 | * - IP => port=+0, relative (IPv4 only) |
| 2687 | * - IP: => port=+0, relative |
| 2688 | * - IP:N => port=N, absolute |
| 2689 | * - IP:+N => port=+N, relative |
| 2690 | * - IP:-N => port=-N, relative |
| 2691 | */ |
| 2692 | if (!(parse_flags & SRV_PARSE_PARSE_ADDR)) |
| 2693 | goto skip_addr; |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2694 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2695 | sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2696 | &errmsg, NULL, &fqdn, |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2697 | (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); |
| 2698 | if (!sk) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2699 | ha_alert("%s\n", errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2700 | err_code |= ERR_ALERT | ERR_FATAL; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2701 | ha_free(&errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2702 | goto out; |
| 2703 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2704 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2705 | if (!port1 || !port2) { |
| 2706 | /* no port specified, +offset, -offset */ |
| 2707 | newsrv->flags |= SRV_F_MAPPORTS; |
| 2708 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2709 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2710 | /* save hostname and create associated name resolution */ |
| 2711 | if (fqdn) { |
| 2712 | if (fqdn[0] == '_') { /* SRV record */ |
| 2713 | /* Check if a SRV request already exists, and if not, create it */ |
| 2714 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2715 | newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn); |
| 2716 | if (newsrv->srvrq == NULL) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2717 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2718 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2719 | } |
Christopher Faulet | 81ba74a | 2021-06-29 20:52:35 +0200 | [diff] [blame] | 2720 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2721 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2722 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2723 | ha_alert("Can't create DNS resolution for server '%s'\n", |
| 2724 | newsrv->id); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2725 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2726 | goto out; |
| 2727 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2728 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2729 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2730 | newsrv->addr = *sk; |
| 2731 | newsrv->svc_port = port; |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 2732 | /* |
| 2733 | * we don't need to lock the server here, because |
| 2734 | * we are in the process of initializing. |
| 2735 | * |
| 2736 | * Note that the server is not attached into the proxy tree if |
| 2737 | * this is a dynamic server. |
| 2738 | */ |
| 2739 | srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC)); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2740 | |
Willy Tarreau | 14e7f29 | 2021-10-27 17:41:07 +0200 | [diff] [blame] | 2741 | if (!newsrv->srvrq && !newsrv->hostname && |
| 2742 | !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2743 | ha_alert("Unknown protocol family %d '%s'\n", |
| 2744 | newsrv->addr.ss_family, args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2745 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2746 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2747 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2748 | |
| 2749 | (*cur_arg)++; |
| 2750 | skip_addr: |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2751 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2752 | /* Copy default server settings to new server */ |
| 2753 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
| 2754 | } else { |
| 2755 | /* Initialize dynamic server weight to 1 */ |
| 2756 | newsrv->uweight = newsrv->iweight = 1; |
| 2757 | |
| 2758 | /* A dynamic server is disabled on startup */ |
| 2759 | newsrv->next_admin = SRV_ADMF_FMAINT; |
| 2760 | newsrv->next_state = SRV_ST_STOPPED; |
| 2761 | server_recalc_eweight(newsrv, 0); |
Amaury Denoyelle | fca1817 | 2021-07-22 16:03:36 +0200 | [diff] [blame] | 2762 | |
| 2763 | /* Set default values for checks */ |
| 2764 | newsrv->check.inter = DEF_CHKINTR; |
| 2765 | newsrv->check.rise = DEF_RISETIME; |
| 2766 | newsrv->check.fall = DEF_FALLTIME; |
| 2767 | |
| 2768 | newsrv->agent.inter = DEF_CHKINTR; |
| 2769 | newsrv->agent.rise = DEF_AGENT_RISETIME; |
| 2770 | newsrv->agent.fall = DEF_AGENT_FALLTIME; |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2771 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2772 | HA_SPIN_INIT(&newsrv->lock); |
| 2773 | } |
| 2774 | else { |
| 2775 | *srv = newsrv = &curproxy->defsrv; |
| 2776 | *cur_arg = 1; |
| 2777 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 2778 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2779 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2780 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2781 | free(fqdn); |
| 2782 | return 0; |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2783 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2784 | out: |
| 2785 | free(fqdn); |
| 2786 | return err_code; |
| 2787 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2788 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2789 | /* Parse the server keyword in <args>. |
| 2790 | * <cur_arg> is incremented beyond the keyword optional value. Note that this |
| 2791 | * might not be the case if an error is reported. |
| 2792 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2793 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2794 | * initialize a new server on startup. |
| 2795 | * |
| 2796 | * A mask of errors is returned. ERR_FATAL is set if the parsing should be |
| 2797 | * interrupted. |
| 2798 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2799 | static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg, |
| 2800 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2801 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2802 | { |
| 2803 | int err_code = 0; |
| 2804 | struct srv_kw *kw; |
| 2805 | const char *best; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2806 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2807 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2808 | kw = srv_find_kw(args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2809 | if (!kw) { |
| 2810 | best = srv_find_best_kw(args[*cur_arg]); |
| 2811 | if (best) |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2812 | ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n", |
| 2813 | args[*cur_arg], best, |
| 2814 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2815 | " Hint: no address was expected for this server."); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2816 | else |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2817 | ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg], |
| 2818 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2819 | " Hint: no address was expected for this server."); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2820 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2821 | return ERR_ALERT | ERR_FATAL; |
| 2822 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2823 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2824 | if (!kw->parse) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2825 | ha_alert("'%s' option is not implemented in this version (check build options)\n", |
| 2826 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2827 | err_code = ERR_ALERT | ERR_FATAL; |
| 2828 | goto out; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2829 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2830 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2831 | if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2832 | ha_alert("'%s' option is not accepted in default-server sections\n", |
| 2833 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2834 | err_code = ERR_ALERT; |
| 2835 | goto out; |
| 2836 | } |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2837 | else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2838 | ha_alert("'%s' option is not accepted for dynamic server\n", |
| 2839 | args[*cur_arg]); |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2840 | err_code |= ERR_ALERT; |
| 2841 | goto out; |
| 2842 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2843 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2844 | err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg); |
| 2845 | if (err_code) { |
| 2846 | display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg); |
| 2847 | free(errmsg); |
| 2848 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2849 | |
| 2850 | out: |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2851 | if (kw->skip != -1) |
| 2852 | *cur_arg += 1 + kw->skip; |
| 2853 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2854 | return err_code; |
| 2855 | } |
| 2856 | |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2857 | /* This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2858 | * initialize a new server on startup. |
| 2859 | */ |
| 2860 | static int _srv_parse_sni_expr_init(char **args, int cur_arg, |
| 2861 | struct server *srv, struct proxy *proxy, |
| 2862 | char **errmsg) |
| 2863 | { |
| 2864 | int ret; |
| 2865 | |
| 2866 | if (!srv->sni_expr) |
| 2867 | return 0; |
| 2868 | |
| 2869 | ret = server_parse_sni_expr(srv, proxy, errmsg); |
| 2870 | if (!ret) |
| 2871 | return 0; |
| 2872 | |
| 2873 | return ret; |
| 2874 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2875 | |
| 2876 | /* Server initializations finalization. |
| 2877 | * Initialize health check, agent check and SNI expression if enabled. |
| 2878 | * Must not be called for a default server instance. |
| 2879 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2880 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2881 | * initialize a new server on startup. |
| 2882 | */ |
| 2883 | static int _srv_parse_finalize(char **args, int cur_arg, |
| 2884 | struct server *srv, struct proxy *px, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2885 | int parse_flags) |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2886 | { |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2887 | int ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2888 | char *errmsg = NULL; |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2889 | |
| 2890 | if (srv->do_check && srv->trackit) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2891 | 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] | 2892 | return ERR_ALERT | ERR_FATAL; |
| 2893 | } |
| 2894 | |
| 2895 | if (srv->do_agent && !srv->agent.port) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2896 | ha_alert("server %s does not have agent port. Agent check has been disabled.\n", |
| 2897 | srv->id); |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2898 | return ERR_ALERT | ERR_FATAL; |
| 2899 | } |
| 2900 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2901 | if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) { |
| 2902 | if (errmsg) { |
| 2903 | ha_alert("%s\n", errmsg); |
| 2904 | free(errmsg); |
| 2905 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2906 | return ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2907 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2908 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2909 | /* A dynamic server is disabled on startup. It must not be counted as |
| 2910 | * an active backend entry. |
| 2911 | */ |
| 2912 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2913 | if (srv->flags & SRV_F_BACKUP) |
| 2914 | px->srv_bck++; |
| 2915 | else |
| 2916 | px->srv_act++; |
| 2917 | } |
| 2918 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2919 | srv_lb_commit_status(srv); |
| 2920 | |
| 2921 | return 0; |
| 2922 | } |
| 2923 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2924 | int parse_server(const char *file, int linenum, char **args, |
| 2925 | struct proxy *curproxy, const struct proxy *defproxy, |
| 2926 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2927 | { |
| 2928 | struct server *newsrv = NULL; |
| 2929 | int err_code = 0; |
| 2930 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2931 | int cur_arg; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2932 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2933 | set_usermsgs_ctx(file, linenum, NULL); |
| 2934 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2935 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) { |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2936 | ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2937 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2938 | goto out; |
| 2939 | } |
| 2940 | else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) { |
| 2941 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2942 | goto out; |
| 2943 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2944 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2945 | if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) == |
| 2946 | (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) { |
| 2947 | if (!*args[2]) |
| 2948 | return 0; |
| 2949 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2950 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2951 | err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2952 | parse_flags); |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 2953 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2954 | /* the servers are linked backwards first */ |
| 2955 | if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2956 | newsrv->next = curproxy->srv; |
| 2957 | curproxy->srv = newsrv; |
| 2958 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2959 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2960 | if (err_code & ERR_CODE) |
| 2961 | goto out; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2962 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2963 | newsrv->conf.file = strdup(file); |
| 2964 | newsrv->conf.line = linenum; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2965 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2966 | while (*args[cur_arg]) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2967 | err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2968 | parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2969 | if (err_code & ERR_FATAL) |
| 2970 | goto out; |
| 2971 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2972 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2973 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2974 | err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2975 | if (err_code & ERR_FATAL) |
| 2976 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2977 | } |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 2978 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2979 | if (parse_flags & SRV_PARSE_TEMPLATE) |
| 2980 | _srv_parse_tmpl_init(newsrv, curproxy); |
| 2981 | |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 2982 | /* If the server id is fixed, insert it in the proxy used_id tree. |
| 2983 | * This is needed to detect a later duplicate id via srv_parse_id. |
| 2984 | * |
| 2985 | * If no is specified, a dynamic one is generated in |
| 2986 | * check_config_validity. |
| 2987 | */ |
| 2988 | if (newsrv->flags & SRV_F_FORCED_ID) |
| 2989 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 2990 | |
Amaury Denoyelle | 24abb0c | 2021-05-07 15:13:51 +0200 | [diff] [blame] | 2991 | HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight, |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2992 | "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] | 2993 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2994 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2995 | return 0; |
| 2996 | |
| 2997 | out: |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2998 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2999 | return err_code; |
| 3000 | } |
| 3001 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 3002 | /* Returns a pointer to the first server matching either id <id>. |
| 3003 | * NULL is returned if no match is found. |
| 3004 | * the lookup is performed in the backend <bk> |
| 3005 | */ |
| 3006 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 3007 | { |
| 3008 | struct eb32_node *eb32; |
| 3009 | struct server *curserver; |
| 3010 | |
| 3011 | if (!bk || (id ==0)) |
| 3012 | return NULL; |
| 3013 | |
| 3014 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3015 | if (!(bk->cap & PR_CAP_BE)) |
| 3016 | return NULL; |
| 3017 | |
| 3018 | curserver = NULL; |
| 3019 | |
| 3020 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 3021 | if (eb32) |
| 3022 | curserver = container_of(eb32, struct server, conf.id); |
| 3023 | |
| 3024 | return curserver; |
| 3025 | } |
| 3026 | |
| 3027 | /* Returns a pointer to the first server matching either name <name>, or id |
| 3028 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 3029 | * the lookup is performed in the backend <bk> |
| 3030 | */ |
| 3031 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 3032 | { |
| 3033 | struct server *curserver; |
| 3034 | |
| 3035 | if (!bk || !name) |
| 3036 | return NULL; |
| 3037 | |
| 3038 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3039 | if (!(bk->cap & PR_CAP_BE)) |
| 3040 | return NULL; |
| 3041 | |
| 3042 | curserver = NULL; |
| 3043 | if (*name == '#') { |
| 3044 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 3045 | if (curserver) |
| 3046 | return curserver; |
| 3047 | } |
| 3048 | else { |
| 3049 | curserver = bk->srv; |
| 3050 | |
| 3051 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 3052 | curserver = curserver->next; |
| 3053 | |
| 3054 | if (curserver) |
| 3055 | return curserver; |
| 3056 | } |
| 3057 | |
| 3058 | return NULL; |
| 3059 | } |
| 3060 | |
| 3061 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 3062 | { |
| 3063 | struct server *byname; |
| 3064 | struct server *byid; |
| 3065 | |
| 3066 | if (!name && !id) |
| 3067 | return NULL; |
| 3068 | |
| 3069 | if (diff) |
| 3070 | *diff = 0; |
| 3071 | |
| 3072 | byname = byid = NULL; |
| 3073 | |
| 3074 | if (name) { |
| 3075 | byname = server_find_by_name(bk, name); |
| 3076 | if (byname && (!id || byname->puid == id)) |
| 3077 | return byname; |
| 3078 | } |
| 3079 | |
| 3080 | /* remaining possibilities : |
| 3081 | * - name not set |
| 3082 | * - name set but not found |
| 3083 | * - name found but ID doesn't match |
| 3084 | */ |
| 3085 | if (id) { |
| 3086 | byid = server_find_by_id(bk, id); |
| 3087 | if (byid) { |
| 3088 | if (byname) { |
| 3089 | /* use id only if forced by configuration */ |
| 3090 | if (byid->flags & SRV_F_FORCED_ID) { |
| 3091 | if (diff) |
| 3092 | *diff |= 2; |
| 3093 | return byid; |
| 3094 | } |
| 3095 | else { |
| 3096 | if (diff) |
| 3097 | *diff |= 1; |
| 3098 | return byname; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | /* remaining possibilities: |
| 3103 | * - name not set |
| 3104 | * - name set but not found |
| 3105 | */ |
| 3106 | if (name && diff) |
| 3107 | *diff |= 2; |
| 3108 | return byid; |
| 3109 | } |
| 3110 | |
| 3111 | /* id bot found */ |
| 3112 | if (byname) { |
| 3113 | if (diff) |
| 3114 | *diff |= 1; |
| 3115 | return byname; |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | return NULL; |
| 3120 | } |
| 3121 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3122 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3123 | * update a server's current IP address. |
| 3124 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3125 | * ip is in network format. |
| 3126 | * updater is a string which contains an information about the requester of the update. |
| 3127 | * updater is used if not NULL. |
| 3128 | * |
| 3129 | * 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] | 3130 | * |
| 3131 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3132 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3133 | 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] | 3134 | { |
Christopher Faulet | d6c6b5f | 2020-09-08 10:27:24 +0200 | [diff] [blame] | 3135 | /* save the new IP family & address if necessary */ |
| 3136 | switch (ip_sin_family) { |
| 3137 | case AF_INET: |
| 3138 | if (s->addr.ss_family == ip_sin_family && |
| 3139 | !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4)) |
| 3140 | return 0; |
| 3141 | break; |
| 3142 | case AF_INET6: |
| 3143 | if (s->addr.ss_family == ip_sin_family && |
| 3144 | !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16)) |
| 3145 | return 0; |
| 3146 | break; |
| 3147 | }; |
| 3148 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3149 | /* generates a log line and a warning on stderr */ |
| 3150 | if (1) { |
| 3151 | /* book enough space for both IPv4 and IPv6 */ |
| 3152 | char oldip[INET6_ADDRSTRLEN]; |
| 3153 | char newip[INET6_ADDRSTRLEN]; |
| 3154 | |
| 3155 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3156 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3157 | |
| 3158 | /* copy old IP address in a string */ |
| 3159 | switch (s->addr.ss_family) { |
| 3160 | case AF_INET: |
| 3161 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3162 | break; |
| 3163 | case AF_INET6: |
| 3164 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3165 | break; |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3166 | default: |
Willy Tarreau | fc458ec | 2023-04-07 18:11:39 +0200 | [diff] [blame] | 3167 | strlcpy2(oldip, "(none)", sizeof(oldip)); |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3168 | break; |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3169 | }; |
| 3170 | |
| 3171 | /* copy new IP address in a string */ |
| 3172 | switch (ip_sin_family) { |
| 3173 | case AF_INET: |
| 3174 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3175 | break; |
| 3176 | case AF_INET6: |
| 3177 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3178 | break; |
| 3179 | }; |
| 3180 | |
| 3181 | /* save log line into a buffer */ |
| 3182 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3183 | s->proxy->id, s->id, oldip, newip, updater); |
| 3184 | |
| 3185 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3186 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3187 | |
| 3188 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3189 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | /* save the new IP family */ |
| 3193 | s->addr.ss_family = ip_sin_family; |
| 3194 | /* save the new IP address */ |
| 3195 | switch (ip_sin_family) { |
| 3196 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3197 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3198 | break; |
| 3199 | case AF_INET6: |
| 3200 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3201 | break; |
| 3202 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3203 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3204 | srv_set_addr_desc(s, 1); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3205 | |
| 3206 | return 0; |
| 3207 | } |
| 3208 | |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 3209 | /* update agent health check address and port |
| 3210 | * addr can be ip4/ip6 or a hostname |
| 3211 | * if one error occurs, don't apply anything |
| 3212 | * must be called with the server lock held. |
| 3213 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3214 | 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] | 3215 | { |
| 3216 | struct sockaddr_storage sk; |
| 3217 | struct buffer *msg; |
| 3218 | int new_port; |
| 3219 | |
| 3220 | msg = get_trash_chunk(); |
| 3221 | chunk_reset(msg); |
| 3222 | |
| 3223 | if (!(s->agent.state & CHK_ST_ENABLED)) { |
| 3224 | chunk_strcat(msg, "agent checks are not enabled on this server"); |
| 3225 | goto out; |
| 3226 | } |
| 3227 | if (addr) { |
| 3228 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3229 | if (str2ip(addr, &sk) == NULL) { |
| 3230 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3231 | goto out; |
| 3232 | } |
| 3233 | } |
| 3234 | if (port) { |
| 3235 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3236 | chunk_appendf(msg, "provided port is not an integer"); |
| 3237 | goto out; |
| 3238 | } |
| 3239 | if (new_port < 0 || new_port > 65535) { |
| 3240 | chunk_appendf(msg, "provided port is invalid"); |
| 3241 | goto out; |
| 3242 | } |
| 3243 | } |
| 3244 | out: |
| 3245 | if (msg->data) |
| 3246 | return msg->area; |
| 3247 | else { |
| 3248 | if (addr) |
| 3249 | set_srv_agent_addr(s, &sk); |
| 3250 | if (port) |
| 3251 | set_srv_agent_port(s, new_port); |
| 3252 | } |
| 3253 | return NULL; |
| 3254 | } |
| 3255 | |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 3256 | /* update server health check address and port |
| 3257 | * addr must be ip4 or ip6, it won't be resolved |
| 3258 | * if one error occurs, don't apply anything |
| 3259 | * must be called with the server lock held. |
| 3260 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3261 | 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] | 3262 | { |
| 3263 | struct sockaddr_storage sk; |
| 3264 | struct buffer *msg; |
| 3265 | int new_port; |
| 3266 | |
| 3267 | msg = get_trash_chunk(); |
| 3268 | chunk_reset(msg); |
| 3269 | |
| 3270 | if (!(s->check.state & CHK_ST_ENABLED)) { |
| 3271 | chunk_strcat(msg, "health checks are not enabled on this server"); |
| 3272 | goto out; |
| 3273 | } |
| 3274 | if (addr) { |
| 3275 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3276 | if (str2ip2(addr, &sk, 0) == NULL) { |
| 3277 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3278 | goto out; |
| 3279 | } |
| 3280 | } |
| 3281 | if (port) { |
| 3282 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3283 | chunk_appendf(msg, "provided port is not an integer"); |
| 3284 | goto out; |
| 3285 | } |
| 3286 | if (new_port < 0 || new_port > 65535) { |
| 3287 | chunk_appendf(msg, "provided port is invalid"); |
| 3288 | goto out; |
| 3289 | } |
| 3290 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 3291 | if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) { |
| 3292 | chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use"); |
| 3293 | goto out; |
| 3294 | } |
| 3295 | } |
| 3296 | out: |
| 3297 | if (msg->data) |
| 3298 | return msg->area; |
| 3299 | else { |
| 3300 | if (addr) |
| 3301 | s->check.addr = sk; |
| 3302 | if (port) |
| 3303 | s->check.port = new_port; |
| 3304 | } |
| 3305 | return NULL; |
| 3306 | } |
| 3307 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3308 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3309 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3310 | * |
| 3311 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3312 | * returned by the function. |
| 3313 | * |
| 3314 | * The function first does the following, in that order: |
| 3315 | * - validates the new addr and/or port |
| 3316 | * - checks if an update is required (new IP or port is different than current ones) |
| 3317 | * - checks the update is allowed: |
| 3318 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3319 | * - allow all changes if no CHECKS are configured |
| 3320 | * - if CHECK is configured: |
| 3321 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3322 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3323 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3324 | * |
| 3325 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3326 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3327 | 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] | 3328 | { |
| 3329 | struct sockaddr_storage sa; |
| 3330 | int ret, port_change_required; |
| 3331 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3332 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3333 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3334 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3335 | |
| 3336 | msg = get_trash_chunk(); |
| 3337 | chunk_reset(msg); |
| 3338 | |
| 3339 | if (addr) { |
| 3340 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3341 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3342 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3343 | goto out; |
| 3344 | } |
| 3345 | |
| 3346 | /* changes are allowed on AF_INET* families only */ |
| 3347 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3348 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3349 | goto out; |
| 3350 | } |
| 3351 | |
| 3352 | /* collecting data currently setup */ |
| 3353 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3354 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3355 | /* changes are allowed on AF_INET* families only */ |
| 3356 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3357 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3358 | goto out; |
| 3359 | } |
| 3360 | |
| 3361 | /* applying ADDR changes if required and allowed |
| 3362 | * ipcmp returns 0 when both ADDR are the same |
| 3363 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3364 | if (ipcmp(&s->addr, &sa, 0) == 0) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3365 | chunk_appendf(msg, "no need to change the addr"); |
| 3366 | goto port; |
| 3367 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3368 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3369 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3370 | |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3371 | /* update report for caller */ |
| 3372 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3373 | } |
| 3374 | |
| 3375 | port: |
| 3376 | if (port) { |
| 3377 | char sign = '\0'; |
| 3378 | char *endptr; |
| 3379 | |
| 3380 | if (addr) |
| 3381 | chunk_appendf(msg, ", "); |
| 3382 | |
| 3383 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3384 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3385 | |
| 3386 | /* check if PORT change is required */ |
| 3387 | port_change_required = 0; |
| 3388 | |
| 3389 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3390 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3391 | new_port = strtol(port, &endptr, 10); |
| 3392 | if ((errno != 0) || (port == endptr)) { |
| 3393 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3394 | goto out; |
| 3395 | } |
| 3396 | |
| 3397 | /* check if caller triggers a port mapped or offset */ |
| 3398 | if (sign == '-' || (sign == '+')) { |
| 3399 | /* check if server currently uses port map */ |
| 3400 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3401 | /* switch from fixed port to port map mandatorily triggers |
| 3402 | * a port change */ |
| 3403 | port_change_required = 1; |
| 3404 | /* check is configured |
| 3405 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3406 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3407 | * to port mapping */ |
William Dauchy | 69f118d | 2021-02-03 22:30:07 +0100 | [diff] [blame] | 3408 | if (!s->check.port) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3409 | 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."); |
| 3410 | goto out; |
| 3411 | } |
| 3412 | } |
| 3413 | /* we're already using port maps */ |
| 3414 | else { |
| 3415 | port_change_required = current_port != new_port; |
| 3416 | } |
| 3417 | } |
| 3418 | /* fixed port */ |
| 3419 | else { |
| 3420 | port_change_required = current_port != new_port; |
| 3421 | } |
| 3422 | |
| 3423 | /* applying PORT changes if required and update response message */ |
| 3424 | if (port_change_required) { |
| 3425 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3426 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3427 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3428 | |
| 3429 | /* prepare message */ |
| 3430 | chunk_appendf(msg, "port changed from '"); |
| 3431 | if (s->flags & SRV_F_MAPPORTS) |
| 3432 | chunk_appendf(msg, "+"); |
| 3433 | chunk_appendf(msg, "%d' to '", current_port); |
| 3434 | |
| 3435 | if (sign == '-') { |
| 3436 | s->flags |= SRV_F_MAPPORTS; |
| 3437 | chunk_appendf(msg, "%c", sign); |
| 3438 | /* just use for result output */ |
| 3439 | new_port = -new_port; |
| 3440 | } |
| 3441 | else if (sign == '+') { |
| 3442 | s->flags |= SRV_F_MAPPORTS; |
| 3443 | chunk_appendf(msg, "%c", sign); |
| 3444 | } |
| 3445 | else { |
| 3446 | s->flags &= ~SRV_F_MAPPORTS; |
| 3447 | } |
| 3448 | |
| 3449 | chunk_appendf(msg, "%d'", new_port); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3450 | } |
| 3451 | else { |
| 3452 | chunk_appendf(msg, "no need to change the port"); |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | out: |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3457 | if (changed) { |
| 3458 | /* force connection cleanup on the given server */ |
| 3459 | srv_cleanup_connections(s); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3460 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3461 | srv_set_addr_desc(s, 1); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3462 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3463 | if (updater) |
| 3464 | chunk_appendf(msg, " by '%s'", updater); |
| 3465 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3466 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3467 | } |
| 3468 | |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3469 | /* |
| 3470 | * update server status based on result of SRV resolution |
| 3471 | * returns: |
| 3472 | * 0 if server status is updated |
| 3473 | * 1 if server status has not changed |
| 3474 | * |
| 3475 | * Must be called with the server lock held. |
| 3476 | */ |
| 3477 | int srvrq_update_srv_status(struct server *s, int has_no_ip) |
| 3478 | { |
| 3479 | if (!s->srvrq) |
| 3480 | return 1; |
| 3481 | |
| 3482 | /* since this server has an IP, it can go back in production */ |
| 3483 | if (has_no_ip == 0) { |
| 3484 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3485 | return 1; |
| 3486 | } |
| 3487 | |
| 3488 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3489 | return 1; |
| 3490 | |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3491 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOENT); |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3492 | return 0; |
| 3493 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3494 | |
| 3495 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3496 | * update server status based on result of name resolution |
| 3497 | * returns: |
| 3498 | * 0 if server status is updated |
| 3499 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3500 | * |
| 3501 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3502 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3503 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3504 | { |
Emeric Brun | 750fe79 | 2020-12-23 16:51:12 +0100 | [diff] [blame] | 3505 | struct resolvers *resolvers = s->resolvers; |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3506 | struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3507 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3508 | |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3509 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3510 | if (resolution == NULL) |
| 3511 | return srvrq_update_srv_status(s, has_no_ip); |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3512 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3513 | switch (resolution->status) { |
| 3514 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3515 | /* status when HAProxy has just (re)started. |
| 3516 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3517 | break; |
| 3518 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3519 | case RSLV_STATUS_VALID: |
| 3520 | /* |
| 3521 | * resume health checks |
| 3522 | * server will be turned back on if health check is safe |
| 3523 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3524 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3525 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3526 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3527 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOIP); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3528 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3529 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3530 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3531 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3532 | return 1; |
| 3533 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3534 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3535 | s->proxy->id, s->id); |
| 3536 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3537 | ha_warning("%s.\n", trash.area); |
| 3538 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3539 | return 0; |
| 3540 | |
| 3541 | case RSLV_STATUS_NX: |
| 3542 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3543 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3544 | if (!tick_is_expired(exp, now_ms)) |
| 3545 | break; |
| 3546 | |
| 3547 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3548 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3549 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NX); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3550 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3551 | |
| 3552 | case RSLV_STATUS_TIMEOUT: |
| 3553 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3554 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3555 | if (!tick_is_expired(exp, now_ms)) |
| 3556 | break; |
| 3557 | |
| 3558 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3559 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3560 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_TIMEOUT); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3561 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3562 | |
| 3563 | case RSLV_STATUS_REFUSED: |
| 3564 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3565 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3566 | if (!tick_is_expired(exp, now_ms)) |
| 3567 | break; |
| 3568 | |
| 3569 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3570 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3571 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_REFUSED); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3572 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3573 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3574 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3575 | /* stop server if resolution failed for a long enough period */ |
| 3576 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3577 | if (!tick_is_expired(exp, now_ms)) |
| 3578 | break; |
| 3579 | |
| 3580 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3581 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3582 | srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_UNSPEC); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3583 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | return 1; |
| 3587 | } |
| 3588 | |
| 3589 | /* |
| 3590 | * Server Name Resolution valid response callback |
| 3591 | * It expects: |
| 3592 | * - <nameserver>: the name server which answered the valid response |
| 3593 | * - <response>: buffer containing a valid DNS response |
| 3594 | * - <response_len>: size of <response> |
| 3595 | * It performs the following actions: |
| 3596 | * - ignore response if current ip found and server family not met |
| 3597 | * - update with first new ip found if family is met and current IP is not found |
| 3598 | * returns: |
| 3599 | * 0 on error |
| 3600 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3601 | * |
| 3602 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3603 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3604 | int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3605 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3606 | struct server *s = NULL; |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3607 | struct resolv_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3608 | void *serverip, *firstip; |
| 3609 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3610 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3611 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3612 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3613 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3614 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3615 | if (!s) |
| 3616 | return 1; |
| 3617 | |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3618 | if (s->srvrq) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3619 | /* If DNS resolution is disabled ignore it. |
| 3620 | * This is the case if the server was associated to |
| 3621 | * a SRV record and this record is now expired. |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3622 | */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3623 | if (s->flags & SRV_F_NO_RESOLUTION) |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3624 | return 1; |
| 3625 | } |
| 3626 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3627 | resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3628 | if (!resolution) |
| 3629 | return 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3630 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3631 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3632 | firstip = NULL; /* pointer to the first valid response found */ |
| 3633 | /* it will be used as the new IP if a change is required */ |
| 3634 | firstip_sin_family = AF_UNSPEC; |
| 3635 | serverip = NULL; /* current server IP address */ |
| 3636 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3637 | /* initializing server IP pointer */ |
| 3638 | server_sin_family = s->addr.ss_family; |
| 3639 | switch (server_sin_family) { |
| 3640 | case AF_INET: |
| 3641 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3642 | break; |
| 3643 | |
| 3644 | case AF_INET6: |
| 3645 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3646 | break; |
| 3647 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3648 | case AF_UNSPEC: |
| 3649 | break; |
| 3650 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3651 | default: |
| 3652 | goto invalid; |
| 3653 | } |
| 3654 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3655 | ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts, |
| 3656 | serverip, server_sin_family, &firstip, |
| 3657 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3658 | |
| 3659 | switch (ret) { |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3660 | case RSLV_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3661 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3662 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3663 | case RSLV_UPD_SRVIP_NOT_FOUND: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3664 | goto save_ip; |
| 3665 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3666 | case RSLV_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3667 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3668 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3669 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3670 | case RSLV_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3671 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3672 | resolution->status = RSLV_STATUS_OTHER; |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3673 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3674 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3675 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3676 | default: |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3677 | has_no_ip = 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3678 | goto invalid; |
| 3679 | |
| 3680 | } |
| 3681 | |
| 3682 | save_ip: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3683 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3684 | counters->app.resolver.update++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3685 | /* save the first ip we found */ |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3686 | chunk_printf(chk, "%s/%s", counters->pid, counters->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3687 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3688 | else |
| 3689 | chunk_printf(chk, "DNS cache"); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3690 | srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3691 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3692 | update_status: |
Christopher Faulet | a8ce497 | 2021-06-24 15:26:03 +0200 | [diff] [blame] | 3693 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3694 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3695 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3696 | |
| 3697 | invalid: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3698 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3699 | counters->app.resolver.invalid++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3700 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 3701 | } |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3702 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3703 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3704 | return 0; |
| 3705 | } |
| 3706 | |
| 3707 | /* |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3708 | * SRV record error management callback |
| 3709 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3710 | * 0 if we can trash answser items. |
| 3711 | * 1 when safely ignored and we must kept answer items |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3712 | * |
| 3713 | * Grabs the server's lock. |
| 3714 | */ |
| 3715 | int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code) |
| 3716 | { |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3717 | struct resolv_srvrq *srvrq; |
| 3718 | struct resolv_resolution *res; |
| 3719 | struct resolvers *resolvers; |
| 3720 | int exp; |
| 3721 | |
| 3722 | /* SRV records */ |
| 3723 | srvrq = objt_resolv_srvrq(requester->owner); |
| 3724 | if (!srvrq) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3725 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3726 | |
| 3727 | resolvers = srvrq->resolvers; |
| 3728 | res = requester->resolution; |
| 3729 | |
| 3730 | switch (res->status) { |
| 3731 | |
| 3732 | case RSLV_STATUS_NX: |
| 3733 | /* stop server if resolution is NX for a long enough period */ |
| 3734 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 3735 | if (!tick_is_expired(exp, now_ms)) |
| 3736 | return 1; |
| 3737 | break; |
| 3738 | |
| 3739 | case RSLV_STATUS_TIMEOUT: |
| 3740 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 3741 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 3742 | if (!tick_is_expired(exp, now_ms)) |
| 3743 | return 1; |
| 3744 | break; |
| 3745 | |
| 3746 | case RSLV_STATUS_REFUSED: |
| 3747 | /* stop server if resolution is REFUSED for a long enough period */ |
| 3748 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 3749 | if (!tick_is_expired(exp, now_ms)) |
| 3750 | return 1; |
| 3751 | break; |
| 3752 | |
| 3753 | default: |
| 3754 | /* stop server if resolution failed for a long enough period */ |
| 3755 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 3756 | if (!tick_is_expired(exp, now_ms)) |
| 3757 | return 1; |
| 3758 | } |
| 3759 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3760 | /* Remove any associated server ref */ |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3761 | resolv_detach_from_resolution_answer_items(res, requester); |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3762 | |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3763 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3767 | * Server Name Resolution error management callback |
| 3768 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3769 | * 0 if we can trash answser items. |
| 3770 | * 1 when safely ignored and we must kept answer items |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3771 | * |
| 3772 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3773 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3774 | int snr_resolution_error_cb(struct resolv_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3775 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3776 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3777 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3778 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3779 | if (!s) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3780 | return 0; |
| 3781 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3782 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3783 | if (!snr_update_srv_status(s, 1)) { |
Christopher Faulet | 5130c21 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 3784 | memset(&s->addr, 0, sizeof(s->addr)); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3785 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3786 | resolv_detach_from_resolution_answer_items(requester->resolution, requester); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3787 | return 0; |
| 3788 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3789 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3790 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3791 | return 1; |
| 3792 | } |
| 3793 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3794 | /* |
| 3795 | * 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] | 3796 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3797 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 3798 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3799 | * |
| 3800 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3801 | */ |
| 3802 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 3803 | { |
| 3804 | struct server *tmpsrv; |
| 3805 | struct proxy *be; |
| 3806 | |
| 3807 | if (!srv) |
| 3808 | return NULL; |
| 3809 | |
| 3810 | be = srv->proxy; |
| 3811 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3812 | /* we found the current server is the same, ignore it */ |
| 3813 | if (srv == tmpsrv) |
| 3814 | continue; |
| 3815 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3816 | /* We want to compare the IP in the record with the IP of the servers in the |
| 3817 | * same backend, only if: |
| 3818 | * * DNS resolution is enabled on the server |
| 3819 | * * the hostname used for the resolution by our server is the same than the |
| 3820 | * one used for the server found in the backend |
| 3821 | * * the server found in the backend is not our current server |
| 3822 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3823 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3824 | if ((tmpsrv->hostname_dn == NULL) || |
| 3825 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3826 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3827 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3828 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3829 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3830 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3831 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3832 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3833 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3834 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3835 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3836 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3837 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3838 | /* At this point, we have 2 different servers using the same DNS hostname |
| 3839 | * for their respective resolution. |
| 3840 | */ |
| 3841 | if (*ip_family == tmpsrv->addr.ss_family && |
| 3842 | ((tmpsrv->addr.ss_family == AF_INET && |
| 3843 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 3844 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 3845 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3846 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3847 | return tmpsrv; |
| 3848 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3849 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3850 | } |
| 3851 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3852 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3853 | return NULL; |
| 3854 | } |
| 3855 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3856 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 3857 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 3858 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 3859 | * not NULL, is filled up. |
| 3860 | */ |
| 3861 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 3862 | { |
| 3863 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3864 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 3865 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3866 | return 1; |
| 3867 | } |
| 3868 | return 0; |
| 3869 | } |
| 3870 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3871 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 3872 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3873 | * |
| 3874 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3875 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3876 | 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] | 3877 | { |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3878 | struct resolv_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3879 | char *hostname_dn; |
| 3880 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3881 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 3882 | /* Note that the server lock is already held. */ |
| 3883 | if (!srv->resolvers) |
| 3884 | return -1; |
| 3885 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3886 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3887 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3888 | /* run time DNS/SRV resolution was not active for this server |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3889 | * and we can't enable it at run time for now. |
| 3890 | */ |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3891 | if (!srv->resolv_requester && !srv->srvrq) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3892 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3893 | |
| 3894 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3895 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3896 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 3897 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3898 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3899 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3900 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3901 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3902 | resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3903 | if (resolution && |
| 3904 | resolution->hostname_dn && |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3905 | resolution->hostname_dn_len == hostname_dn_len && |
| 3906 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3907 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3908 | |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3909 | resolv_unlink_resolution(srv->resolv_requester); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3910 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3911 | free(srv->hostname); |
| 3912 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3913 | srv->hostname = strdup(hostname); |
| 3914 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3915 | srv->hostname_dn_len = hostname_dn_len; |
| 3916 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3917 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3918 | |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 3919 | if (srv->flags & SRV_F_NO_RESOLUTION) |
| 3920 | goto end; |
| 3921 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3922 | if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3923 | goto err; |
| 3924 | |
| 3925 | end: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3926 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3927 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3928 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3929 | |
| 3930 | err: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3931 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3932 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3933 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3934 | } |
| 3935 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3936 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 3937 | * from the state file. This is suited for initial address configuration. |
| 3938 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 3939 | * *err_code, if not NULL, is filled up. |
| 3940 | */ |
| 3941 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 3942 | { |
| 3943 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 3944 | if (err_code) |
| 3945 | *err_code |= ERR_WARN; |
| 3946 | return 1; |
| 3947 | } |
| 3948 | return 0; |
| 3949 | } |
| 3950 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3951 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 3952 | static int srv_iterate_initaddr(struct server *srv) |
| 3953 | { |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3954 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3955 | int return_code = 0; |
| 3956 | int err_code; |
| 3957 | unsigned int methods; |
| 3958 | |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3959 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 3960 | if (!name && srv->srvrq) |
| 3961 | name = srv->srvrq->name; |
| 3962 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3963 | methods = srv->init_addr_methods; |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3964 | if (!methods) { |
| 3965 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3966 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 3967 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 3968 | if (srv->resolvers_id) { |
| 3969 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 3970 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 3971 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3972 | } |
| 3973 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 3974 | /* "-dr" : always append "none" so that server addresses resolution |
| 3975 | * failures are silently ignored, this is convenient to validate some |
| 3976 | * configs out of their environment. |
| 3977 | */ |
| 3978 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 3979 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 3980 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3981 | while (methods) { |
| 3982 | err_code = 0; |
| 3983 | switch (srv_get_next_initaddr(&methods)) { |
| 3984 | case SRV_IADDR_LAST: |
| 3985 | if (!srv->lastaddr) |
| 3986 | continue; |
| 3987 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3988 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3989 | return_code |= err_code; |
| 3990 | break; |
| 3991 | |
| 3992 | case SRV_IADDR_LIBC: |
| 3993 | if (!srv->hostname) |
| 3994 | continue; |
| 3995 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3996 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 3997 | return_code |= err_code; |
| 3998 | break; |
| 3999 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4000 | case SRV_IADDR_NONE: |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 4001 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4002 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4003 | ha_warning("could not resolve address '%s', disabling server.\n", |
| 4004 | name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4005 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4006 | return return_code; |
| 4007 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4008 | case SRV_IADDR_IP: |
| 4009 | ipcpy(&srv->init_addr, &srv->addr); |
| 4010 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4011 | ha_warning("could not resolve address '%s', falling back to configured address.\n", |
| 4012 | name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4013 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4014 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4015 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4016 | default: /* unhandled method */ |
| 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4021 | if (!return_code) |
| 4022 | ha_alert("no method found to resolve address '%s'.\n", name); |
| 4023 | else |
| 4024 | ha_alert("could not resolve address '%s'.\n", name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4025 | |
| 4026 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4027 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4028 | out: |
| 4029 | srv_set_dyncookie(srv); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 4030 | srv_set_addr_desc(srv, 1); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4031 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4032 | } |
| 4033 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4034 | /* |
| 4035 | * This function parses all backends and all servers within each backend |
| 4036 | * and performs servers' addr resolution based on information provided by: |
| 4037 | * - configuration file |
| 4038 | * - server-state file (states provided by an 'old' haproxy process) |
| 4039 | * |
| 4040 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4041 | */ |
| 4042 | int srv_init_addr(void) |
| 4043 | { |
| 4044 | struct proxy *curproxy; |
| 4045 | int return_code = 0; |
| 4046 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4047 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4048 | while (curproxy) { |
| 4049 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4050 | |
| 4051 | /* servers are in backend only */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4052 | 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] | 4053 | goto srv_init_addr_next; |
| 4054 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4055 | for (srv = curproxy->srv; srv; srv = srv->next) { |
| 4056 | set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4057 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4058 | return_code |= srv_iterate_initaddr(srv); |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4059 | reset_usermsgs_ctx(); |
| 4060 | } |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4061 | |
| 4062 | srv_init_addr_next: |
| 4063 | curproxy = curproxy->next; |
| 4064 | } |
| 4065 | |
| 4066 | return return_code; |
| 4067 | } |
| 4068 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4069 | /* |
| 4070 | * Must be called with the server lock held. |
| 4071 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4072 | 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] | 4073 | { |
| 4074 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4075 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4076 | |
| 4077 | msg = get_trash_chunk(); |
| 4078 | chunk_reset(msg); |
| 4079 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4080 | if (server->hostname && strcmp(fqdn, server->hostname) == 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4081 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4082 | goto out; |
| 4083 | } |
| 4084 | |
| 4085 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4086 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4087 | goto out; |
| 4088 | } |
| 4089 | |
| 4090 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4091 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4092 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4093 | if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4094 | chunk_reset(msg); |
| 4095 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4096 | server->proxy->id, server->id); |
| 4097 | goto out; |
| 4098 | } |
| 4099 | |
| 4100 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4101 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4102 | |
| 4103 | out: |
| 4104 | if (updater) |
| 4105 | chunk_appendf(msg, " by '%s'", updater); |
| 4106 | chunk_appendf(msg, "\n"); |
| 4107 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4108 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4112 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4113 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4114 | * 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] | 4115 | * used for CLI commands requiring a server name. |
| 4116 | * Important: the <arg> is modified to remove the '/'. |
| 4117 | */ |
| 4118 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4119 | { |
| 4120 | struct proxy *px; |
| 4121 | struct server *sv; |
| 4122 | char *line; |
| 4123 | |
| 4124 | /* split "backend/server" and make <line> point to server */ |
| 4125 | for (line = arg; *line; line++) |
| 4126 | if (*line == '/') { |
| 4127 | *line++ = '\0'; |
| 4128 | break; |
| 4129 | } |
| 4130 | |
| 4131 | if (!*line || !*arg) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4132 | cli_err(appctx, "Require 'backend/server'.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4133 | return NULL; |
| 4134 | } |
| 4135 | |
| 4136 | if (!get_backend_server(arg, line, &px, &sv)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4137 | cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4138 | return NULL; |
| 4139 | } |
| 4140 | |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4141 | if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4142 | cli_err(appctx, "Proxy is disabled.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4143 | return NULL; |
| 4144 | } |
| 4145 | |
| 4146 | return sv; |
| 4147 | } |
| 4148 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4149 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4150 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4151 | 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] | 4152 | { |
| 4153 | struct server *sv; |
| 4154 | const char *warning; |
| 4155 | |
| 4156 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4157 | return 1; |
| 4158 | |
| 4159 | sv = cli_find_server(appctx, args[2]); |
| 4160 | if (!sv) |
| 4161 | return 1; |
| 4162 | |
| 4163 | if (strcmp(args[3], "weight") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4164 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4165 | warning = server_parse_weight_change_request(sv, args[4]); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4166 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4167 | if (warning) |
| 4168 | cli_err(appctx, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4169 | } |
| 4170 | else if (strcmp(args[3], "state") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4171 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4172 | if (strcmp(args[4], "ready") == 0) |
| 4173 | srv_adm_set_ready(sv); |
| 4174 | else if (strcmp(args[4], "drain") == 0) |
| 4175 | srv_adm_set_drain(sv); |
| 4176 | else if (strcmp(args[4], "maint") == 0) |
| 4177 | srv_adm_set_maint(sv); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4178 | else |
| 4179 | 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] | 4180 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4181 | } |
| 4182 | else if (strcmp(args[3], "health") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4183 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4184 | if (sv->track) |
| 4185 | cli_err(appctx, "cannot change health on a tracking server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4186 | else if (strcmp(args[4], "up") == 0) { |
| 4187 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4188 | srv_set_running(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4189 | } |
| 4190 | else if (strcmp(args[4], "stopping") == 0) { |
| 4191 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4192 | srv_set_stopping(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4193 | } |
| 4194 | else if (strcmp(args[4], "down") == 0) { |
| 4195 | sv->check.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4196 | srv_set_stopped(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4197 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4198 | else |
| 4199 | 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] | 4200 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4201 | } |
| 4202 | else if (strcmp(args[3], "agent") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4203 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4204 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4205 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4206 | else if (strcmp(args[4], "up") == 0) { |
| 4207 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4208 | srv_set_running(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4209 | } |
| 4210 | else if (strcmp(args[4], "down") == 0) { |
| 4211 | sv->agent.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4212 | srv_set_stopped(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4213 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4214 | else |
| 4215 | cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4216 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4217 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4218 | else if (strcmp(args[3], "agent-addr") == 0) { |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4219 | char *addr = NULL; |
| 4220 | char *port = NULL; |
| 4221 | if (strlen(args[4]) == 0) { |
| 4222 | cli_err(appctx, "set server <b>/<s> agent-addr requires" |
| 4223 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4224 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4225 | } |
| 4226 | addr = args[4]; |
| 4227 | if (strcmp(args[5], "port") == 0) |
| 4228 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4229 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4230 | warning = srv_update_agent_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4231 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4232 | if (warning) |
| 4233 | cli_msg(appctx, LOG_WARNING, warning); |
| 4234 | } |
| 4235 | else if (strcmp(args[3], "agent-port") == 0) { |
| 4236 | char *port = NULL; |
| 4237 | if (strlen(args[4]) == 0) { |
| 4238 | cli_err(appctx, "set server <b>/<s> agent-port requires" |
| 4239 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4240 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4241 | } |
| 4242 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4243 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4244 | warning = srv_update_agent_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4245 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4246 | if (warning) |
| 4247 | cli_msg(appctx, LOG_WARNING, warning); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4248 | } |
| 4249 | else if (strcmp(args[3], "agent-send") == 0) { |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4250 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4251 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4252 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
| 4253 | else { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4254 | if (!set_srv_agent_send(sv, args[4])) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4255 | cli_err(appctx, "cannot allocate memory for new string.\n"); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4256 | } |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4257 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4258 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4259 | else if (strcmp(args[3], "check-addr") == 0) { |
| 4260 | char *addr = NULL; |
| 4261 | char *port = NULL; |
| 4262 | if (strlen(args[4]) == 0) { |
| 4263 | cli_err(appctx, "set server <b>/<s> check-addr requires" |
| 4264 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4265 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4266 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4267 | addr = args[4]; |
| 4268 | if (strcmp(args[5], "port") == 0) |
| 4269 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4270 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4271 | warning = srv_update_check_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4272 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4273 | if (warning) |
| 4274 | cli_msg(appctx, LOG_WARNING, warning); |
| 4275 | } |
| 4276 | else if (strcmp(args[3], "check-port") == 0) { |
| 4277 | char *port = NULL; |
| 4278 | if (strlen(args[4]) == 0) { |
| 4279 | cli_err(appctx, "set server <b>/<s> check-port requires" |
| 4280 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4281 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4282 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4283 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4284 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4285 | warning = srv_update_check_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4286 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4287 | if (warning) |
| 4288 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4289 | } |
| 4290 | else if (strcmp(args[3], "addr") == 0) { |
| 4291 | char *addr = NULL; |
| 4292 | char *port = NULL; |
| 4293 | if (strlen(args[4]) == 0) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4294 | 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] | 4295 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4296 | } |
| 4297 | else { |
| 4298 | addr = args[4]; |
| 4299 | } |
| 4300 | if (strcmp(args[5], "port") == 0) { |
| 4301 | port = args[6]; |
| 4302 | } |
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_addr_port(sv, addr, port, "stats socket command"); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4305 | if (warning) |
| 4306 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4307 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4308 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4309 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4310 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4311 | if (!*args[4]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4312 | cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4313 | goto out; |
| 4314 | } |
| 4315 | if (!sv->resolvers) { |
| 4316 | cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n"); |
| 4317 | goto out; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4318 | } |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4319 | if (sv->srvrq) { |
| 4320 | 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] | 4321 | goto out; |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4322 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4323 | HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock); |
| 4324 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4325 | /* ensure runtime resolver will process this new fqdn */ |
| 4326 | if (sv->flags & SRV_F_NO_RESOLUTION) { |
| 4327 | sv->flags &= ~SRV_F_NO_RESOLUTION; |
| 4328 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4329 | warning = srv_update_fqdn(sv, args[4], "stats socket command", 1); |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4330 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4331 | HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4332 | if (warning) |
| 4333 | cli_msg(appctx, LOG_WARNING, warning); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4334 | } |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4335 | else if (strcmp(args[3], "ssl") == 0) { |
| 4336 | #ifdef USE_OPENSSL |
Amaury Denoyelle | b89d3d3 | 2021-05-19 15:00:54 +0200 | [diff] [blame] | 4337 | if (sv->flags & SRV_F_DYNAMIC) { |
| 4338 | cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n"); |
| 4339 | goto out; |
| 4340 | } |
| 4341 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4342 | if (sv->ssl_ctx.ctx == NULL) { |
| 4343 | cli_err(appctx, "'set server <srv> ssl' cannot be set. " |
| 4344 | " default-server should define ssl settings\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4345 | goto out; |
| 4346 | } |
| 4347 | |
| 4348 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4349 | if (strcmp(args[4], "on") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4350 | srv_set_ssl(sv, 1); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4351 | } else if (strcmp(args[4], "off") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4352 | srv_set_ssl(sv, 0); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4353 | } else { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4354 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4355 | cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4356 | goto out; |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4357 | } |
| 4358 | srv_cleanup_connections(sv); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4359 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4360 | cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n"); |
| 4361 | #else |
| 4362 | cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n"); |
| 4363 | #endif |
| 4364 | } else { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4365 | cli_err(appctx, |
William Dauchy | 3f4ec7d | 2021-02-15 17:22:16 +0100 | [diff] [blame] | 4366 | "usage: set server <backend>/<server> " |
| 4367 | "addr | agent | agent-addr | agent-port | agent-send | " |
| 4368 | "check-addr | check-port | fqdn | health | ssl | " |
| 4369 | "state | weight\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4370 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4371 | out: |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4372 | return 1; |
| 4373 | } |
| 4374 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4375 | 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] | 4376 | { |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4377 | struct proxy *px; |
| 4378 | struct server *sv; |
| 4379 | char *line; |
| 4380 | |
| 4381 | |
| 4382 | /* split "backend/server" and make <line> point to server */ |
| 4383 | for (line = args[2]; *line; line++) |
| 4384 | if (*line == '/') { |
| 4385 | *line++ = '\0'; |
| 4386 | break; |
| 4387 | } |
| 4388 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4389 | if (!*line) |
| 4390 | return cli_err(appctx, "Require 'backend/server'.\n"); |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4391 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4392 | if (!get_backend_server(args[2], line, &px, &sv)) |
| 4393 | 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] | 4394 | |
| 4395 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4396 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4397 | sv->iweight); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 4398 | if (applet_putstr(appctx, trash.area) == -1) |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4399 | return 0; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4400 | return 1; |
| 4401 | } |
| 4402 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4403 | /* Parse a "set weight" command. |
| 4404 | * |
| 4405 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4406 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4407 | 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] | 4408 | { |
| 4409 | struct server *sv; |
| 4410 | const char *warning; |
| 4411 | |
| 4412 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4413 | return 1; |
| 4414 | |
| 4415 | sv = cli_find_server(appctx, args[2]); |
| 4416 | if (!sv) |
| 4417 | return 1; |
| 4418 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4419 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4420 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4421 | warning = server_parse_weight_change_request(sv, args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4422 | if (warning) |
| 4423 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4424 | |
| 4425 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4426 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4427 | return 1; |
| 4428 | } |
| 4429 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4430 | /* parse a "set maxconn server" command. It always returns 1. |
| 4431 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4432 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4433 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4434 | 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] | 4435 | { |
| 4436 | struct server *sv; |
| 4437 | const char *warning; |
| 4438 | |
| 4439 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4440 | return 1; |
| 4441 | |
| 4442 | sv = cli_find_server(appctx, args[3]); |
| 4443 | if (!sv) |
| 4444 | return 1; |
| 4445 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4446 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4447 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4448 | warning = server_parse_maxconn_change_request(sv, args[4]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4449 | if (warning) |
| 4450 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4451 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4452 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4453 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4454 | return 1; |
| 4455 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4456 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4457 | /* parse a "disable agent" command. It always returns 1. |
| 4458 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4459 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4460 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4461 | 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] | 4462 | { |
| 4463 | struct server *sv; |
| 4464 | |
| 4465 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4466 | return 1; |
| 4467 | |
| 4468 | sv = cli_find_server(appctx, args[2]); |
| 4469 | if (!sv) |
| 4470 | return 1; |
| 4471 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4472 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4473 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4474 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4475 | return 1; |
| 4476 | } |
| 4477 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4478 | /* parse a "disable health" command. It always returns 1. |
| 4479 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4480 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4481 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4482 | 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] | 4483 | { |
| 4484 | struct server *sv; |
| 4485 | |
| 4486 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4487 | return 1; |
| 4488 | |
| 4489 | sv = cli_find_server(appctx, args[2]); |
| 4490 | if (!sv) |
| 4491 | return 1; |
| 4492 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4493 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4494 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4495 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4496 | return 1; |
| 4497 | } |
| 4498 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4499 | /* parse a "disable server" command. It always returns 1. |
| 4500 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4501 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4502 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4503 | 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] | 4504 | { |
| 4505 | struct server *sv; |
| 4506 | |
| 4507 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4508 | return 1; |
| 4509 | |
| 4510 | sv = cli_find_server(appctx, args[2]); |
| 4511 | if (!sv) |
| 4512 | return 1; |
| 4513 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4514 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4515 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4516 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4517 | return 1; |
| 4518 | } |
| 4519 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4520 | /* parse a "enable agent" command. It always returns 1. |
| 4521 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4522 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4523 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4524 | 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] | 4525 | { |
| 4526 | struct server *sv; |
| 4527 | |
| 4528 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4529 | return 1; |
| 4530 | |
| 4531 | sv = cli_find_server(appctx, args[2]); |
| 4532 | if (!sv) |
| 4533 | return 1; |
| 4534 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4535 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) |
| 4536 | 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] | 4537 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4538 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4539 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4540 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4541 | return 1; |
| 4542 | } |
| 4543 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4544 | /* parse a "enable health" command. It always returns 1. |
| 4545 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4546 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4547 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4548 | 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] | 4549 | { |
| 4550 | struct server *sv; |
| 4551 | |
| 4552 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4553 | return 1; |
| 4554 | |
| 4555 | sv = cli_find_server(appctx, args[2]); |
| 4556 | if (!sv) |
| 4557 | return 1; |
| 4558 | |
Amaury Denoyelle | 0f456d5 | 2021-09-21 10:29:09 +0200 | [diff] [blame] | 4559 | if (!(sv->check.state & CHK_ST_CONFIGURED)) |
| 4560 | return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n"); |
| 4561 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4562 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4563 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4564 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4565 | return 1; |
| 4566 | } |
| 4567 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4568 | /* parse a "enable server" command. It always returns 1. |
| 4569 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4570 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4571 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4572 | 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] | 4573 | { |
| 4574 | struct server *sv; |
| 4575 | |
| 4576 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4577 | return 1; |
| 4578 | |
| 4579 | sv = cli_find_server(appctx, args[2]); |
| 4580 | if (!sv) |
| 4581 | return 1; |
| 4582 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4583 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4584 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4585 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4586 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4587 | sv->cookie) |
| 4588 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4589 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4590 | return 1; |
| 4591 | } |
| 4592 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4593 | /* Allocates data structure related to load balancing for the server <sv>. It |
| 4594 | * is only required for dynamic servers. |
| 4595 | * |
| 4596 | * At the moment, the server lock is not used as this function is only called |
| 4597 | * for a dynamic server not yet registered. |
| 4598 | * |
| 4599 | * Returns 1 on success, 0 on allocation failure. |
| 4600 | */ |
| 4601 | static int srv_alloc_lb(struct server *sv, struct proxy *be) |
| 4602 | { |
| 4603 | int node; |
| 4604 | |
| 4605 | sv->lb_tree = (sv->flags & SRV_F_BACKUP) ? |
| 4606 | &be->lbprm.chash.bck : &be->lbprm.chash.act; |
| 4607 | sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE; |
| 4608 | sv->lb_nodes_now = 0; |
| 4609 | |
Willy Tarreau | dcb121f | 2021-04-20 11:37:45 +0200 | [diff] [blame] | 4610 | if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) || |
| 4611 | ((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] | 4612 | sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes)); |
| 4613 | |
| 4614 | if (!sv->lb_nodes) |
| 4615 | return 0; |
| 4616 | |
| 4617 | for (node = 0; node < sv->lb_nodes_tot; node++) { |
| 4618 | sv->lb_nodes[node].server = sv; |
| 4619 | sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node); |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | return 1; |
| 4624 | } |
| 4625 | |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4626 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 4627 | * reached, the task automatically stops. Note that any server status change |
| 4628 | * must have updated s->last_change accordingly. |
| 4629 | */ |
| 4630 | static struct task *server_warmup(struct task *t, void *context, unsigned int state) |
| 4631 | { |
| 4632 | struct server *s = context; |
| 4633 | |
| 4634 | /* by default, plan on stopping the task */ |
| 4635 | t->expire = TICK_ETERNITY; |
| 4636 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 4637 | (s->next_state != SRV_ST_STARTING)) |
| 4638 | return t; |
| 4639 | |
| 4640 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4641 | |
| 4642 | /* recalculate the weights and update the state */ |
| 4643 | server_recalc_eweight(s, 1); |
| 4644 | |
| 4645 | /* probably that we can refill this server with a bit more connections */ |
| 4646 | pendconn_grab_from_px(s); |
| 4647 | |
| 4648 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4649 | |
| 4650 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 4651 | * whichever is greater, resulting in small 5% steps. |
| 4652 | */ |
| 4653 | if (s->next_state == SRV_ST_STARTING) |
| 4654 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 4655 | return t; |
| 4656 | } |
| 4657 | |
| 4658 | /* Allocate the slowstart task if the server is configured with a slowstart |
| 4659 | * timer. If server next_state is SRV_ST_STARTING, the task is scheduled. |
| 4660 | * |
| 4661 | * Returns 0 on success else non-zero. |
| 4662 | */ |
| 4663 | static int init_srv_slowstart(struct server *srv) |
| 4664 | { |
| 4665 | struct task *t; |
| 4666 | |
| 4667 | if (srv->slowstart) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4668 | if ((t = task_new_anywhere()) == NULL) { |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4669 | ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id); |
| 4670 | return ERR_ALERT | ERR_FATAL; |
| 4671 | } |
| 4672 | |
| 4673 | /* We need a warmup task that will be called when the server |
| 4674 | * state switches from down to up. |
| 4675 | */ |
| 4676 | srv->warmup = t; |
| 4677 | t->process = server_warmup; |
| 4678 | t->context = srv; |
| 4679 | |
| 4680 | /* server can be in this state only because of */ |
| 4681 | if (srv->next_state == SRV_ST_STARTING) { |
| 4682 | task_schedule(srv->warmup, |
| 4683 | tick_add(now_ms, |
| 4684 | MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20))); |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | return ERR_NONE; |
| 4689 | } |
| 4690 | REGISTER_POST_SERVER_CHECK(init_srv_slowstart); |
| 4691 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4692 | /* Memory allocation and initialization of the per_thr field. |
| 4693 | * Returns 0 if the field has been successfully initialized, -1 on failure. |
| 4694 | */ |
| 4695 | int srv_init_per_thr(struct server *srv) |
| 4696 | { |
| 4697 | int i; |
| 4698 | |
| 4699 | srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr)); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 4700 | srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp)); |
| 4701 | if (!srv->per_thr || !srv->per_tgrp) |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4702 | return -1; |
| 4703 | |
| 4704 | for (i = 0; i < global.nbthread; i++) { |
| 4705 | srv->per_thr[i].idle_conns = EB_ROOT; |
| 4706 | srv->per_thr[i].safe_conns = EB_ROOT; |
| 4707 | srv->per_thr[i].avail_conns = EB_ROOT; |
| 4708 | MT_LIST_INIT(&srv->per_thr[i].streams); |
| 4709 | } |
| 4710 | |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4714 | /* Parse a "add server" command |
| 4715 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4716 | */ |
| 4717 | static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4718 | { |
| 4719 | struct proxy *be; |
| 4720 | struct server *srv; |
| 4721 | char *be_name, *sv_name; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4722 | int errcode, argc; |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4723 | int next_id; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4724 | const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR; |
| 4725 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4726 | usermsgs_clr("CLI"); |
| 4727 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4728 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4729 | return 1; |
| 4730 | |
| 4731 | ++args; |
| 4732 | |
| 4733 | sv_name = be_name = args[1]; |
| 4734 | /* split backend/server arg */ |
| 4735 | while (*sv_name && *(++sv_name)) { |
| 4736 | if (*sv_name == '/') { |
| 4737 | *sv_name = '\0'; |
| 4738 | ++sv_name; |
| 4739 | break; |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | if (!*sv_name) |
| 4744 | return cli_err(appctx, "Require 'backend/server'."); |
| 4745 | |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4746 | be = proxy_be_by_name(be_name); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4747 | if (!be) |
| 4748 | return cli_err(appctx, "No such backend."); |
| 4749 | |
| 4750 | if (!(be->lbprm.algo & BE_LB_PROP_DYN)) { |
Amaury Denoyelle | eafd701 | 2021-04-29 14:59:42 +0200 | [diff] [blame] | 4751 | 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] | 4752 | return 1; |
| 4753 | } |
| 4754 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4755 | /* At this point, some operations might not be thread-safe anymore. This |
| 4756 | * might be the case for parsing handlers which were designed to run |
| 4757 | * only at the starting stage on single-thread mode. |
| 4758 | * |
| 4759 | * Activate thread isolation to ensure thread-safety. |
| 4760 | */ |
| 4761 | thread_isolate(); |
| 4762 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4763 | args[1] = sv_name; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4764 | errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags); |
| 4765 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4766 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4767 | |
| 4768 | while (*args[argc]) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4769 | errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4770 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4771 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4772 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4773 | } |
| 4774 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4775 | errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags); |
| 4776 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4777 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4778 | |
Amaury Denoyelle | efbf35c | 2021-06-10 17:34:10 +0200 | [diff] [blame] | 4779 | /* A dynamic server does not currently support resolution. |
| 4780 | * |
| 4781 | * Initialize it explicitly to the "none" method to ensure no |
| 4782 | * resolution will ever be executed. |
| 4783 | */ |
| 4784 | srv->init_addr_methods = SRV_IADDR_NONE; |
| 4785 | |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4786 | if (srv->mux_proto) { |
| 4787 | 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] | 4788 | ha_alert("MUX protocol is not usable for server.\n"); |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4789 | goto out; |
| 4790 | } |
| 4791 | } |
| 4792 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4793 | if (srv_init_per_thr(srv) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4794 | ha_alert("failed to allocate per-thread lists for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4795 | goto out; |
| 4796 | } |
| 4797 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4798 | if (srv->max_idle_conns != 0) { |
| 4799 | srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr)); |
| 4800 | if (!srv->curr_idle_thr) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4801 | ha_alert("failed to allocate counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4802 | goto out; |
| 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | if (!srv_alloc_lb(srv, be)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4807 | ha_alert("Failed to initialize load-balancing data.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4808 | goto out; |
| 4809 | } |
| 4810 | |
| 4811 | if (!stats_allocate_proxy_counters_internal(&srv->extra_counters, |
| 4812 | COUNTERS_SV, |
| 4813 | STATS_PX_CAP_SRV)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4814 | ha_alert("failed to allocate extra counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4815 | goto out; |
| 4816 | } |
| 4817 | |
Aurelien DARRAGON | 86207e7 | 2023-02-08 11:55:08 +0100 | [diff] [blame] | 4818 | /* ensure minconn/maxconn consistency */ |
| 4819 | srv_minmax_conn_apply(srv); |
| 4820 | |
Amaury Denoyelle | 79b90e8 | 2021-09-20 15:15:19 +0200 | [diff] [blame] | 4821 | if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) || |
| 4822 | srv->check.use_ssl == 1) { |
Amaury Denoyelle | 34897d2 | 2021-05-19 09:49:41 +0200 | [diff] [blame] | 4823 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 4824 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) |
| 4825 | goto out; |
| 4826 | } |
| 4827 | } |
| 4828 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4829 | if (srv->trackit) { |
| 4830 | if (srv_apply_track(srv, be)) |
| 4831 | goto out; |
| 4832 | } |
| 4833 | |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4834 | /* Init check/agent if configured. The check is manually disabled |
| 4835 | * because a dynamic server is started in a disable state. It must be |
| 4836 | * manually activated via a "enable health/agent" command. |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4837 | */ |
| 4838 | if (srv->do_check) { |
| 4839 | if (init_srv_check(srv)) |
| 4840 | goto out; |
| 4841 | |
| 4842 | srv->check.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4843 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4844 | |
| 4845 | if (srv->do_agent) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4846 | if (init_srv_agent_check(srv)) |
| 4847 | goto out; |
| 4848 | |
| 4849 | srv->agent.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4850 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4851 | |
Amaury Denoyelle | cd8a6f2 | 2021-09-21 11:51:54 +0200 | [diff] [blame] | 4852 | /* Init slowstart if needed. */ |
| 4853 | if (init_srv_slowstart(srv)) |
| 4854 | goto out; |
| 4855 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4856 | /* Attach the server to the end of the proxy linked list. Note that this |
| 4857 | * operation is not thread-safe so this is executed under thread |
| 4858 | * isolation. |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4859 | * |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4860 | * 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] | 4861 | */ |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4862 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4863 | /* TODO use a double-linked list for px->srv */ |
| 4864 | if (be->srv) { |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4865 | struct server *next = be->srv; |
| 4866 | |
| 4867 | while (1) { |
| 4868 | /* check for duplicate server */ |
Tim Duesterhus | c5aa113 | 2021-10-16 17:48:15 +0200 | [diff] [blame] | 4869 | if (strcmp(srv->id, next->id) == 0) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4870 | 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] | 4871 | goto out; |
| 4872 | } |
| 4873 | |
| 4874 | if (!next->next) |
| 4875 | break; |
| 4876 | |
| 4877 | next = next->next; |
| 4878 | } |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4879 | |
| 4880 | next->next = srv; |
| 4881 | } |
| 4882 | else { |
| 4883 | srv->next = be->srv; |
| 4884 | be->srv = srv; |
| 4885 | } |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4886 | |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4887 | /* generate the server id if not manually specified */ |
| 4888 | if (!srv->puid) { |
| 4889 | next_id = get_next_id(&be->conf.used_server_id, 1); |
| 4890 | if (!next_id) { |
| 4891 | ha_alert("Cannot attach server : no id left in proxy\n"); |
| 4892 | goto out; |
| 4893 | } |
| 4894 | |
| 4895 | srv->conf.id.key = srv->puid = next_id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4896 | } |
Christopher Faulet | 70f8948 | 2021-12-07 18:49:44 +0100 | [diff] [blame] | 4897 | srv->conf.name.key = srv->id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4898 | |
| 4899 | /* insert the server in the backend trees */ |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 4900 | eb32_insert(&be->conf.used_server_id, &srv->conf.id); |
| 4901 | ebis_insert(&be->conf.used_server_name, &srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 4902 | /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */ |
| 4903 | if (srv->addr_node.key) |
| 4904 | ebis_insert(&be->used_server_addr, &srv->addr_node); |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4905 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 4906 | /* check if LSB bit (odd bit) is set for reuse_cnt */ |
| 4907 | if (srv_id_reuse_cnt & 1) { |
| 4908 | /* cnt must be increased */ |
| 4909 | srv_id_reuse_cnt++; |
| 4910 | } |
| 4911 | /* srv_id_reuse_cnt is always even at this stage, divide by 2 to |
| 4912 | * save some space |
| 4913 | * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt)) |
| 4914 | */ |
| 4915 | srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0; |
| 4916 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 4917 | /* adding server cannot fail when we reach this: |
| 4918 | * publishing EVENT_HDL_SUB_SERVER_ADD |
| 4919 | */ |
| 4920 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1); |
| 4921 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4922 | thread_release(); |
| 4923 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4924 | /* Start the check task. The server must be fully initialized. |
| 4925 | * |
| 4926 | * <srvpos> and <nbcheck> parameters are set to 1 as there should be no |
| 4927 | * need to randomly spread the task interval for dynamic servers. |
| 4928 | */ |
| 4929 | if (srv->check.state & CHK_ST_CONFIGURED) { |
| 4930 | if (!start_check_task(&srv->check, 0, 1, 1)) |
| 4931 | ha_alert("System might be unstable, consider to execute a reload"); |
| 4932 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4933 | if (srv->agent.state & CHK_ST_CONFIGURED) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4934 | if (!start_check_task(&srv->agent, 0, 1, 1)) |
| 4935 | ha_alert("System might be unstable, consider to execute a reload"); |
| 4936 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4937 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4938 | ha_notice("New server registered.\n"); |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 4939 | cli_umsg(appctx, LOG_INFO); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4940 | |
| 4941 | return 0; |
| 4942 | |
| 4943 | out: |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 4944 | if (srv) { |
| 4945 | if (srv->track) |
| 4946 | release_server_track(srv); |
| 4947 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4948 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 4949 | free_check(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4950 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4951 | free_check(&srv->agent); |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4952 | |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 4953 | /* remove the server from the proxy linked list */ |
| 4954 | if (be->srv == srv) { |
| 4955 | be->srv = srv->next; |
| 4956 | } |
| 4957 | else { |
| 4958 | struct server *prev; |
| 4959 | for (prev = be->srv; prev && prev->next != srv; prev = prev->next) |
| 4960 | ; |
| 4961 | if (prev) |
| 4962 | prev->next = srv->next; |
| 4963 | } |
| 4964 | |
| 4965 | } |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 4966 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4967 | thread_release(); |
| 4968 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4969 | if (!usermsgs_empty()) |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 4970 | cli_umsgerr(appctx); |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4971 | |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 4972 | if (srv) |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 4973 | srv_drop(srv); |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4974 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4975 | return 1; |
| 4976 | } |
| 4977 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 4978 | /* Parse a "del server" command |
| 4979 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4980 | */ |
| 4981 | static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4982 | { |
| 4983 | struct proxy *be; |
| 4984 | struct server *srv; |
| 4985 | char *be_name, *sv_name; |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 4986 | struct server *prev_del; |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 4987 | |
| 4988 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4989 | return 1; |
| 4990 | |
| 4991 | ++args; |
| 4992 | |
| 4993 | sv_name = be_name = args[1]; |
| 4994 | /* split backend/server arg */ |
| 4995 | while (*sv_name && *(++sv_name)) { |
| 4996 | if (*sv_name == '/') { |
| 4997 | *sv_name = '\0'; |
| 4998 | ++sv_name; |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
| 5002 | |
| 5003 | if (!*sv_name) |
| 5004 | return cli_err(appctx, "Require 'backend/server'."); |
| 5005 | |
| 5006 | /* 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] | 5007 | * requires thread isolation. In addition, any place referencing the |
| 5008 | * server about to be deleted would be unsafe after our operation, so |
| 5009 | * we must be certain to be alone so that no other thread has even |
| 5010 | * started to grab a temporary reference to this server. |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5011 | */ |
Willy Tarreau | ba3ab79 | 2021-08-04 14:42:37 +0200 | [diff] [blame] | 5012 | thread_isolate_full(); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5013 | |
| 5014 | get_backend_server(be_name, sv_name, &be, &srv); |
| 5015 | if (!be) { |
| 5016 | cli_err(appctx, "No such backend."); |
| 5017 | goto out; |
| 5018 | } |
| 5019 | |
| 5020 | if (!srv) { |
| 5021 | cli_err(appctx, "No such server."); |
| 5022 | goto out; |
| 5023 | } |
| 5024 | |
Amaury Denoyelle | 14c3c5c | 2021-08-23 14:10:51 +0200 | [diff] [blame] | 5025 | if (srv->flags & SRV_F_NON_PURGEABLE) { |
| 5026 | 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] | 5027 | goto out; |
| 5028 | } |
| 5029 | |
| 5030 | /* Only servers in maintenance can be deleted. This ensures that the |
| 5031 | * server is not present anymore in the lb structures (through |
| 5032 | * lbprm.set_server_status_down). |
| 5033 | */ |
| 5034 | if (!(srv->cur_admin & SRV_ADMF_MAINT)) { |
| 5035 | cli_err(appctx, "Only servers in maintenance mode can be deleted."); |
| 5036 | goto out; |
| 5037 | } |
| 5038 | |
| 5039 | /* Ensure that there is no active/idle/pending connection on the server. |
| 5040 | * |
| 5041 | * TODO idle connections should not prevent server deletion. A proper |
| 5042 | * cleanup function should be implemented to be used here. |
| 5043 | */ |
| 5044 | if (srv->cur_sess || srv->curr_idle_conns || |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 5045 | !eb_is_empty(&srv->queue.head)) { |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5046 | cli_err(appctx, "Server still has connections attached to it, cannot remove it."); |
| 5047 | goto out; |
| 5048 | } |
| 5049 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 5050 | /* removing cannot fail anymore when we reach this: |
| 5051 | * publishing EVENT_HDL_SUB_SERVER_DEL |
| 5052 | */ |
| 5053 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1); |
| 5054 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5055 | /* remove srv from tracking list */ |
| 5056 | if (srv->track) |
| 5057 | release_server_track(srv); |
| 5058 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5059 | /* stop the check task if running */ |
| 5060 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5061 | check_purge(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5062 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5063 | check_purge(&srv->agent); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5064 | |
| 5065 | /* detach the server from the proxy linked list |
| 5066 | * The proxy servers list is currently not protected by a lock, so this |
| 5067 | * requires thread_isolate/release. |
| 5068 | */ |
| 5069 | |
| 5070 | /* be->srv cannot be empty since we have already found the server with |
| 5071 | * get_backend_server */ |
| 5072 | BUG_ON(!be->srv); |
| 5073 | if (be->srv == srv) { |
| 5074 | be->srv = srv->next; |
| 5075 | } |
| 5076 | else { |
| 5077 | struct server *next; |
Amaury Denoyelle | d6b4b6d | 2021-04-21 11:50:26 +0200 | [diff] [blame] | 5078 | for (next = be->srv; srv != next->next; next = next->next) { |
| 5079 | /* srv cannot be not found since we have already found |
| 5080 | * it with get_backend_server */ |
| 5081 | BUG_ON(!next); |
| 5082 | } |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5083 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5084 | next->next = srv->next; |
| 5085 | } |
| 5086 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5087 | /* Some deleted servers could still point to us using their 'next', |
| 5088 | * update them as needed |
| 5089 | * Please note the small race between the POP and APPEND, although in |
| 5090 | * this situation this is not an issue as we are under full thread |
| 5091 | * isolation |
| 5092 | */ |
| 5093 | while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) { |
| 5094 | /* update its 'next' ptr */ |
| 5095 | prev_del->next = srv->next; |
| 5096 | if (srv->next) { |
| 5097 | /* now it is our 'next' responsibility */ |
| 5098 | MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted); |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | /* we ourselves need to inform our 'next' that we will still point it */ |
| 5103 | if (srv->next) |
| 5104 | MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted); |
| 5105 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5106 | /* remove srv from addr_node tree */ |
Amaury Denoyelle | 82d7f77 | 2021-06-09 16:00:43 +0200 | [diff] [blame] | 5107 | eb32_delete(&srv->conf.id); |
| 5108 | ebpt_delete(&srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5109 | if (srv->addr_node.key) |
| 5110 | ebpt_delete(&srv->addr_node); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5111 | |
| 5112 | /* remove srv from idle_node tree for idle conn cleanup */ |
| 5113 | eb32_delete(&srv->idle_node); |
| 5114 | |
Aurelien DARRAGON | 75b9d1c | 2023-01-24 14:40:01 +0100 | [diff] [blame] | 5115 | /* flag the server as deleted |
| 5116 | * (despite the server being removed from primary server list, |
| 5117 | * one could still access the server data from a valid ptr) |
| 5118 | * Deleted flag helps detecting when a server is in transient removal |
| 5119 | * state. |
| 5120 | * ie: removed from the list but not yet freed/purged from memory. |
| 5121 | */ |
| 5122 | srv->flags |= SRV_F_DELETED; |
| 5123 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5124 | /* set LSB bit (odd bit) for reuse_cnt */ |
| 5125 | srv_id_reuse_cnt |= 1; |
| 5126 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5127 | thread_release(); |
| 5128 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5129 | ha_notice("Server deleted.\n"); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5130 | srv_drop(srv); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5131 | |
| 5132 | cli_msg(appctx, LOG_INFO, "Server deleted."); |
| 5133 | |
| 5134 | return 0; |
| 5135 | |
| 5136 | out: |
| 5137 | thread_release(); |
| 5138 | |
| 5139 | return 1; |
| 5140 | } |
| 5141 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 5142 | /* register cli keywords */ |
| 5143 | static struct cli_kw_list cli_kws = {{ },{ |
Amaury Denoyelle | c755efd | 2021-08-03 18:05:37 +0200 | [diff] [blame] | 5144 | { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL }, |
| 5145 | { { "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] | 5146 | { { "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] | 5147 | { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL }, |
| 5148 | { { "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] | 5149 | { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL }, |
| 5150 | { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL }, |
| 5151 | { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server }, |
| 5152 | { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight }, |
| 5153 | { { "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] | 5154 | { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL }, |
| 5155 | { { "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] | 5156 | {{},} |
| 5157 | }}; |
| 5158 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5159 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 5160 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5161 | /* Prepare a server <srv> to track check status of another one. <srv>.<trackit> |
| 5162 | * field is used to retrieve the identifier of the tracked server, either with |
| 5163 | * the format "proxy/server" or just "server". <curproxy> must point to the |
| 5164 | * backend owning <srv>; if no proxy is specified in <trackit>, it will be used |
| 5165 | * to find the tracked server. |
| 5166 | * |
| 5167 | * Returns 0 if the server track has been activated else non-zero. |
| 5168 | * |
| 5169 | * Not thread-safe. |
| 5170 | */ |
| 5171 | int srv_apply_track(struct server *srv, struct proxy *curproxy) |
| 5172 | { |
| 5173 | struct proxy *px; |
| 5174 | struct server *strack, *loop; |
| 5175 | char *pname, *sname; |
| 5176 | |
| 5177 | if (!srv->trackit) |
| 5178 | return 1; |
| 5179 | |
| 5180 | pname = srv->trackit; |
| 5181 | sname = strrchr(pname, '/'); |
| 5182 | |
| 5183 | if (sname) { |
| 5184 | *sname++ = '\0'; |
| 5185 | } |
| 5186 | else { |
| 5187 | sname = pname; |
| 5188 | pname = NULL; |
| 5189 | } |
| 5190 | |
| 5191 | if (pname) { |
| 5192 | px = proxy_be_by_name(pname); |
| 5193 | if (!px) { |
| 5194 | ha_alert("unable to find required proxy '%s' for tracking.\n", |
| 5195 | pname); |
| 5196 | return 1; |
| 5197 | } |
| 5198 | } |
| 5199 | else { |
| 5200 | px = curproxy; |
| 5201 | } |
| 5202 | |
| 5203 | strack = findserver(px, sname); |
| 5204 | if (!strack) { |
| 5205 | ha_alert("unable to find required server '%s' for tracking.\n", |
| 5206 | sname); |
| 5207 | return 1; |
| 5208 | } |
| 5209 | |
Amaury Denoyelle | 79f68be | 2021-07-13 10:35:50 +0200 | [diff] [blame] | 5210 | if (strack->flags & SRV_F_DYNAMIC) { |
| 5211 | ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n", |
| 5212 | px->id, strack->id); |
| 5213 | return 1; |
| 5214 | } |
| 5215 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5216 | if (!strack->do_check && !strack->do_agent && !strack->track && |
| 5217 | !strack->trackit) { |
| 5218 | ha_alert("unable to use %s/%s for " |
| 5219 | "tracking as it does not have any check nor agent enabled.\n", |
| 5220 | px->id, strack->id); |
| 5221 | return 1; |
| 5222 | } |
| 5223 | |
| 5224 | for (loop = strack->track; loop && loop != srv; loop = loop->track) |
| 5225 | ; |
| 5226 | |
| 5227 | if (srv == strack || loop) { |
| 5228 | ha_alert("unable to track %s/%s as it " |
| 5229 | "belongs to a tracking chain looping back to %s/%s.\n", |
| 5230 | px->id, strack->id, px->id, |
| 5231 | srv == strack ? strack->id : loop->id); |
| 5232 | return 1; |
| 5233 | } |
| 5234 | |
| 5235 | if (curproxy != px && |
| 5236 | (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) { |
| 5237 | ha_alert("unable to use %s/%s for" |
| 5238 | "tracking: disable-on-404 option inconsistency.\n", |
| 5239 | px->id, strack->id); |
| 5240 | return 1; |
| 5241 | } |
| 5242 | |
| 5243 | srv->track = strack; |
| 5244 | srv->tracknext = strack->trackers; |
| 5245 | strack->trackers = srv; |
Amaury Denoyelle | 0626961 | 2021-08-23 14:05:07 +0200 | [diff] [blame] | 5246 | strack->flags |= SRV_F_NON_PURGEABLE; |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5247 | |
| 5248 | ha_free(&srv->trackit); |
| 5249 | |
| 5250 | return 0; |
| 5251 | } |
| 5252 | |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5253 | /* This function propagates srv state change to lb algorithms */ |
| 5254 | static void srv_lb_propagate(struct server *s) |
| 5255 | { |
| 5256 | struct proxy *px = s->proxy; |
| 5257 | |
| 5258 | if (px->lbprm.update_server_eweight) |
| 5259 | px->lbprm.update_server_eweight(s); |
| 5260 | else if (srv_willbe_usable(s)) { |
| 5261 | if (px->lbprm.set_server_status_up) |
| 5262 | px->lbprm.set_server_status_up(s); |
| 5263 | } |
| 5264 | else { |
| 5265 | if (px->lbprm.set_server_status_down) |
| 5266 | px->lbprm.set_server_status_down(s); |
| 5267 | } |
| 5268 | } |
| 5269 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5270 | /* |
Aurelien DARRAGON | 0ddf052 | 2023-03-27 11:57:28 +0200 | [diff] [blame] | 5271 | * This function applies server's status changes. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5272 | * |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5273 | * Must be called with the server lock held. This may also be called at init |
| 5274 | * time as the result of parsing the state file, in which case no lock will be |
| 5275 | * held, and the server's warmup task can be null. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5276 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5277 | static void srv_update_status(struct server *s) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5278 | { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5279 | int xferred; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5280 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 5281 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5282 | enum srv_state srv_prev_state = s->cur_state; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5283 | int log_level; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5284 | struct buffer *tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5285 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5286 | /* If currently main is not set we try to apply pending state changes */ |
| 5287 | if (!(s->cur_admin & SRV_ADMF_MAINT)) { |
| 5288 | int next_admin; |
| 5289 | |
| 5290 | /* Backup next admin */ |
| 5291 | next_admin = s->next_admin; |
| 5292 | |
| 5293 | /* restore current admin state */ |
| 5294 | s->next_admin = s->cur_admin; |
| 5295 | |
| 5296 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5297 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5298 | |
| 5299 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5300 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5301 | |
| 5302 | /* we might have streams queued on this server and waiting for |
| 5303 | * a connection. Those which are redispatchable will be queued |
| 5304 | * to another server or to the proxy itself. |
| 5305 | */ |
| 5306 | xferred = pendconn_redistribute(s); |
| 5307 | |
Aurelien DARRAGON | a63f490 | 2023-03-23 14:39:51 +0100 | [diff] [blame] | 5308 | /* no maintenance + server DOWN: publish event SERVER DOWN */ |
| 5309 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0); |
| 5310 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5311 | tmptrash = alloc_trash_chunk(); |
| 5312 | if (tmptrash) { |
| 5313 | chunk_printf(tmptrash, |
| 5314 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5315 | s->proxy->id, s->id); |
| 5316 | |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 5317 | srv_append_op_chg_cause(tmptrash, s); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5318 | srv_append_more(tmptrash, s, xferred, 0); |
| 5319 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5320 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5321 | |
| 5322 | /* we don't send an alert if the server was previously paused */ |
| 5323 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5324 | send_log(s->proxy, log_level, "%s.\n", |
| 5325 | tmptrash->area); |
| 5326 | send_email_alert(s, log_level, "%s", |
| 5327 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5328 | free_trash_chunk(tmptrash); |
| 5329 | tmptrash = NULL; |
| 5330 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5331 | } |
| 5332 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5333 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* we might have streams queued on this server and waiting for |
| 5336 | * a connection. Those which are redispatchable will be queued |
| 5337 | * to another server or to the proxy itself. |
| 5338 | */ |
| 5339 | xferred = pendconn_redistribute(s); |
| 5340 | |
| 5341 | tmptrash = alloc_trash_chunk(); |
| 5342 | if (tmptrash) { |
| 5343 | chunk_printf(tmptrash, |
| 5344 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5345 | s->proxy->id, s->id); |
| 5346 | |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 5347 | srv_append_op_chg_cause(tmptrash, s); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5348 | srv_append_more(tmptrash, s, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5349 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5350 | ha_warning("%s.\n", tmptrash->area); |
| 5351 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5352 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5353 | free_trash_chunk(tmptrash); |
| 5354 | tmptrash = NULL; |
| 5355 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5356 | } |
| 5357 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5358 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5359 | |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5360 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5361 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5362 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5363 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5364 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5365 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5366 | |
| 5367 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5368 | * and it's not a backup server and its effective weight is > 0, |
| 5369 | * then it can accept new connections, so we shut down all streams |
| 5370 | * on all backup servers. |
| 5371 | */ |
| 5372 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5373 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5374 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5375 | |
| 5376 | /* check if we can handle some connections queued at the proxy. We |
| 5377 | * will take as many as we can handle. |
| 5378 | */ |
| 5379 | xferred = pendconn_grab_from_px(s); |
| 5380 | |
Aurelien DARRAGON | a63f490 | 2023-03-23 14:39:51 +0100 | [diff] [blame] | 5381 | /* no maintenance + server going UP: publish event SERVER UP */ |
| 5382 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0); |
| 5383 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5384 | tmptrash = alloc_trash_chunk(); |
| 5385 | if (tmptrash) { |
| 5386 | chunk_printf(tmptrash, |
| 5387 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5388 | s->proxy->id, s->id); |
| 5389 | |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 5390 | srv_append_op_chg_cause(tmptrash, s); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5391 | srv_append_more(tmptrash, s, xferred, 0); |
| 5392 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5393 | ha_warning("%s.\n", tmptrash->area); |
| 5394 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5395 | tmptrash->area); |
| 5396 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5397 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5398 | free_trash_chunk(tmptrash); |
| 5399 | tmptrash = NULL; |
| 5400 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5401 | } |
| 5402 | else if (s->cur_eweight != s->next_eweight) { |
| 5403 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5404 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | s->next_admin = next_admin; |
| 5408 | } |
| 5409 | |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 5410 | /* reset operational change cause */ |
| 5411 | s->op_st_chg_cause = SRV_OP_STCHGC_NONE; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5412 | |
| 5413 | /* Now we try to apply pending admin changes */ |
| 5414 | |
| 5415 | /* Maintenance must also disable health checks */ |
| 5416 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5417 | if (s->check.state & CHK_ST_ENABLED) { |
| 5418 | s->check.state |= CHK_ST_PAUSED; |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame^] | 5419 | s->check.health = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5423 | tmptrash = alloc_trash_chunk(); |
| 5424 | if (tmptrash) { |
| 5425 | chunk_printf(tmptrash, |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5426 | "%sServer %s/%s was DOWN and now enters maintenance", |
| 5427 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
| 5428 | srv_append_adm_chg_cause(tmptrash, s); |
| 5429 | srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5430 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5431 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5432 | ha_warning("%s.\n", tmptrash->area); |
| 5433 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5434 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5435 | } |
| 5436 | free_trash_chunk(tmptrash); |
| 5437 | tmptrash = NULL; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5438 | } |
| 5439 | } |
| 5440 | else { /* server was still running */ |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame^] | 5441 | s->check.health = 0; /* failure */ |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5442 | |
| 5443 | s->next_state = SRV_ST_STOPPED; |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5444 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5445 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5446 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5447 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5448 | |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5449 | /* force connection cleanup on the given server */ |
| 5450 | srv_cleanup_connections(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5451 | /* we might have streams queued on this server and waiting for |
| 5452 | * a connection. Those which are redispatchable will be queued |
| 5453 | * to another server or to the proxy itself. |
| 5454 | */ |
| 5455 | xferred = pendconn_redistribute(s); |
| 5456 | |
Aurelien DARRAGON | a63f490 | 2023-03-23 14:39:51 +0100 | [diff] [blame] | 5457 | /* maintenance on previously running server: publish event SERVER DOWN */ |
| 5458 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0); |
| 5459 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5460 | tmptrash = alloc_trash_chunk(); |
| 5461 | if (tmptrash) { |
| 5462 | chunk_printf(tmptrash, |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5463 | "%sServer %s/%s is going DOWN for maintenance", |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5464 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5465 | s->proxy->id, s->id); |
| 5466 | srv_append_adm_chg_cause(tmptrash, s); |
| 5467 | srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5468 | |
| 5469 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5470 | ha_warning("%s.\n", tmptrash->area); |
| 5471 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5472 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5473 | } |
| 5474 | free_trash_chunk(tmptrash); |
| 5475 | tmptrash = NULL; |
| 5476 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5477 | } |
| 5478 | } |
| 5479 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5480 | /* OK here we're leaving maintenance, we have many things to check, |
| 5481 | * because the server might possibly be coming back up depending on |
| 5482 | * its state. In practice, leaving maintenance means that we should |
| 5483 | * immediately turn to UP (more or less the slowstart) under the |
| 5484 | * following conditions : |
| 5485 | * - server is neither checked nor tracked |
| 5486 | * - server tracks another server which is not checked |
| 5487 | * - server tracks another server which is already up |
| 5488 | * Which sums up as something simpler : |
| 5489 | * "either the tracking server is up or the server's checks are disabled |
| 5490 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5491 | * case associated to the stopping state which can be inherited. Note |
| 5492 | * that the server might still be in drain mode, which is naturally dealt |
| 5493 | * with by the lower level functions. |
| 5494 | */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5495 | if (s->check.state & CHK_ST_ENABLED) { |
| 5496 | s->check.state &= ~CHK_ST_PAUSED; |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame^] | 5497 | s->check.health = s->check.rise; /* start OK but check immediately */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5501 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5502 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5503 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5504 | s->next_state = SRV_ST_STOPPING; |
| 5505 | } |
| 5506 | else { |
| 5507 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5508 | if (s->slowstart > 0) { |
| 5509 | if (s->warmup) |
| 5510 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5511 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5512 | else |
| 5513 | s->next_state = SRV_ST_RUNNING; |
| 5514 | } |
| 5515 | |
| 5516 | } |
| 5517 | |
Aurelien DARRAGON | 22f82f8 | 2022-11-25 18:07:49 +0100 | [diff] [blame] | 5518 | /* ignore if server stays down when leaving maintenance mode */ |
| 5519 | if (s->next_state != SRV_ST_STOPPED) { |
| 5520 | /* leaving maintenance + server UP: publish event SERVER UP */ |
| 5521 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0); |
| 5522 | } |
| 5523 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5524 | tmptrash = alloc_trash_chunk(); |
| 5525 | if (tmptrash) { |
| 5526 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5527 | chunk_printf(tmptrash, |
| 5528 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5529 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5530 | s->proxy->id, s->id, |
| 5531 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5532 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5533 | } |
| 5534 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5535 | chunk_printf(tmptrash, |
| 5536 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5537 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5538 | s->proxy->id, s->id, s->hostname, |
| 5539 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5540 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5541 | } |
| 5542 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5543 | chunk_printf(tmptrash, |
| 5544 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5545 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5546 | s->proxy->id, s->id, |
| 5547 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5548 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5549 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5550 | ha_warning("%s.\n", tmptrash->area); |
| 5551 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5552 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5553 | free_trash_chunk(tmptrash); |
| 5554 | tmptrash = NULL; |
| 5555 | } |
| 5556 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5557 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5558 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5559 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5560 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5561 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5562 | * and it's not a backup server and its effective weight is > 0, |
| 5563 | * then it can accept new connections, so we shut down all streams |
| 5564 | * on all backup servers. |
| 5565 | */ |
| 5566 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5567 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5568 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5569 | |
| 5570 | /* check if we can handle some connections queued at the proxy. We |
| 5571 | * will take as many as we can handle. |
| 5572 | */ |
| 5573 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5574 | } |
| 5575 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5576 | /* remaining in maintenance mode, let's inform precisely about the |
| 5577 | * situation. |
| 5578 | */ |
| 5579 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5580 | tmptrash = alloc_trash_chunk(); |
| 5581 | if (tmptrash) { |
| 5582 | chunk_printf(tmptrash, |
| 5583 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5584 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5585 | s->proxy->id, s->id); |
| 5586 | |
| 5587 | if (s->track) /* normally it's mandatory here */ |
| 5588 | chunk_appendf(tmptrash, " via %s/%s", |
| 5589 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5590 | ha_warning("%s.\n", tmptrash->area); |
| 5591 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5592 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5593 | free_trash_chunk(tmptrash); |
| 5594 | tmptrash = NULL; |
| 5595 | } |
| 5596 | } |
| 5597 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5598 | tmptrash = alloc_trash_chunk(); |
| 5599 | if (tmptrash) { |
| 5600 | chunk_printf(tmptrash, |
| 5601 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5602 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5603 | s->proxy->id, s->id, s->hostname); |
| 5604 | |
| 5605 | if (s->track) /* normally it's mandatory here */ |
| 5606 | chunk_appendf(tmptrash, " via %s/%s", |
| 5607 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5608 | ha_warning("%s.\n", tmptrash->area); |
| 5609 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5610 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5611 | free_trash_chunk(tmptrash); |
| 5612 | tmptrash = NULL; |
| 5613 | } |
| 5614 | } |
| 5615 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5616 | tmptrash = alloc_trash_chunk(); |
| 5617 | if (tmptrash) { |
| 5618 | chunk_printf(tmptrash, |
| 5619 | "%sServer %s/%s remains in forced maintenance", |
| 5620 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5621 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5622 | ha_warning("%s.\n", tmptrash->area); |
| 5623 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5624 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5625 | free_trash_chunk(tmptrash); |
| 5626 | tmptrash = NULL; |
| 5627 | } |
| 5628 | } |
| 5629 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5633 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5634 | /* drain state is applied only if not yet in maint */ |
| 5635 | |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5636 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5637 | |
| 5638 | /* we might have streams queued on this server and waiting for |
| 5639 | * a connection. Those which are redispatchable will be queued |
| 5640 | * to another server or to the proxy itself. |
| 5641 | */ |
| 5642 | xferred = pendconn_redistribute(s); |
| 5643 | |
| 5644 | tmptrash = alloc_trash_chunk(); |
| 5645 | if (tmptrash) { |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5646 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state", |
| 5647 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
| 5648 | srv_append_adm_chg_cause(tmptrash, s); |
| 5649 | srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5650 | |
| 5651 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5652 | ha_warning("%s.\n", tmptrash->area); |
| 5653 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5654 | tmptrash->area); |
| 5655 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5656 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5657 | } |
| 5658 | free_trash_chunk(tmptrash); |
| 5659 | tmptrash = NULL; |
| 5660 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5661 | } |
| 5662 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5663 | /* OK completely leaving drain mode */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5664 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5665 | |
| 5666 | tmptrash = alloc_trash_chunk(); |
| 5667 | if (tmptrash) { |
Aurelien DARRAGON | 9a1df02 | 2023-03-27 10:17:31 +0200 | [diff] [blame] | 5668 | if (s->cur_admin & SRV_ADMF_FDRAIN) { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5669 | chunk_printf(tmptrash, |
| 5670 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5671 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5672 | s->proxy->id, s->id, |
| 5673 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5674 | } |
| 5675 | else { |
| 5676 | chunk_printf(tmptrash, |
| 5677 | "%sServer %s/%s is %s (leaving drain)", |
| 5678 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5679 | s->proxy->id, s->id, |
| 5680 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5681 | if (s->track) /* normally it's mandatory here */ |
| 5682 | chunk_appendf(tmptrash, " via %s/%s", |
| 5683 | s->track->proxy->id, s->track->id); |
| 5684 | } |
| 5685 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5686 | ha_warning("%s.\n", tmptrash->area); |
| 5687 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5688 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5689 | free_trash_chunk(tmptrash); |
| 5690 | tmptrash = NULL; |
| 5691 | } |
| 5692 | |
| 5693 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5694 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5695 | } |
| 5696 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5697 | /* remaining in drain mode after removing one of its flags */ |
| 5698 | |
| 5699 | tmptrash = alloc_trash_chunk(); |
| 5700 | if (tmptrash) { |
| 5701 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5702 | chunk_printf(tmptrash, |
| 5703 | "%sServer %s/%s is leaving forced drain but remains in drain mode", |
| 5704 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5705 | s->proxy->id, s->id); |
| 5706 | |
| 5707 | if (s->track) /* normally it's mandatory here */ |
| 5708 | chunk_appendf(tmptrash, " via %s/%s", |
| 5709 | s->track->proxy->id, s->track->id); |
| 5710 | } |
| 5711 | else { |
| 5712 | chunk_printf(tmptrash, |
| 5713 | "%sServer %s/%s remains in forced drain mode", |
| 5714 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5715 | s->proxy->id, s->id); |
| 5716 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5717 | ha_warning("%s.\n", tmptrash->area); |
| 5718 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5719 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5720 | free_trash_chunk(tmptrash); |
| 5721 | tmptrash = NULL; |
| 5722 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 5726 | /* Re-set adm st change to none */ |
| 5727 | s->adm_st_chg_cause = SRV_ADM_STCHGC_NONE; |
Aurelien DARRAGON | 22151c7 | 2023-04-17 17:45:58 +0200 | [diff] [blame] | 5728 | |
| 5729 | /* explicitly commit state changes (even if it was already applied implicitly |
| 5730 | * by some lb state change function), so we don't miss anything |
| 5731 | */ |
| 5732 | srv_lb_commit_status(s); |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5733 | |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5734 | /* check if server stats must be updated due the the server state change */ |
| 5735 | if (srv_prev_state != s->cur_state) { |
| 5736 | if (srv_prev_state == SRV_ST_STOPPED) { |
| 5737 | /* server was down and no longer is */ |
| 5738 | if (s->last_change < now.tv_sec) // ignore negative times |
| 5739 | s->down_time += now.tv_sec - s->last_change; |
| 5740 | } |
Aurelien DARRAGON | 5f80f8b | 2023-04-19 18:22:21 +0200 | [diff] [blame] | 5741 | else if (s->cur_state == SRV_ST_STOPPED) { |
| 5742 | /* server was up and is currently down */ |
| 5743 | s->counters.down_trans++; |
| 5744 | } |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5745 | s->last_change = now.tv_sec; |
| 5746 | } |
| 5747 | |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5748 | /* check if backend stats must be updated due to the server state change */ |
| 5749 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5750 | set_backend_down(s->proxy); /* backend going down */ |
| 5751 | else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) { |
| 5752 | /* backend was down and is back up again: |
| 5753 | * no helper function, updating last_change and backend downtime stats |
| 5754 | */ |
| 5755 | if (s->proxy->last_change < now.tv_sec) // ignore negative times |
| 5756 | s->proxy->down_time += now.tv_sec - s->proxy->last_change; |
| 5757 | s->proxy->last_change = now.tv_sec; |
| 5758 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5759 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5760 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5761 | 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] | 5762 | { |
| 5763 | struct connection *conn; |
| 5764 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5765 | while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns, |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5766 | struct connection *, toremove_list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5767 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5768 | } |
| 5769 | |
| 5770 | return task; |
| 5771 | } |
| 5772 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5773 | /* Move toremove_nb connections from idle_tree to toremove_list, -1 means |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5774 | * moving them all. |
| 5775 | * Returns the number of connections moved. |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5776 | * |
| 5777 | * Must be called with idle_conns_lock held. |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5778 | */ |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5779 | 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] | 5780 | { |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5781 | struct eb_node *node, *next; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5782 | struct conn_hash_node *hash_node; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5783 | int i = 0; |
| 5784 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5785 | node = eb_first(idle_tree); |
| 5786 | while (node) { |
| 5787 | next = eb_next(node); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5788 | if (toremove_nb != -1 && i >= toremove_nb) |
| 5789 | break; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5790 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5791 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5792 | eb_delete(node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5793 | MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5794 | i++; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5795 | |
| 5796 | node = next; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5797 | } |
| 5798 | return i; |
| 5799 | } |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5800 | /* cleanup connections for a given server |
| 5801 | * might be useful when going on forced maintenance or live changing ip/port |
| 5802 | */ |
William Dauchy | 707ad32 | 2020-05-04 13:52:40 +0200 | [diff] [blame] | 5803 | static void srv_cleanup_connections(struct server *srv) |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5804 | { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5805 | int did_remove; |
| 5806 | int i; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5807 | |
Amaury Denoyelle | 10d5c31 | 2021-01-06 14:28:51 +0100 | [diff] [blame] | 5808 | /* nothing to do if pool-max-conn is null */ |
| 5809 | if (!srv->max_idle_conns) |
| 5810 | return; |
| 5811 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5812 | /* check all threads starting with ours */ |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5813 | for (i = tid;;) { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5814 | did_remove = 0; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5815 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5816 | 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] | 5817 | did_remove = 1; |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5818 | 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] | 5819 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5820 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5821 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5822 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5823 | |
| 5824 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 5825 | break; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5826 | } |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | /* removes an idle conn after updating the server idle conns counters */ |
| 5830 | void srv_release_conn(struct server *srv, struct connection *conn) |
| 5831 | { |
| 5832 | if (conn->flags & CO_FL_LIST_MASK) { |
| 5833 | /* The connection is currently in the server's idle list, so tell it |
| 5834 | * there's one less connection available in that list. |
| 5835 | */ |
| 5836 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5837 | _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb); |
| 5838 | _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]); |
| 5839 | } |
| 5840 | else { |
| 5841 | /* The connection is not private and not in any server's idle |
| 5842 | * list, so decrement the current number of used connections |
| 5843 | */ |
| 5844 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5845 | } |
| 5846 | |
| 5847 | /* Remove the connection from any tree (safe, idle or available) */ |
| 5848 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5849 | conn_delete_from_tree(&conn->hash_node->node); |
Christopher Faulet | 5e1b0e7 | 2023-02-28 15:39:38 +0100 | [diff] [blame] | 5850 | conn->flags &= ~CO_FL_LIST_MASK; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5851 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5852 | } |
| 5853 | |
| 5854 | /* retrieve a connection from its <hash> in <tree> |
| 5855 | * returns NULL if no connection found |
| 5856 | */ |
| 5857 | struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash) |
| 5858 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5859 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5860 | struct connection *conn = NULL; |
| 5861 | struct conn_hash_node *hash_node = NULL; |
| 5862 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5863 | node = eb64_lookup(tree, hash); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5864 | if (node) { |
| 5865 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 5866 | conn = hash_node->conn; |
| 5867 | } |
| 5868 | |
| 5869 | return conn; |
| 5870 | } |
| 5871 | |
| 5872 | /* retrieve the next connection sharing the same hash as <conn> |
| 5873 | * returns NULL if no connection found |
| 5874 | */ |
| 5875 | struct connection *srv_lookup_conn_next(struct connection *conn) |
| 5876 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5877 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5878 | struct connection *next_conn = NULL; |
| 5879 | struct conn_hash_node *hash_node = NULL; |
| 5880 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5881 | node = eb64_next_dup(&conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5882 | if (node) { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5883 | hash_node = eb64_entry(node, struct conn_hash_node, node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5884 | next_conn = hash_node->conn; |
| 5885 | } |
| 5886 | |
| 5887 | return next_conn; |
| 5888 | } |
| 5889 | |
| 5890 | /* This adds an idle connection to the server's list if the connection is |
| 5891 | * reusable, not held by any owner anymore, but still has available streams. |
| 5892 | */ |
| 5893 | int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe) |
| 5894 | { |
| 5895 | /* we try to keep the connection in the server's idle list |
| 5896 | * if we don't have too many FD in use, and if the number of |
| 5897 | * idle+current conns is lower than what was observed before |
| 5898 | * last purge, or if we already don't have idle conns for the |
| 5899 | * current thread and we don't exceed last count by global.nbthread. |
| 5900 | */ |
| 5901 | if (!(conn->flags & CO_FL_PRIVATE) && |
| 5902 | srv && srv->pool_purge_delay > 0 && |
| 5903 | ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) && |
| 5904 | ha_used_fds < global.tune.pool_high_count && |
| 5905 | (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) && |
| 5906 | ((eb_is_empty(&srv->per_thr[tid].safe_conns) && |
| 5907 | (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) || |
| 5908 | (ha_used_fds < global.tune.pool_low_count && |
| 5909 | (srv->curr_used_conns + srv->curr_idle_conns <= |
| 5910 | MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) && |
| 5911 | !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) { |
| 5912 | int retadd; |
| 5913 | |
| 5914 | retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1); |
| 5915 | if (retadd > srv->max_idle_conns) { |
| 5916 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5917 | return 0; |
| 5918 | } |
| 5919 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5920 | |
| 5921 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5922 | conn_delete_from_tree(&conn->hash_node->node); |
| 5923 | |
| 5924 | if (is_safe) { |
| 5925 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5926 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5927 | _HA_ATOMIC_INC(&srv->curr_safe_nb); |
| 5928 | } else { |
| 5929 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5930 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5931 | _HA_ATOMIC_INC(&srv->curr_idle_nb); |
| 5932 | } |
| 5933 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5934 | _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]); |
| 5935 | |
| 5936 | __ha_barrier_full(); |
| 5937 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 5938 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5939 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 5940 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 5941 | now_ms); |
| 5942 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 5943 | if (!task_in_wq(idle_conn_task) && ! |
| 5944 | task_in_rq(idle_conn_task)) { |
| 5945 | task_schedule(idle_conn_task, |
| 5946 | srv->idle_node.key); |
| 5947 | } |
| 5948 | |
| 5949 | } |
| 5950 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5951 | } |
| 5952 | return 1; |
| 5953 | } |
| 5954 | return 0; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5955 | } |
| 5956 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5957 | 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] | 5958 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5959 | struct server *srv; |
| 5960 | struct eb32_node *eb; |
| 5961 | int i; |
| 5962 | unsigned int next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5963 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 5964 | next_wakeup = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5965 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 5966 | while (1) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 5967 | int exceed_conns; |
| 5968 | int to_kill; |
| 5969 | int curr_idle; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 5970 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5971 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 5972 | if (!eb) { |
| 5973 | /* we might have reached the end of the tree, typically because |
| 5974 | * <now_ms> is in the first half and we're first scanning the last |
| 5975 | * half. Let's loop back to the beginning of the tree now. |
| 5976 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5977 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5978 | eb = eb32_first(&idle_conn_srv); |
| 5979 | if (likely(!eb)) |
| 5980 | break; |
| 5981 | } |
| 5982 | if (tick_is_lt(now_ms, eb->key)) { |
| 5983 | /* timer not expired yet, revisit it later */ |
| 5984 | next_wakeup = eb->key; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 5985 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5986 | } |
| 5987 | srv = eb32_entry(eb, struct server, idle_node); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 5988 | |
| 5989 | /* Calculate how many idle connections we want to kill : |
| 5990 | * we want to remove half the difference between the total |
| 5991 | * of established connections (used or idle) and the max |
| 5992 | * number of used connections. |
| 5993 | */ |
| 5994 | curr_idle = srv->curr_idle_conns; |
| 5995 | if (curr_idle == 0) |
| 5996 | goto remove; |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 5997 | 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] | 5998 | exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1); |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 5999 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6000 | 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] | 6001 | if (srv->est_need_conns < srv->max_used_conns) |
| 6002 | srv->est_need_conns = srv->max_used_conns; |
| 6003 | |
Willy Tarreau | 9dc231a | 2022-11-21 14:32:33 +0100 | [diff] [blame] | 6004 | HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6005 | |
Willy Tarreau | 18ed789 | 2020-07-02 19:05:30 +0200 | [diff] [blame] | 6006 | if (exceed_conns <= 0) |
| 6007 | goto remove; |
| 6008 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6009 | /* check all threads starting with ours */ |
| 6010 | for (i = tid;;) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6011 | int max_conn; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6012 | int j; |
| 6013 | int did_remove = 0; |
| 6014 | |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6015 | max_conn = (exceed_conns * srv->curr_idle_thr[i]) / |
| 6016 | curr_idle + 1; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6017 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6018 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6019 | 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] | 6020 | if (j > 0) |
| 6021 | did_remove = 1; |
| 6022 | if (max_conn - j > 0 && |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6023 | 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] | 6024 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6025 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6026 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6027 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 6028 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6029 | |
| 6030 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 6031 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6032 | } |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6033 | remove: |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6034 | eb32_delete(&srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6035 | |
| 6036 | if (srv->curr_idle_conns) { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6037 | /* There are still more idle connections, add the |
| 6038 | * server back in the tree. |
| 6039 | */ |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6040 | srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6041 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6042 | next_wakeup = tick_first(next_wakeup, srv->idle_node.key); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6043 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6044 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6045 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6046 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6047 | task->expire = next_wakeup; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6048 | return task; |
| 6049 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6050 | |
Amaury Denoyelle | 3109ccf | 2021-04-29 17:30:05 +0200 | [diff] [blame] | 6051 | /* Close remaining idle connections. This functions is designed to be run on |
| 6052 | * process shutdown. This guarantees a proper socket shutdown to avoid |
| 6053 | * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is |
| 6054 | * bypassed. |
| 6055 | * |
| 6056 | * This function is not thread-safe so it must only be called via a global |
| 6057 | * deinit function. |
| 6058 | */ |
| 6059 | static void srv_close_idle_conns(struct server *srv) |
| 6060 | { |
| 6061 | struct eb_root **cleaned_tree; |
| 6062 | int i; |
| 6063 | |
| 6064 | for (i = 0; i < global.nbthread; ++i) { |
| 6065 | struct eb_root *conn_trees[] = { |
| 6066 | &srv->per_thr[i].idle_conns, |
| 6067 | &srv->per_thr[i].safe_conns, |
| 6068 | &srv->per_thr[i].avail_conns, |
| 6069 | NULL |
| 6070 | }; |
| 6071 | |
| 6072 | for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) { |
| 6073 | while (!eb_is_empty(*cleaned_tree)) { |
| 6074 | struct ebmb_node *node = ebmb_first(*cleaned_tree); |
| 6075 | struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 6076 | struct connection *conn = conn_hash_node->conn; |
| 6077 | |
| 6078 | if (conn->ctrl->ctrl_close) |
| 6079 | conn->ctrl->ctrl_close(conn); |
| 6080 | ebmb_delete(node); |
| 6081 | } |
| 6082 | } |
| 6083 | } |
| 6084 | } |
| 6085 | |
| 6086 | REGISTER_SERVER_DEINIT(srv_close_idle_conns); |
| 6087 | |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6088 | /* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */ |
| 6089 | 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] | 6090 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6091 | char **err) |
| 6092 | { |
| 6093 | if (too_many_args(1, args, err, NULL)) |
| 6094 | return -1; |
| 6095 | |
| 6096 | if (strcmp(args[1], "on") == 0) |
| 6097 | global.tune.options |= GTUNE_IDLE_POOL_SHARED; |
| 6098 | else if (strcmp(args[1], "off") == 0) |
| 6099 | global.tune.options &= ~GTUNE_IDLE_POOL_SHARED; |
| 6100 | else { |
| 6101 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 6102 | return -1; |
| 6103 | } |
| 6104 | return 0; |
| 6105 | } |
| 6106 | |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6107 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 6108 | 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] | 6109 | const struct proxy *defpx, const char *file, int line, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6110 | char **err) |
| 6111 | { |
| 6112 | int arg = -1; |
| 6113 | |
| 6114 | if (too_many_args(1, args, err, NULL)) |
| 6115 | return -1; |
| 6116 | |
| 6117 | if (*(args[1]) != 0) |
| 6118 | arg = atoi(args[1]); |
| 6119 | |
| 6120 | if (arg < 0 || arg > 100) { |
| 6121 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 6122 | return -1; |
| 6123 | } |
| 6124 | |
| 6125 | if (args[0][10] == 'h') |
| 6126 | global.tune.pool_high_ratio = arg; |
| 6127 | else |
| 6128 | global.tune.pool_low_ratio = arg; |
| 6129 | return 0; |
| 6130 | } |
| 6131 | |
| 6132 | /* config keyword parsers */ |
| 6133 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6134 | { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared }, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6135 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6136 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6137 | { 0, NULL, NULL } |
| 6138 | }}; |
| 6139 | |
| 6140 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 6141 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 6142 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6143 | * Local variables: |
| 6144 | * c-indent-level: 8 |
| 6145 | * c-basic-offset: 8 |
| 6146 | * End: |
| 6147 | */ |