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 | |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 53 | static void srv_update_status(struct server *s, int type, int cause); |
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 | { |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 141 | if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= ns_to_sec(now_ns)) // ignore negative time |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 142 | return s->down_time; |
| 143 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 144 | return ns_to_sec(now_ns) - s->last_change + s->down_time; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 150 | return ns_to_sec(now_ns) - s->counters.last_sess; |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 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 | |
Aurelien DARRAGON | 23f352f | 2023-03-24 17:02:53 +0100 | [diff] [blame] | 169 | /* fill common server event data members struct |
| 170 | * must be called with server lock or under thread isolate |
| 171 | */ |
| 172 | static inline void _srv_event_hdl_prepare(struct event_hdl_cb_data_server *cb_data, |
| 173 | struct server *srv, uint8_t thread_isolate) |
| 174 | { |
| 175 | /* safe data assignments */ |
| 176 | cb_data->safe.puid = srv->puid; |
| 177 | cb_data->safe.rid = srv->rid; |
| 178 | cb_data->safe.flags = srv->flags; |
| 179 | snprintf(cb_data->safe.name, sizeof(cb_data->safe.name), "%s", srv->id); |
| 180 | cb_data->safe.proxy_name[0] = '\0'; |
| 181 | cb_data->safe.proxy_uuid = -1; /* default value */ |
| 182 | if (srv->proxy) { |
| 183 | cb_data->safe.proxy_uuid = srv->proxy->uuid; |
| 184 | snprintf(cb_data->safe.proxy_name, sizeof(cb_data->safe.proxy_name), "%s", srv->proxy->id); |
| 185 | } |
| 186 | /* unsafe data assignments */ |
| 187 | cb_data->unsafe.ptr = srv; |
| 188 | cb_data->unsafe.thread_isolate = thread_isolate; |
| 189 | cb_data->unsafe.srv_lock = !thread_isolate; |
| 190 | } |
| 191 | |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 192 | /* take an event-check snapshot from a live check */ |
| 193 | void _srv_event_hdl_prepare_checkres(struct event_hdl_cb_data_server_checkres *checkres, |
| 194 | struct check *check) |
| 195 | { |
| 196 | checkres->agent = !!(check->state & CHK_ST_AGENT); |
| 197 | checkres->result = check->result; |
| 198 | checkres->duration = check->duration; |
| 199 | checkres->reason.status = check->status; |
| 200 | checkres->reason.code = check->code; |
| 201 | checkres->health.cur = check->health; |
| 202 | checkres->health.rise = check->rise; |
| 203 | checkres->health.fall = check->fall; |
| 204 | } |
| 205 | |
| 206 | /* Prepare SERVER_STATE event |
| 207 | * |
| 208 | * This special event will contain extra hints related to the state change |
| 209 | * |
| 210 | * Must be called with server lock held |
| 211 | */ |
| 212 | void _srv_event_hdl_prepare_state(struct event_hdl_cb_data_server_state *cb_data, |
| 213 | struct server *srv, int type, int cause, |
| 214 | enum srv_state prev_state, int requeued) |
| 215 | { |
| 216 | /* state event provides additional info about the server state change */ |
| 217 | cb_data->safe.type = type; |
| 218 | cb_data->safe.new_state = srv->cur_state; |
| 219 | cb_data->safe.old_state = prev_state; |
| 220 | cb_data->safe.requeued = requeued; |
| 221 | if (type) { |
| 222 | /* administrative */ |
| 223 | cb_data->safe.adm_st_chg.cause = cause; |
| 224 | } |
| 225 | else { |
| 226 | /* operational */ |
| 227 | cb_data->safe.op_st_chg.cause = cause; |
| 228 | if (cause == SRV_OP_STCHGC_HEALTH || cause == SRV_OP_STCHGC_AGENT) { |
| 229 | struct check *check = (cause == SRV_OP_STCHGC_HEALTH) ? &srv->check : &srv->agent; |
| 230 | |
| 231 | /* provide additional check-related state change result */ |
| 232 | _srv_event_hdl_prepare_checkres(&cb_data->safe.op_st_chg.check, check); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Aurelien DARRAGON | 306a5fc | 2023-04-25 10:44:41 +0200 | [diff] [blame] | 237 | /* server event publishing helper: publish in both global and |
| 238 | * server dedicated subscription list. |
| 239 | */ |
| 240 | #define _srv_event_hdl_publish(e, d, s) \ |
| 241 | ({ \ |
| 242 | /* publish in server dedicated sub list */ \ |
| 243 | event_hdl_publish(&s->e_subs, e, EVENT_HDL_CB_DATA(&d));\ |
| 244 | /* publish in global subscription list */ \ |
| 245 | event_hdl_publish(NULL, e, EVENT_HDL_CB_DATA(&d)); \ |
| 246 | }) |
| 247 | |
| 248 | /* General server event publishing: |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 249 | * Use this to publish EVENT_HDL_SUB_SERVER family type event |
Aurelien DARRAGON | 306a5fc | 2023-04-25 10:44:41 +0200 | [diff] [blame] | 250 | * from srv facility. |
| 251 | * |
| 252 | * server ptr must be valid. |
| 253 | * Must be called with srv lock or under thread_isolate. |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 254 | */ |
Aurelien DARRAGON | 23f352f | 2023-03-24 17:02:53 +0100 | [diff] [blame] | 255 | static void srv_event_hdl_publish(struct event_hdl_sub_type event, |
| 256 | struct server *srv, uint8_t thread_isolate) |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 257 | { |
| 258 | struct event_hdl_cb_data_server cb_data; |
| 259 | |
Aurelien DARRAGON | 23f352f | 2023-03-24 17:02:53 +0100 | [diff] [blame] | 260 | /* prepare event data */ |
| 261 | _srv_event_hdl_prepare(&cb_data, srv, thread_isolate); |
Aurelien DARRAGON | 306a5fc | 2023-04-25 10:44:41 +0200 | [diff] [blame] | 262 | _srv_event_hdl_publish(event, cb_data, srv); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Aurelien DARRAGON | dcbc2d2 | 2023-03-30 10:19:08 +0200 | [diff] [blame] | 265 | /* Publish SERVER_CHECK event |
| 266 | * |
| 267 | * This special event will contain extra hints related to the check itself |
| 268 | * |
| 269 | * Must be called with server lock held |
| 270 | */ |
| 271 | void srv_event_hdl_publish_check(struct server *srv, struct check *check) |
| 272 | { |
| 273 | struct event_hdl_cb_data_server_check cb_data; |
| 274 | |
| 275 | /* check event provides additional info about the server check */ |
| 276 | _srv_event_hdl_prepare_checkres(&cb_data.safe.res, check); |
| 277 | |
| 278 | cb_data.unsafe.ptr = check; |
| 279 | |
| 280 | /* prepare event data (common server data) */ |
| 281 | _srv_event_hdl_prepare((struct event_hdl_cb_data_server *)&cb_data, srv, 0); |
| 282 | |
| 283 | _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_CHECK, cb_data, srv); |
| 284 | } |
| 285 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 286 | /* |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 287 | * Check that we did not get a hash collision. |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 288 | * Unlikely, but it can happen. The server's proxy must be at least |
| 289 | * read-locked. |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 290 | */ |
| 291 | static inline void srv_check_for_dup_dyncookie(struct server *s) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 292 | { |
| 293 | struct proxy *p = s->proxy; |
| 294 | struct server *tmpserv; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 295 | |
| 296 | for (tmpserv = p->srv; tmpserv != NULL; |
| 297 | tmpserv = tmpserv->next) { |
| 298 | if (tmpserv == s) |
| 299 | continue; |
| 300 | if (tmpserv->next_admin & SRV_ADMF_FMAINT) |
| 301 | continue; |
| 302 | if (tmpserv->cookie && |
| 303 | strcmp(tmpserv->cookie, s->cookie) == 0) { |
| 304 | ha_warning("We generated two equal cookies for two different servers.\n" |
| 305 | "Please change the secret key for '%s'.\n", |
| 306 | s->proxy->id); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | } |
| 311 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 312 | /* |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 313 | * 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] | 314 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 315 | void srv_set_dyncookie(struct server *s) |
| 316 | { |
| 317 | struct proxy *p = s->proxy; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 318 | char *tmpbuf; |
| 319 | unsigned long long hash_value; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 320 | size_t key_len; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 321 | size_t buffer_len; |
| 322 | int addr_len; |
| 323 | int port; |
| 324 | |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 325 | HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 326 | |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 327 | if ((s->flags & SRV_F_COOKIESET) || |
| 328 | !(s->proxy->ck_opts & PR_CK_DYNAMIC) || |
| 329 | s->proxy->dyncookie_key == NULL) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 330 | goto out; |
Olivier Houchard | 2cb49eb | 2017-03-15 15:11:06 +0100 | [diff] [blame] | 331 | key_len = strlen(p->dyncookie_key); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 332 | |
| 333 | if (s->addr.ss_family != AF_INET && |
| 334 | s->addr.ss_family != AF_INET6) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 335 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 336 | /* |
| 337 | * Buffer to calculate the cookie value. |
| 338 | * The buffer contains the secret key + the server IP address |
| 339 | * + the TCP port. |
| 340 | */ |
| 341 | addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16; |
| 342 | /* |
| 343 | * The TCP port should use only 2 bytes, but is stored in |
| 344 | * an unsigned int in struct server, so let's use 4, to be |
| 345 | * on the safe side. |
| 346 | */ |
| 347 | buffer_len = key_len + addr_len + 4; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 348 | tmpbuf = trash.area; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 349 | memcpy(tmpbuf, p->dyncookie_key, key_len); |
| 350 | memcpy(&(tmpbuf[key_len]), |
| 351 | s->addr.ss_family == AF_INET ? |
| 352 | (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr : |
| 353 | (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr), |
| 354 | addr_len); |
| 355 | /* |
| 356 | * Make sure it's the same across all the load balancers, |
| 357 | * no matter their endianness. |
| 358 | */ |
| 359 | port = htonl(s->svc_port); |
| 360 | memcpy(&tmpbuf[key_len + addr_len], &port, 4); |
| 361 | hash_value = XXH64(tmpbuf, buffer_len, 0); |
| 362 | memprintf(&s->cookie, "%016llx", hash_value); |
| 363 | if (!s->cookie) |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 364 | goto out; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 365 | s->cklen = 16; |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 366 | |
| 367 | /* Don't bother checking if the dyncookie is duplicated if |
| 368 | * the server is marked as "disabled", maybe it doesn't have |
| 369 | * its real IP yet, but just a place holder. |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 370 | */ |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 371 | if (!(s->next_admin & SRV_ADMF_FMAINT)) |
| 372 | srv_check_for_dup_dyncookie(s); |
Willy Tarreau | 5e83d99 | 2019-07-30 11:59:34 +0200 | [diff] [blame] | 373 | out: |
Willy Tarreau | 595e767 | 2020-10-20 17:30:08 +0200 | [diff] [blame] | 374 | HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 377 | /* Returns true if it's possible to reuse an idle connection from server <srv> |
| 378 | * for a websocket stream. This is the case if server is configured to use the |
| 379 | * same protocol for both HTTP and websocket streams. This depends on the value |
| 380 | * of "proto", "alpn" and "ws" keywords. |
| 381 | */ |
| 382 | int srv_check_reuse_ws(struct server *srv) |
| 383 | { |
| 384 | if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) { |
| 385 | /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used |
| 386 | * for mux selection. |
| 387 | */ |
| 388 | const struct ist srv_mux = srv->mux_proto ? |
| 389 | srv->mux_proto->token : IST_NULL; |
| 390 | |
| 391 | switch (srv->ws) { |
| 392 | /* "auto" means use the same protocol : reuse is possible. */ |
| 393 | case SRV_WS_AUTO: |
| 394 | return 1; |
| 395 | |
| 396 | /* "h2" means use h2 for websocket : reuse is possible if |
| 397 | * server mux is h2. |
| 398 | */ |
| 399 | case SRV_WS_H2: |
| 400 | if (srv->mux_proto && isteq(srv_mux, ist("h2"))) |
| 401 | return 1; |
| 402 | break; |
| 403 | |
| 404 | /* "h1" means use h1 for websocket : reuse is possible if |
| 405 | * server mux is h1. |
| 406 | */ |
| 407 | case SRV_WS_H1: |
| 408 | if (!srv->mux_proto || isteq(srv_mux, ist("h1"))) |
| 409 | return 1; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | else { |
| 414 | /* ALPN selection. |
| 415 | * Based on the assumption that only "h2" and "http/1.1" token |
| 416 | * are used on server ALPN. |
| 417 | */ |
| 418 | const struct ist alpn = ist2(srv->ssl_ctx.alpn_str, |
| 419 | srv->ssl_ctx.alpn_len); |
| 420 | |
| 421 | switch (srv->ws) { |
| 422 | case SRV_WS_AUTO: |
| 423 | /* for auto mode, consider reuse as possible if the |
| 424 | * server uses a single protocol ALPN |
| 425 | */ |
| 426 | if (!istchr(alpn, ',')) |
| 427 | return 1; |
| 428 | break; |
| 429 | |
| 430 | case SRV_WS_H2: |
| 431 | return isteq(alpn, ist("\x02h2")); |
| 432 | |
| 433 | case SRV_WS_H1: |
| 434 | return isteq(alpn, ist("\x08http/1.1")); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* Return the proto to used for a websocket stream on <srv> without ALPN. NULL |
| 442 | * is a valid value indicating to use the fallback mux. |
| 443 | */ |
| 444 | const struct mux_ops *srv_get_ws_proto(struct server *srv) |
| 445 | { |
| 446 | const struct mux_proto_list *mux = NULL; |
| 447 | |
| 448 | switch (srv->ws) { |
| 449 | case SRV_WS_AUTO: |
| 450 | mux = srv->mux_proto; |
| 451 | break; |
| 452 | |
| 453 | case SRV_WS_H1: |
| 454 | mux = get_mux_proto(ist("h1")); |
| 455 | break; |
| 456 | |
| 457 | case SRV_WS_H2: |
| 458 | mux = get_mux_proto(ist("h2")); |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | return mux ? mux->mux : NULL; |
| 463 | } |
| 464 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 465 | /* |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 466 | * Must be called with the server lock held. The server is first removed from |
| 467 | * the proxy tree if it was already attached. If <reattach> is true, the server |
| 468 | * will then be attached in the proxy tree. The proxy lock is held to |
| 469 | * manipulate the tree. |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 470 | */ |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 471 | static void srv_set_addr_desc(struct server *s, int reattach) |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 472 | { |
| 473 | struct proxy *p = s->proxy; |
| 474 | char *key; |
| 475 | |
| 476 | key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS); |
| 477 | |
| 478 | if (s->addr_node.key) { |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 479 | if (key && strcmp(key, s->addr_node.key) == 0) { |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 480 | free(key); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 485 | ebpt_delete(&s->addr_node); |
| 486 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 487 | |
| 488 | free(s->addr_node.key); |
| 489 | } |
| 490 | |
| 491 | s->addr_node.key = key; |
| 492 | |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 493 | if (reattach) { |
| 494 | if (s->addr_node.key) { |
| 495 | HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock); |
| 496 | ebis_insert(&p->used_server_addr, &s->addr_node); |
| 497 | HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock); |
| 498 | } |
Thayne McCombs | 24da7e1 | 2021-01-05 23:10:09 -0700 | [diff] [blame] | 499 | } |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 503 | * Registers the server keyword list <kwl> as a list of valid keywords for next |
| 504 | * parsing sessions. |
| 505 | */ |
| 506 | void srv_register_keywords(struct srv_kw_list *kwl) |
| 507 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 508 | LIST_APPEND(&srv_keywords.list, &kwl->list); |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* Return a pointer to the server keyword <kw>, or NULL if not found. If the |
| 512 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 513 | * find one with a valid ->parse() function. This way it is possible to declare |
| 514 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 515 | * if some options are met. Note that if the requested keyword contains an |
| 516 | * opening parenthesis, everything from this point is ignored. |
| 517 | */ |
| 518 | struct srv_kw *srv_find_kw(const char *kw) |
| 519 | { |
| 520 | int index; |
| 521 | const char *kwend; |
| 522 | struct srv_kw_list *kwl; |
| 523 | struct srv_kw *ret = NULL; |
| 524 | |
| 525 | kwend = strchr(kw, '('); |
| 526 | if (!kwend) |
| 527 | kwend = kw + strlen(kw); |
| 528 | |
| 529 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 530 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 531 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 532 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 533 | if (kwl->kw[index].parse) |
| 534 | return &kwl->kw[index]; /* found it !*/ |
| 535 | else |
| 536 | ret = &kwl->kw[index]; /* may be OK */ |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /* Dumps all registered "server" keywords to the <out> string pointer. The |
| 544 | * unsupported keywords are only dumped if their supported form was not |
| 545 | * found. |
| 546 | */ |
| 547 | void srv_dump_kws(char **out) |
| 548 | { |
| 549 | struct srv_kw_list *kwl; |
| 550 | int index; |
| 551 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 552 | if (!out) |
| 553 | return; |
| 554 | |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 555 | *out = NULL; |
| 556 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 557 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 558 | if (kwl->kw[index].parse || |
| 559 | srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 560 | memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "", |
| 561 | kwl->scope, |
| 562 | kwl->kw[index].kw, |
| 563 | kwl->kw[index].skip ? " <arg>" : "", |
| 564 | kwl->kw[index].default_ok ? " [dflt_ok]" : "", |
| 565 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 566 | } |
| 567 | } |
| 568 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* Try to find in srv_keyword the word that looks closest to <word> by counting |
| 572 | * transitions between letters, digits and other characters. Will return the |
| 573 | * best matching word if found, otherwise NULL. An optional array of extra |
| 574 | * words to compare may be passed in <extra>, but it must then be terminated |
| 575 | * by a NULL entry. If unused it may be NULL. |
| 576 | */ |
| 577 | static const char *srv_find_best_kw(const char *word) |
| 578 | { |
| 579 | uint8_t word_sig[1024]; |
| 580 | uint8_t list_sig[1024]; |
| 581 | const struct srv_kw_list *kwl; |
| 582 | const char *best_ptr = NULL; |
| 583 | int dist, best_dist = INT_MAX; |
| 584 | const char **extra; |
| 585 | int index; |
| 586 | |
| 587 | make_word_fingerprint(word_sig, word); |
| 588 | list_for_each_entry(kwl, &srv_keywords.list, list) { |
| 589 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 590 | make_word_fingerprint(list_sig, kwl->kw[index].kw); |
| 591 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 592 | if (dist < best_dist) { |
| 593 | best_dist = dist; |
| 594 | best_ptr = kwl->kw[index].kw; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 599 | for (extra = extra_kw_list; *extra; extra++) { |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 600 | make_word_fingerprint(list_sig, *extra); |
| 601 | dist = word_fingerprint_distance(word_sig, list_sig); |
| 602 | if (dist < best_dist) { |
| 603 | best_dist = dist; |
| 604 | best_ptr = *extra; |
| 605 | } |
Willy Tarreau | 49c2b45 | 2021-03-12 09:58:04 +0100 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr))) |
| 609 | best_ptr = NULL; |
| 610 | |
| 611 | return best_ptr; |
Willy Tarreau | 21faa91 | 2012-10-10 08:27:36 +0200 | [diff] [blame] | 612 | } |
Krzysztof Piotr Oledzki | 5259dfe | 2008-01-21 01:54:06 +0100 | [diff] [blame] | 613 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 614 | /* Parse the "backup" server keyword */ |
| 615 | static int srv_parse_backup(char **args, int *cur_arg, |
| 616 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 617 | { |
| 618 | newsrv->flags |= SRV_F_BACKUP; |
| 619 | return 0; |
| 620 | } |
| 621 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 622 | |
Frédéric Lécaille | 9d1b95b | 2017-03-15 09:13:33 +0100 | [diff] [blame] | 623 | /* Parse the "cookie" server keyword */ |
| 624 | static int srv_parse_cookie(char **args, int *cur_arg, |
| 625 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 626 | { |
| 627 | char *arg; |
| 628 | |
| 629 | arg = args[*cur_arg + 1]; |
| 630 | if (!*arg) { |
| 631 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 632 | return ERR_ALERT | ERR_FATAL; |
| 633 | } |
| 634 | |
| 635 | free(newsrv->cookie); |
| 636 | newsrv->cookie = strdup(arg); |
| 637 | newsrv->cklen = strlen(arg); |
| 638 | newsrv->flags |= SRV_F_COOKIESET; |
| 639 | return 0; |
| 640 | } |
| 641 | |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 642 | /* Parse the "disabled" server keyword */ |
| 643 | static int srv_parse_disabled(char **args, int *cur_arg, |
| 644 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 645 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 646 | newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 647 | newsrv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 648 | newsrv->check.state |= CHK_ST_PAUSED; |
| 649 | newsrv->check.health = 0; |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* Parse the "enabled" server keyword */ |
| 654 | static int srv_parse_enabled(char **args, int *cur_arg, |
| 655 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 656 | { |
Amaury Denoyelle | e4308e9 | 2024-03-29 17:47:23 +0100 | [diff] [blame] | 657 | if (newsrv->flags & SRV_F_DYNAMIC) { |
| 658 | ha_warning("Keyword 'enabled' is ignored for dynamic servers. It will be rejected from 3.0 onward."); |
| 659 | return 0; |
| 660 | } |
| 661 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 662 | newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT; |
| 663 | newsrv->next_state = SRV_ST_RUNNING; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 664 | newsrv->check.state &= ~CHK_ST_PAUSED; |
| 665 | newsrv->check.health = newsrv->check.rise; |
| 666 | return 0; |
| 667 | } |
| 668 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 669 | /* Parse the "error-limit" server keyword */ |
| 670 | static int srv_parse_error_limit(char **args, int *cur_arg, |
| 671 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 672 | { |
| 673 | if (!*args[*cur_arg + 1]) { |
| 674 | memprintf(err, "'%s' expects an integer argument.", |
| 675 | args[*cur_arg]); |
| 676 | return ERR_ALERT | ERR_FATAL; |
| 677 | } |
| 678 | |
| 679 | newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]); |
| 680 | |
| 681 | if (newsrv->consecutive_errors_limit <= 0) { |
| 682 | memprintf(err, "%s has to be > 0.", |
| 683 | args[*cur_arg]); |
| 684 | return ERR_ALERT | ERR_FATAL; |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
Amaury Denoyelle | f9d5957 | 2021-10-18 14:40:29 +0200 | [diff] [blame] | 690 | /* Parse the "ws" keyword */ |
| 691 | static int srv_parse_ws(char **args, int *cur_arg, |
| 692 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 693 | { |
| 694 | if (!args[*cur_arg + 1]) { |
| 695 | memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]); |
| 696 | return ERR_ALERT | ERR_FATAL; |
| 697 | } |
| 698 | |
| 699 | if (strcmp(args[*cur_arg + 1], "h1") == 0) { |
| 700 | newsrv->ws = SRV_WS_H1; |
| 701 | } |
| 702 | else if (strcmp(args[*cur_arg + 1], "h2") == 0) { |
| 703 | newsrv->ws = SRV_WS_H2; |
| 704 | } |
| 705 | else if (strcmp(args[*cur_arg + 1], "auto") == 0) { |
| 706 | newsrv->ws = SRV_WS_AUTO; |
| 707 | } |
| 708 | else { |
| 709 | memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]); |
| 710 | return ERR_ALERT | ERR_FATAL; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 717 | /* Parse the "init-addr" server keyword */ |
| 718 | static int srv_parse_init_addr(char **args, int *cur_arg, |
| 719 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 720 | { |
| 721 | char *p, *end; |
| 722 | int done; |
| 723 | struct sockaddr_storage sa; |
| 724 | |
| 725 | newsrv->init_addr_methods = 0; |
| 726 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 727 | |
| 728 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 729 | /* cut on next comma */ |
| 730 | for (end = p; *end && *end != ','; end++); |
| 731 | if (*end) |
| 732 | *(end++) = 0; |
| 733 | |
| 734 | memset(&sa, 0, sizeof(sa)); |
| 735 | if (strcmp(p, "libc") == 0) { |
| 736 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 737 | } |
| 738 | else if (strcmp(p, "last") == 0) { |
| 739 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 740 | } |
| 741 | else if (strcmp(p, "none") == 0) { |
| 742 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 743 | } |
| 744 | else if (str2ip2(p, &sa, 0)) { |
| 745 | if (is_addr(&newsrv->init_addr)) { |
| 746 | memprintf(err, "'%s' : initial address already specified, cannot add '%s'.", |
| 747 | args[*cur_arg], p); |
| 748 | return ERR_ALERT | ERR_FATAL; |
| 749 | } |
| 750 | newsrv->init_addr = sa; |
| 751 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 752 | } |
| 753 | else { |
| 754 | memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.", |
| 755 | args[*cur_arg], p); |
| 756 | return ERR_ALERT | ERR_FATAL; |
| 757 | } |
| 758 | if (!done) { |
| 759 | memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'", |
| 760 | args[*cur_arg], p); |
| 761 | return ERR_ALERT | ERR_FATAL; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | /* Parse the "log-proto" server keyword */ |
| 769 | static int srv_parse_log_proto(char **args, int *cur_arg, |
| 770 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 771 | { |
| 772 | if (strcmp(args[*cur_arg + 1], "legacy") == 0) |
| 773 | newsrv->log_proto = SRV_LOG_PROTO_LEGACY; |
| 774 | else if (strcmp(args[*cur_arg + 1], "octet-count") == 0) |
| 775 | newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING; |
| 776 | else { |
| 777 | memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'", |
| 778 | args[*cur_arg], args[*cur_arg + 1]); |
| 779 | return ERR_ALERT | ERR_FATAL; |
| 780 | } |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | /* Parse the "maxconn" server keyword */ |
| 786 | static int srv_parse_maxconn(char **args, int *cur_arg, |
| 787 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 788 | { |
| 789 | newsrv->maxconn = atol(args[*cur_arg + 1]); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /* Parse the "maxqueue" server keyword */ |
| 794 | static int srv_parse_maxqueue(char **args, int *cur_arg, |
| 795 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 796 | { |
| 797 | newsrv->maxqueue = atol(args[*cur_arg + 1]); |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | /* Parse the "minconn" server keyword */ |
| 802 | static int srv_parse_minconn(char **args, int *cur_arg, |
| 803 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 804 | { |
| 805 | newsrv->minconn = atol(args[*cur_arg + 1]); |
| 806 | return 0; |
| 807 | } |
| 808 | |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 809 | static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 810 | { |
| 811 | char *arg; |
| 812 | |
| 813 | arg = args[*cur_arg + 1]; |
| 814 | if (!*arg) { |
| 815 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 816 | return ERR_ALERT | ERR_FATAL; |
| 817 | } |
| 818 | newsrv->max_reuse = atoi(arg); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 823 | 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] | 824 | { |
| 825 | const char *res; |
| 826 | char *arg; |
| 827 | unsigned int time; |
| 828 | |
| 829 | arg = args[*cur_arg + 1]; |
| 830 | if (!*arg) { |
| 831 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 832 | return ERR_ALERT | ERR_FATAL; |
| 833 | } |
| 834 | res = parse_time_err(arg, &time, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 835 | if (res == PARSE_TIME_OVER) { |
| 836 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 837 | args[*cur_arg+1], args[*cur_arg]); |
| 838 | return ERR_ALERT | ERR_FATAL; |
| 839 | } |
| 840 | else if (res == PARSE_TIME_UNDER) { |
| 841 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 842 | args[*cur_arg+1], args[*cur_arg]); |
| 843 | return ERR_ALERT | ERR_FATAL; |
| 844 | } |
| 845 | else if (res) { |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 846 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", |
| 847 | *res, args[*cur_arg]); |
| 848 | return ERR_ALERT | ERR_FATAL; |
| 849 | } |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 850 | newsrv->pool_purge_delay = time; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 855 | static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 856 | { |
| 857 | char *arg; |
| 858 | |
| 859 | arg = args[*cur_arg + 1]; |
| 860 | if (!*arg) { |
| 861 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 862 | return ERR_ALERT | ERR_FATAL; |
| 863 | } |
| 864 | |
| 865 | newsrv->low_idle_conns = atoi(arg); |
| 866 | return 0; |
| 867 | } |
| 868 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 869 | static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 870 | { |
| 871 | char *arg; |
| 872 | |
| 873 | arg = args[*cur_arg + 1]; |
| 874 | if (!*arg) { |
| 875 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 876 | return ERR_ALERT | ERR_FATAL; |
| 877 | } |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 878 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 879 | newsrv->max_idle_conns = atoi(arg); |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 880 | if ((int)newsrv->max_idle_conns < -1) { |
| 881 | memprintf(err, "'%s' must be >= -1", args[*cur_arg]); |
| 882 | return ERR_ALERT | ERR_FATAL; |
| 883 | } |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 888 | /* parse the "id" server keyword */ |
| 889 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 890 | { |
| 891 | struct eb32_node *node; |
| 892 | |
| 893 | if (!*args[*cur_arg + 1]) { |
| 894 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 895 | return ERR_ALERT | ERR_FATAL; |
| 896 | } |
| 897 | |
| 898 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 899 | newsrv->conf.id.key = newsrv->puid; |
| 900 | |
| 901 | if (newsrv->puid <= 0) { |
| 902 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 903 | return ERR_ALERT | ERR_FATAL; |
| 904 | } |
| 905 | |
| 906 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 907 | if (node) { |
| 908 | struct server *target = container_of(node, struct server, conf.id); |
| 909 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 910 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 911 | target->id); |
| 912 | return ERR_ALERT | ERR_FATAL; |
| 913 | } |
| 914 | |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 915 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 916 | return 0; |
| 917 | } |
| 918 | |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 919 | /* Parse the "namespace" server keyword */ |
| 920 | static int srv_parse_namespace(char **args, int *cur_arg, |
| 921 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 922 | { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 923 | #ifdef USE_NS |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 924 | char *arg; |
| 925 | |
| 926 | arg = args[*cur_arg + 1]; |
| 927 | if (!*arg) { |
| 928 | memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]); |
| 929 | return ERR_ALERT | ERR_FATAL; |
| 930 | } |
| 931 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 932 | if (strcmp(arg, "*") == 0) { |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 933 | /* Use the namespace associated with the connection (if present). */ |
| 934 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * As this parser may be called several times for the same 'default-server' |
| 940 | * object, or for a new 'server' instance deriving from a 'default-server' |
| 941 | * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it. |
| 942 | */ |
| 943 | newsrv->flags &= ~SRV_F_USE_NS_FROM_PP; |
| 944 | |
| 945 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 946 | if (!newsrv->netns) |
| 947 | newsrv->netns = netns_store_insert(arg); |
| 948 | |
| 949 | if (!newsrv->netns) { |
| 950 | memprintf(err, "Cannot open namespace '%s'", arg); |
| 951 | return ERR_ALERT | ERR_FATAL; |
| 952 | } |
| 953 | |
| 954 | return 0; |
| 955 | #else |
| 956 | memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]); |
| 957 | return ERR_ALERT | ERR_FATAL; |
| 958 | #endif |
| 959 | } |
| 960 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 961 | /* Parse the "no-backup" server keyword */ |
| 962 | static int srv_parse_no_backup(char **args, int *cur_arg, |
| 963 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 964 | { |
| 965 | newsrv->flags &= ~SRV_F_BACKUP; |
| 966 | return 0; |
| 967 | } |
| 968 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 969 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 970 | /* Disable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 971 | 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] | 972 | { |
| 973 | srv->pp_opts &= ~flags; |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | /* Parse the "no-send-proxy" server keyword */ |
| 978 | static int srv_parse_no_send_proxy(char **args, int *cur_arg, |
| 979 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 980 | { |
| 981 | return srv_disable_pp_flags(newsrv, SRV_PP_V1); |
| 982 | } |
| 983 | |
| 984 | /* Parse the "no-send-proxy-v2" server keyword */ |
| 985 | static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg, |
| 986 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 987 | { |
| 988 | return srv_disable_pp_flags(newsrv, SRV_PP_V2); |
| 989 | } |
| 990 | |
Frédéric Lécaille | 36d1565 | 2022-10-17 14:58:19 +0200 | [diff] [blame] | 991 | /* Parse the "shard" server keyword */ |
| 992 | static int srv_parse_shard(char **args, int *cur_arg, |
| 993 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 994 | { |
| 995 | newsrv->shard = atol(args[*cur_arg + 1]); |
| 996 | return 0; |
| 997 | } |
| 998 | |
Frédéric Lécaille | 1b9423d | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 999 | /* Parse the "no-tfo" server keyword */ |
| 1000 | static int srv_parse_no_tfo(char **args, int *cur_arg, |
| 1001 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1002 | { |
| 1003 | newsrv->flags &= ~SRV_F_FASTOPEN; |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1007 | /* Parse the "non-stick" server keyword */ |
| 1008 | static int srv_parse_non_stick(char **args, int *cur_arg, |
| 1009 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1010 | { |
| 1011 | newsrv->flags |= SRV_F_NON_STICK; |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1015 | /* Enable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 1016 | 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] | 1017 | { |
| 1018 | srv->pp_opts |= flags; |
| 1019 | return 0; |
| 1020 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1021 | /* parse the "proto" server keyword */ |
| 1022 | static int srv_parse_proto(char **args, int *cur_arg, |
| 1023 | struct proxy *px, struct server *newsrv, char **err) |
| 1024 | { |
| 1025 | struct ist proto; |
| 1026 | |
| 1027 | if (!*args[*cur_arg + 1]) { |
| 1028 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 1029 | return ERR_ALERT | ERR_FATAL; |
| 1030 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 1031 | proto = ist(args[*cur_arg + 1]); |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1032 | newsrv->mux_proto = get_mux_proto(proto); |
| 1033 | if (!newsrv->mux_proto) { |
| 1034 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 1035 | return ERR_ALERT | ERR_FATAL; |
| 1036 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1039 | |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1040 | /* parse the "proxy-v2-options" */ |
| 1041 | static int srv_parse_proxy_v2_options(char **args, int *cur_arg, |
| 1042 | struct proxy *px, struct server *newsrv, char **err) |
| 1043 | { |
| 1044 | char *p, *n; |
| 1045 | for (p = args[*cur_arg+1]; p; p = n) { |
| 1046 | n = strchr(p, ','); |
| 1047 | if (n) |
| 1048 | *n++ = '\0'; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1049 | if (strcmp(p, "ssl") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1050 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1051 | } else if (strcmp(p, "cert-cn") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1052 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1053 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1054 | } else if (strcmp(p, "cert-key") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1055 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1056 | newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1057 | } else if (strcmp(p, "cert-sig") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1058 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1059 | newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1060 | } else if (strcmp(p, "ssl-cipher") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1061 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1062 | newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1063 | } else if (strcmp(p, "authority") == 0) { |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1064 | newsrv->pp_opts |= SRV_PP_V2_AUTHORITY; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1065 | } else if (strcmp(p, "crc32c") == 0) { |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1066 | newsrv->pp_opts |= SRV_PP_V2_CRC32C; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1067 | } else if (strcmp(p, "unique-id") == 0) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1068 | newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID; |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1069 | } else |
| 1070 | goto fail; |
| 1071 | } |
| 1072 | return 0; |
| 1073 | fail: |
| 1074 | if (err) |
| 1075 | memprintf(err, "'%s' : proxy v2 option not implemented", p); |
| 1076 | return ERR_ALERT | ERR_FATAL; |
| 1077 | } |
| 1078 | |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1079 | /* Parse the "observe" server keyword */ |
| 1080 | static int srv_parse_observe(char **args, int *cur_arg, |
| 1081 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1082 | { |
| 1083 | char *arg; |
| 1084 | |
| 1085 | arg = args[*cur_arg + 1]; |
| 1086 | if (!*arg) { |
| 1087 | memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]); |
| 1088 | return ERR_ALERT | ERR_FATAL; |
| 1089 | } |
| 1090 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1091 | if (strcmp(arg, "none") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1092 | newsrv->observe = HANA_OBS_NONE; |
| 1093 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1094 | else if (strcmp(arg, "layer4") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1095 | newsrv->observe = HANA_OBS_LAYER4; |
| 1096 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1097 | else if (strcmp(arg, "layer7") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1098 | if (curproxy->mode != PR_MODE_HTTP) { |
| 1099 | memprintf(err, "'%s' can only be used in http proxies.\n", arg); |
| 1100 | return ERR_ALERT; |
| 1101 | } |
| 1102 | newsrv->observe = HANA_OBS_LAYER7; |
| 1103 | } |
| 1104 | else { |
| 1105 | memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' " |
| 1106 | "but got '%s'\n", args[*cur_arg], arg); |
| 1107 | return ERR_ALERT | ERR_FATAL; |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1113 | /* Parse the "on-error" server keyword */ |
| 1114 | static int srv_parse_on_error(char **args, int *cur_arg, |
| 1115 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1116 | { |
| 1117 | if (strcmp(args[*cur_arg + 1], "fastinter") == 0) |
| 1118 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 1119 | else if (strcmp(args[*cur_arg + 1], "fail-check") == 0) |
| 1120 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 1121 | else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0) |
| 1122 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 1123 | else if (strcmp(args[*cur_arg + 1], "mark-down") == 0) |
| 1124 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 1125 | else { |
| 1126 | memprintf(err, "'%s' expects one of 'fastinter', " |
| 1127 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'", |
| 1128 | args[*cur_arg], args[*cur_arg + 1]); |
| 1129 | return ERR_ALERT | ERR_FATAL; |
| 1130 | } |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | /* Parse the "on-marked-down" server keyword */ |
| 1136 | static int srv_parse_on_marked_down(char **args, int *cur_arg, |
| 1137 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1138 | { |
| 1139 | if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0) |
| 1140 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1141 | else { |
| 1142 | memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'", |
| 1143 | args[*cur_arg], args[*cur_arg + 1]); |
| 1144 | return ERR_ALERT | ERR_FATAL; |
| 1145 | } |
| 1146 | |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | /* Parse the "on-marked-up" server keyword */ |
| 1151 | static int srv_parse_on_marked_up(char **args, int *cur_arg, |
| 1152 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1153 | { |
| 1154 | if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0) |
| 1155 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1156 | else { |
| 1157 | memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'", |
| 1158 | args[*cur_arg], args[*cur_arg + 1]); |
| 1159 | return ERR_ALERT | ERR_FATAL; |
| 1160 | } |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 1165 | /* Parse the "redir" server keyword */ |
| 1166 | static int srv_parse_redir(char **args, int *cur_arg, |
| 1167 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1168 | { |
| 1169 | char *arg; |
| 1170 | |
| 1171 | arg = args[*cur_arg + 1]; |
| 1172 | if (!*arg) { |
| 1173 | memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]); |
| 1174 | return ERR_ALERT | ERR_FATAL; |
| 1175 | } |
| 1176 | |
| 1177 | free(newsrv->rdr_pfx); |
| 1178 | newsrv->rdr_pfx = strdup(arg); |
| 1179 | newsrv->rdr_len = strlen(arg); |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1184 | /* Parse the "resolvers" server keyword */ |
| 1185 | static int srv_parse_resolvers(char **args, int *cur_arg, |
| 1186 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1187 | { |
| 1188 | free(newsrv->resolvers_id); |
| 1189 | newsrv->resolvers_id = strdup(args[*cur_arg + 1]); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* Parse the "resolve-net" server keyword */ |
| 1194 | static int srv_parse_resolve_net(char **args, int *cur_arg, |
| 1195 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1196 | { |
| 1197 | char *p, *e; |
| 1198 | unsigned char mask; |
| 1199 | struct resolv_options *opt; |
| 1200 | |
| 1201 | if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') { |
| 1202 | memprintf(err, "'%s' expects a list of networks.", |
| 1203 | args[*cur_arg]); |
| 1204 | return ERR_ALERT | ERR_FATAL; |
| 1205 | } |
| 1206 | |
| 1207 | opt = &newsrv->resolv_opts; |
| 1208 | |
| 1209 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 1210 | * string network in in_addr or in6_addr. |
| 1211 | */ |
| 1212 | p = args[*cur_arg + 1]; |
| 1213 | e = p; |
| 1214 | while (*p != '\0') { |
| 1215 | /* If no room available, return error. */ |
| 1216 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
| 1217 | memprintf(err, "'%s' exceed %d networks.", |
| 1218 | args[*cur_arg], SRV_MAX_PREF_NET); |
| 1219 | return ERR_ALERT | ERR_FATAL; |
| 1220 | } |
| 1221 | /* look for end or comma. */ |
| 1222 | while (*e != ',' && *e != '\0') |
| 1223 | e++; |
| 1224 | if (*e == ',') { |
| 1225 | *e = '\0'; |
| 1226 | e++; |
| 1227 | } |
| 1228 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 1229 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 1230 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 1231 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 1232 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 1233 | &mask)) { |
| 1234 | /* Try to convert input string from ipv6 network. */ |
| 1235 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 1236 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 1237 | } else { |
| 1238 | /* All network conversions fail, return error. */ |
| 1239 | memprintf(err, "'%s' invalid network '%s'.", |
| 1240 | args[*cur_arg], p); |
| 1241 | return ERR_ALERT | ERR_FATAL; |
| 1242 | } |
| 1243 | opt->pref_net_nb++; |
| 1244 | p = e; |
| 1245 | } |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /* Parse the "resolve-opts" server keyword */ |
| 1251 | static int srv_parse_resolve_opts(char **args, int *cur_arg, |
| 1252 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1253 | { |
| 1254 | char *p, *end; |
| 1255 | |
| 1256 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 1257 | /* cut on next comma */ |
| 1258 | for (end = p; *end && *end != ','; end++); |
| 1259 | if (*end) |
| 1260 | *(end++) = 0; |
| 1261 | |
| 1262 | if (strcmp(p, "allow-dup-ip") == 0) { |
| 1263 | newsrv->resolv_opts.accept_duplicate_ip = 1; |
| 1264 | } |
| 1265 | else if (strcmp(p, "ignore-weight") == 0) { |
| 1266 | newsrv->resolv_opts.ignore_weight = 1; |
| 1267 | } |
| 1268 | else if (strcmp(p, "prevent-dup-ip") == 0) { |
| 1269 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
| 1270 | } |
| 1271 | else { |
| 1272 | memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.", |
| 1273 | args[*cur_arg], p); |
| 1274 | return ERR_ALERT | ERR_FATAL; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | /* Parse the "resolve-prefer" server keyword */ |
| 1282 | static int srv_parse_resolve_prefer(char **args, int *cur_arg, |
| 1283 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1284 | { |
| 1285 | if (strcmp(args[*cur_arg + 1], "ipv4") == 0) |
| 1286 | newsrv->resolv_opts.family_prio = AF_INET; |
| 1287 | else if (strcmp(args[*cur_arg + 1], "ipv6") == 0) |
| 1288 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 1289 | else { |
| 1290 | memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.", |
| 1291 | args[*cur_arg]); |
| 1292 | return ERR_ALERT | ERR_FATAL; |
| 1293 | } |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1298 | /* Parse the "send-proxy" server keyword */ |
| 1299 | static int srv_parse_send_proxy(char **args, int *cur_arg, |
| 1300 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1301 | { |
| 1302 | return srv_enable_pp_flags(newsrv, SRV_PP_V1); |
| 1303 | } |
| 1304 | |
| 1305 | /* Parse the "send-proxy-v2" server keyword */ |
| 1306 | static int srv_parse_send_proxy_v2(char **args, int *cur_arg, |
| 1307 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1308 | { |
| 1309 | return srv_enable_pp_flags(newsrv, SRV_PP_V2); |
| 1310 | } |
| 1311 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1312 | /* Parse the "slowstart" server keyword */ |
| 1313 | static int srv_parse_slowstart(char **args, int *cur_arg, |
| 1314 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1315 | { |
| 1316 | /* slowstart is stored in seconds */ |
| 1317 | unsigned int val; |
| 1318 | const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS); |
| 1319 | |
| 1320 | if (time_err == PARSE_TIME_OVER) { |
| 1321 | memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1322 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1323 | return ERR_ALERT | ERR_FATAL; |
| 1324 | } |
| 1325 | else if (time_err == PARSE_TIME_UNDER) { |
| 1326 | memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1327 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1328 | return ERR_ALERT | ERR_FATAL; |
| 1329 | } |
| 1330 | else if (time_err) { |
| 1331 | memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.", |
| 1332 | *time_err, newsrv->id); |
| 1333 | return ERR_ALERT | ERR_FATAL; |
| 1334 | } |
| 1335 | newsrv->slowstart = (val + 999) / 1000; |
| 1336 | |
| 1337 | return 0; |
| 1338 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1339 | |
| 1340 | /* Parse the "source" server keyword */ |
| 1341 | static int srv_parse_source(char **args, int *cur_arg, |
| 1342 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1343 | { |
| 1344 | char *errmsg; |
| 1345 | int port_low, port_high; |
| 1346 | struct sockaddr_storage *sk; |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1347 | |
| 1348 | errmsg = NULL; |
| 1349 | |
| 1350 | if (!*args[*cur_arg + 1]) { |
| 1351 | memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, " |
| 1352 | "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface"); |
| 1353 | goto err; |
| 1354 | } |
| 1355 | |
| 1356 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1357 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1358 | &errmsg, NULL, NULL, |
| 1359 | 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] | 1360 | if (!sk) { |
| 1361 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1362 | goto err; |
| 1363 | } |
| 1364 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1365 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1366 | newsrv->conn_src.source_addr = *sk; |
| 1367 | |
| 1368 | if (port_low != port_high) { |
| 1369 | int i; |
| 1370 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1371 | 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] | 1372 | if (!newsrv->conn_src.sport_range) { |
| 1373 | ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]); |
| 1374 | goto err; |
| 1375 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1376 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1377 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1378 | } |
| 1379 | |
| 1380 | *cur_arg += 2; |
| 1381 | while (*(args[*cur_arg])) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1382 | 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] | 1383 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 1384 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1385 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 1386 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1387 | goto err; |
| 1388 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1389 | if (strcmp(args[*cur_arg + 1], "client") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1390 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1391 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1392 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1393 | else if (strcmp(args[*cur_arg + 1], "clientip") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1394 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1395 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1396 | } |
| 1397 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 1398 | char *name, *end; |
| 1399 | |
| 1400 | name = args[*cur_arg + 1] + 7; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1401 | while (isspace((unsigned char)*name)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1402 | name++; |
| 1403 | |
| 1404 | end = name; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1405 | while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')') |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1406 | end++; |
| 1407 | |
| 1408 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1409 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1410 | free(newsrv->conn_src.bind_hdr_name); |
| 1411 | 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] | 1412 | if (!newsrv->conn_src.bind_hdr_name) { |
| 1413 | ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]); |
| 1414 | goto err; |
| 1415 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1416 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1417 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1418 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 1419 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1420 | |
| 1421 | /* now look for an occurrence number */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1422 | while (isspace((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1423 | end++; |
| 1424 | if (*end == ',') { |
| 1425 | end++; |
| 1426 | name = end; |
| 1427 | if (*end == '-') |
| 1428 | end++; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1429 | while (isdigit((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1430 | end++; |
| 1431 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 1432 | } |
| 1433 | |
| 1434 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1435 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 1436 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1437 | goto err; |
| 1438 | } |
| 1439 | } |
| 1440 | else { |
| 1441 | struct sockaddr_storage *sk; |
| 1442 | int port1, port2; |
| 1443 | |
| 1444 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1445 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, |
| 1446 | &errmsg, NULL, NULL, |
| 1447 | 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] | 1448 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1449 | 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] | 1450 | goto err; |
| 1451 | } |
| 1452 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1453 | newsrv->conn_src.tproxy_addr = *sk; |
| 1454 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1455 | } |
| 1456 | global.last_checks |= LSTCHK_NETADM; |
| 1457 | *cur_arg += 2; |
| 1458 | continue; |
| 1459 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1460 | 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] | 1461 | goto err; |
| 1462 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 1463 | } /* "usesrc" */ |
| 1464 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1465 | 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] | 1466 | #ifdef SO_BINDTODEVICE |
| 1467 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1468 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1469 | goto err; |
| 1470 | } |
| 1471 | free(newsrv->conn_src.iface_name); |
| 1472 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 1473 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1474 | global.last_checks |= LSTCHK_NETADM; |
| 1475 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1476 | 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] | 1477 | goto err; |
| 1478 | #endif |
| 1479 | *cur_arg += 2; |
| 1480 | continue; |
| 1481 | } |
| 1482 | /* this keyword in not an option of "source" */ |
| 1483 | break; |
| 1484 | } /* while */ |
| 1485 | |
| 1486 | return 0; |
| 1487 | |
| 1488 | err: |
| 1489 | free(errmsg); |
| 1490 | return ERR_ALERT | ERR_FATAL; |
| 1491 | } |
| 1492 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1493 | /* Parse the "stick" server keyword */ |
| 1494 | static int srv_parse_stick(char **args, int *cur_arg, |
| 1495 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1496 | { |
| 1497 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1501 | /* Parse the "track" server keyword */ |
| 1502 | static int srv_parse_track(char **args, int *cur_arg, |
| 1503 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1504 | { |
| 1505 | char *arg; |
| 1506 | |
| 1507 | arg = args[*cur_arg + 1]; |
| 1508 | if (!*arg) { |
| 1509 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 1510 | return ERR_ALERT | ERR_FATAL; |
| 1511 | } |
| 1512 | |
| 1513 | free(newsrv->trackit); |
| 1514 | newsrv->trackit = strdup(arg); |
| 1515 | |
| 1516 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /* Parse the "socks4" server keyword */ |
| 1520 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 1521 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1522 | { |
| 1523 | char *errmsg; |
| 1524 | int port_low, port_high; |
| 1525 | struct sockaddr_storage *sk; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1526 | |
| 1527 | errmsg = NULL; |
| 1528 | |
| 1529 | if (!*args[*cur_arg + 1]) { |
| 1530 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 1531 | goto err; |
| 1532 | } |
| 1533 | |
| 1534 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1535 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1536 | &errmsg, NULL, NULL, |
| 1537 | 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] | 1538 | if (!sk) { |
| 1539 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1540 | goto err; |
| 1541 | } |
| 1542 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1543 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 1544 | newsrv->socks4_addr = *sk; |
| 1545 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1546 | return 0; |
| 1547 | |
| 1548 | err: |
| 1549 | free(errmsg); |
| 1550 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1551 | } |
| 1552 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 1554 | /* parse the "tfo" server keyword */ |
| 1555 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1556 | { |
| 1557 | newsrv->flags |= SRV_F_FASTOPEN; |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1561 | /* parse the "usesrc" server keyword */ |
| 1562 | static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1563 | { |
| 1564 | memprintf(err, "'%s' only allowed after a '%s' statement.", |
| 1565 | "usesrc", "source"); |
| 1566 | return ERR_ALERT | ERR_FATAL; |
| 1567 | } |
| 1568 | |
| 1569 | /* parse the "weight" server keyword */ |
| 1570 | static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1571 | { |
| 1572 | int w; |
| 1573 | |
| 1574 | w = atol(args[*cur_arg + 1]); |
| 1575 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1576 | memprintf(err, "weight of server %s is not within 0 and %d (%d).", |
| 1577 | newsrv->id, SRV_UWGHT_MAX, w); |
| 1578 | return ERR_ALERT | ERR_FATAL; |
| 1579 | } |
| 1580 | newsrv->uweight = newsrv->iweight = w; |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
Aurelien DARRAGON | 4bc0909 | 2023-09-21 14:18:50 +0200 | [diff] [blame] | 1585 | /* Returns 1 if the server has streams pointing to it, and 0 otherwise. |
| 1586 | * |
| 1587 | * Must be called with the server lock held. |
| 1588 | */ |
| 1589 | static int srv_has_streams(struct server *srv) |
| 1590 | { |
| 1591 | int thr; |
| 1592 | |
| 1593 | for (thr = 0; thr < global.nbthread; thr++) |
| 1594 | if (!MT_LIST_ISEMPTY(&srv->per_thr[thr].streams)) |
| 1595 | return 1; |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1599 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1600 | * 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] | 1601 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1602 | * |
| 1603 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1604 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1605 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1606 | { |
Willy Tarreau | 751153e | 2021-02-17 13:33:24 +0100 | [diff] [blame] | 1607 | struct stream *stream; |
| 1608 | struct mt_list *elt1, elt2; |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1609 | int thr; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1610 | |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1611 | for (thr = 0; thr < global.nbthread; thr++) |
| 1612 | mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2) |
| 1613 | if (stream->srv_conn == srv) |
| 1614 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /* 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] | 1618 | * 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] | 1619 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1620 | * |
| 1621 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1622 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1623 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1624 | { |
| 1625 | struct server *srv; |
| 1626 | |
| 1627 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1628 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1629 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1632 | static void srv_append_op_chg_cause(struct buffer *msg, struct server *s, enum srv_op_st_chg_cause cause) |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1633 | { |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1634 | switch (cause) { |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1635 | case SRV_OP_STCHGC_NONE: |
| 1636 | break; /* do nothing */ |
| 1637 | case SRV_OP_STCHGC_HEALTH: |
| 1638 | check_append_info(msg, &s->check); |
| 1639 | break; |
| 1640 | case SRV_OP_STCHGC_AGENT: |
| 1641 | check_append_info(msg, &s->agent); |
| 1642 | break; |
| 1643 | default: |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1644 | chunk_appendf(msg, ", %s", srv_op_st_chg_cause(cause)); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1645 | break; |
| 1646 | } |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1649 | static void srv_append_adm_chg_cause(struct buffer *msg, struct server *s, enum srv_adm_st_chg_cause cause) |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1650 | { |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1651 | if (cause) |
| 1652 | chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(cause)); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /* Appends some information to a message string related to a server tracking |
| 1656 | * or requeued connections info. |
| 1657 | * |
| 1658 | * If <forced> is null and the server tracks another one, a "via" |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1659 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1660 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1661 | * |
| 1662 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1663 | */ |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1664 | static void srv_append_more(struct buffer *msg, struct server *s, |
| 1665 | int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1666 | { |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1667 | if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1668 | 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] | 1669 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1670 | |
| 1671 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1672 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1673 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1674 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1675 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1676 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1677 | s->cur_sess, xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1678 | else |
| 1679 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1680 | " %d sessions requeued, %d total in queue", |
| 1681 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1682 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1683 | xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | |
Aurelien DARRAGON | 10518c0 | 2023-04-19 10:33:02 +0200 | [diff] [blame] | 1687 | /* Marks server <s> down, regardless of its checks' statuses. The server |
| 1688 | * transfers queued streams whenever possible to other servers at a sync |
| 1689 | * point. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1690 | * |
| 1691 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1692 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1693 | 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] | 1694 | { |
| 1695 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1696 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1697 | 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] | 1698 | return; |
| 1699 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1700 | s->next_state = SRV_ST_STOPPED; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1701 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1702 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1703 | srv_update_status(s, 0, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1704 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1705 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1706 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1707 | srv_set_stopped(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1708 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1709 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1712 | /* Marks server <s> up regardless of its checks' statuses and provided it isn't |
Aurelien DARRAGON | 10518c0 | 2023-04-19 10:33:02 +0200 | [diff] [blame] | 1713 | * in maintenance. The server tries to grab requests from the proxy at a sync |
| 1714 | * point. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1715 | * |
| 1716 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1717 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1718 | 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] | 1719 | { |
| 1720 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1721 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1722 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1723 | return; |
| 1724 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1725 | 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] | 1726 | return; |
| 1727 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1728 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1729 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1730 | if (s->slowstart <= 0) |
| 1731 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1732 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1733 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1734 | srv_update_status(s, 0, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1735 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1736 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1737 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1738 | srv_set_running(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1739 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1740 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1743 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Aurelien DARRAGON | 10518c0 | 2023-04-19 10:33:02 +0200 | [diff] [blame] | 1744 | * isn't in maintenance. The server tries to redispatch pending requests |
| 1745 | * to the proxy. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1746 | * |
| 1747 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1748 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1749 | 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] | 1750 | { |
| 1751 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1752 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1753 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1754 | return; |
| 1755 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1756 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1757 | return; |
| 1758 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1759 | s->next_state = SRV_ST_STOPPING; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1760 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1761 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1762 | srv_update_status(s, 0, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1763 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1764 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1765 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1766 | srv_set_stopping(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1767 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1768 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1769 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1770 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1771 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1772 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1773 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1774 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1775 | * 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] | 1776 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1777 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1778 | * |
| 1779 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1780 | */ |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1781 | 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] | 1782 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1783 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1784 | |
| 1785 | if (!mode) |
| 1786 | return; |
| 1787 | |
| 1788 | /* 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] | 1789 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1790 | return; |
| 1791 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1792 | s->next_admin |= mode; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1793 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1794 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1795 | srv_update_status(s, 1, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1796 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1797 | /* 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] | 1798 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1799 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1800 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1801 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1802 | /* compute the inherited flag to propagate */ |
| 1803 | if (mode & SRV_ADMF_MAINT) |
| 1804 | mode = SRV_ADMF_IMAINT; |
| 1805 | else if (mode & SRV_ADMF_DRAIN) |
| 1806 | mode = SRV_ADMF_IDRAIN; |
| 1807 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1808 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1809 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1810 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1811 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1812 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1813 | } |
| 1814 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1815 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1816 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1817 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1818 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1819 | * 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] | 1820 | * |
| 1821 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1822 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1823 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1824 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1825 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1826 | |
| 1827 | if (!mode) |
| 1828 | return; |
| 1829 | |
| 1830 | /* 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] | 1831 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1832 | return; |
| 1833 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1834 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1835 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1836 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1837 | srv_update_status(s, 1, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1838 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1839 | /* 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] | 1840 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1841 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1842 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1843 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1844 | if (mode & SRV_ADMF_MAINT) |
| 1845 | mode = SRV_ADMF_IMAINT; |
| 1846 | else if (mode & SRV_ADMF_DRAIN) |
| 1847 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1848 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1849 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1850 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1851 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1852 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1853 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1856 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1857 | * upon startup so that inherited states are correct. |
| 1858 | */ |
| 1859 | static void srv_propagate_admin_state(struct server *srv) |
| 1860 | { |
| 1861 | struct server *srv2; |
| 1862 | |
| 1863 | if (!srv->trackers) |
| 1864 | return; |
| 1865 | |
| 1866 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1867 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1868 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1869 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1870 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1871 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1872 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1873 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1878 | * Only servers *not* tracking another one are considered, because other |
| 1879 | * ones will be handled when the server they track is visited. |
| 1880 | */ |
| 1881 | void srv_compute_all_admin_states(struct proxy *px) |
| 1882 | { |
| 1883 | struct server *srv; |
| 1884 | |
| 1885 | for (srv = px->srv; srv; srv = srv->next) { |
| 1886 | if (srv->track) |
| 1887 | continue; |
| 1888 | srv_propagate_admin_state(srv); |
| 1889 | } |
| 1890 | } |
| 1891 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1892 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1893 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1894 | * all code contributors. |
| 1895 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1896 | * the config parser can report an appropriate error when a known keyword was |
| 1897 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1898 | * 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] | 1899 | */ |
| 1900 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1901 | { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */ |
Amaury Denoyelle | ae8882d | 2024-03-27 10:50:21 +0100 | [diff] [blame] | 1902 | { "cookie", srv_parse_cookie, 1, 1, 1 }, /* Assign a cookie to the server */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1903 | { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */ |
| 1904 | { "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] | 1905 | { "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] | 1906 | { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1907 | { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1908 | { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */ |
| 1909 | { "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] | 1910 | { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */ |
| 1911 | { "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] | 1912 | { "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] | 1913 | { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1914 | { "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] | 1915 | { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */ |
| 1916 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1917 | { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */ |
| 1918 | { "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] | 1919 | { "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] | 1920 | { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */ |
| 1921 | { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */ |
| 1922 | { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */ |
| 1923 | { "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] | 1924 | { "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 */ |
| 1925 | { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */ |
| 1926 | { "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] | 1927 | { "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] | 1928 | { "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] | 1929 | { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */ |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 1930 | { "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] | 1931 | { "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] | 1932 | { "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] | 1933 | { "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] | 1934 | { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1935 | { "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] | 1936 | { "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] | 1937 | { "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] | 1938 | { "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] | 1939 | { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1940 | { "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] | 1941 | { "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] | 1942 | { "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] | 1943 | { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */ |
| 1944 | { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */ |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1945 | { NULL, NULL, 0 }, |
| 1946 | }}; |
| 1947 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1948 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1949 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1950 | /* 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] | 1951 | * 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] | 1952 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1953 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1954 | * |
| 1955 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1956 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1957 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1958 | { |
| 1959 | struct proxy *px = sv->proxy; |
| 1960 | unsigned w; |
| 1961 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 1962 | if (ns_to_sec(now_ns) < sv->last_change || ns_to_sec(now_ns) >= sv->last_change + sv->slowstart) { |
Damien Claisse | 2c2699e | 2024-04-09 15:37:07 +0000 | [diff] [blame] | 1963 | /* go to full throttle if the slowstart interval is reached unless server is currently down */ |
| 1964 | if ((sv->cur_state != SRV_ST_STOPPED) && (sv->next_state == SRV_ST_STARTING)) |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1965 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1969 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1970 | */ |
Damien Claisse | 2c2699e | 2024-04-09 15:37:07 +0000 | [diff] [blame] | 1971 | if ((sv->cur_state == SRV_ST_STOPPED) && (sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
| 1972 | w = 1; |
| 1973 | else if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 1974 | w = (px->lbprm.wdiv * (ns_to_sec(now_ns) - sv->last_change) + sv->slowstart) / sv->slowstart; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1975 | else |
| 1976 | w = px->lbprm.wdiv; |
| 1977 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1978 | 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] | 1979 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1980 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1981 | if (must_update) |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1982 | srv_update_status(sv, 0, SRV_OP_STCHGC_NONE); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1983 | } |
| 1984 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1985 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1986 | * Parses weight_str and configures sv accordingly. |
| 1987 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1988 | * |
| 1989 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1990 | */ |
| 1991 | const char *server_parse_weight_change_request(struct server *sv, |
| 1992 | const char *weight_str) |
| 1993 | { |
| 1994 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1995 | long int w; |
| 1996 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1997 | |
| 1998 | px = sv->proxy; |
| 1999 | |
| 2000 | /* if the weight is terminated with '%', it is set relative to |
| 2001 | * the initial weight, otherwise it is absolute. |
| 2002 | */ |
| 2003 | if (!*weight_str) |
| 2004 | return "Require <weight> or <weight%>.\n"; |
| 2005 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 2006 | w = strtol(weight_str, &end, 10); |
| 2007 | if (end == weight_str) |
| 2008 | return "Empty weight string empty or preceded by garbage"; |
| 2009 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 2010 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2011 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 2012 | /* Avoid integer overflow */ |
| 2013 | if (w > 25600) |
| 2014 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2015 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 2016 | if (w > 256) |
| 2017 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2018 | } |
| 2019 | else if (w < 0 || w > 256) |
| 2020 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 2021 | else if (end[0] != '\0') |
| 2022 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2023 | |
| 2024 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 2025 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 2026 | |
| 2027 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 2028 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2029 | |
| 2030 | return NULL; |
| 2031 | } |
| 2032 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2033 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 2034 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 2035 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2036 | * Returns: |
| 2037 | * - error string on error |
| 2038 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 2039 | * |
| 2040 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2041 | */ |
| 2042 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 2043 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2044 | { |
| 2045 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 2046 | |
| 2047 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2048 | srv_update_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2049 | return NULL; |
| 2050 | } |
| 2051 | if (inet_pton(AF_INET, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2052 | srv_update_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2053 | return NULL; |
| 2054 | } |
| 2055 | |
| 2056 | return "Could not understand IP address format.\n"; |
| 2057 | } |
| 2058 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 2059 | /* |
| 2060 | * Must be called with the server lock held. |
| 2061 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 2062 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 2063 | const char *maxconn_str) |
| 2064 | { |
| 2065 | long int v; |
| 2066 | char *end; |
| 2067 | |
| 2068 | if (!*maxconn_str) |
| 2069 | return "Require <maxconn>.\n"; |
| 2070 | |
| 2071 | v = strtol(maxconn_str, &end, 10); |
| 2072 | if (end == maxconn_str) |
| 2073 | return "maxconn string empty or preceded by garbage"; |
| 2074 | else if (end[0] != '\0') |
| 2075 | return "Trailing garbage in maxconn string"; |
| 2076 | |
| 2077 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 2078 | sv->maxconn = sv->minconn = v; |
| 2079 | } else { // dynamic maxconn |
| 2080 | sv->maxconn = v; |
| 2081 | } |
| 2082 | |
| 2083 | if (may_dequeue_tasks(sv, sv->proxy)) |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 2084 | process_srv_queue(sv); |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 2085 | |
| 2086 | return NULL; |
| 2087 | } |
| 2088 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2089 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 2090 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2091 | { |
| 2092 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2093 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2094 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2095 | NULL, |
| 2096 | }; |
| 2097 | |
| 2098 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 2099 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2100 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2101 | 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] | 2102 | } |
| 2103 | |
William Lallemand | 0d05867 | 2022-03-16 15:44:42 +0100 | [diff] [blame] | 2104 | 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] | 2105 | { |
| 2106 | struct sample_expr *expr; |
| 2107 | |
| 2108 | 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] | 2109 | if (!expr) { |
| 2110 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 2111 | return ERR_ALERT | ERR_FATAL; |
| 2112 | } |
| 2113 | |
| 2114 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 2115 | memprintf(err, "error detected while parsing sni expression : " |
| 2116 | " fetch method '%s' extracts information from '%s', " |
Amaury Denoyelle | c008a63 | 2021-05-28 11:01:22 +0200 | [diff] [blame] | 2117 | "none of which is available here.", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2118 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2119 | return ERR_ALERT | ERR_FATAL; |
| 2120 | } |
| 2121 | |
| 2122 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 2123 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2124 | newsrv->ssl_ctx.sni = expr; |
| 2125 | |
| 2126 | return 0; |
| 2127 | } |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2128 | |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2129 | 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] | 2130 | { |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2131 | char *msg = "error encountered while processing "; |
| 2132 | char *quote = "'"; |
| 2133 | char *token = args[cur_arg]; |
| 2134 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2135 | if (err && *err) { |
| 2136 | indent_msg(err, 2); |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2137 | msg = *err; |
| 2138 | quote = ""; |
| 2139 | token = ""; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2140 | } |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2141 | |
| 2142 | if (err_code & ERR_WARN && !(err_code & ERR_ALERT)) |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2143 | 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] | 2144 | else |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2145 | 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] | 2146 | } |
| 2147 | |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2148 | 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] | 2149 | { |
| 2150 | int range_sz; |
| 2151 | |
| 2152 | range_sz = src->conn_src.sport_range->size; |
| 2153 | if (range_sz > 0) { |
| 2154 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 2155 | if (srv->conn_src.sport_range != NULL) { |
| 2156 | int i; |
| 2157 | |
| 2158 | for (i = 0; i < range_sz; i++) { |
| 2159 | srv->conn_src.sport_range->ports[i] = |
| 2160 | src->conn_src.sport_range->ports[i]; |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | /* |
| 2167 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 2168 | */ |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2169 | 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] | 2170 | { |
| 2171 | srv->conn_src.opts = src->conn_src.opts; |
| 2172 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 2173 | |
| 2174 | /* Source port range copy. */ |
| 2175 | if (src->conn_src.sport_range != NULL) |
| 2176 | srv_conn_src_sport_range_cpy(srv, src); |
| 2177 | |
| 2178 | #ifdef CONFIG_HAP_TRANSPARENT |
| 2179 | if (src->conn_src.bind_hdr_name != NULL) { |
| 2180 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 2181 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 2182 | } |
| 2183 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 2184 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 2185 | #endif |
Aurelien DARRAGON | 92b935e | 2024-03-26 10:42:48 +0100 | [diff] [blame] | 2186 | if (src->conn_src.iface_name != NULL) { |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2187 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
Aurelien DARRAGON | 92b935e | 2024-03-26 10:42:48 +0100 | [diff] [blame] | 2188 | srv->conn_src.iface_len = src->conn_src.iface_len; |
| 2189 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * Copy <src> server SSL settings to <srv> server allocating |
| 2194 | * everything needed. |
| 2195 | */ |
| 2196 | #if defined(USE_OPENSSL) |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2197 | 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] | 2198 | { |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2199 | /* <src> is the current proxy's default server and SSL is enabled */ |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2200 | BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */ |
| 2201 | |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2202 | if (src == &srv->proxy->defsrv && src->use_ssl == 1) |
| 2203 | srv->flags |= SRV_F_DEFSRV_USE_SSL; |
| 2204 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2205 | if (src->ssl_ctx.ca_file != NULL) |
| 2206 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 2207 | if (src->ssl_ctx.crl_file != NULL) |
| 2208 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2209 | if (src->ssl_ctx.client_crt != NULL) |
| 2210 | 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] | 2211 | |
| 2212 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 2213 | |
Remi Tricot-Le Breton | 0498fa4 | 2021-07-13 18:28:22 +0200 | [diff] [blame] | 2214 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2215 | if (src->ssl_ctx.verify_host != NULL) |
| 2216 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 2217 | if (src->ssl_ctx.ciphers != NULL) |
| 2218 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 2e8d52f | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 2219 | if (src->ssl_ctx.options) |
| 2220 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 2221 | if (src->ssl_ctx.methods.flags) |
| 2222 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 2223 | if (src->ssl_ctx.methods.min) |
| 2224 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 2225 | if (src->ssl_ctx.methods.max) |
| 2226 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 2227 | |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 2228 | if (src->ssl_ctx.ciphersuites != NULL) |
| 2229 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2230 | if (src->sni_expr != NULL) |
| 2231 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2232 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2233 | if (src->ssl_ctx.alpn_str) { |
| 2234 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 2235 | if (srv->ssl_ctx.alpn_str) { |
| 2236 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 2237 | src->ssl_ctx.alpn_len); |
| 2238 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 2239 | } |
| 2240 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2241 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2242 | if (src->ssl_ctx.npn_str) { |
| 2243 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 2244 | if (srv->ssl_ctx.npn_str) { |
| 2245 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 2246 | src->ssl_ctx.npn_len); |
| 2247 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 2248 | } |
| 2249 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2250 | } |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2251 | |
| 2252 | /* Activate ssl on server <s>. |
| 2253 | * do nothing if there is no change to apply |
| 2254 | * |
| 2255 | * Must be called with the server lock held. |
| 2256 | */ |
| 2257 | void srv_set_ssl(struct server *s, int use_ssl) |
| 2258 | { |
| 2259 | if (s->use_ssl == use_ssl) |
| 2260 | return; |
| 2261 | |
| 2262 | s->use_ssl = use_ssl; |
| 2263 | if (s->use_ssl) |
| 2264 | s->xprt = xprt_get(XPRT_SSL); |
| 2265 | else |
William Dauchy | a087f87 | 2022-01-06 16:57:15 +0100 | [diff] [blame] | 2266 | s->xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | #endif /* USE_OPENSSL */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2270 | |
| 2271 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2272 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2273 | * 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] | 2274 | * 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] | 2275 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2276 | 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] | 2277 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2278 | char *hostname_dn; |
| 2279 | int hostname_len, hostname_dn_len; |
| 2280 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2281 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2282 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2283 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2284 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2285 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 2286 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 2287 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2288 | if (hostname_dn_len == -1) |
| 2289 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2290 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2291 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2292 | free(srv->hostname); |
| 2293 | free(srv->hostname_dn); |
| 2294 | srv->hostname = strdup(hostname); |
| 2295 | srv->hostname_dn = strdup(hostname_dn); |
| 2296 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2297 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2298 | goto err; |
| 2299 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2300 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2301 | |
| 2302 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2303 | ha_free(&srv->hostname); |
| 2304 | ha_free(&srv->hostname_dn); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2305 | return -1; |
| 2306 | } |
| 2307 | |
Amaury Denoyelle | 5cc599e | 2024-02-27 15:33:59 +0100 | [diff] [blame] | 2308 | /* Initialize default values for <srv>. Used both for dynamic servers and |
| 2309 | * default servers. The latter are not initialized via new_server(), hence this |
| 2310 | * function purpose. For static servers, srv_settings_cpy() is used instead |
| 2311 | * reusing their default server instance. |
| 2312 | */ |
| 2313 | void srv_settings_init(struct server *srv) |
| 2314 | { |
| 2315 | srv->check.inter = DEF_CHKINTR; |
| 2316 | srv->check.fastinter = 0; |
| 2317 | srv->check.downinter = 0; |
| 2318 | srv->check.rise = DEF_RISETIME; |
| 2319 | srv->check.fall = DEF_FALLTIME; |
| 2320 | srv->check.port = 0; |
| 2321 | |
| 2322 | srv->agent.inter = DEF_CHKINTR; |
| 2323 | srv->agent.fastinter = 0; |
| 2324 | srv->agent.downinter = 0; |
| 2325 | srv->agent.rise = DEF_AGENT_RISETIME; |
| 2326 | srv->agent.fall = DEF_AGENT_FALLTIME; |
| 2327 | srv->agent.port = 0; |
| 2328 | |
| 2329 | srv->maxqueue = 0; |
| 2330 | srv->minconn = 0; |
| 2331 | srv->maxconn = 0; |
| 2332 | |
| 2333 | srv->max_reuse = -1; |
| 2334 | srv->max_idle_conns = -1; |
| 2335 | srv->pool_purge_delay = 5000; |
| 2336 | |
| 2337 | srv->slowstart = 0; |
| 2338 | |
| 2339 | srv->onerror = DEF_HANA_ONERR; |
| 2340 | srv->consecutive_errors_limit = DEF_HANA_ERRLIMIT; |
| 2341 | |
| 2342 | srv->uweight = srv->iweight = 1; |
| 2343 | } |
| 2344 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2345 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2346 | * Copy <src> server settings to <srv> server allocating |
| 2347 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2348 | * This function is not supposed to be called at any time, but only |
| 2349 | * during server settings parsing or during server allocations from |
| 2350 | * a server template, and just after having calloc()'ed a new server. |
| 2351 | * So, <src> may only be a default server (when parsing server settings) |
| 2352 | * or a server template (during server allocations from a server template). |
| 2353 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 2354 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2355 | */ |
Christopher Faulet | b32cb9b | 2022-08-03 11:28:08 +0200 | [diff] [blame] | 2356 | 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] | 2357 | { |
| 2358 | /* Connection source settings copy */ |
| 2359 | srv_conn_src_cpy(srv, src); |
| 2360 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2361 | if (srv_tmpl) { |
| 2362 | srv->addr = src->addr; |
| 2363 | srv->svc_port = src->svc_port; |
| 2364 | } |
| 2365 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2366 | srv->pp_opts = src->pp_opts; |
| 2367 | if (src->rdr_pfx != NULL) { |
| 2368 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 2369 | srv->rdr_len = src->rdr_len; |
| 2370 | } |
| 2371 | if (src->cookie != NULL) { |
| 2372 | srv->cookie = strdup(src->cookie); |
| 2373 | srv->cklen = src->cklen; |
| 2374 | } |
| 2375 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | 2444108 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 2376 | srv->check.addr = src->check.addr; |
| 2377 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2378 | srv->check.use_ssl = src->check.use_ssl; |
| 2379 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 2380 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 2381 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 2382 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2383 | /* Note: 'flags' field has potentially been already initialized. */ |
| 2384 | srv->flags |= src->flags; |
| 2385 | srv->do_check = src->do_check; |
| 2386 | srv->do_agent = src->do_agent; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2387 | srv->check.inter = src->check.inter; |
| 2388 | srv->check.fastinter = src->check.fastinter; |
| 2389 | srv->check.downinter = src->check.downinter; |
| 2390 | srv->agent.use_ssl = src->agent.use_ssl; |
| 2391 | srv->agent.port = src->agent.port; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2392 | |
| 2393 | if (src->agent.tcpcheck_rules) { |
| 2394 | srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules)); |
| 2395 | if (srv->agent.tcpcheck_rules) { |
| 2396 | srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags; |
| 2397 | srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list; |
| 2398 | LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars); |
| 2399 | dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars, |
| 2400 | &src->agent.tcpcheck_rules->preset_vars); |
| 2401 | } |
| 2402 | } |
| 2403 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2404 | srv->agent.inter = src->agent.inter; |
| 2405 | srv->agent.fastinter = src->agent.fastinter; |
| 2406 | srv->agent.downinter = src->agent.downinter; |
| 2407 | srv->maxqueue = src->maxqueue; |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 2408 | srv->ws = src->ws; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2409 | srv->minconn = src->minconn; |
| 2410 | srv->maxconn = src->maxconn; |
| 2411 | srv->slowstart = src->slowstart; |
| 2412 | srv->observe = src->observe; |
| 2413 | srv->onerror = src->onerror; |
| 2414 | srv->onmarkeddown = src->onmarkeddown; |
| 2415 | srv->onmarkedup = src->onmarkedup; |
| 2416 | if (src->trackit != NULL) |
| 2417 | srv->trackit = strdup(src->trackit); |
| 2418 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 2419 | srv->uweight = srv->iweight = src->iweight; |
| 2420 | |
| 2421 | srv->check.send_proxy = src->check.send_proxy; |
| 2422 | /* health: up, but will fall down at first failure */ |
| 2423 | srv->check.rise = srv->check.health = src->check.rise; |
| 2424 | srv->check.fall = src->check.fall; |
| 2425 | |
| 2426 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2427 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 2428 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 2429 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2430 | srv->check.state |= CHK_ST_PAUSED; |
| 2431 | srv->check.health = 0; |
| 2432 | } |
| 2433 | |
| 2434 | /* health: up but will fall down at first failure */ |
| 2435 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 2436 | srv->agent.fall = src->agent.fall; |
| 2437 | |
| 2438 | if (src->resolvers_id != NULL) |
| 2439 | srv->resolvers_id = strdup(src->resolvers_id); |
Emeric Brun | 21fbeed | 2020-12-23 18:01:04 +0100 | [diff] [blame] | 2440 | srv->resolv_opts.family_prio = src->resolv_opts.family_prio; |
| 2441 | srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip; |
| 2442 | srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight; |
| 2443 | if (srv->resolv_opts.family_prio == AF_UNSPEC) |
| 2444 | srv->resolv_opts.family_prio = AF_INET6; |
| 2445 | memcpy(srv->resolv_opts.pref_net, |
| 2446 | src->resolv_opts.pref_net, |
| 2447 | sizeof srv->resolv_opts.pref_net); |
| 2448 | 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] | 2449 | |
| 2450 | srv->init_addr_methods = src->init_addr_methods; |
| 2451 | srv->init_addr = src->init_addr; |
| 2452 | #if defined(USE_OPENSSL) |
| 2453 | srv_ssl_settings_cpy(srv, src); |
| 2454 | #endif |
| 2455 | #ifdef TCP_USER_TIMEOUT |
| 2456 | srv->tcp_ut = src->tcp_ut; |
| 2457 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2458 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 2459 | srv->pool_purge_delay = src->pool_purge_delay; |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 2460 | srv->low_idle_conns = src->low_idle_conns; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 2461 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 2462 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2463 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2464 | if (srv_tmpl) |
| 2465 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2466 | |
Aurelien DARRAGON | 60d185d | 2023-06-14 09:53:32 +0200 | [diff] [blame] | 2467 | srv->netns = src->netns; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2468 | srv->check.via_socks4 = src->check.via_socks4; |
| 2469 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2470 | } |
| 2471 | |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 2472 | /* allocate a server and attach it to the global servers_list. Returns |
| 2473 | * the server on success, otherwise NULL. |
| 2474 | */ |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 2475 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2476 | { |
| 2477 | struct server *srv; |
| 2478 | |
| 2479 | srv = calloc(1, sizeof *srv); |
| 2480 | if (!srv) |
| 2481 | return NULL; |
| 2482 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2483 | srv_take(srv); |
| 2484 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2485 | srv->obj_type = OBJ_TYPE_SERVER; |
| 2486 | srv->proxy = proxy; |
Willy Tarreau | cdc83e0 | 2021-06-23 16:11:02 +0200 | [diff] [blame] | 2487 | queue_init(&srv->queue, proxy, srv); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2488 | LIST_APPEND(&servers_list, &srv->global_list); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2489 | LIST_INIT(&srv->srv_rec_item); |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 2490 | LIST_INIT(&srv->ip_rec_item); |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2491 | MT_LIST_INIT(&srv->prev_deleted); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2492 | event_hdl_sub_list_init(&srv->e_subs); |
Aurelien DARRAGON | 9a55efb | 2023-07-07 15:19:36 +0200 | [diff] [blame] | 2493 | srv->rid = 0; /* rid defaults to 0 */ |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 2494 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2495 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 2496 | srv->last_change = ns_to_sec(now_ns); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2497 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2498 | srv->check.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2499 | srv->check.status = HCHK_STATUS_INI; |
| 2500 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 2501 | srv->check.proxy = proxy; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 2502 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2503 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2504 | srv->agent.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2505 | srv->agent.status = HCHK_STATUS_INI; |
| 2506 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 2507 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2508 | srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW); |
| 2509 | |
Amaury Denoyelle | 1d71bf5 | 2024-03-12 14:09:55 +0100 | [diff] [blame] | 2510 | MT_LIST_INIT(&srv->sess_conns); |
| 2511 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2512 | srv->extra_counters = NULL; |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 2513 | #ifdef USE_OPENSSL |
| 2514 | HA_RWLOCK_INIT(&srv->ssl_ctx.lock); |
| 2515 | #endif |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2516 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2517 | /* please don't put default server settings here, they are set in |
Willy Tarreau | 144289b | 2021-02-12 08:19:01 +0100 | [diff] [blame] | 2518 | * proxy_preset_defaults(). |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2519 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2520 | return srv; |
| 2521 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2522 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2523 | /* Increment the server refcount. */ |
| 2524 | void srv_take(struct server *srv) |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2525 | { |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2526 | HA_ATOMIC_INC(&srv->refcount); |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2527 | } |
| 2528 | |
Aurelien DARRAGON | 899b547 | 2023-06-01 12:07:43 +0200 | [diff] [blame] | 2529 | /* deallocate common server parameters (may be used by default-servers) */ |
| 2530 | void srv_free_params(struct server *srv) |
| 2531 | { |
| 2532 | free(srv->cookie); |
Aurelien DARRAGON | d2d7fbd | 2023-09-15 00:42:55 +0200 | [diff] [blame] | 2533 | free(srv->rdr_pfx); |
Aurelien DARRAGON | 899b547 | 2023-06-01 12:07:43 +0200 | [diff] [blame] | 2534 | free(srv->hostname); |
| 2535 | free(srv->hostname_dn); |
| 2536 | free((char*)srv->conf.file); |
| 2537 | free(srv->per_thr); |
| 2538 | free(srv->per_tgrp); |
| 2539 | free(srv->curr_idle_thr); |
| 2540 | free(srv->resolvers_id); |
| 2541 | free(srv->addr_node.key); |
| 2542 | free(srv->lb_nodes); |
| 2543 | |
| 2544 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2545 | xprt_get(XPRT_SSL)->destroy_srv(srv); |
| 2546 | } |
| 2547 | |
Amaury Denoyelle | 13f2e2c | 2021-08-09 15:08:54 +0200 | [diff] [blame] | 2548 | /* Deallocate a server <srv> and its member. <srv> must be allocated. For |
| 2549 | * dynamic servers, its refcount is decremented first. The free operations are |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2550 | * conducted only if the refcount is nul. |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2551 | * |
| 2552 | * 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] | 2553 | * when calling srv_drop on the list of servers. |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2554 | */ |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2555 | struct server *srv_drop(struct server *srv) |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2556 | { |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2557 | struct server *next = NULL; |
| 2558 | |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2559 | if (!srv) |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2560 | goto end; |
| 2561 | |
| 2562 | next = srv->next; |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2563 | |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2564 | /* For dynamic servers, decrement the reference counter. Only free the |
| 2565 | * server when reaching zero. |
| 2566 | */ |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2567 | if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1)) |
| 2568 | goto end; |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2569 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2570 | /* make sure we are removed from our 'next->prev_deleted' list |
| 2571 | * This doesn't require full thread isolation as we're using mt lists |
| 2572 | * However this could easily be turned into regular list if required |
| 2573 | * (with the proper use of thread isolation) |
| 2574 | */ |
| 2575 | MT_LIST_DELETE(&srv->prev_deleted); |
| 2576 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2577 | task_destroy(srv->warmup); |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 2578 | task_destroy(srv->srvrq_check); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2579 | |
| 2580 | free(srv->id); |
Aurelien DARRAGON | 899b547 | 2023-06-01 12:07:43 +0200 | [diff] [blame] | 2581 | srv_free_params(srv); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2582 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2583 | HA_SPIN_DESTROY(&srv->lock); |
| 2584 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2585 | LIST_DELETE(&srv->global_list); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2586 | event_hdl_sub_list_destroy(&srv->e_subs); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2587 | |
| 2588 | EXTRA_COUNTERS_FREE(srv->extra_counters); |
| 2589 | |
Tim Duesterhus | 025b93e | 2021-11-04 21:03:52 +0100 | [diff] [blame] | 2590 | ha_free(&srv); |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2591 | |
| 2592 | end: |
| 2593 | return next; |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2594 | } |
| 2595 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 2596 | /* Remove a server <srv> from a tracking list if <srv> is tracking another |
| 2597 | * server. No special care is taken if <srv> is tracked itself by another one : |
| 2598 | * this situation should be avoided by the caller. |
| 2599 | * |
| 2600 | * Not thread-safe. |
| 2601 | */ |
| 2602 | static void release_server_track(struct server *srv) |
| 2603 | { |
| 2604 | struct server *strack = srv->track; |
| 2605 | struct server **base; |
| 2606 | |
| 2607 | if (!strack) |
| 2608 | return; |
| 2609 | |
| 2610 | for (base = &strack->trackers; *base; base = &((*base)->tracknext)) { |
| 2611 | if (*base == srv) { |
| 2612 | *base = srv->tracknext; |
| 2613 | return; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | /* srv not found on the tracking list, this should never happen */ |
| 2618 | BUG_ON(!*base); |
| 2619 | } |
| 2620 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2621 | /* |
| 2622 | * Parse as much as possible such a range string argument: low[-high] |
| 2623 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2624 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2625 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2626 | * Returns 0 if succeeded, -1 if not. |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2627 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2628 | static int _srv_parse_tmpl_range(struct server *srv, const char *arg, |
| 2629 | int *nb_low, int *nb_high) |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2630 | { |
| 2631 | char *nb_high_arg; |
| 2632 | |
| 2633 | *nb_high = 0; |
| 2634 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2635 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2636 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2637 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2638 | *nb_high_arg++ = '\0'; |
| 2639 | *nb_high = atoi(nb_high_arg); |
| 2640 | } |
| 2641 | else { |
| 2642 | *nb_high += *nb_low; |
| 2643 | *nb_low = 1; |
| 2644 | } |
| 2645 | |
| 2646 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 2647 | return -1; |
| 2648 | |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2652 | /* Parse as much as possible such a range string argument: low[-high] |
| 2653 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2654 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2655 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2656 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2657 | * initialize a new server on startup. |
| 2658 | * |
| 2659 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2660 | * Returns 0 if succeeded, -1 if not. |
| 2661 | */ |
| 2662 | static inline void _srv_parse_set_id_from_prefix(struct server *srv, |
| 2663 | const char *prefix, int nb) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2664 | { |
| 2665 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 2666 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2667 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2668 | } |
| 2669 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2670 | /* 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] | 2671 | * Note that a server template is a special server with |
| 2672 | * a few different parameters than a server which has |
| 2673 | * been parsed mostly the same way as a server. |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2674 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2675 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2676 | * initialize a new server on startup. |
| 2677 | * |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2678 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2679 | * 'srv' template included. |
| 2680 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2681 | 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] | 2682 | { |
| 2683 | int i; |
| 2684 | struct server *newsrv; |
| 2685 | |
| 2686 | 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] | 2687 | newsrv = new_server(px); |
| 2688 | if (!newsrv) |
| 2689 | goto err; |
| 2690 | |
Christopher Faulet | 75bef00 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 2691 | newsrv->conf.file = strdup(srv->conf.file); |
| 2692 | newsrv->conf.line = srv->conf.line; |
| 2693 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2694 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2695 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2696 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2697 | if (newsrv->sni_expr) { |
| 2698 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 2699 | if (!newsrv->ssl_ctx.sni) |
| 2700 | goto err; |
| 2701 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2702 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2703 | /* append to list of servers available to receive an hostname */ |
Emeric Brun | caef19e | 2021-06-14 10:02:18 +0200 | [diff] [blame] | 2704 | if (newsrv->srvrq) |
| 2705 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2706 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2707 | /* Set this new server ID. */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2708 | _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] | 2709 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2710 | /* Linked backwards first. This will be restablished after parsing. */ |
| 2711 | newsrv->next = px->srv; |
| 2712 | px->srv = newsrv; |
| 2713 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2714 | _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] | 2715 | |
| 2716 | return i - srv->tmpl_info.nb_low; |
| 2717 | |
| 2718 | err: |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2719 | _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] | 2720 | if (newsrv) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2721 | release_sample_expr(newsrv->ssl_ctx.sni); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2722 | free_check(&newsrv->agent); |
| 2723 | free_check(&newsrv->check); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2724 | LIST_DELETE(&newsrv->global_list); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2725 | } |
| 2726 | free(newsrv); |
| 2727 | return i - srv->tmpl_info.nb_low; |
| 2728 | } |
| 2729 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2730 | /* Allocate a new server pointed by <srv> and try to parse the first arguments |
| 2731 | * in <args> as an address for a server or an address-range for a template or |
| 2732 | * nothing for a default-server. <cur_arg> is incremented to the next argument. |
| 2733 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2734 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2735 | * initialize a new server on startup. |
| 2736 | * |
| 2737 | * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case |
| 2738 | * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated, |
| 2739 | * <srv> will be set to NULL. |
| 2740 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2741 | static int _srv_parse_init(struct server **srv, char **args, int *cur_arg, |
| 2742 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2743 | int parse_flags) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2744 | { |
| 2745 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2746 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2747 | int err_code = 0; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2748 | char *fqdn = NULL; |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2749 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2750 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2751 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2752 | *srv = NULL; |
| 2753 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2754 | /* There is no mandatory first arguments for default server. */ |
| 2755 | if (parse_flags & SRV_PARSE_PARSE_ADDR) { |
| 2756 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2757 | if (!*args[3]) { |
| 2758 | /* 'server-template' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2759 | ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n", |
| 2760 | args[0]); |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2761 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2762 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2763 | } |
| 2764 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2765 | err = invalid_prefix_char(args[1]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2766 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2767 | else { |
| 2768 | if (!*args[2]) { |
| 2769 | /* 'server' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2770 | ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2771 | args[0]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2772 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2773 | goto out; |
| 2774 | } |
| 2775 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2776 | err = invalid_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2779 | if (err) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2780 | ha_alert("character '%c' is not permitted in %s %s '%s'.\n", |
| 2781 | *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2782 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2783 | goto out; |
| 2784 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2785 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2786 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2787 | *cur_arg = 2; |
| 2788 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2789 | /* Parse server-template <nb | range> arg. */ |
| 2790 | 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] | 2791 | ha_alert("Wrong %s number or range arg '%s'.\n", |
| 2792 | args[0], args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2793 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2794 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2795 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2796 | (*cur_arg)++; |
| 2797 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2798 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2799 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2800 | struct sockaddr_storage *sk; |
| 2801 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2802 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2803 | *srv = newsrv = new_server(curproxy); |
| 2804 | if (!newsrv) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2805 | ha_alert("out of memory.\n"); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2806 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2807 | goto out; |
| 2808 | } |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2809 | register_parsing_obj(&newsrv->obj_type); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2810 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2811 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2812 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2813 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2814 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2815 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2816 | if (parse_flags & SRV_PARSE_DYNAMIC) |
| 2817 | newsrv->flags |= SRV_F_DYNAMIC; |
| 2818 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2819 | /* Note: for a server template, its id is its prefix. |
| 2820 | * This is a temporary id which will be used for server allocations to come |
| 2821 | * after parsing. |
| 2822 | */ |
| 2823 | if (!(parse_flags & SRV_PARSE_TEMPLATE)) |
| 2824 | newsrv->id = strdup(args[1]); |
| 2825 | else |
| 2826 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2827 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2828 | /* several ways to check the port component : |
| 2829 | * - IP => port=+0, relative (IPv4 only) |
| 2830 | * - IP: => port=+0, relative |
| 2831 | * - IP:N => port=N, absolute |
| 2832 | * - IP:+N => port=+N, relative |
| 2833 | * - IP:-N => port=-N, relative |
| 2834 | */ |
| 2835 | if (!(parse_flags & SRV_PARSE_PARSE_ADDR)) |
| 2836 | goto skip_addr; |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2837 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2838 | sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2839 | &errmsg, NULL, &fqdn, |
Christopher Faulet | af27ada | 2023-06-02 14:10:36 +0200 | [diff] [blame] | 2840 | (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | |
| 2841 | (parse_flags & SRV_PARSE_IN_PEER_SECTION ? PA_O_PORT_MAND : PA_O_PORT_OFS) | |
| 2842 | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2843 | if (!sk) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2844 | ha_alert("%s\n", errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2845 | err_code |= ERR_ALERT | ERR_FATAL; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2846 | ha_free(&errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2847 | goto out; |
| 2848 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2849 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2850 | if (!port1 || !port2) { |
| 2851 | /* no port specified, +offset, -offset */ |
| 2852 | newsrv->flags |= SRV_F_MAPPORTS; |
| 2853 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2854 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2855 | /* save hostname and create associated name resolution */ |
| 2856 | if (fqdn) { |
| 2857 | if (fqdn[0] == '_') { /* SRV record */ |
| 2858 | /* Check if a SRV request already exists, and if not, create it */ |
| 2859 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2860 | newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn); |
| 2861 | if (newsrv->srvrq == NULL) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2862 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2863 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2864 | } |
Christopher Faulet | 81ba74a | 2021-06-29 20:52:35 +0200 | [diff] [blame] | 2865 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2866 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2867 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2868 | ha_alert("Can't create DNS resolution for server '%s'\n", |
| 2869 | newsrv->id); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2870 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2871 | goto out; |
| 2872 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2873 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2874 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2875 | newsrv->addr = *sk; |
| 2876 | newsrv->svc_port = port; |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 2877 | /* |
| 2878 | * we don't need to lock the server here, because |
| 2879 | * we are in the process of initializing. |
| 2880 | * |
| 2881 | * Note that the server is not attached into the proxy tree if |
| 2882 | * this is a dynamic server. |
| 2883 | */ |
| 2884 | srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC)); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2885 | |
Willy Tarreau | 14e7f29 | 2021-10-27 17:41:07 +0200 | [diff] [blame] | 2886 | if (!newsrv->srvrq && !newsrv->hostname && |
| 2887 | !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2888 | ha_alert("Unknown protocol family %d '%s'\n", |
| 2889 | newsrv->addr.ss_family, args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2890 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2891 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2892 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2893 | |
| 2894 | (*cur_arg)++; |
| 2895 | skip_addr: |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2896 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2897 | /* Copy default server settings to new server */ |
| 2898 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
| 2899 | } else { |
Amaury Denoyelle | 5cc599e | 2024-02-27 15:33:59 +0100 | [diff] [blame] | 2900 | srv_settings_init(newsrv); |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2901 | |
| 2902 | /* A dynamic server is disabled on startup */ |
| 2903 | newsrv->next_admin = SRV_ADMF_FMAINT; |
| 2904 | newsrv->next_state = SRV_ST_STOPPED; |
| 2905 | server_recalc_eweight(newsrv, 0); |
| 2906 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2907 | HA_SPIN_INIT(&newsrv->lock); |
| 2908 | } |
| 2909 | else { |
| 2910 | *srv = newsrv = &curproxy->defsrv; |
| 2911 | *cur_arg = 1; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2912 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2913 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2914 | free(fqdn); |
| 2915 | return 0; |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2916 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2917 | out: |
| 2918 | free(fqdn); |
| 2919 | return err_code; |
| 2920 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2921 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2922 | /* Parse the server keyword in <args>. |
| 2923 | * <cur_arg> is incremented beyond the keyword optional value. Note that this |
| 2924 | * might not be the case if an error is reported. |
| 2925 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2926 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2927 | * initialize a new server on startup. |
| 2928 | * |
| 2929 | * A mask of errors is returned. ERR_FATAL is set if the parsing should be |
| 2930 | * interrupted. |
| 2931 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2932 | static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg, |
| 2933 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2934 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2935 | { |
| 2936 | int err_code = 0; |
| 2937 | struct srv_kw *kw; |
| 2938 | const char *best; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2939 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2940 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2941 | kw = srv_find_kw(args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2942 | if (!kw) { |
| 2943 | best = srv_find_best_kw(args[*cur_arg]); |
| 2944 | if (best) |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2945 | ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n", |
| 2946 | args[*cur_arg], best, |
| 2947 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2948 | " Hint: no address was expected for this server."); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2949 | else |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2950 | ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg], |
| 2951 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2952 | " Hint: no address was expected for this server."); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2953 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2954 | return ERR_ALERT | ERR_FATAL; |
| 2955 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2956 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2957 | if (!kw->parse) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2958 | ha_alert("'%s' option is not implemented in this version (check build options)\n", |
| 2959 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2960 | err_code = ERR_ALERT | ERR_FATAL; |
| 2961 | goto out; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2962 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2963 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2964 | if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2965 | ha_alert("'%s' option is not accepted in default-server sections\n", |
| 2966 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2967 | err_code = ERR_ALERT; |
| 2968 | goto out; |
| 2969 | } |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2970 | else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2971 | ha_alert("'%s' option is not accepted for dynamic server\n", |
| 2972 | args[*cur_arg]); |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2973 | err_code |= ERR_ALERT; |
| 2974 | goto out; |
| 2975 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2976 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2977 | err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg); |
| 2978 | if (err_code) { |
| 2979 | display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg); |
| 2980 | free(errmsg); |
| 2981 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2982 | |
| 2983 | out: |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2984 | if (kw->skip != -1) |
| 2985 | *cur_arg += 1 + kw->skip; |
| 2986 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2987 | return err_code; |
| 2988 | } |
| 2989 | |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2990 | /* This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2991 | * initialize a new server on startup. |
| 2992 | */ |
| 2993 | static int _srv_parse_sni_expr_init(char **args, int cur_arg, |
| 2994 | struct server *srv, struct proxy *proxy, |
| 2995 | char **errmsg) |
| 2996 | { |
| 2997 | int ret; |
| 2998 | |
| 2999 | if (!srv->sni_expr) |
| 3000 | return 0; |
| 3001 | |
| 3002 | ret = server_parse_sni_expr(srv, proxy, errmsg); |
| 3003 | if (!ret) |
| 3004 | return 0; |
| 3005 | |
| 3006 | return ret; |
| 3007 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3008 | |
| 3009 | /* Server initializations finalization. |
| 3010 | * Initialize health check, agent check and SNI expression if enabled. |
| 3011 | * Must not be called for a default server instance. |
| 3012 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 3013 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3014 | * initialize a new server on startup. |
| 3015 | */ |
| 3016 | static int _srv_parse_finalize(char **args, int cur_arg, |
| 3017 | struct server *srv, struct proxy *px, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3018 | int parse_flags) |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3019 | { |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3020 | int ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3021 | char *errmsg = NULL; |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3022 | |
| 3023 | if (srv->do_check && srv->trackit) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3024 | 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] | 3025 | return ERR_ALERT | ERR_FATAL; |
| 3026 | } |
| 3027 | |
| 3028 | if (srv->do_agent && !srv->agent.port) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3029 | ha_alert("server %s does not have agent port. Agent check has been disabled.\n", |
| 3030 | srv->id); |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3031 | return ERR_ALERT | ERR_FATAL; |
| 3032 | } |
| 3033 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3034 | if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) { |
| 3035 | if (errmsg) { |
| 3036 | ha_alert("%s\n", errmsg); |
| 3037 | free(errmsg); |
| 3038 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3039 | return ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3040 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3041 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 3042 | /* A dynamic server is disabled on startup. It must not be counted as |
| 3043 | * an active backend entry. |
| 3044 | */ |
| 3045 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 3046 | if (srv->flags & SRV_F_BACKUP) |
| 3047 | px->srv_bck++; |
| 3048 | else |
| 3049 | px->srv_act++; |
| 3050 | } |
| 3051 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3052 | srv_lb_commit_status(srv); |
| 3053 | |
| 3054 | return 0; |
| 3055 | } |
| 3056 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3057 | int parse_server(const char *file, int linenum, char **args, |
| 3058 | struct proxy *curproxy, const struct proxy *defproxy, |
| 3059 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 3060 | { |
| 3061 | struct server *newsrv = NULL; |
| 3062 | int err_code = 0; |
| 3063 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3064 | int cur_arg; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3065 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3066 | set_usermsgs_ctx(file, linenum, NULL); |
| 3067 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3068 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) { |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3069 | ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3070 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3071 | goto out; |
| 3072 | } |
| 3073 | else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) { |
| 3074 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3075 | goto out; |
| 3076 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 3077 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3078 | if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) == |
| 3079 | (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) { |
| 3080 | if (!*args[2]) |
| 3081 | return 0; |
| 3082 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3083 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3084 | err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3085 | parse_flags); |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 3086 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3087 | /* the servers are linked backwards first */ |
| 3088 | if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 3089 | newsrv->next = curproxy->srv; |
| 3090 | curproxy->srv = newsrv; |
| 3091 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 3092 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3093 | if (err_code & ERR_CODE) |
| 3094 | goto out; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3095 | |
Willy Tarreau | 8bb771a | 2023-11-23 14:28:14 +0100 | [diff] [blame] | 3096 | if (!newsrv->conf.file) // note: do it only once for default-server |
| 3097 | newsrv->conf.file = strdup(file); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3098 | newsrv->conf.line = linenum; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3099 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3100 | while (*args[cur_arg]) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3101 | err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3102 | parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3103 | if (err_code & ERR_FATAL) |
| 3104 | goto out; |
| 3105 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3106 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3107 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3108 | err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3109 | if (err_code & ERR_FATAL) |
| 3110 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3111 | } |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 3112 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3113 | if (parse_flags & SRV_PARSE_TEMPLATE) |
| 3114 | _srv_parse_tmpl_init(newsrv, curproxy); |
| 3115 | |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 3116 | /* If the server id is fixed, insert it in the proxy used_id tree. |
| 3117 | * This is needed to detect a later duplicate id via srv_parse_id. |
| 3118 | * |
| 3119 | * If no is specified, a dynamic one is generated in |
| 3120 | * check_config_validity. |
| 3121 | */ |
| 3122 | if (newsrv->flags & SRV_F_FORCED_ID) |
| 3123 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 3124 | |
Amaury Denoyelle | 24abb0c | 2021-05-07 15:13:51 +0200 | [diff] [blame] | 3125 | HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight, |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3126 | "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] | 3127 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3128 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3129 | return 0; |
| 3130 | |
| 3131 | out: |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3132 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3133 | return err_code; |
| 3134 | } |
| 3135 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 3136 | /* Returns a pointer to the first server matching either id <id>. |
| 3137 | * NULL is returned if no match is found. |
| 3138 | * the lookup is performed in the backend <bk> |
| 3139 | */ |
| 3140 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 3141 | { |
| 3142 | struct eb32_node *eb32; |
| 3143 | struct server *curserver; |
| 3144 | |
| 3145 | if (!bk || (id ==0)) |
| 3146 | return NULL; |
| 3147 | |
| 3148 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3149 | if (!(bk->cap & PR_CAP_BE)) |
| 3150 | return NULL; |
| 3151 | |
| 3152 | curserver = NULL; |
| 3153 | |
| 3154 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 3155 | if (eb32) |
| 3156 | curserver = container_of(eb32, struct server, conf.id); |
| 3157 | |
| 3158 | return curserver; |
| 3159 | } |
| 3160 | |
| 3161 | /* Returns a pointer to the first server matching either name <name>, or id |
| 3162 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 3163 | * the lookup is performed in the backend <bk> |
| 3164 | */ |
| 3165 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 3166 | { |
| 3167 | struct server *curserver; |
| 3168 | |
| 3169 | if (!bk || !name) |
| 3170 | return NULL; |
| 3171 | |
| 3172 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3173 | if (!(bk->cap & PR_CAP_BE)) |
| 3174 | return NULL; |
| 3175 | |
| 3176 | curserver = NULL; |
| 3177 | if (*name == '#') { |
| 3178 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 3179 | if (curserver) |
| 3180 | return curserver; |
| 3181 | } |
| 3182 | else { |
| 3183 | curserver = bk->srv; |
| 3184 | |
| 3185 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 3186 | curserver = curserver->next; |
| 3187 | |
| 3188 | if (curserver) |
| 3189 | return curserver; |
| 3190 | } |
| 3191 | |
| 3192 | return NULL; |
| 3193 | } |
| 3194 | |
| 3195 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 3196 | { |
| 3197 | struct server *byname; |
| 3198 | struct server *byid; |
| 3199 | |
| 3200 | if (!name && !id) |
| 3201 | return NULL; |
| 3202 | |
| 3203 | if (diff) |
| 3204 | *diff = 0; |
| 3205 | |
| 3206 | byname = byid = NULL; |
| 3207 | |
| 3208 | if (name) { |
| 3209 | byname = server_find_by_name(bk, name); |
| 3210 | if (byname && (!id || byname->puid == id)) |
| 3211 | return byname; |
| 3212 | } |
| 3213 | |
| 3214 | /* remaining possibilities : |
| 3215 | * - name not set |
| 3216 | * - name set but not found |
| 3217 | * - name found but ID doesn't match |
| 3218 | */ |
| 3219 | if (id) { |
| 3220 | byid = server_find_by_id(bk, id); |
| 3221 | if (byid) { |
| 3222 | if (byname) { |
| 3223 | /* use id only if forced by configuration */ |
| 3224 | if (byid->flags & SRV_F_FORCED_ID) { |
| 3225 | if (diff) |
| 3226 | *diff |= 2; |
| 3227 | return byid; |
| 3228 | } |
| 3229 | else { |
| 3230 | if (diff) |
| 3231 | *diff |= 1; |
| 3232 | return byname; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | /* remaining possibilities: |
| 3237 | * - name not set |
| 3238 | * - name set but not found |
| 3239 | */ |
| 3240 | if (name && diff) |
| 3241 | *diff |= 2; |
| 3242 | return byid; |
| 3243 | } |
| 3244 | |
| 3245 | /* id bot found */ |
| 3246 | if (byname) { |
| 3247 | if (diff) |
| 3248 | *diff |= 1; |
| 3249 | return byname; |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | return NULL; |
| 3254 | } |
| 3255 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3256 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3257 | * update a server's current IP address. |
| 3258 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3259 | * ip is in network format. |
| 3260 | * updater is a string which contains an information about the requester of the update. |
| 3261 | * updater is used if not NULL. |
| 3262 | * |
| 3263 | * 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] | 3264 | * |
| 3265 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3266 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3267 | 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] | 3268 | { |
Christopher Faulet | d6c6b5f | 2020-09-08 10:27:24 +0200 | [diff] [blame] | 3269 | /* save the new IP family & address if necessary */ |
| 3270 | switch (ip_sin_family) { |
| 3271 | case AF_INET: |
| 3272 | if (s->addr.ss_family == ip_sin_family && |
| 3273 | !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4)) |
| 3274 | return 0; |
| 3275 | break; |
| 3276 | case AF_INET6: |
| 3277 | if (s->addr.ss_family == ip_sin_family && |
| 3278 | !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16)) |
| 3279 | return 0; |
| 3280 | break; |
| 3281 | }; |
| 3282 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3283 | /* generates a log line and a warning on stderr */ |
| 3284 | if (1) { |
| 3285 | /* book enough space for both IPv4 and IPv6 */ |
| 3286 | char oldip[INET6_ADDRSTRLEN]; |
| 3287 | char newip[INET6_ADDRSTRLEN]; |
| 3288 | |
| 3289 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3290 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3291 | |
| 3292 | /* copy old IP address in a string */ |
| 3293 | switch (s->addr.ss_family) { |
| 3294 | case AF_INET: |
| 3295 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3296 | break; |
| 3297 | case AF_INET6: |
| 3298 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3299 | break; |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3300 | default: |
Willy Tarreau | fc458ec | 2023-04-07 18:11:39 +0200 | [diff] [blame] | 3301 | strlcpy2(oldip, "(none)", sizeof(oldip)); |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3302 | break; |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3303 | }; |
| 3304 | |
| 3305 | /* copy new IP address in a string */ |
| 3306 | switch (ip_sin_family) { |
| 3307 | case AF_INET: |
| 3308 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3309 | break; |
| 3310 | case AF_INET6: |
| 3311 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3312 | break; |
| 3313 | }; |
| 3314 | |
| 3315 | /* save log line into a buffer */ |
| 3316 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3317 | s->proxy->id, s->id, oldip, newip, updater); |
| 3318 | |
| 3319 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3320 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3321 | |
| 3322 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3323 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | /* save the new IP family */ |
| 3327 | s->addr.ss_family = ip_sin_family; |
| 3328 | /* save the new IP address */ |
| 3329 | switch (ip_sin_family) { |
| 3330 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3331 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3332 | break; |
| 3333 | case AF_INET6: |
| 3334 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3335 | break; |
| 3336 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3337 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3338 | srv_set_addr_desc(s, 1); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3339 | |
| 3340 | return 0; |
| 3341 | } |
| 3342 | |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 3343 | /* update agent health check address and port |
| 3344 | * addr can be ip4/ip6 or a hostname |
| 3345 | * if one error occurs, don't apply anything |
| 3346 | * must be called with the server lock held. |
| 3347 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3348 | 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] | 3349 | { |
| 3350 | struct sockaddr_storage sk; |
| 3351 | struct buffer *msg; |
| 3352 | int new_port; |
| 3353 | |
| 3354 | msg = get_trash_chunk(); |
| 3355 | chunk_reset(msg); |
| 3356 | |
| 3357 | if (!(s->agent.state & CHK_ST_ENABLED)) { |
| 3358 | chunk_strcat(msg, "agent checks are not enabled on this server"); |
| 3359 | goto out; |
| 3360 | } |
| 3361 | if (addr) { |
| 3362 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3363 | if (str2ip(addr, &sk) == NULL) { |
| 3364 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3365 | goto out; |
| 3366 | } |
| 3367 | } |
| 3368 | if (port) { |
| 3369 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3370 | chunk_appendf(msg, "provided port is not an integer"); |
| 3371 | goto out; |
| 3372 | } |
| 3373 | if (new_port < 0 || new_port > 65535) { |
| 3374 | chunk_appendf(msg, "provided port is invalid"); |
| 3375 | goto out; |
| 3376 | } |
| 3377 | } |
| 3378 | out: |
| 3379 | if (msg->data) |
| 3380 | return msg->area; |
| 3381 | else { |
| 3382 | if (addr) |
| 3383 | set_srv_agent_addr(s, &sk); |
| 3384 | if (port) |
| 3385 | set_srv_agent_port(s, new_port); |
| 3386 | } |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 3390 | /* update server health check address and port |
| 3391 | * addr must be ip4 or ip6, it won't be resolved |
| 3392 | * if one error occurs, don't apply anything |
| 3393 | * must be called with the server lock held. |
| 3394 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3395 | 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] | 3396 | { |
| 3397 | struct sockaddr_storage sk; |
| 3398 | struct buffer *msg; |
| 3399 | int new_port; |
| 3400 | |
| 3401 | msg = get_trash_chunk(); |
| 3402 | chunk_reset(msg); |
| 3403 | |
| 3404 | if (!(s->check.state & CHK_ST_ENABLED)) { |
| 3405 | chunk_strcat(msg, "health checks are not enabled on this server"); |
| 3406 | goto out; |
| 3407 | } |
| 3408 | if (addr) { |
| 3409 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3410 | if (str2ip2(addr, &sk, 0) == NULL) { |
| 3411 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3412 | goto out; |
| 3413 | } |
| 3414 | } |
| 3415 | if (port) { |
| 3416 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3417 | chunk_appendf(msg, "provided port is not an integer"); |
| 3418 | goto out; |
| 3419 | } |
| 3420 | if (new_port < 0 || new_port > 65535) { |
| 3421 | chunk_appendf(msg, "provided port is invalid"); |
| 3422 | goto out; |
| 3423 | } |
| 3424 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 3425 | if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) { |
| 3426 | chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use"); |
| 3427 | goto out; |
| 3428 | } |
| 3429 | } |
| 3430 | out: |
| 3431 | if (msg->data) |
| 3432 | return msg->area; |
| 3433 | else { |
| 3434 | if (addr) |
| 3435 | s->check.addr = sk; |
| 3436 | if (port) |
| 3437 | s->check.port = new_port; |
| 3438 | } |
| 3439 | return NULL; |
| 3440 | } |
| 3441 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3442 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3443 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3444 | * |
| 3445 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3446 | * returned by the function. |
| 3447 | * |
| 3448 | * The function first does the following, in that order: |
| 3449 | * - validates the new addr and/or port |
| 3450 | * - checks if an update is required (new IP or port is different than current ones) |
| 3451 | * - checks the update is allowed: |
| 3452 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3453 | * - allow all changes if no CHECKS are configured |
| 3454 | * - if CHECK is configured: |
| 3455 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3456 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3457 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3458 | * |
| 3459 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3460 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3461 | 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] | 3462 | { |
| 3463 | struct sockaddr_storage sa; |
| 3464 | int ret, port_change_required; |
| 3465 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3466 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3467 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3468 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3469 | |
| 3470 | msg = get_trash_chunk(); |
| 3471 | chunk_reset(msg); |
| 3472 | |
| 3473 | if (addr) { |
| 3474 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3475 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3476 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3477 | goto out; |
| 3478 | } |
| 3479 | |
| 3480 | /* changes are allowed on AF_INET* families only */ |
| 3481 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3482 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3483 | goto out; |
| 3484 | } |
| 3485 | |
| 3486 | /* collecting data currently setup */ |
| 3487 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3488 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3489 | /* changes are allowed on AF_INET* families only */ |
| 3490 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3491 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3492 | goto out; |
| 3493 | } |
| 3494 | |
| 3495 | /* applying ADDR changes if required and allowed |
| 3496 | * ipcmp returns 0 when both ADDR are the same |
| 3497 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3498 | if (ipcmp(&s->addr, &sa, 0) == 0) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3499 | chunk_appendf(msg, "no need to change the addr"); |
| 3500 | goto port; |
| 3501 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3502 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3503 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3504 | |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3505 | /* update report for caller */ |
| 3506 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3507 | } |
| 3508 | |
| 3509 | port: |
| 3510 | if (port) { |
| 3511 | char sign = '\0'; |
| 3512 | char *endptr; |
| 3513 | |
| 3514 | if (addr) |
| 3515 | chunk_appendf(msg, ", "); |
| 3516 | |
| 3517 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3518 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3519 | |
| 3520 | /* check if PORT change is required */ |
| 3521 | port_change_required = 0; |
| 3522 | |
| 3523 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3524 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3525 | new_port = strtol(port, &endptr, 10); |
| 3526 | if ((errno != 0) || (port == endptr)) { |
| 3527 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3528 | goto out; |
| 3529 | } |
| 3530 | |
| 3531 | /* check if caller triggers a port mapped or offset */ |
| 3532 | if (sign == '-' || (sign == '+')) { |
| 3533 | /* check if server currently uses port map */ |
| 3534 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3535 | /* switch from fixed port to port map mandatorily triggers |
| 3536 | * a port change */ |
| 3537 | port_change_required = 1; |
| 3538 | /* check is configured |
| 3539 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3540 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3541 | * to port mapping */ |
William Dauchy | 69f118d | 2021-02-03 22:30:07 +0100 | [diff] [blame] | 3542 | if (!s->check.port) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3543 | 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."); |
| 3544 | goto out; |
| 3545 | } |
| 3546 | } |
| 3547 | /* we're already using port maps */ |
| 3548 | else { |
| 3549 | port_change_required = current_port != new_port; |
| 3550 | } |
| 3551 | } |
| 3552 | /* fixed port */ |
| 3553 | else { |
| 3554 | port_change_required = current_port != new_port; |
| 3555 | } |
| 3556 | |
| 3557 | /* applying PORT changes if required and update response message */ |
| 3558 | if (port_change_required) { |
| 3559 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3560 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3561 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3562 | |
| 3563 | /* prepare message */ |
| 3564 | chunk_appendf(msg, "port changed from '"); |
| 3565 | if (s->flags & SRV_F_MAPPORTS) |
| 3566 | chunk_appendf(msg, "+"); |
| 3567 | chunk_appendf(msg, "%d' to '", current_port); |
| 3568 | |
| 3569 | if (sign == '-') { |
| 3570 | s->flags |= SRV_F_MAPPORTS; |
| 3571 | chunk_appendf(msg, "%c", sign); |
| 3572 | /* just use for result output */ |
| 3573 | new_port = -new_port; |
| 3574 | } |
| 3575 | else if (sign == '+') { |
| 3576 | s->flags |= SRV_F_MAPPORTS; |
| 3577 | chunk_appendf(msg, "%c", sign); |
| 3578 | } |
| 3579 | else { |
| 3580 | s->flags &= ~SRV_F_MAPPORTS; |
| 3581 | } |
| 3582 | |
| 3583 | chunk_appendf(msg, "%d'", new_port); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3584 | } |
| 3585 | else { |
| 3586 | chunk_appendf(msg, "no need to change the port"); |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | out: |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3591 | if (changed) { |
| 3592 | /* force connection cleanup on the given server */ |
| 3593 | srv_cleanup_connections(s); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3594 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3595 | srv_set_addr_desc(s, 1); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3596 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3597 | if (updater) |
| 3598 | chunk_appendf(msg, " by '%s'", updater); |
| 3599 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3600 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3601 | } |
| 3602 | |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3603 | /* |
| 3604 | * update server status based on result of SRV resolution |
| 3605 | * returns: |
| 3606 | * 0 if server status is updated |
| 3607 | * 1 if server status has not changed |
| 3608 | * |
| 3609 | * Must be called with the server lock held. |
| 3610 | */ |
| 3611 | int srvrq_update_srv_status(struct server *s, int has_no_ip) |
| 3612 | { |
| 3613 | if (!s->srvrq) |
| 3614 | return 1; |
| 3615 | |
| 3616 | /* since this server has an IP, it can go back in production */ |
| 3617 | if (has_no_ip == 0) { |
| 3618 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3619 | return 1; |
| 3620 | } |
| 3621 | |
| 3622 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3623 | return 1; |
| 3624 | |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3625 | 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] | 3626 | return 0; |
| 3627 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3628 | |
| 3629 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3630 | * update server status based on result of name resolution |
| 3631 | * returns: |
| 3632 | * 0 if server status is updated |
| 3633 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3634 | * |
| 3635 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3636 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3637 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3638 | { |
Emeric Brun | 750fe79 | 2020-12-23 16:51:12 +0100 | [diff] [blame] | 3639 | struct resolvers *resolvers = s->resolvers; |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3640 | struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3641 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3642 | |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3643 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3644 | if (resolution == NULL) |
| 3645 | return srvrq_update_srv_status(s, has_no_ip); |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3646 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3647 | switch (resolution->status) { |
| 3648 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3649 | /* status when HAProxy has just (re)started. |
| 3650 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3651 | break; |
| 3652 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3653 | case RSLV_STATUS_VALID: |
| 3654 | /* |
| 3655 | * resume health checks |
| 3656 | * server will be turned back on if health check is safe |
| 3657 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3658 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3659 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3660 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3661 | 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] | 3662 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3663 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3664 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3665 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3666 | return 1; |
| 3667 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3668 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3669 | s->proxy->id, s->id); |
| 3670 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3671 | ha_warning("%s.\n", trash.area); |
| 3672 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3673 | return 0; |
| 3674 | |
| 3675 | case RSLV_STATUS_NX: |
| 3676 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3677 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3678 | if (!tick_is_expired(exp, now_ms)) |
| 3679 | break; |
| 3680 | |
| 3681 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3682 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3683 | 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] | 3684 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3685 | |
| 3686 | case RSLV_STATUS_TIMEOUT: |
| 3687 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3688 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3689 | if (!tick_is_expired(exp, now_ms)) |
| 3690 | break; |
| 3691 | |
| 3692 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3693 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3694 | 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] | 3695 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3696 | |
| 3697 | case RSLV_STATUS_REFUSED: |
| 3698 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3699 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3700 | if (!tick_is_expired(exp, now_ms)) |
| 3701 | break; |
| 3702 | |
| 3703 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3704 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3705 | 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] | 3706 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3707 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3708 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3709 | /* stop server if resolution failed for a long enough period */ |
| 3710 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3711 | if (!tick_is_expired(exp, now_ms)) |
| 3712 | break; |
| 3713 | |
| 3714 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3715 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3716 | 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] | 3717 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | return 1; |
| 3721 | } |
| 3722 | |
| 3723 | /* |
| 3724 | * Server Name Resolution valid response callback |
| 3725 | * It expects: |
| 3726 | * - <nameserver>: the name server which answered the valid response |
| 3727 | * - <response>: buffer containing a valid DNS response |
| 3728 | * - <response_len>: size of <response> |
| 3729 | * It performs the following actions: |
| 3730 | * - ignore response if current ip found and server family not met |
| 3731 | * - update with first new ip found if family is met and current IP is not found |
| 3732 | * returns: |
| 3733 | * 0 on error |
| 3734 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3735 | * |
| 3736 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3737 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3738 | int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3739 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3740 | struct server *s = NULL; |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3741 | struct resolv_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3742 | void *serverip, *firstip; |
| 3743 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3744 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3745 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3746 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3747 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3748 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3749 | if (!s) |
| 3750 | return 1; |
| 3751 | |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3752 | if (s->srvrq) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3753 | /* If DNS resolution is disabled ignore it. |
| 3754 | * This is the case if the server was associated to |
| 3755 | * a SRV record and this record is now expired. |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3756 | */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3757 | if (s->flags & SRV_F_NO_RESOLUTION) |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3758 | return 1; |
| 3759 | } |
| 3760 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3761 | resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3762 | if (!resolution) |
| 3763 | return 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3764 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3765 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3766 | firstip = NULL; /* pointer to the first valid response found */ |
| 3767 | /* it will be used as the new IP if a change is required */ |
| 3768 | firstip_sin_family = AF_UNSPEC; |
| 3769 | serverip = NULL; /* current server IP address */ |
| 3770 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3771 | /* initializing server IP pointer */ |
| 3772 | server_sin_family = s->addr.ss_family; |
| 3773 | switch (server_sin_family) { |
| 3774 | case AF_INET: |
| 3775 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3776 | break; |
| 3777 | |
| 3778 | case AF_INET6: |
| 3779 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3780 | break; |
| 3781 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3782 | case AF_UNSPEC: |
| 3783 | break; |
| 3784 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3785 | default: |
| 3786 | goto invalid; |
| 3787 | } |
| 3788 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3789 | ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts, |
| 3790 | serverip, server_sin_family, &firstip, |
| 3791 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3792 | |
| 3793 | switch (ret) { |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3794 | case RSLV_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3795 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3796 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3797 | case RSLV_UPD_SRVIP_NOT_FOUND: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3798 | goto save_ip; |
| 3799 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3800 | case RSLV_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3801 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3802 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3803 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3804 | case RSLV_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3805 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3806 | resolution->status = RSLV_STATUS_OTHER; |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3807 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3808 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3809 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3810 | default: |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3811 | has_no_ip = 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3812 | goto invalid; |
| 3813 | |
| 3814 | } |
| 3815 | |
| 3816 | save_ip: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3817 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3818 | counters->app.resolver.update++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3819 | /* save the first ip we found */ |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3820 | chunk_printf(chk, "%s/%s", counters->pid, counters->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3821 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3822 | else |
| 3823 | chunk_printf(chk, "DNS cache"); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3824 | srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3825 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3826 | update_status: |
Christopher Faulet | a8ce497 | 2021-06-24 15:26:03 +0200 | [diff] [blame] | 3827 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3828 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3829 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3830 | |
| 3831 | invalid: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3832 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3833 | counters->app.resolver.invalid++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3834 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 3835 | } |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3836 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3837 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3838 | return 0; |
| 3839 | } |
| 3840 | |
| 3841 | /* |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3842 | * SRV record error management callback |
| 3843 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3844 | * 0 if we can trash answser items. |
| 3845 | * 1 when safely ignored and we must kept answer items |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3846 | * |
| 3847 | * Grabs the server's lock. |
| 3848 | */ |
| 3849 | int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code) |
| 3850 | { |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3851 | struct resolv_srvrq *srvrq; |
| 3852 | struct resolv_resolution *res; |
| 3853 | struct resolvers *resolvers; |
| 3854 | int exp; |
| 3855 | |
| 3856 | /* SRV records */ |
| 3857 | srvrq = objt_resolv_srvrq(requester->owner); |
| 3858 | if (!srvrq) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3859 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3860 | |
| 3861 | resolvers = srvrq->resolvers; |
| 3862 | res = requester->resolution; |
| 3863 | |
| 3864 | switch (res->status) { |
| 3865 | |
| 3866 | case RSLV_STATUS_NX: |
| 3867 | /* stop server if resolution is NX for a long enough period */ |
| 3868 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 3869 | if (!tick_is_expired(exp, now_ms)) |
| 3870 | return 1; |
| 3871 | break; |
| 3872 | |
| 3873 | case RSLV_STATUS_TIMEOUT: |
| 3874 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 3875 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 3876 | if (!tick_is_expired(exp, now_ms)) |
| 3877 | return 1; |
| 3878 | break; |
| 3879 | |
| 3880 | case RSLV_STATUS_REFUSED: |
| 3881 | /* stop server if resolution is REFUSED for a long enough period */ |
| 3882 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 3883 | if (!tick_is_expired(exp, now_ms)) |
| 3884 | return 1; |
| 3885 | break; |
| 3886 | |
| 3887 | default: |
| 3888 | /* stop server if resolution failed for a long enough period */ |
| 3889 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 3890 | if (!tick_is_expired(exp, now_ms)) |
| 3891 | return 1; |
| 3892 | } |
| 3893 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3894 | /* Remove any associated server ref */ |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3895 | resolv_detach_from_resolution_answer_items(res, requester); |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3896 | |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3897 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3901 | * Server Name Resolution error management callback |
| 3902 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3903 | * 0 if we can trash answser items. |
| 3904 | * 1 when safely ignored and we must kept answer items |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3905 | * |
| 3906 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3907 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3908 | int snr_resolution_error_cb(struct resolv_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3909 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3910 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3911 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3912 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3913 | if (!s) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3914 | return 0; |
| 3915 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3916 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3917 | if (!snr_update_srv_status(s, 1)) { |
Christopher Faulet | 5130c21 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 3918 | memset(&s->addr, 0, sizeof(s->addr)); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3919 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3920 | resolv_detach_from_resolution_answer_items(requester->resolution, requester); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3921 | return 0; |
| 3922 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3923 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3924 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3925 | return 1; |
| 3926 | } |
| 3927 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3928 | /* |
| 3929 | * 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] | 3930 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3931 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 3932 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3933 | * |
| 3934 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3935 | */ |
| 3936 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 3937 | { |
| 3938 | struct server *tmpsrv; |
| 3939 | struct proxy *be; |
| 3940 | |
| 3941 | if (!srv) |
| 3942 | return NULL; |
| 3943 | |
| 3944 | be = srv->proxy; |
| 3945 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3946 | /* we found the current server is the same, ignore it */ |
| 3947 | if (srv == tmpsrv) |
| 3948 | continue; |
| 3949 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3950 | /* We want to compare the IP in the record with the IP of the servers in the |
| 3951 | * same backend, only if: |
| 3952 | * * DNS resolution is enabled on the server |
| 3953 | * * the hostname used for the resolution by our server is the same than the |
| 3954 | * one used for the server found in the backend |
| 3955 | * * the server found in the backend is not our current server |
| 3956 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3957 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3958 | if ((tmpsrv->hostname_dn == NULL) || |
| 3959 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3960 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3961 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3962 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3963 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3964 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3965 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3966 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3967 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3968 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3969 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3970 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3971 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3972 | /* At this point, we have 2 different servers using the same DNS hostname |
| 3973 | * for their respective resolution. |
| 3974 | */ |
| 3975 | if (*ip_family == tmpsrv->addr.ss_family && |
| 3976 | ((tmpsrv->addr.ss_family == AF_INET && |
| 3977 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 3978 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 3979 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3980 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3981 | return tmpsrv; |
| 3982 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3983 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3986 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3987 | return NULL; |
| 3988 | } |
| 3989 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3990 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 3991 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 3992 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 3993 | * not NULL, is filled up. |
| 3994 | */ |
| 3995 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 3996 | { |
| 3997 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3998 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 3999 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4000 | return 1; |
| 4001 | } |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4005 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 4006 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4007 | * |
| 4008 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4009 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4010 | 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] | 4011 | { |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4012 | struct resolv_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4013 | char *hostname_dn; |
| 4014 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4015 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 4016 | /* Note that the server lock is already held. */ |
| 4017 | if (!srv->resolvers) |
| 4018 | return -1; |
| 4019 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4020 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4021 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 4022 | /* run time DNS/SRV resolution was not active for this server |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4023 | * and we can't enable it at run time for now. |
| 4024 | */ |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 4025 | if (!srv->resolv_requester && !srv->srvrq) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4026 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4027 | |
| 4028 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4029 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4030 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 4031 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 4032 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4033 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4034 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4035 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 4036 | resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4037 | if (resolution && |
| 4038 | resolution->hostname_dn && |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 4039 | resolution->hostname_dn_len == hostname_dn_len && |
| 4040 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4041 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4042 | |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 4043 | resolv_unlink_resolution(srv->resolv_requester); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4044 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4045 | free(srv->hostname); |
| 4046 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4047 | srv->hostname = strdup(hostname); |
| 4048 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4049 | srv->hostname_dn_len = hostname_dn_len; |
| 4050 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4051 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4052 | |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4053 | if (srv->flags & SRV_F_NO_RESOLUTION) |
| 4054 | goto end; |
| 4055 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 4056 | if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4057 | goto err; |
| 4058 | |
| 4059 | end: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4060 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4061 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4062 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4063 | |
| 4064 | err: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4065 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4066 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4067 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4070 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 4071 | * from the state file. This is suited for initial address configuration. |
| 4072 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 4073 | * *err_code, if not NULL, is filled up. |
| 4074 | */ |
| 4075 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 4076 | { |
| 4077 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 4078 | if (err_code) |
| 4079 | *err_code |= ERR_WARN; |
| 4080 | return 1; |
| 4081 | } |
| 4082 | return 0; |
| 4083 | } |
| 4084 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4085 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 4086 | static int srv_iterate_initaddr(struct server *srv) |
| 4087 | { |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4088 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4089 | int return_code = 0; |
| 4090 | int err_code; |
| 4091 | unsigned int methods; |
| 4092 | |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4093 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 4094 | if (!name && srv->srvrq) |
| 4095 | name = srv->srvrq->name; |
| 4096 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4097 | methods = srv->init_addr_methods; |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4098 | if (!methods) { |
| 4099 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4100 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 4101 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4102 | if (srv->resolvers_id) { |
| 4103 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 4104 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4105 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4106 | } |
| 4107 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 4108 | /* "-dr" : always append "none" so that server addresses resolution |
| 4109 | * failures are silently ignored, this is convenient to validate some |
| 4110 | * configs out of their environment. |
| 4111 | */ |
| 4112 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 4113 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4114 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4115 | while (methods) { |
| 4116 | err_code = 0; |
| 4117 | switch (srv_get_next_initaddr(&methods)) { |
| 4118 | case SRV_IADDR_LAST: |
| 4119 | if (!srv->lastaddr) |
| 4120 | continue; |
| 4121 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4122 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4123 | return_code |= err_code; |
| 4124 | break; |
| 4125 | |
| 4126 | case SRV_IADDR_LIBC: |
| 4127 | if (!srv->hostname) |
| 4128 | continue; |
| 4129 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4130 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4131 | return_code |= err_code; |
| 4132 | break; |
| 4133 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4134 | case SRV_IADDR_NONE: |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 4135 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4136 | if (return_code) { |
Christopher Faulet | 521d99f | 2023-07-20 17:25:54 +0200 | [diff] [blame] | 4137 | ha_notice("could not resolve address '%s', disabling server.\n", |
| 4138 | name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4139 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4140 | return return_code; |
| 4141 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4142 | case SRV_IADDR_IP: |
| 4143 | ipcpy(&srv->init_addr, &srv->addr); |
| 4144 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4145 | ha_warning("could not resolve address '%s', falling back to configured address.\n", |
| 4146 | name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4147 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4148 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4149 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4150 | default: /* unhandled method */ |
| 4151 | break; |
| 4152 | } |
| 4153 | } |
| 4154 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4155 | if (!return_code) |
| 4156 | ha_alert("no method found to resolve address '%s'.\n", name); |
| 4157 | else |
| 4158 | ha_alert("could not resolve address '%s'.\n", name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4159 | |
| 4160 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4161 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4162 | out: |
| 4163 | srv_set_dyncookie(srv); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 4164 | srv_set_addr_desc(srv, 1); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4165 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4166 | } |
| 4167 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4168 | /* |
| 4169 | * This function parses all backends and all servers within each backend |
| 4170 | * and performs servers' addr resolution based on information provided by: |
| 4171 | * - configuration file |
| 4172 | * - server-state file (states provided by an 'old' haproxy process) |
| 4173 | * |
| 4174 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4175 | */ |
| 4176 | int srv_init_addr(void) |
| 4177 | { |
| 4178 | struct proxy *curproxy; |
| 4179 | int return_code = 0; |
| 4180 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4181 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4182 | while (curproxy) { |
| 4183 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4184 | |
| 4185 | /* servers are in backend only */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4186 | 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] | 4187 | goto srv_init_addr_next; |
| 4188 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4189 | for (srv = curproxy->srv; srv; srv = srv->next) { |
| 4190 | set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4191 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4192 | return_code |= srv_iterate_initaddr(srv); |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4193 | reset_usermsgs_ctx(); |
| 4194 | } |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4195 | |
| 4196 | srv_init_addr_next: |
| 4197 | curproxy = curproxy->next; |
| 4198 | } |
| 4199 | |
| 4200 | return return_code; |
| 4201 | } |
| 4202 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4203 | /* |
| 4204 | * Must be called with the server lock held. |
| 4205 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4206 | 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] | 4207 | { |
| 4208 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4209 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4210 | |
| 4211 | msg = get_trash_chunk(); |
| 4212 | chunk_reset(msg); |
| 4213 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4214 | if (server->hostname && strcmp(fqdn, server->hostname) == 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4215 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4216 | goto out; |
| 4217 | } |
| 4218 | |
| 4219 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4220 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4221 | goto out; |
| 4222 | } |
| 4223 | |
| 4224 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4225 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4226 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4227 | if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4228 | chunk_reset(msg); |
| 4229 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4230 | server->proxy->id, server->id); |
| 4231 | goto out; |
| 4232 | } |
| 4233 | |
| 4234 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4235 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4236 | |
| 4237 | out: |
| 4238 | if (updater) |
| 4239 | chunk_appendf(msg, " by '%s'", updater); |
| 4240 | chunk_appendf(msg, "\n"); |
| 4241 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4242 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4246 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4247 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4248 | * 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] | 4249 | * used for CLI commands requiring a server name. |
| 4250 | * Important: the <arg> is modified to remove the '/'. |
| 4251 | */ |
| 4252 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4253 | { |
| 4254 | struct proxy *px; |
| 4255 | struct server *sv; |
| 4256 | char *line; |
| 4257 | |
| 4258 | /* split "backend/server" and make <line> point to server */ |
| 4259 | for (line = arg; *line; line++) |
| 4260 | if (*line == '/') { |
| 4261 | *line++ = '\0'; |
| 4262 | break; |
| 4263 | } |
| 4264 | |
| 4265 | if (!*line || !*arg) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4266 | cli_err(appctx, "Require 'backend/server'.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4267 | return NULL; |
| 4268 | } |
| 4269 | |
| 4270 | if (!get_backend_server(arg, line, &px, &sv)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4271 | cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4272 | return NULL; |
| 4273 | } |
| 4274 | |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4275 | if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4276 | cli_err(appctx, "Proxy is disabled.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4277 | return NULL; |
| 4278 | } |
| 4279 | |
| 4280 | return sv; |
| 4281 | } |
| 4282 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4283 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4284 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4285 | 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] | 4286 | { |
| 4287 | struct server *sv; |
| 4288 | const char *warning; |
| 4289 | |
| 4290 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4291 | return 1; |
| 4292 | |
| 4293 | sv = cli_find_server(appctx, args[2]); |
| 4294 | if (!sv) |
| 4295 | return 1; |
| 4296 | |
| 4297 | if (strcmp(args[3], "weight") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4298 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4299 | warning = server_parse_weight_change_request(sv, args[4]); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4300 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4301 | if (warning) |
| 4302 | cli_err(appctx, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4303 | } |
| 4304 | else if (strcmp(args[3], "state") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4305 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4306 | if (strcmp(args[4], "ready") == 0) |
| 4307 | srv_adm_set_ready(sv); |
| 4308 | else if (strcmp(args[4], "drain") == 0) |
| 4309 | srv_adm_set_drain(sv); |
| 4310 | else if (strcmp(args[4], "maint") == 0) |
| 4311 | srv_adm_set_maint(sv); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4312 | else |
| 4313 | 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] | 4314 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4315 | } |
| 4316 | else if (strcmp(args[3], "health") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4317 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4318 | if (sv->track) |
| 4319 | cli_err(appctx, "cannot change health on a tracking server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4320 | else if (strcmp(args[4], "up") == 0) { |
| 4321 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4322 | srv_set_running(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4323 | } |
| 4324 | else if (strcmp(args[4], "stopping") == 0) { |
| 4325 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4326 | srv_set_stopping(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4327 | } |
| 4328 | else if (strcmp(args[4], "down") == 0) { |
| 4329 | sv->check.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4330 | srv_set_stopped(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4331 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4332 | else |
| 4333 | 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] | 4334 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4335 | } |
| 4336 | else if (strcmp(args[3], "agent") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4337 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4338 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4339 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4340 | else if (strcmp(args[4], "up") == 0) { |
| 4341 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4342 | srv_set_running(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4343 | } |
| 4344 | else if (strcmp(args[4], "down") == 0) { |
| 4345 | sv->agent.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4346 | srv_set_stopped(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4347 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4348 | else |
| 4349 | cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4350 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4351 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4352 | else if (strcmp(args[3], "agent-addr") == 0) { |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4353 | char *addr = NULL; |
| 4354 | char *port = NULL; |
| 4355 | if (strlen(args[4]) == 0) { |
| 4356 | cli_err(appctx, "set server <b>/<s> agent-addr requires" |
| 4357 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4358 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4359 | } |
| 4360 | addr = args[4]; |
| 4361 | if (strcmp(args[5], "port") == 0) |
| 4362 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4363 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4364 | warning = srv_update_agent_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4365 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4366 | if (warning) |
| 4367 | cli_msg(appctx, LOG_WARNING, warning); |
| 4368 | } |
| 4369 | else if (strcmp(args[3], "agent-port") == 0) { |
| 4370 | char *port = NULL; |
| 4371 | if (strlen(args[4]) == 0) { |
| 4372 | cli_err(appctx, "set server <b>/<s> agent-port requires" |
| 4373 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4374 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4375 | } |
| 4376 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4377 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4378 | warning = srv_update_agent_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4379 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4380 | if (warning) |
| 4381 | cli_msg(appctx, LOG_WARNING, warning); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4382 | } |
| 4383 | else if (strcmp(args[3], "agent-send") == 0) { |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4384 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4385 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4386 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
| 4387 | else { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4388 | if (!set_srv_agent_send(sv, args[4])) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4389 | cli_err(appctx, "cannot allocate memory for new string.\n"); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4390 | } |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4391 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4392 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4393 | else if (strcmp(args[3], "check-addr") == 0) { |
| 4394 | char *addr = NULL; |
| 4395 | char *port = NULL; |
| 4396 | if (strlen(args[4]) == 0) { |
| 4397 | cli_err(appctx, "set server <b>/<s> check-addr requires" |
| 4398 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4399 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4400 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4401 | addr = args[4]; |
| 4402 | if (strcmp(args[5], "port") == 0) |
| 4403 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4404 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4405 | warning = srv_update_check_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4406 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4407 | if (warning) |
| 4408 | cli_msg(appctx, LOG_WARNING, warning); |
| 4409 | } |
| 4410 | else if (strcmp(args[3], "check-port") == 0) { |
| 4411 | char *port = NULL; |
| 4412 | if (strlen(args[4]) == 0) { |
| 4413 | cli_err(appctx, "set server <b>/<s> check-port requires" |
| 4414 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4415 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4416 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4417 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4418 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4419 | warning = srv_update_check_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4420 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4421 | if (warning) |
| 4422 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4423 | } |
| 4424 | else if (strcmp(args[3], "addr") == 0) { |
| 4425 | char *addr = NULL; |
| 4426 | char *port = NULL; |
| 4427 | if (strlen(args[4]) == 0) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4428 | 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] | 4429 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4430 | } |
| 4431 | else { |
| 4432 | addr = args[4]; |
| 4433 | } |
| 4434 | if (strcmp(args[5], "port") == 0) { |
| 4435 | port = args[6]; |
| 4436 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4437 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4438 | warning = srv_update_addr_port(sv, addr, port, "stats socket command"); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4439 | if (warning) |
| 4440 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4441 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4442 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4443 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4444 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4445 | if (!*args[4]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4446 | cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4447 | goto out; |
| 4448 | } |
| 4449 | if (!sv->resolvers) { |
| 4450 | cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n"); |
| 4451 | goto out; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4452 | } |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4453 | if (sv->srvrq) { |
| 4454 | 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] | 4455 | goto out; |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4456 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4457 | HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock); |
| 4458 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4459 | /* ensure runtime resolver will process this new fqdn */ |
| 4460 | if (sv->flags & SRV_F_NO_RESOLUTION) { |
| 4461 | sv->flags &= ~SRV_F_NO_RESOLUTION; |
| 4462 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4463 | warning = srv_update_fqdn(sv, args[4], "stats socket command", 1); |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4464 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4465 | HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4466 | if (warning) |
| 4467 | cli_msg(appctx, LOG_WARNING, warning); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4468 | } |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4469 | else if (strcmp(args[3], "ssl") == 0) { |
| 4470 | #ifdef USE_OPENSSL |
Amaury Denoyelle | b89d3d3 | 2021-05-19 15:00:54 +0200 | [diff] [blame] | 4471 | if (sv->flags & SRV_F_DYNAMIC) { |
| 4472 | cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n"); |
| 4473 | goto out; |
| 4474 | } |
| 4475 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4476 | if (sv->ssl_ctx.ctx == NULL) { |
| 4477 | cli_err(appctx, "'set server <srv> ssl' cannot be set. " |
| 4478 | " default-server should define ssl settings\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4479 | goto out; |
| 4480 | } |
| 4481 | |
| 4482 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4483 | if (strcmp(args[4], "on") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4484 | srv_set_ssl(sv, 1); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4485 | } else if (strcmp(args[4], "off") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4486 | srv_set_ssl(sv, 0); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4487 | } else { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4488 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4489 | cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4490 | goto out; |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4491 | } |
| 4492 | srv_cleanup_connections(sv); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4493 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4494 | cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n"); |
| 4495 | #else |
| 4496 | cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n"); |
| 4497 | #endif |
| 4498 | } else { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4499 | cli_err(appctx, |
William Dauchy | 3f4ec7d | 2021-02-15 17:22:16 +0100 | [diff] [blame] | 4500 | "usage: set server <backend>/<server> " |
| 4501 | "addr | agent | agent-addr | agent-port | agent-send | " |
| 4502 | "check-addr | check-port | fqdn | health | ssl | " |
| 4503 | "state | weight\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4504 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4505 | out: |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4506 | return 1; |
| 4507 | } |
| 4508 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4509 | 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] | 4510 | { |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4511 | struct proxy *px; |
| 4512 | struct server *sv; |
| 4513 | char *line; |
| 4514 | |
| 4515 | |
| 4516 | /* split "backend/server" and make <line> point to server */ |
| 4517 | for (line = args[2]; *line; line++) |
| 4518 | if (*line == '/') { |
| 4519 | *line++ = '\0'; |
| 4520 | break; |
| 4521 | } |
| 4522 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4523 | if (!*line) |
| 4524 | return cli_err(appctx, "Require 'backend/server'.\n"); |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4525 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4526 | if (!get_backend_server(args[2], line, &px, &sv)) |
| 4527 | 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] | 4528 | |
| 4529 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4530 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4531 | sv->iweight); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 4532 | if (applet_putstr(appctx, trash.area) == -1) |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4533 | return 0; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4534 | return 1; |
| 4535 | } |
| 4536 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4537 | /* Parse a "set weight" command. |
| 4538 | * |
| 4539 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4540 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4541 | 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] | 4542 | { |
| 4543 | struct server *sv; |
| 4544 | const char *warning; |
| 4545 | |
| 4546 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4547 | return 1; |
| 4548 | |
| 4549 | sv = cli_find_server(appctx, args[2]); |
| 4550 | if (!sv) |
| 4551 | return 1; |
| 4552 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4553 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4554 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4555 | warning = server_parse_weight_change_request(sv, args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4556 | if (warning) |
| 4557 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4558 | |
| 4559 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4560 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4561 | return 1; |
| 4562 | } |
| 4563 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4564 | /* parse a "set maxconn server" command. It always returns 1. |
| 4565 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4566 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4567 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4568 | 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] | 4569 | { |
| 4570 | struct server *sv; |
| 4571 | const char *warning; |
| 4572 | |
| 4573 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4574 | return 1; |
| 4575 | |
| 4576 | sv = cli_find_server(appctx, args[3]); |
| 4577 | if (!sv) |
| 4578 | return 1; |
| 4579 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4580 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4581 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4582 | warning = server_parse_maxconn_change_request(sv, args[4]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4583 | if (warning) |
| 4584 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4585 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4586 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4587 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4588 | return 1; |
| 4589 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4590 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4591 | /* parse a "disable agent" command. It always returns 1. |
| 4592 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4593 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4594 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4595 | 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] | 4596 | { |
| 4597 | struct server *sv; |
| 4598 | |
| 4599 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4600 | return 1; |
| 4601 | |
| 4602 | sv = cli_find_server(appctx, args[2]); |
| 4603 | if (!sv) |
| 4604 | return 1; |
| 4605 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4606 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4607 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4608 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4609 | return 1; |
| 4610 | } |
| 4611 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4612 | /* parse a "disable health" command. It always returns 1. |
| 4613 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4614 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4615 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4616 | 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] | 4617 | { |
| 4618 | struct server *sv; |
| 4619 | |
| 4620 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4621 | return 1; |
| 4622 | |
| 4623 | sv = cli_find_server(appctx, args[2]); |
| 4624 | if (!sv) |
| 4625 | return 1; |
| 4626 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4627 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4628 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4629 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4630 | return 1; |
| 4631 | } |
| 4632 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4633 | /* parse a "disable server" command. It always returns 1. |
| 4634 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4635 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4636 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4637 | 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] | 4638 | { |
| 4639 | struct server *sv; |
| 4640 | |
| 4641 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4642 | return 1; |
| 4643 | |
| 4644 | sv = cli_find_server(appctx, args[2]); |
| 4645 | if (!sv) |
| 4646 | return 1; |
| 4647 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4648 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4649 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4650 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4651 | return 1; |
| 4652 | } |
| 4653 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4654 | /* parse a "enable agent" command. It always returns 1. |
| 4655 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4656 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4657 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4658 | 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] | 4659 | { |
| 4660 | struct server *sv; |
| 4661 | |
| 4662 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4663 | return 1; |
| 4664 | |
| 4665 | sv = cli_find_server(appctx, args[2]); |
| 4666 | if (!sv) |
| 4667 | return 1; |
| 4668 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4669 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) |
| 4670 | 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] | 4671 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4672 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4673 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4674 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4675 | return 1; |
| 4676 | } |
| 4677 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4678 | /* parse a "enable health" command. It always returns 1. |
| 4679 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4680 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4681 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4682 | 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] | 4683 | { |
| 4684 | struct server *sv; |
| 4685 | |
| 4686 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4687 | return 1; |
| 4688 | |
| 4689 | sv = cli_find_server(appctx, args[2]); |
| 4690 | if (!sv) |
| 4691 | return 1; |
| 4692 | |
Amaury Denoyelle | 0f456d5 | 2021-09-21 10:29:09 +0200 | [diff] [blame] | 4693 | if (!(sv->check.state & CHK_ST_CONFIGURED)) |
| 4694 | return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n"); |
| 4695 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4696 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4697 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4698 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4699 | return 1; |
| 4700 | } |
| 4701 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4702 | /* parse a "enable server" command. It always returns 1. |
| 4703 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4704 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4705 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4706 | 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] | 4707 | { |
| 4708 | struct server *sv; |
| 4709 | |
| 4710 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4711 | return 1; |
| 4712 | |
| 4713 | sv = cli_find_server(appctx, args[2]); |
| 4714 | if (!sv) |
| 4715 | return 1; |
| 4716 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4717 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4718 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4719 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4720 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4721 | sv->cookie) |
| 4722 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4723 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4724 | return 1; |
| 4725 | } |
| 4726 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4727 | /* Allocates data structure related to load balancing for the server <sv>. It |
| 4728 | * is only required for dynamic servers. |
| 4729 | * |
| 4730 | * At the moment, the server lock is not used as this function is only called |
| 4731 | * for a dynamic server not yet registered. |
| 4732 | * |
| 4733 | * Returns 1 on success, 0 on allocation failure. |
| 4734 | */ |
| 4735 | static int srv_alloc_lb(struct server *sv, struct proxy *be) |
| 4736 | { |
| 4737 | int node; |
| 4738 | |
| 4739 | sv->lb_tree = (sv->flags & SRV_F_BACKUP) ? |
| 4740 | &be->lbprm.chash.bck : &be->lbprm.chash.act; |
| 4741 | sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE; |
| 4742 | sv->lb_nodes_now = 0; |
| 4743 | |
Willy Tarreau | dcb121f | 2021-04-20 11:37:45 +0200 | [diff] [blame] | 4744 | if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) || |
| 4745 | ((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] | 4746 | sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes)); |
| 4747 | |
| 4748 | if (!sv->lb_nodes) |
| 4749 | return 0; |
| 4750 | |
| 4751 | for (node = 0; node < sv->lb_nodes_tot; node++) { |
| 4752 | sv->lb_nodes[node].server = sv; |
| 4753 | sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node); |
| 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | return 1; |
| 4758 | } |
| 4759 | |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4760 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 4761 | * reached, the task automatically stops. Note that any server status change |
| 4762 | * must have updated s->last_change accordingly. |
| 4763 | */ |
| 4764 | static struct task *server_warmup(struct task *t, void *context, unsigned int state) |
| 4765 | { |
| 4766 | struct server *s = context; |
| 4767 | |
| 4768 | /* by default, plan on stopping the task */ |
| 4769 | t->expire = TICK_ETERNITY; |
| 4770 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 4771 | (s->next_state != SRV_ST_STARTING)) |
| 4772 | return t; |
| 4773 | |
| 4774 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4775 | |
| 4776 | /* recalculate the weights and update the state */ |
| 4777 | server_recalc_eweight(s, 1); |
| 4778 | |
| 4779 | /* probably that we can refill this server with a bit more connections */ |
| 4780 | pendconn_grab_from_px(s); |
| 4781 | |
| 4782 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4783 | |
| 4784 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 4785 | * whichever is greater, resulting in small 5% steps. |
| 4786 | */ |
| 4787 | if (s->next_state == SRV_ST_STARTING) |
| 4788 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 4789 | return t; |
| 4790 | } |
| 4791 | |
| 4792 | /* Allocate the slowstart task if the server is configured with a slowstart |
| 4793 | * timer. If server next_state is SRV_ST_STARTING, the task is scheduled. |
| 4794 | * |
| 4795 | * Returns 0 on success else non-zero. |
| 4796 | */ |
| 4797 | static int init_srv_slowstart(struct server *srv) |
| 4798 | { |
| 4799 | struct task *t; |
| 4800 | |
| 4801 | if (srv->slowstart) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4802 | if ((t = task_new_anywhere()) == NULL) { |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4803 | ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id); |
| 4804 | return ERR_ALERT | ERR_FATAL; |
| 4805 | } |
| 4806 | |
| 4807 | /* We need a warmup task that will be called when the server |
| 4808 | * state switches from down to up. |
| 4809 | */ |
| 4810 | srv->warmup = t; |
| 4811 | t->process = server_warmup; |
| 4812 | t->context = srv; |
| 4813 | |
| 4814 | /* server can be in this state only because of */ |
| 4815 | if (srv->next_state == SRV_ST_STARTING) { |
| 4816 | task_schedule(srv->warmup, |
| 4817 | tick_add(now_ms, |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 4818 | MS_TO_TICKS(MAX(1000, (ns_to_sec(now_ns) - srv->last_change)) / 20))); |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | return ERR_NONE; |
| 4823 | } |
| 4824 | REGISTER_POST_SERVER_CHECK(init_srv_slowstart); |
| 4825 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4826 | /* Memory allocation and initialization of the per_thr field. |
| 4827 | * Returns 0 if the field has been successfully initialized, -1 on failure. |
| 4828 | */ |
| 4829 | int srv_init_per_thr(struct server *srv) |
| 4830 | { |
| 4831 | int i; |
| 4832 | |
| 4833 | srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr)); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 4834 | srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp)); |
| 4835 | if (!srv->per_thr || !srv->per_tgrp) |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4836 | return -1; |
| 4837 | |
| 4838 | for (i = 0; i < global.nbthread; i++) { |
| 4839 | srv->per_thr[i].idle_conns = EB_ROOT; |
| 4840 | srv->per_thr[i].safe_conns = EB_ROOT; |
| 4841 | srv->per_thr[i].avail_conns = EB_ROOT; |
| 4842 | MT_LIST_INIT(&srv->per_thr[i].streams); |
| 4843 | } |
| 4844 | |
| 4845 | return 0; |
| 4846 | } |
| 4847 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4848 | /* Parse a "add server" command |
| 4849 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4850 | */ |
| 4851 | static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4852 | { |
| 4853 | struct proxy *be; |
| 4854 | struct server *srv; |
| 4855 | char *be_name, *sv_name; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4856 | int errcode, argc; |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4857 | int next_id; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4858 | const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR; |
| 4859 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4860 | usermsgs_clr("CLI"); |
| 4861 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4862 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4863 | return 1; |
| 4864 | |
| 4865 | ++args; |
| 4866 | |
| 4867 | sv_name = be_name = args[1]; |
| 4868 | /* split backend/server arg */ |
| 4869 | while (*sv_name && *(++sv_name)) { |
| 4870 | if (*sv_name == '/') { |
| 4871 | *sv_name = '\0'; |
| 4872 | ++sv_name; |
| 4873 | break; |
| 4874 | } |
| 4875 | } |
| 4876 | |
| 4877 | if (!*sv_name) |
| 4878 | return cli_err(appctx, "Require 'backend/server'."); |
| 4879 | |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4880 | be = proxy_be_by_name(be_name); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4881 | if (!be) |
| 4882 | return cli_err(appctx, "No such backend."); |
| 4883 | |
| 4884 | if (!(be->lbprm.algo & BE_LB_PROP_DYN)) { |
Amaury Denoyelle | eafd701 | 2021-04-29 14:59:42 +0200 | [diff] [blame] | 4885 | 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] | 4886 | return 1; |
| 4887 | } |
| 4888 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4889 | /* At this point, some operations might not be thread-safe anymore. This |
| 4890 | * might be the case for parsing handlers which were designed to run |
| 4891 | * only at the starting stage on single-thread mode. |
| 4892 | * |
| 4893 | * Activate thread isolation to ensure thread-safety. |
| 4894 | */ |
| 4895 | thread_isolate(); |
| 4896 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4897 | args[1] = sv_name; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4898 | errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags); |
| 4899 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4900 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4901 | |
| 4902 | while (*args[argc]) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4903 | errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4904 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4905 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4906 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4907 | } |
| 4908 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4909 | errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags); |
| 4910 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4911 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4912 | |
Amaury Denoyelle | efbf35c | 2021-06-10 17:34:10 +0200 | [diff] [blame] | 4913 | /* A dynamic server does not currently support resolution. |
| 4914 | * |
| 4915 | * Initialize it explicitly to the "none" method to ensure no |
| 4916 | * resolution will ever be executed. |
| 4917 | */ |
| 4918 | srv->init_addr_methods = SRV_IADDR_NONE; |
| 4919 | |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4920 | if (srv->mux_proto) { |
Aurelien DARRAGON | d0086f7 | 2023-10-19 16:15:50 +0200 | [diff] [blame] | 4921 | int proto_mode = conn_pr_mode_to_proto_mode(be->mode); |
| 4922 | const struct mux_proto_list *mux_ent; |
| 4923 | |
| 4924 | mux_ent = conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, proto_mode); |
| 4925 | |
| 4926 | if (!mux_ent || !isteq(mux_ent->token, srv->mux_proto->token)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4927 | ha_alert("MUX protocol is not usable for server.\n"); |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4928 | goto out; |
| 4929 | } |
| 4930 | } |
| 4931 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4932 | if (srv_init_per_thr(srv) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4933 | ha_alert("failed to allocate per-thread lists for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4934 | goto out; |
| 4935 | } |
| 4936 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4937 | if (srv->max_idle_conns != 0) { |
| 4938 | srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr)); |
| 4939 | if (!srv->curr_idle_thr) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4940 | ha_alert("failed to allocate counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4941 | goto out; |
| 4942 | } |
| 4943 | } |
| 4944 | |
| 4945 | if (!srv_alloc_lb(srv, be)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4946 | ha_alert("Failed to initialize load-balancing data.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4947 | goto out; |
| 4948 | } |
| 4949 | |
| 4950 | if (!stats_allocate_proxy_counters_internal(&srv->extra_counters, |
| 4951 | COUNTERS_SV, |
| 4952 | STATS_PX_CAP_SRV)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4953 | ha_alert("failed to allocate extra counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4954 | goto out; |
| 4955 | } |
| 4956 | |
Aurelien DARRAGON | 86207e7 | 2023-02-08 11:55:08 +0100 | [diff] [blame] | 4957 | /* ensure minconn/maxconn consistency */ |
| 4958 | srv_minmax_conn_apply(srv); |
| 4959 | |
Amaury Denoyelle | 79b90e8 | 2021-09-20 15:15:19 +0200 | [diff] [blame] | 4960 | if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) || |
| 4961 | srv->check.use_ssl == 1) { |
Amaury Denoyelle | 34897d2 | 2021-05-19 09:49:41 +0200 | [diff] [blame] | 4962 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 4963 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) |
| 4964 | goto out; |
| 4965 | } |
| 4966 | } |
| 4967 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4968 | if (srv->trackit) { |
| 4969 | if (srv_apply_track(srv, be)) |
| 4970 | goto out; |
| 4971 | } |
| 4972 | |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4973 | /* Init check/agent if configured. The check is manually disabled |
| 4974 | * because a dynamic server is started in a disable state. It must be |
| 4975 | * manually activated via a "enable health/agent" command. |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4976 | */ |
| 4977 | if (srv->do_check) { |
| 4978 | if (init_srv_check(srv)) |
| 4979 | goto out; |
| 4980 | |
| 4981 | srv->check.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4982 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4983 | |
| 4984 | if (srv->do_agent) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4985 | if (init_srv_agent_check(srv)) |
| 4986 | goto out; |
| 4987 | |
| 4988 | srv->agent.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4989 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4990 | |
Amaury Denoyelle | cd8a6f2 | 2021-09-21 11:51:54 +0200 | [diff] [blame] | 4991 | /* Init slowstart if needed. */ |
| 4992 | if (init_srv_slowstart(srv)) |
| 4993 | goto out; |
| 4994 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4995 | /* Attach the server to the end of the proxy linked list. Note that this |
| 4996 | * operation is not thread-safe so this is executed under thread |
| 4997 | * isolation. |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4998 | * |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4999 | * 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] | 5000 | */ |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 5001 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5002 | /* TODO use a double-linked list for px->srv */ |
| 5003 | if (be->srv) { |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 5004 | struct server *next = be->srv; |
| 5005 | |
| 5006 | while (1) { |
| 5007 | /* check for duplicate server */ |
Tim Duesterhus | c5aa113 | 2021-10-16 17:48:15 +0200 | [diff] [blame] | 5008 | if (strcmp(srv->id, next->id) == 0) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5009 | 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] | 5010 | goto out; |
| 5011 | } |
| 5012 | |
| 5013 | if (!next->next) |
| 5014 | break; |
| 5015 | |
| 5016 | next = next->next; |
| 5017 | } |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5018 | |
| 5019 | next->next = srv; |
| 5020 | } |
| 5021 | else { |
| 5022 | srv->next = be->srv; |
| 5023 | be->srv = srv; |
| 5024 | } |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 5025 | |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5026 | /* generate the server id if not manually specified */ |
| 5027 | if (!srv->puid) { |
| 5028 | next_id = get_next_id(&be->conf.used_server_id, 1); |
| 5029 | if (!next_id) { |
| 5030 | ha_alert("Cannot attach server : no id left in proxy\n"); |
| 5031 | goto out; |
| 5032 | } |
| 5033 | |
| 5034 | srv->conf.id.key = srv->puid = next_id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5035 | } |
Christopher Faulet | 70f8948 | 2021-12-07 18:49:44 +0100 | [diff] [blame] | 5036 | srv->conf.name.key = srv->id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5037 | |
| 5038 | /* insert the server in the backend trees */ |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 5039 | eb32_insert(&be->conf.used_server_id, &srv->conf.id); |
| 5040 | ebis_insert(&be->conf.used_server_name, &srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5041 | /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */ |
| 5042 | if (srv->addr_node.key) |
| 5043 | ebis_insert(&be->used_server_addr, &srv->addr_node); |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5044 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5045 | /* check if LSB bit (odd bit) is set for reuse_cnt */ |
| 5046 | if (srv_id_reuse_cnt & 1) { |
| 5047 | /* cnt must be increased */ |
| 5048 | srv_id_reuse_cnt++; |
| 5049 | } |
| 5050 | /* srv_id_reuse_cnt is always even at this stage, divide by 2 to |
| 5051 | * save some space |
| 5052 | * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt)) |
| 5053 | */ |
| 5054 | srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0; |
| 5055 | |
Damien Claisse | 8ed726b | 2024-03-27 14:34:25 +0000 | [diff] [blame] | 5056 | /* generate new server's dynamic cookie if enabled on backend */ |
| 5057 | if (be->ck_opts & PR_CK_DYNAMIC) { |
| 5058 | srv_set_dyncookie(srv); |
| 5059 | } |
| 5060 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 5061 | /* adding server cannot fail when we reach this: |
| 5062 | * publishing EVENT_HDL_SUB_SERVER_ADD |
| 5063 | */ |
| 5064 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1); |
| 5065 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5066 | thread_release(); |
| 5067 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5068 | /* Start the check task. The server must be fully initialized. |
| 5069 | * |
| 5070 | * <srvpos> and <nbcheck> parameters are set to 1 as there should be no |
| 5071 | * need to randomly spread the task interval for dynamic servers. |
| 5072 | */ |
| 5073 | if (srv->check.state & CHK_ST_CONFIGURED) { |
| 5074 | if (!start_check_task(&srv->check, 0, 1, 1)) |
| 5075 | ha_alert("System might be unstable, consider to execute a reload"); |
| 5076 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5077 | if (srv->agent.state & CHK_ST_CONFIGURED) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5078 | if (!start_check_task(&srv->agent, 0, 1, 1)) |
| 5079 | ha_alert("System might be unstable, consider to execute a reload"); |
| 5080 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5081 | |
Amaury Denoyelle | ae8882d | 2024-03-27 10:50:21 +0100 | [diff] [blame] | 5082 | if (srv->cklen && be->mode != PR_MODE_HTTP) |
| 5083 | ha_warning("Ignoring cookie as HTTP mode is disabled.\n"); |
| 5084 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5085 | ha_notice("New server registered.\n"); |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 5086 | cli_umsg(appctx, LOG_INFO); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5087 | |
| 5088 | return 0; |
| 5089 | |
| 5090 | out: |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 5091 | if (srv) { |
| 5092 | if (srv->track) |
| 5093 | release_server_track(srv); |
| 5094 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5095 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5096 | free_check(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5097 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5098 | free_check(&srv->agent); |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5099 | |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 5100 | /* remove the server from the proxy linked list */ |
| 5101 | if (be->srv == srv) { |
| 5102 | be->srv = srv->next; |
| 5103 | } |
| 5104 | else { |
| 5105 | struct server *prev; |
| 5106 | for (prev = be->srv; prev && prev->next != srv; prev = prev->next) |
| 5107 | ; |
| 5108 | if (prev) |
| 5109 | prev->next = srv->next; |
| 5110 | } |
| 5111 | |
| 5112 | } |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 5113 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 5114 | thread_release(); |
| 5115 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5116 | if (!usermsgs_empty()) |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 5117 | cli_umsgerr(appctx); |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5118 | |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 5119 | if (srv) |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5120 | srv_drop(srv); |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5121 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5122 | return 1; |
| 5123 | } |
| 5124 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5125 | /* Parse a "del server" command |
| 5126 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 5127 | */ |
| 5128 | static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 5129 | { |
| 5130 | struct proxy *be; |
| 5131 | struct server *srv; |
| 5132 | char *be_name, *sv_name; |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5133 | struct server *prev_del; |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5134 | |
| 5135 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 5136 | return 1; |
| 5137 | |
| 5138 | ++args; |
| 5139 | |
| 5140 | sv_name = be_name = args[1]; |
| 5141 | /* split backend/server arg */ |
| 5142 | while (*sv_name && *(++sv_name)) { |
| 5143 | if (*sv_name == '/') { |
| 5144 | *sv_name = '\0'; |
| 5145 | ++sv_name; |
| 5146 | break; |
| 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | if (!*sv_name) |
| 5151 | return cli_err(appctx, "Require 'backend/server'."); |
| 5152 | |
| 5153 | /* 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] | 5154 | * requires thread isolation. In addition, any place referencing the |
| 5155 | * server about to be deleted would be unsafe after our operation, so |
| 5156 | * we must be certain to be alone so that no other thread has even |
| 5157 | * started to grab a temporary reference to this server. |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5158 | */ |
Willy Tarreau | ba3ab79 | 2021-08-04 14:42:37 +0200 | [diff] [blame] | 5159 | thread_isolate_full(); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5160 | |
| 5161 | get_backend_server(be_name, sv_name, &be, &srv); |
| 5162 | if (!be) { |
| 5163 | cli_err(appctx, "No such backend."); |
| 5164 | goto out; |
| 5165 | } |
| 5166 | |
| 5167 | if (!srv) { |
| 5168 | cli_err(appctx, "No such server."); |
| 5169 | goto out; |
| 5170 | } |
| 5171 | |
Amaury Denoyelle | 14c3c5c | 2021-08-23 14:10:51 +0200 | [diff] [blame] | 5172 | if (srv->flags & SRV_F_NON_PURGEABLE) { |
| 5173 | 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] | 5174 | goto out; |
| 5175 | } |
| 5176 | |
| 5177 | /* Only servers in maintenance can be deleted. This ensures that the |
| 5178 | * server is not present anymore in the lb structures (through |
| 5179 | * lbprm.set_server_status_down). |
| 5180 | */ |
| 5181 | if (!(srv->cur_admin & SRV_ADMF_MAINT)) { |
| 5182 | cli_err(appctx, "Only servers in maintenance mode can be deleted."); |
| 5183 | goto out; |
| 5184 | } |
| 5185 | |
| 5186 | /* Ensure that there is no active/idle/pending connection on the server. |
| 5187 | * |
| 5188 | * TODO idle connections should not prevent server deletion. A proper |
| 5189 | * cleanup function should be implemented to be used here. |
| 5190 | */ |
Amaury Denoyelle | d8514b9 | 2024-02-21 15:54:11 +0100 | [diff] [blame] | 5191 | if (srv->curr_used_conns || srv->curr_idle_conns || |
Amaury Denoyelle | 1d71bf5 | 2024-03-12 14:09:55 +0100 | [diff] [blame] | 5192 | !MT_LIST_ISEMPTY(&srv->sess_conns) || |
Aurelien DARRAGON | 4bc0909 | 2023-09-21 14:18:50 +0200 | [diff] [blame] | 5193 | !eb_is_empty(&srv->queue.head) || srv_has_streams(srv)) { |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5194 | cli_err(appctx, "Server still has connections attached to it, cannot remove it."); |
| 5195 | goto out; |
| 5196 | } |
| 5197 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 5198 | /* removing cannot fail anymore when we reach this: |
| 5199 | * publishing EVENT_HDL_SUB_SERVER_DEL |
| 5200 | */ |
| 5201 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1); |
| 5202 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5203 | /* remove srv from tracking list */ |
| 5204 | if (srv->track) |
| 5205 | release_server_track(srv); |
| 5206 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5207 | /* stop the check task if running */ |
| 5208 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5209 | check_purge(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5210 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5211 | check_purge(&srv->agent); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5212 | |
| 5213 | /* detach the server from the proxy linked list |
| 5214 | * The proxy servers list is currently not protected by a lock, so this |
| 5215 | * requires thread_isolate/release. |
| 5216 | */ |
| 5217 | |
| 5218 | /* be->srv cannot be empty since we have already found the server with |
| 5219 | * get_backend_server */ |
| 5220 | BUG_ON(!be->srv); |
| 5221 | if (be->srv == srv) { |
| 5222 | be->srv = srv->next; |
| 5223 | } |
| 5224 | else { |
| 5225 | struct server *next; |
Amaury Denoyelle | d6b4b6d | 2021-04-21 11:50:26 +0200 | [diff] [blame] | 5226 | for (next = be->srv; srv != next->next; next = next->next) { |
| 5227 | /* srv cannot be not found since we have already found |
| 5228 | * it with get_backend_server */ |
| 5229 | BUG_ON(!next); |
| 5230 | } |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5231 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5232 | next->next = srv->next; |
| 5233 | } |
| 5234 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5235 | /* Some deleted servers could still point to us using their 'next', |
| 5236 | * update them as needed |
| 5237 | * Please note the small race between the POP and APPEND, although in |
| 5238 | * this situation this is not an issue as we are under full thread |
| 5239 | * isolation |
| 5240 | */ |
| 5241 | while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) { |
| 5242 | /* update its 'next' ptr */ |
| 5243 | prev_del->next = srv->next; |
| 5244 | if (srv->next) { |
| 5245 | /* now it is our 'next' responsibility */ |
| 5246 | MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted); |
| 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | /* we ourselves need to inform our 'next' that we will still point it */ |
| 5251 | if (srv->next) |
| 5252 | MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted); |
| 5253 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5254 | /* remove srv from addr_node tree */ |
Amaury Denoyelle | 82d7f77 | 2021-06-09 16:00:43 +0200 | [diff] [blame] | 5255 | eb32_delete(&srv->conf.id); |
| 5256 | ebpt_delete(&srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5257 | if (srv->addr_node.key) |
| 5258 | ebpt_delete(&srv->addr_node); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5259 | |
| 5260 | /* remove srv from idle_node tree for idle conn cleanup */ |
| 5261 | eb32_delete(&srv->idle_node); |
| 5262 | |
Aurelien DARRAGON | 75b9d1c | 2023-01-24 14:40:01 +0100 | [diff] [blame] | 5263 | /* flag the server as deleted |
| 5264 | * (despite the server being removed from primary server list, |
| 5265 | * one could still access the server data from a valid ptr) |
| 5266 | * Deleted flag helps detecting when a server is in transient removal |
| 5267 | * state. |
| 5268 | * ie: removed from the list but not yet freed/purged from memory. |
| 5269 | */ |
| 5270 | srv->flags |= SRV_F_DELETED; |
| 5271 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5272 | /* set LSB bit (odd bit) for reuse_cnt */ |
| 5273 | srv_id_reuse_cnt |= 1; |
| 5274 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5275 | thread_release(); |
| 5276 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5277 | ha_notice("Server deleted.\n"); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5278 | srv_drop(srv); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5279 | |
| 5280 | cli_msg(appctx, LOG_INFO, "Server deleted."); |
| 5281 | |
| 5282 | return 0; |
| 5283 | |
| 5284 | out: |
| 5285 | thread_release(); |
| 5286 | |
| 5287 | return 1; |
| 5288 | } |
| 5289 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 5290 | /* register cli keywords */ |
| 5291 | static struct cli_kw_list cli_kws = {{ },{ |
Amaury Denoyelle | c755efd | 2021-08-03 18:05:37 +0200 | [diff] [blame] | 5292 | { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL }, |
| 5293 | { { "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] | 5294 | { { "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] | 5295 | { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL }, |
| 5296 | { { "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] | 5297 | { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL }, |
| 5298 | { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL }, |
| 5299 | { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server }, |
| 5300 | { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight }, |
| 5301 | { { "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] | 5302 | { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL }, |
| 5303 | { { "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] | 5304 | {{},} |
| 5305 | }}; |
| 5306 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5307 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 5308 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5309 | /* Prepare a server <srv> to track check status of another one. <srv>.<trackit> |
| 5310 | * field is used to retrieve the identifier of the tracked server, either with |
| 5311 | * the format "proxy/server" or just "server". <curproxy> must point to the |
| 5312 | * backend owning <srv>; if no proxy is specified in <trackit>, it will be used |
| 5313 | * to find the tracked server. |
| 5314 | * |
| 5315 | * Returns 0 if the server track has been activated else non-zero. |
| 5316 | * |
| 5317 | * Not thread-safe. |
| 5318 | */ |
| 5319 | int srv_apply_track(struct server *srv, struct proxy *curproxy) |
| 5320 | { |
| 5321 | struct proxy *px; |
| 5322 | struct server *strack, *loop; |
| 5323 | char *pname, *sname; |
| 5324 | |
| 5325 | if (!srv->trackit) |
| 5326 | return 1; |
| 5327 | |
| 5328 | pname = srv->trackit; |
| 5329 | sname = strrchr(pname, '/'); |
| 5330 | |
| 5331 | if (sname) { |
| 5332 | *sname++ = '\0'; |
| 5333 | } |
| 5334 | else { |
| 5335 | sname = pname; |
| 5336 | pname = NULL; |
| 5337 | } |
| 5338 | |
| 5339 | if (pname) { |
| 5340 | px = proxy_be_by_name(pname); |
| 5341 | if (!px) { |
| 5342 | ha_alert("unable to find required proxy '%s' for tracking.\n", |
| 5343 | pname); |
| 5344 | return 1; |
| 5345 | } |
| 5346 | } |
| 5347 | else { |
| 5348 | px = curproxy; |
| 5349 | } |
| 5350 | |
| 5351 | strack = findserver(px, sname); |
| 5352 | if (!strack) { |
| 5353 | ha_alert("unable to find required server '%s' for tracking.\n", |
| 5354 | sname); |
| 5355 | return 1; |
| 5356 | } |
| 5357 | |
Amaury Denoyelle | 79f68be | 2021-07-13 10:35:50 +0200 | [diff] [blame] | 5358 | if (strack->flags & SRV_F_DYNAMIC) { |
| 5359 | ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n", |
| 5360 | px->id, strack->id); |
| 5361 | return 1; |
| 5362 | } |
| 5363 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5364 | if (!strack->do_check && !strack->do_agent && !strack->track && |
| 5365 | !strack->trackit) { |
| 5366 | ha_alert("unable to use %s/%s for " |
| 5367 | "tracking as it does not have any check nor agent enabled.\n", |
| 5368 | px->id, strack->id); |
| 5369 | return 1; |
| 5370 | } |
| 5371 | |
| 5372 | for (loop = strack->track; loop && loop != srv; loop = loop->track) |
| 5373 | ; |
| 5374 | |
| 5375 | if (srv == strack || loop) { |
| 5376 | ha_alert("unable to track %s/%s as it " |
| 5377 | "belongs to a tracking chain looping back to %s/%s.\n", |
| 5378 | px->id, strack->id, px->id, |
| 5379 | srv == strack ? strack->id : loop->id); |
| 5380 | return 1; |
| 5381 | } |
| 5382 | |
| 5383 | if (curproxy != px && |
| 5384 | (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) { |
| 5385 | ha_alert("unable to use %s/%s for" |
| 5386 | "tracking: disable-on-404 option inconsistency.\n", |
| 5387 | px->id, strack->id); |
| 5388 | return 1; |
| 5389 | } |
| 5390 | |
| 5391 | srv->track = strack; |
| 5392 | srv->tracknext = strack->trackers; |
| 5393 | strack->trackers = srv; |
Amaury Denoyelle | 0626961 | 2021-08-23 14:05:07 +0200 | [diff] [blame] | 5394 | strack->flags |= SRV_F_NON_PURGEABLE; |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5395 | |
| 5396 | ha_free(&srv->trackit); |
| 5397 | |
| 5398 | return 0; |
| 5399 | } |
| 5400 | |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5401 | /* This function propagates srv state change to lb algorithms */ |
| 5402 | static void srv_lb_propagate(struct server *s) |
| 5403 | { |
| 5404 | struct proxy *px = s->proxy; |
| 5405 | |
| 5406 | if (px->lbprm.update_server_eweight) |
| 5407 | px->lbprm.update_server_eweight(s); |
| 5408 | else if (srv_willbe_usable(s)) { |
| 5409 | if (px->lbprm.set_server_status_up) |
| 5410 | px->lbprm.set_server_status_up(s); |
| 5411 | } |
| 5412 | else { |
| 5413 | if (px->lbprm.set_server_status_down) |
| 5414 | px->lbprm.set_server_status_down(s); |
| 5415 | } |
| 5416 | } |
| 5417 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5418 | /* directly update server state based on an operational change |
| 5419 | * (compare current and next state to know which transition to apply) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5420 | * |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5421 | * The function returns the number of requeued sessions (either taken by |
| 5422 | * the server or redispatched to others servers) due to the server state |
| 5423 | * change. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5424 | */ |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5425 | static int _srv_update_status_op(struct server *s, enum srv_op_st_chg_cause cause) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5426 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5427 | struct buffer *tmptrash = NULL; |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5428 | int log_level; |
| 5429 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 5430 | int xferred = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5431 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5432 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
| 5433 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5434 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5435 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5436 | srv_shutdown_streams(s, SF_ERR_DOWN); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5437 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5438 | /* we might have streams queued on this server and waiting for |
| 5439 | * a connection. Those which are redispatchable will be queued |
| 5440 | * to another server or to the proxy itself. |
| 5441 | */ |
| 5442 | xferred = pendconn_redistribute(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5443 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5444 | tmptrash = alloc_trash_chunk(); |
| 5445 | if (tmptrash) { |
| 5446 | chunk_printf(tmptrash, |
| 5447 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5448 | s->proxy->id, s->id); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5449 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5450 | srv_append_op_chg_cause(tmptrash, s, cause); |
| 5451 | srv_append_more(tmptrash, s, xferred, 0); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5452 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5453 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5454 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5455 | /* we don't send an alert if the server was previously paused */ |
| 5456 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
| 5457 | send_log(s->proxy, log_level, "%s.\n", |
| 5458 | tmptrash->area); |
| 5459 | send_email_alert(s, log_level, "%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5460 | tmptrash->area); |
Aurelien DARRAGON | 0d2f1ac | 2023-05-15 18:03:35 +0200 | [diff] [blame] | 5461 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5462 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5463 | } |
| 5464 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
| 5465 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5466 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5467 | /* we might have streams queued on this server and waiting for |
| 5468 | * a connection. Those which are redispatchable will be queued |
| 5469 | * to another server or to the proxy itself. |
| 5470 | */ |
| 5471 | xferred = pendconn_redistribute(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5472 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5473 | tmptrash = alloc_trash_chunk(); |
| 5474 | if (tmptrash) { |
| 5475 | chunk_printf(tmptrash, |
| 5476 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5477 | s->proxy->id, s->id); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5478 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5479 | srv_append_op_chg_cause(tmptrash, s, cause); |
| 5480 | srv_append_more(tmptrash, s, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5481 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5482 | ha_warning("%s.\n", tmptrash->area); |
| 5483 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5484 | tmptrash->area); |
| 5485 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5486 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5487 | } |
| 5488 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5489 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5490 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5491 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
| 5492 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5493 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5494 | server_recalc_eweight(s, 0); |
| 5495 | /* now propagate the status change to any LB algorithms */ |
| 5496 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5497 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5498 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5499 | * and it's not a backup server and its effective weight is > 0, |
| 5500 | * then it can accept new connections, so we shut down all streams |
| 5501 | * on all backup servers. |
| 5502 | */ |
| 5503 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5504 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5505 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5506 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5507 | /* check if we can handle some connections queued at the proxy. We |
| 5508 | * will take as many as we can handle. |
| 5509 | */ |
| 5510 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5511 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5512 | tmptrash = alloc_trash_chunk(); |
| 5513 | if (tmptrash) { |
| 5514 | chunk_printf(tmptrash, |
| 5515 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5516 | s->proxy->id, s->id); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5517 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5518 | srv_append_op_chg_cause(tmptrash, s, cause); |
| 5519 | srv_append_more(tmptrash, s, xferred, 0); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5520 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5521 | ha_warning("%s.\n", tmptrash->area); |
| 5522 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5523 | tmptrash->area); |
| 5524 | send_email_alert(s, LOG_NOTICE, "%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5525 | tmptrash->area); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5526 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5527 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5528 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5529 | else if (s->cur_eweight != s->next_eweight) { |
| 5530 | /* now propagate the status change to any LB algorithms */ |
| 5531 | srv_lb_propagate(s); |
| 5532 | } |
| 5533 | return xferred; |
| 5534 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5535 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5536 | /* deduct and update server state from an administrative change |
| 5537 | * (use current and next admin to deduct the administrative transition that |
| 5538 | * may result in server state update) |
| 5539 | * |
| 5540 | * The function returns the number of requeued sessions (either taken by |
| 5541 | * the server or redispatched to others servers) due to the server state |
| 5542 | * change. |
| 5543 | */ |
| 5544 | static int _srv_update_status_adm(struct server *s, enum srv_adm_st_chg_cause cause) |
| 5545 | { |
| 5546 | struct buffer *tmptrash = NULL; |
| 5547 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 5548 | int xferred = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5549 | |
| 5550 | /* Maintenance must also disable health checks */ |
| 5551 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5552 | if (s->check.state & CHK_ST_ENABLED) { |
| 5553 | s->check.state |= CHK_ST_PAUSED; |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame] | 5554 | s->check.health = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5558 | tmptrash = alloc_trash_chunk(); |
| 5559 | if (tmptrash) { |
| 5560 | chunk_printf(tmptrash, |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5561 | "%sServer %s/%s was DOWN and now enters maintenance", |
| 5562 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5563 | srv_append_adm_chg_cause(tmptrash, s, cause); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5564 | srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5565 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5566 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5567 | ha_warning("%s.\n", tmptrash->area); |
| 5568 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5569 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5570 | } |
| 5571 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | else { /* server was still running */ |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame] | 5575 | s->check.health = 0; /* failure */ |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5576 | |
| 5577 | s->next_state = SRV_ST_STOPPED; |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5578 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5579 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5580 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5581 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5582 | |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5583 | /* force connection cleanup on the given server */ |
| 5584 | srv_cleanup_connections(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5585 | /* we might have streams queued on this server and waiting for |
| 5586 | * a connection. Those which are redispatchable will be queued |
| 5587 | * to another server or to the proxy itself. |
| 5588 | */ |
| 5589 | xferred = pendconn_redistribute(s); |
| 5590 | |
| 5591 | tmptrash = alloc_trash_chunk(); |
| 5592 | if (tmptrash) { |
| 5593 | chunk_printf(tmptrash, |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5594 | "%sServer %s/%s is going DOWN for maintenance", |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5595 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5596 | s->proxy->id, s->id); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5597 | srv_append_adm_chg_cause(tmptrash, s, cause); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5598 | srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5599 | |
| 5600 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5601 | ha_warning("%s.\n", tmptrash->area); |
| 5602 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5603 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5604 | } |
| 5605 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5606 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5607 | } |
| 5608 | } |
| 5609 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5610 | /* OK here we're leaving maintenance, we have many things to check, |
| 5611 | * because the server might possibly be coming back up depending on |
| 5612 | * its state. In practice, leaving maintenance means that we should |
| 5613 | * immediately turn to UP (more or less the slowstart) under the |
| 5614 | * following conditions : |
| 5615 | * - server is neither checked nor tracked |
| 5616 | * - server tracks another server which is not checked |
| 5617 | * - server tracks another server which is already up |
| 5618 | * Which sums up as something simpler : |
| 5619 | * "either the tracking server is up or the server's checks are disabled |
| 5620 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5621 | * case associated to the stopping state which can be inherited. Note |
| 5622 | * that the server might still be in drain mode, which is naturally dealt |
| 5623 | * with by the lower level functions. |
| 5624 | */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5625 | if (s->check.state & CHK_ST_ENABLED) { |
| 5626 | s->check.state &= ~CHK_ST_PAUSED; |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame] | 5627 | s->check.health = s->check.rise; /* start OK but check immediately */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5631 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5632 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5633 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5634 | s->next_state = SRV_ST_STOPPING; |
| 5635 | } |
| 5636 | else { |
| 5637 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5638 | if (s->slowstart > 0) { |
| 5639 | if (s->warmup) |
| 5640 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5641 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5642 | else |
| 5643 | s->next_state = SRV_ST_RUNNING; |
| 5644 | } |
| 5645 | |
| 5646 | } |
| 5647 | |
| 5648 | tmptrash = alloc_trash_chunk(); |
| 5649 | if (tmptrash) { |
| 5650 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5651 | chunk_printf(tmptrash, |
| 5652 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5653 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5654 | s->proxy->id, s->id, |
| 5655 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5656 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5657 | } |
| 5658 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5659 | chunk_printf(tmptrash, |
| 5660 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5661 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5662 | s->proxy->id, s->id, s->hostname, |
| 5663 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5664 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5665 | } |
| 5666 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5667 | chunk_printf(tmptrash, |
| 5668 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5669 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5670 | s->proxy->id, s->id, |
| 5671 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5672 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5673 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5674 | ha_warning("%s.\n", tmptrash->area); |
| 5675 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5676 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5677 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5678 | } |
| 5679 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5680 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5681 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5682 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5683 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5684 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5685 | * and it's not a backup server and its effective weight is > 0, |
| 5686 | * then it can accept new connections, so we shut down all streams |
| 5687 | * on all backup servers. |
| 5688 | */ |
| 5689 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5690 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5691 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5692 | |
| 5693 | /* check if we can handle some connections queued at the proxy. We |
| 5694 | * will take as many as we can handle. |
| 5695 | */ |
| 5696 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5697 | } |
| 5698 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5699 | /* remaining in maintenance mode, let's inform precisely about the |
| 5700 | * situation. |
| 5701 | */ |
| 5702 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5703 | tmptrash = alloc_trash_chunk(); |
| 5704 | if (tmptrash) { |
| 5705 | chunk_printf(tmptrash, |
| 5706 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5707 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5708 | s->proxy->id, s->id); |
| 5709 | |
| 5710 | if (s->track) /* normally it's mandatory here */ |
| 5711 | chunk_appendf(tmptrash, " via %s/%s", |
| 5712 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5713 | ha_warning("%s.\n", tmptrash->area); |
| 5714 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5715 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5716 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5717 | } |
| 5718 | } |
| 5719 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5720 | tmptrash = alloc_trash_chunk(); |
| 5721 | if (tmptrash) { |
| 5722 | chunk_printf(tmptrash, |
| 5723 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5724 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5725 | s->proxy->id, s->id, s->hostname); |
| 5726 | |
| 5727 | if (s->track) /* normally it's mandatory here */ |
| 5728 | chunk_appendf(tmptrash, " via %s/%s", |
| 5729 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5730 | ha_warning("%s.\n", tmptrash->area); |
| 5731 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5732 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5733 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5734 | } |
| 5735 | } |
| 5736 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5737 | tmptrash = alloc_trash_chunk(); |
| 5738 | if (tmptrash) { |
| 5739 | chunk_printf(tmptrash, |
| 5740 | "%sServer %s/%s remains in forced maintenance", |
| 5741 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5742 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5743 | ha_warning("%s.\n", tmptrash->area); |
| 5744 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5745 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5746 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5747 | } |
| 5748 | } |
| 5749 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5753 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5754 | /* drain state is applied only if not yet in maint */ |
| 5755 | |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5756 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5757 | |
| 5758 | /* we might have streams queued on this server and waiting for |
| 5759 | * a connection. Those which are redispatchable will be queued |
| 5760 | * to another server or to the proxy itself. |
| 5761 | */ |
| 5762 | xferred = pendconn_redistribute(s); |
| 5763 | |
| 5764 | tmptrash = alloc_trash_chunk(); |
| 5765 | if (tmptrash) { |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5766 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state", |
| 5767 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5768 | srv_append_adm_chg_cause(tmptrash, s, cause); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5769 | srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5770 | |
| 5771 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5772 | ha_warning("%s.\n", tmptrash->area); |
| 5773 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5774 | tmptrash->area); |
| 5775 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5776 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5777 | } |
| 5778 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5779 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5780 | } |
| 5781 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5782 | /* OK completely leaving drain mode */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5783 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5784 | |
| 5785 | tmptrash = alloc_trash_chunk(); |
| 5786 | if (tmptrash) { |
Aurelien DARRAGON | 9a1df02 | 2023-03-27 10:17:31 +0200 | [diff] [blame] | 5787 | if (s->cur_admin & SRV_ADMF_FDRAIN) { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5788 | chunk_printf(tmptrash, |
| 5789 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5790 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5791 | s->proxy->id, s->id, |
| 5792 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5793 | } |
| 5794 | else { |
| 5795 | chunk_printf(tmptrash, |
| 5796 | "%sServer %s/%s is %s (leaving drain)", |
| 5797 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5798 | s->proxy->id, s->id, |
| 5799 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5800 | if (s->track) /* normally it's mandatory here */ |
| 5801 | chunk_appendf(tmptrash, " via %s/%s", |
| 5802 | s->track->proxy->id, s->track->id); |
| 5803 | } |
| 5804 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5805 | ha_warning("%s.\n", tmptrash->area); |
| 5806 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5807 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5808 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5809 | } |
| 5810 | |
| 5811 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5812 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5813 | } |
| 5814 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5815 | /* remaining in drain mode after removing one of its flags */ |
| 5816 | |
| 5817 | tmptrash = alloc_trash_chunk(); |
| 5818 | if (tmptrash) { |
| 5819 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5820 | chunk_printf(tmptrash, |
Aurelien DARRAGON | 977688b | 2023-05-03 10:36:54 +0200 | [diff] [blame] | 5821 | "%sServer %s/%s remains in drain mode", |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5822 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5823 | s->proxy->id, s->id); |
| 5824 | |
| 5825 | if (s->track) /* normally it's mandatory here */ |
| 5826 | chunk_appendf(tmptrash, " via %s/%s", |
| 5827 | s->track->proxy->id, s->track->id); |
| 5828 | } |
| 5829 | else { |
| 5830 | chunk_printf(tmptrash, |
| 5831 | "%sServer %s/%s remains in forced drain mode", |
| 5832 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5833 | s->proxy->id, s->id); |
| 5834 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5835 | ha_warning("%s.\n", tmptrash->area); |
| 5836 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5837 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5838 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5839 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5840 | } |
| 5841 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5842 | return xferred; |
| 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | * This function applies server's status changes. |
| 5847 | * |
| 5848 | * Must be called with the server lock held. This may also be called at init |
| 5849 | * time as the result of parsing the state file, in which case no lock will be |
| 5850 | * held, and the server's warmup task can be null. |
| 5851 | * <type> should be 0 for operational and 1 for administrative |
| 5852 | * <cause> must be srv_op_st_chg_cause enum for operational and |
| 5853 | * srv_adm_st_chg_cause enum for administrative |
| 5854 | */ |
| 5855 | static void srv_update_status(struct server *s, int type, int cause) |
| 5856 | { |
| 5857 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 5858 | enum srv_state srv_prev_state = s->cur_state; |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5859 | union { |
| 5860 | struct event_hdl_cb_data_server_state state; |
Aurelien DARRAGON | a163d65 | 2023-04-21 18:06:58 +0200 | [diff] [blame] | 5861 | struct event_hdl_cb_data_server_admin admin; |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5862 | struct event_hdl_cb_data_server common; |
| 5863 | } cb_data; |
| 5864 | int requeued; |
| 5865 | |
| 5866 | /* prepare common server event data */ |
| 5867 | _srv_event_hdl_prepare(&cb_data.common, s, 0); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5868 | |
Aurelien DARRAGON | a163d65 | 2023-04-21 18:06:58 +0200 | [diff] [blame] | 5869 | if (type) { |
| 5870 | cb_data.admin.safe.cause = cause; |
| 5871 | cb_data.admin.safe.old_admin = s->cur_admin; |
| 5872 | cb_data.admin.safe.new_admin = s->next_admin; |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5873 | requeued = _srv_update_status_adm(s, cause); |
Aurelien DARRAGON | a163d65 | 2023-04-21 18:06:58 +0200 | [diff] [blame] | 5874 | cb_data.admin.safe.requeued = requeued; |
| 5875 | /* publish admin change */ |
| 5876 | _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADMIN, cb_data.admin, s); |
| 5877 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5878 | else |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5879 | requeued = _srv_update_status_op(s, cause); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5880 | |
Aurelien DARRAGON | 22151c7 | 2023-04-17 17:45:58 +0200 | [diff] [blame] | 5881 | /* explicitly commit state changes (even if it was already applied implicitly |
| 5882 | * by some lb state change function), so we don't miss anything |
| 5883 | */ |
| 5884 | srv_lb_commit_status(s); |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5885 | |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5886 | /* check if server stats must be updated due the the server state change */ |
| 5887 | if (srv_prev_state != s->cur_state) { |
| 5888 | if (srv_prev_state == SRV_ST_STOPPED) { |
| 5889 | /* server was down and no longer is */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 5890 | if (s->last_change < ns_to_sec(now_ns)) // ignore negative times |
| 5891 | s->down_time += ns_to_sec(now_ns) - s->last_change; |
Aurelien DARRAGON | c249f6d | 2023-04-19 22:34:01 +0200 | [diff] [blame] | 5892 | _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, cb_data.common, s); |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5893 | } |
Aurelien DARRAGON | 5f80f8b | 2023-04-19 18:22:21 +0200 | [diff] [blame] | 5894 | else if (s->cur_state == SRV_ST_STOPPED) { |
| 5895 | /* server was up and is currently down */ |
| 5896 | s->counters.down_trans++; |
Aurelien DARRAGON | c249f6d | 2023-04-19 22:34:01 +0200 | [diff] [blame] | 5897 | _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, cb_data.common, s); |
Aurelien DARRAGON | 5f80f8b | 2023-04-19 18:22:21 +0200 | [diff] [blame] | 5898 | } |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 5899 | s->last_change = ns_to_sec(now_ns); |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5900 | |
| 5901 | /* publish the state change */ |
| 5902 | _srv_event_hdl_prepare_state(&cb_data.state, |
| 5903 | s, type, cause, srv_prev_state, requeued); |
| 5904 | _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_STATE, cb_data.state, s); |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5905 | } |
| 5906 | |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5907 | /* check if backend stats must be updated due to the server state change */ |
| 5908 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5909 | set_backend_down(s->proxy); /* backend going down */ |
| 5910 | else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) { |
| 5911 | /* backend was down and is back up again: |
| 5912 | * no helper function, updating last_change and backend downtime stats |
| 5913 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 5914 | if (s->proxy->last_change < ns_to_sec(now_ns)) // ignore negative times |
| 5915 | s->proxy->down_time += ns_to_sec(now_ns) - s->proxy->last_change; |
| 5916 | s->proxy->last_change = ns_to_sec(now_ns); |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5917 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5918 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5919 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5920 | 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] | 5921 | { |
| 5922 | struct connection *conn; |
| 5923 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5924 | while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns, |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5925 | struct connection *, toremove_list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5926 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | return task; |
| 5930 | } |
| 5931 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5932 | /* Move toremove_nb connections from idle_tree to toremove_list, -1 means |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5933 | * moving them all. |
| 5934 | * Returns the number of connections moved. |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5935 | * |
| 5936 | * Must be called with idle_conns_lock held. |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5937 | */ |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5938 | 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] | 5939 | { |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5940 | struct eb_node *node, *next; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5941 | struct conn_hash_node *hash_node; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5942 | int i = 0; |
| 5943 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5944 | node = eb_first(idle_tree); |
| 5945 | while (node) { |
| 5946 | next = eb_next(node); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5947 | if (toremove_nb != -1 && i >= toremove_nb) |
| 5948 | break; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5949 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5950 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5951 | eb_delete(node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5952 | MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5953 | i++; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5954 | |
| 5955 | node = next; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5956 | } |
| 5957 | return i; |
| 5958 | } |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5959 | /* cleanup connections for a given server |
| 5960 | * might be useful when going on forced maintenance or live changing ip/port |
| 5961 | */ |
William Dauchy | 707ad32 | 2020-05-04 13:52:40 +0200 | [diff] [blame] | 5962 | static void srv_cleanup_connections(struct server *srv) |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5963 | { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5964 | int did_remove; |
| 5965 | int i; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5966 | |
Amaury Denoyelle | 10d5c31 | 2021-01-06 14:28:51 +0100 | [diff] [blame] | 5967 | /* nothing to do if pool-max-conn is null */ |
| 5968 | if (!srv->max_idle_conns) |
| 5969 | return; |
| 5970 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5971 | /* check all threads starting with ours */ |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5972 | for (i = tid;;) { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5973 | did_remove = 0; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5974 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5975 | 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] | 5976 | did_remove = 1; |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5977 | 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] | 5978 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5979 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5980 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5981 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5982 | |
| 5983 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 5984 | break; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5985 | } |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5986 | } |
| 5987 | |
| 5988 | /* removes an idle conn after updating the server idle conns counters */ |
| 5989 | void srv_release_conn(struct server *srv, struct connection *conn) |
| 5990 | { |
| 5991 | if (conn->flags & CO_FL_LIST_MASK) { |
| 5992 | /* The connection is currently in the server's idle list, so tell it |
| 5993 | * there's one less connection available in that list. |
| 5994 | */ |
| 5995 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5996 | _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb); |
| 5997 | _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]); |
| 5998 | } |
| 5999 | else { |
| 6000 | /* The connection is not private and not in any server's idle |
| 6001 | * list, so decrement the current number of used connections |
| 6002 | */ |
| 6003 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 6004 | } |
| 6005 | |
| 6006 | /* Remove the connection from any tree (safe, idle or available) */ |
| 6007 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6008 | conn_delete_from_tree(&conn->hash_node->node); |
Christopher Faulet | 5e1b0e7 | 2023-02-28 15:39:38 +0100 | [diff] [blame] | 6009 | conn->flags &= ~CO_FL_LIST_MASK; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6010 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6011 | } |
| 6012 | |
| 6013 | /* retrieve a connection from its <hash> in <tree> |
| 6014 | * returns NULL if no connection found |
| 6015 | */ |
| 6016 | struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash) |
| 6017 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6018 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6019 | struct connection *conn = NULL; |
| 6020 | struct conn_hash_node *hash_node = NULL; |
| 6021 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6022 | node = eb64_lookup(tree, hash); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6023 | if (node) { |
| 6024 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 6025 | conn = hash_node->conn; |
| 6026 | } |
| 6027 | |
| 6028 | return conn; |
| 6029 | } |
| 6030 | |
| 6031 | /* retrieve the next connection sharing the same hash as <conn> |
| 6032 | * returns NULL if no connection found |
| 6033 | */ |
| 6034 | struct connection *srv_lookup_conn_next(struct connection *conn) |
| 6035 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6036 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6037 | struct connection *next_conn = NULL; |
| 6038 | struct conn_hash_node *hash_node = NULL; |
| 6039 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6040 | node = eb64_next_dup(&conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6041 | if (node) { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6042 | hash_node = eb64_entry(node, struct conn_hash_node, node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6043 | next_conn = hash_node->conn; |
| 6044 | } |
| 6045 | |
| 6046 | return next_conn; |
| 6047 | } |
| 6048 | |
| 6049 | /* This adds an idle connection to the server's list if the connection is |
| 6050 | * reusable, not held by any owner anymore, but still has available streams. |
| 6051 | */ |
| 6052 | int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe) |
| 6053 | { |
| 6054 | /* we try to keep the connection in the server's idle list |
| 6055 | * if we don't have too many FD in use, and if the number of |
| 6056 | * idle+current conns is lower than what was observed before |
| 6057 | * last purge, or if we already don't have idle conns for the |
| 6058 | * current thread and we don't exceed last count by global.nbthread. |
| 6059 | */ |
| 6060 | if (!(conn->flags & CO_FL_PRIVATE) && |
| 6061 | srv && srv->pool_purge_delay > 0 && |
| 6062 | ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) && |
| 6063 | ha_used_fds < global.tune.pool_high_count && |
| 6064 | (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) && |
| 6065 | ((eb_is_empty(&srv->per_thr[tid].safe_conns) && |
| 6066 | (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) || |
| 6067 | (ha_used_fds < global.tune.pool_low_count && |
| 6068 | (srv->curr_used_conns + srv->curr_idle_conns <= |
| 6069 | MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) && |
| 6070 | !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) { |
| 6071 | int retadd; |
| 6072 | |
| 6073 | retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1); |
| 6074 | if (retadd > srv->max_idle_conns) { |
| 6075 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 6076 | return 0; |
| 6077 | } |
| 6078 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 6079 | |
| 6080 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6081 | conn_delete_from_tree(&conn->hash_node->node); |
| 6082 | |
| 6083 | if (is_safe) { |
| 6084 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6085 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6086 | _HA_ATOMIC_INC(&srv->curr_safe_nb); |
| 6087 | } else { |
| 6088 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6089 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6090 | _HA_ATOMIC_INC(&srv->curr_idle_nb); |
| 6091 | } |
| 6092 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6093 | _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]); |
| 6094 | |
| 6095 | __ha_barrier_full(); |
| 6096 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 6097 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6098 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 6099 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 6100 | now_ms); |
| 6101 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 6102 | if (!task_in_wq(idle_conn_task) && ! |
| 6103 | task_in_rq(idle_conn_task)) { |
| 6104 | task_schedule(idle_conn_task, |
| 6105 | srv->idle_node.key); |
| 6106 | } |
| 6107 | |
| 6108 | } |
| 6109 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6110 | } |
| 6111 | return 1; |
| 6112 | } |
| 6113 | return 0; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 6114 | } |
| 6115 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6116 | 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] | 6117 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6118 | struct server *srv; |
| 6119 | struct eb32_node *eb; |
| 6120 | int i; |
| 6121 | unsigned int next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 6122 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6123 | next_wakeup = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6124 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6125 | while (1) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6126 | int exceed_conns; |
| 6127 | int to_kill; |
| 6128 | int curr_idle; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 6129 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6130 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 6131 | if (!eb) { |
| 6132 | /* we might have reached the end of the tree, typically because |
| 6133 | * <now_ms> is in the first half and we're first scanning the last |
| 6134 | * half. Let's loop back to the beginning of the tree now. |
| 6135 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6136 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6137 | eb = eb32_first(&idle_conn_srv); |
| 6138 | if (likely(!eb)) |
| 6139 | break; |
| 6140 | } |
| 6141 | if (tick_is_lt(now_ms, eb->key)) { |
| 6142 | /* timer not expired yet, revisit it later */ |
| 6143 | next_wakeup = eb->key; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6144 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6145 | } |
| 6146 | srv = eb32_entry(eb, struct server, idle_node); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6147 | |
| 6148 | /* Calculate how many idle connections we want to kill : |
| 6149 | * we want to remove half the difference between the total |
| 6150 | * of established connections (used or idle) and the max |
| 6151 | * number of used connections. |
| 6152 | */ |
| 6153 | curr_idle = srv->curr_idle_conns; |
| 6154 | if (curr_idle == 0) |
| 6155 | goto remove; |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6156 | 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] | 6157 | exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1); |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6158 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6159 | 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] | 6160 | if (srv->est_need_conns < srv->max_used_conns) |
| 6161 | srv->est_need_conns = srv->max_used_conns; |
| 6162 | |
Willy Tarreau | 9dc231a | 2022-11-21 14:32:33 +0100 | [diff] [blame] | 6163 | HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6164 | |
Willy Tarreau | 18ed789 | 2020-07-02 19:05:30 +0200 | [diff] [blame] | 6165 | if (exceed_conns <= 0) |
| 6166 | goto remove; |
| 6167 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6168 | /* check all threads starting with ours */ |
| 6169 | for (i = tid;;) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6170 | int max_conn; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6171 | int j; |
| 6172 | int did_remove = 0; |
| 6173 | |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6174 | max_conn = (exceed_conns * srv->curr_idle_thr[i]) / |
| 6175 | curr_idle + 1; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6176 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6177 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6178 | 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] | 6179 | if (j > 0) |
| 6180 | did_remove = 1; |
| 6181 | if (max_conn - j > 0 && |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6182 | 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] | 6183 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6184 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6185 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6186 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 6187 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6188 | |
| 6189 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 6190 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6191 | } |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6192 | remove: |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6193 | eb32_delete(&srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6194 | |
| 6195 | if (srv->curr_idle_conns) { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6196 | /* There are still more idle connections, add the |
| 6197 | * server back in the tree. |
| 6198 | */ |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6199 | srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6200 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6201 | next_wakeup = tick_first(next_wakeup, srv->idle_node.key); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6202 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6203 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6204 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6205 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6206 | task->expire = next_wakeup; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6207 | return task; |
| 6208 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6209 | |
Amaury Denoyelle | 3109ccf | 2021-04-29 17:30:05 +0200 | [diff] [blame] | 6210 | /* Close remaining idle connections. This functions is designed to be run on |
| 6211 | * process shutdown. This guarantees a proper socket shutdown to avoid |
| 6212 | * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is |
| 6213 | * bypassed. |
| 6214 | * |
| 6215 | * This function is not thread-safe so it must only be called via a global |
| 6216 | * deinit function. |
| 6217 | */ |
| 6218 | static void srv_close_idle_conns(struct server *srv) |
| 6219 | { |
| 6220 | struct eb_root **cleaned_tree; |
| 6221 | int i; |
| 6222 | |
| 6223 | for (i = 0; i < global.nbthread; ++i) { |
| 6224 | struct eb_root *conn_trees[] = { |
| 6225 | &srv->per_thr[i].idle_conns, |
| 6226 | &srv->per_thr[i].safe_conns, |
| 6227 | &srv->per_thr[i].avail_conns, |
| 6228 | NULL |
| 6229 | }; |
| 6230 | |
| 6231 | for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) { |
| 6232 | while (!eb_is_empty(*cleaned_tree)) { |
| 6233 | struct ebmb_node *node = ebmb_first(*cleaned_tree); |
| 6234 | struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 6235 | struct connection *conn = conn_hash_node->conn; |
| 6236 | |
| 6237 | if (conn->ctrl->ctrl_close) |
| 6238 | conn->ctrl->ctrl_close(conn); |
| 6239 | ebmb_delete(node); |
| 6240 | } |
| 6241 | } |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | REGISTER_SERVER_DEINIT(srv_close_idle_conns); |
| 6246 | |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6247 | /* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */ |
| 6248 | 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] | 6249 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6250 | char **err) |
| 6251 | { |
| 6252 | if (too_many_args(1, args, err, NULL)) |
| 6253 | return -1; |
| 6254 | |
| 6255 | if (strcmp(args[1], "on") == 0) |
| 6256 | global.tune.options |= GTUNE_IDLE_POOL_SHARED; |
| 6257 | else if (strcmp(args[1], "off") == 0) |
| 6258 | global.tune.options &= ~GTUNE_IDLE_POOL_SHARED; |
| 6259 | else { |
| 6260 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 6261 | return -1; |
| 6262 | } |
| 6263 | return 0; |
| 6264 | } |
| 6265 | |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6266 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 6267 | 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] | 6268 | const struct proxy *defpx, const char *file, int line, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6269 | char **err) |
| 6270 | { |
| 6271 | int arg = -1; |
| 6272 | |
| 6273 | if (too_many_args(1, args, err, NULL)) |
| 6274 | return -1; |
| 6275 | |
| 6276 | if (*(args[1]) != 0) |
| 6277 | arg = atoi(args[1]); |
| 6278 | |
| 6279 | if (arg < 0 || arg > 100) { |
| 6280 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 6281 | return -1; |
| 6282 | } |
| 6283 | |
| 6284 | if (args[0][10] == 'h') |
| 6285 | global.tune.pool_high_ratio = arg; |
| 6286 | else |
| 6287 | global.tune.pool_low_ratio = arg; |
| 6288 | return 0; |
| 6289 | } |
| 6290 | |
| 6291 | /* config keyword parsers */ |
| 6292 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6293 | { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared }, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6294 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6295 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6296 | { 0, NULL, NULL } |
| 6297 | }}; |
| 6298 | |
| 6299 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 6300 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 6301 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6302 | * Local variables: |
| 6303 | * c-indent-level: 8 |
| 6304 | * c-basic-offset: 8 |
| 6305 | * End: |
| 6306 | */ |