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 | { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 657 | newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT; |
| 658 | newsrv->next_state = SRV_ST_RUNNING; |
Frédéric Lécaille | 2a0d061 | 2017-03-21 11:53:54 +0100 | [diff] [blame] | 659 | newsrv->check.state &= ~CHK_ST_PAUSED; |
| 660 | newsrv->check.health = newsrv->check.rise; |
| 661 | return 0; |
| 662 | } |
| 663 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 664 | /* Parse the "error-limit" server keyword */ |
| 665 | static int srv_parse_error_limit(char **args, int *cur_arg, |
| 666 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 667 | { |
| 668 | if (!*args[*cur_arg + 1]) { |
| 669 | memprintf(err, "'%s' expects an integer argument.", |
| 670 | args[*cur_arg]); |
| 671 | return ERR_ALERT | ERR_FATAL; |
| 672 | } |
| 673 | |
| 674 | newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]); |
| 675 | |
| 676 | if (newsrv->consecutive_errors_limit <= 0) { |
| 677 | memprintf(err, "%s has to be > 0.", |
| 678 | args[*cur_arg]); |
| 679 | return ERR_ALERT | ERR_FATAL; |
| 680 | } |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Amaury Denoyelle | f9d5957 | 2021-10-18 14:40:29 +0200 | [diff] [blame] | 685 | /* Parse the "ws" keyword */ |
| 686 | static int srv_parse_ws(char **args, int *cur_arg, |
| 687 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 688 | { |
| 689 | if (!args[*cur_arg + 1]) { |
| 690 | memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]); |
| 691 | return ERR_ALERT | ERR_FATAL; |
| 692 | } |
| 693 | |
| 694 | if (strcmp(args[*cur_arg + 1], "h1") == 0) { |
| 695 | newsrv->ws = SRV_WS_H1; |
| 696 | } |
| 697 | else if (strcmp(args[*cur_arg + 1], "h2") == 0) { |
| 698 | newsrv->ws = SRV_WS_H2; |
| 699 | } |
| 700 | else if (strcmp(args[*cur_arg + 1], "auto") == 0) { |
| 701 | newsrv->ws = SRV_WS_AUTO; |
| 702 | } |
| 703 | else { |
| 704 | memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]); |
| 705 | return ERR_ALERT | ERR_FATAL; |
| 706 | } |
| 707 | |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 712 | /* Parse the "init-addr" server keyword */ |
| 713 | static int srv_parse_init_addr(char **args, int *cur_arg, |
| 714 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 715 | { |
| 716 | char *p, *end; |
| 717 | int done; |
| 718 | struct sockaddr_storage sa; |
| 719 | |
| 720 | newsrv->init_addr_methods = 0; |
| 721 | memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr)); |
| 722 | |
| 723 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 724 | /* cut on next comma */ |
| 725 | for (end = p; *end && *end != ','; end++); |
| 726 | if (*end) |
| 727 | *(end++) = 0; |
| 728 | |
| 729 | memset(&sa, 0, sizeof(sa)); |
| 730 | if (strcmp(p, "libc") == 0) { |
| 731 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC); |
| 732 | } |
| 733 | else if (strcmp(p, "last") == 0) { |
| 734 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST); |
| 735 | } |
| 736 | else if (strcmp(p, "none") == 0) { |
| 737 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE); |
| 738 | } |
| 739 | else if (str2ip2(p, &sa, 0)) { |
| 740 | if (is_addr(&newsrv->init_addr)) { |
| 741 | memprintf(err, "'%s' : initial address already specified, cannot add '%s'.", |
| 742 | args[*cur_arg], p); |
| 743 | return ERR_ALERT | ERR_FATAL; |
| 744 | } |
| 745 | newsrv->init_addr = sa; |
| 746 | done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP); |
| 747 | } |
| 748 | else { |
| 749 | memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.", |
| 750 | args[*cur_arg], p); |
| 751 | return ERR_ALERT | ERR_FATAL; |
| 752 | } |
| 753 | if (!done) { |
| 754 | memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'", |
| 755 | args[*cur_arg], p); |
| 756 | return ERR_ALERT | ERR_FATAL; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* Parse the "log-proto" server keyword */ |
| 764 | static int srv_parse_log_proto(char **args, int *cur_arg, |
| 765 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 766 | { |
| 767 | if (strcmp(args[*cur_arg + 1], "legacy") == 0) |
| 768 | newsrv->log_proto = SRV_LOG_PROTO_LEGACY; |
| 769 | else if (strcmp(args[*cur_arg + 1], "octet-count") == 0) |
| 770 | newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING; |
| 771 | else { |
| 772 | memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'", |
| 773 | args[*cur_arg], args[*cur_arg + 1]); |
| 774 | return ERR_ALERT | ERR_FATAL; |
| 775 | } |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* Parse the "maxconn" server keyword */ |
| 781 | static int srv_parse_maxconn(char **args, int *cur_arg, |
| 782 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 783 | { |
| 784 | newsrv->maxconn = atol(args[*cur_arg + 1]); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | /* Parse the "maxqueue" server keyword */ |
| 789 | static int srv_parse_maxqueue(char **args, int *cur_arg, |
| 790 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 791 | { |
| 792 | newsrv->maxqueue = atol(args[*cur_arg + 1]); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* Parse the "minconn" server keyword */ |
| 797 | static int srv_parse_minconn(char **args, int *cur_arg, |
| 798 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 799 | { |
| 800 | newsrv->minconn = atol(args[*cur_arg + 1]); |
| 801 | return 0; |
| 802 | } |
| 803 | |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 804 | static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 805 | { |
| 806 | char *arg; |
| 807 | |
| 808 | arg = args[*cur_arg + 1]; |
| 809 | if (!*arg) { |
| 810 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 811 | return ERR_ALERT | ERR_FATAL; |
| 812 | } |
| 813 | newsrv->max_reuse = atoi(arg); |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 818 | 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] | 819 | { |
| 820 | const char *res; |
| 821 | char *arg; |
| 822 | unsigned int time; |
| 823 | |
| 824 | arg = args[*cur_arg + 1]; |
| 825 | if (!*arg) { |
| 826 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 827 | return ERR_ALERT | ERR_FATAL; |
| 828 | } |
| 829 | res = parse_time_err(arg, &time, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 830 | if (res == PARSE_TIME_OVER) { |
| 831 | memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)", |
| 832 | args[*cur_arg+1], args[*cur_arg]); |
| 833 | return ERR_ALERT | ERR_FATAL; |
| 834 | } |
| 835 | else if (res == PARSE_TIME_UNDER) { |
| 836 | memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)", |
| 837 | args[*cur_arg+1], args[*cur_arg]); |
| 838 | return ERR_ALERT | ERR_FATAL; |
| 839 | } |
| 840 | else if (res) { |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 841 | memprintf(err, "unexpected character '%c' in argument to <%s>.\n", |
| 842 | *res, args[*cur_arg]); |
| 843 | return ERR_ALERT | ERR_FATAL; |
| 844 | } |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 845 | newsrv->pool_purge_delay = time; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 850 | static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 851 | { |
| 852 | char *arg; |
| 853 | |
| 854 | arg = args[*cur_arg + 1]; |
| 855 | if (!*arg) { |
| 856 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 857 | return ERR_ALERT | ERR_FATAL; |
| 858 | } |
| 859 | |
| 860 | newsrv->low_idle_conns = atoi(arg); |
| 861 | return 0; |
| 862 | } |
| 863 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 864 | static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 865 | { |
| 866 | char *arg; |
| 867 | |
| 868 | arg = args[*cur_arg + 1]; |
| 869 | if (!*arg) { |
| 870 | memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]); |
| 871 | return ERR_ALERT | ERR_FATAL; |
| 872 | } |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 873 | |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 874 | newsrv->max_idle_conns = atoi(arg); |
Willy Tarreau | cb923d5 | 2019-01-23 10:39:27 +0100 | [diff] [blame] | 875 | if ((int)newsrv->max_idle_conns < -1) { |
| 876 | memprintf(err, "'%s' must be >= -1", args[*cur_arg]); |
| 877 | return ERR_ALERT | ERR_FATAL; |
| 878 | } |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 883 | /* parse the "id" server keyword */ |
| 884 | static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err) |
| 885 | { |
| 886 | struct eb32_node *node; |
| 887 | |
| 888 | if (!*args[*cur_arg + 1]) { |
| 889 | memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]); |
| 890 | return ERR_ALERT | ERR_FATAL; |
| 891 | } |
| 892 | |
| 893 | newsrv->puid = atol(args[*cur_arg + 1]); |
| 894 | newsrv->conf.id.key = newsrv->puid; |
| 895 | |
| 896 | if (newsrv->puid <= 0) { |
| 897 | memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]); |
| 898 | return ERR_ALERT | ERR_FATAL; |
| 899 | } |
| 900 | |
| 901 | node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid); |
| 902 | if (node) { |
| 903 | struct server *target = container_of(node, struct server, conf.id); |
| 904 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')", |
| 905 | args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line, |
| 906 | target->id); |
| 907 | return ERR_ALERT | ERR_FATAL; |
| 908 | } |
| 909 | |
Baptiste Assmann | 7cc419a | 2015-07-07 22:02:20 +0200 | [diff] [blame] | 910 | newsrv->flags |= SRV_F_FORCED_ID; |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 914 | /* Parse the "namespace" server keyword */ |
| 915 | static int srv_parse_namespace(char **args, int *cur_arg, |
| 916 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 917 | { |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 918 | #ifdef USE_NS |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 919 | char *arg; |
| 920 | |
| 921 | arg = args[*cur_arg + 1]; |
| 922 | if (!*arg) { |
| 923 | memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]); |
| 924 | return ERR_ALERT | ERR_FATAL; |
| 925 | } |
| 926 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 927 | if (strcmp(arg, "*") == 0) { |
Frédéric Lécaille | 22f41a2 | 2017-03-16 17:17:36 +0100 | [diff] [blame] | 928 | /* Use the namespace associated with the connection (if present). */ |
| 929 | newsrv->flags |= SRV_F_USE_NS_FROM_PP; |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * As this parser may be called several times for the same 'default-server' |
| 935 | * object, or for a new 'server' instance deriving from a 'default-server' |
| 936 | * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it. |
| 937 | */ |
| 938 | newsrv->flags &= ~SRV_F_USE_NS_FROM_PP; |
| 939 | |
| 940 | newsrv->netns = netns_store_lookup(arg, strlen(arg)); |
| 941 | if (!newsrv->netns) |
| 942 | newsrv->netns = netns_store_insert(arg); |
| 943 | |
| 944 | if (!newsrv->netns) { |
| 945 | memprintf(err, "Cannot open namespace '%s'", arg); |
| 946 | return ERR_ALERT | ERR_FATAL; |
| 947 | } |
| 948 | |
| 949 | return 0; |
| 950 | #else |
| 951 | memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]); |
| 952 | return ERR_ALERT | ERR_FATAL; |
| 953 | #endif |
| 954 | } |
| 955 | |
Frédéric Lécaille | f5bf903 | 2017-03-10 11:51:05 +0100 | [diff] [blame] | 956 | /* Parse the "no-backup" server keyword */ |
| 957 | static int srv_parse_no_backup(char **args, int *cur_arg, |
| 958 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 959 | { |
| 960 | newsrv->flags &= ~SRV_F_BACKUP; |
| 961 | return 0; |
| 962 | } |
| 963 | |
Frédéric Lécaille | 25df890 | 2017-03-10 14:04:31 +0100 | [diff] [blame] | 964 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 965 | /* Disable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 966 | 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] | 967 | { |
| 968 | srv->pp_opts &= ~flags; |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /* Parse the "no-send-proxy" server keyword */ |
| 973 | static int srv_parse_no_send_proxy(char **args, int *cur_arg, |
| 974 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 975 | { |
| 976 | return srv_disable_pp_flags(newsrv, SRV_PP_V1); |
| 977 | } |
| 978 | |
| 979 | /* Parse the "no-send-proxy-v2" server keyword */ |
| 980 | static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg, |
| 981 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 982 | { |
| 983 | return srv_disable_pp_flags(newsrv, SRV_PP_V2); |
| 984 | } |
| 985 | |
Frédéric Lécaille | 36d1565 | 2022-10-17 14:58:19 +0200 | [diff] [blame] | 986 | /* Parse the "shard" server keyword */ |
| 987 | static int srv_parse_shard(char **args, int *cur_arg, |
| 988 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 989 | { |
| 990 | newsrv->shard = atol(args[*cur_arg + 1]); |
| 991 | return 0; |
| 992 | } |
| 993 | |
Frédéric Lécaille | 1b9423d | 2019-07-04 14:19:06 +0200 | [diff] [blame] | 994 | /* Parse the "no-tfo" server keyword */ |
| 995 | static int srv_parse_no_tfo(char **args, int *cur_arg, |
| 996 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 997 | { |
| 998 | newsrv->flags &= ~SRV_F_FASTOPEN; |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1002 | /* Parse the "non-stick" server keyword */ |
| 1003 | static int srv_parse_non_stick(char **args, int *cur_arg, |
| 1004 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1005 | { |
| 1006 | newsrv->flags |= SRV_F_NON_STICK; |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1010 | /* Enable server PROXY protocol flags. */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 1011 | 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] | 1012 | { |
| 1013 | srv->pp_opts |= flags; |
| 1014 | return 0; |
| 1015 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1016 | /* parse the "proto" server keyword */ |
| 1017 | static int srv_parse_proto(char **args, int *cur_arg, |
| 1018 | struct proxy *px, struct server *newsrv, char **err) |
| 1019 | { |
| 1020 | struct ist proto; |
| 1021 | |
| 1022 | if (!*args[*cur_arg + 1]) { |
| 1023 | memprintf(err, "'%s' : missing value", args[*cur_arg]); |
| 1024 | return ERR_ALERT | ERR_FATAL; |
| 1025 | } |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 1026 | proto = ist(args[*cur_arg + 1]); |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1027 | newsrv->mux_proto = get_mux_proto(proto); |
| 1028 | if (!newsrv->mux_proto) { |
| 1029 | memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]); |
| 1030 | return ERR_ALERT | ERR_FATAL; |
| 1031 | } |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 1032 | return 0; |
| 1033 | } |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1034 | |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1035 | /* parse the "proxy-v2-options" */ |
| 1036 | static int srv_parse_proxy_v2_options(char **args, int *cur_arg, |
| 1037 | struct proxy *px, struct server *newsrv, char **err) |
| 1038 | { |
| 1039 | char *p, *n; |
| 1040 | for (p = args[*cur_arg+1]; p; p = n) { |
| 1041 | n = strchr(p, ','); |
| 1042 | if (n) |
| 1043 | *n++ = '\0'; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1044 | if (strcmp(p, "ssl") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1045 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1046 | } else if (strcmp(p, "cert-cn") == 0) { |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1047 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1048 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1049 | } else if (strcmp(p, "cert-key") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1050 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1051 | newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1052 | } else if (strcmp(p, "cert-sig") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1053 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1054 | newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1055 | } else if (strcmp(p, "ssl-cipher") == 0) { |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1056 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 1057 | newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1058 | } else if (strcmp(p, "authority") == 0) { |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1059 | newsrv->pp_opts |= SRV_PP_V2_AUTHORITY; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1060 | } else if (strcmp(p, "crc32c") == 0) { |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1061 | newsrv->pp_opts |= SRV_PP_V2_CRC32C; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1062 | } else if (strcmp(p, "unique-id") == 0) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1063 | newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID; |
Emmanuel Hocdet | f643b80 | 2018-02-01 15:20:32 +0100 | [diff] [blame] | 1064 | } else |
| 1065 | goto fail; |
| 1066 | } |
| 1067 | return 0; |
| 1068 | fail: |
| 1069 | if (err) |
| 1070 | memprintf(err, "'%s' : proxy v2 option not implemented", p); |
| 1071 | return ERR_ALERT | ERR_FATAL; |
| 1072 | } |
| 1073 | |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1074 | /* Parse the "observe" server keyword */ |
| 1075 | static int srv_parse_observe(char **args, int *cur_arg, |
| 1076 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1077 | { |
| 1078 | char *arg; |
| 1079 | |
| 1080 | arg = args[*cur_arg + 1]; |
| 1081 | if (!*arg) { |
| 1082 | memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]); |
| 1083 | return ERR_ALERT | ERR_FATAL; |
| 1084 | } |
| 1085 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1086 | if (strcmp(arg, "none") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1087 | newsrv->observe = HANA_OBS_NONE; |
| 1088 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1089 | else if (strcmp(arg, "layer4") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1090 | newsrv->observe = HANA_OBS_LAYER4; |
| 1091 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1092 | else if (strcmp(arg, "layer7") == 0) { |
Frédéric Lécaille | 547356e | 2017-03-15 08:55:39 +0100 | [diff] [blame] | 1093 | if (curproxy->mode != PR_MODE_HTTP) { |
| 1094 | memprintf(err, "'%s' can only be used in http proxies.\n", arg); |
| 1095 | return ERR_ALERT; |
| 1096 | } |
| 1097 | newsrv->observe = HANA_OBS_LAYER7; |
| 1098 | } |
| 1099 | else { |
| 1100 | memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' " |
| 1101 | "but got '%s'\n", args[*cur_arg], arg); |
| 1102 | return ERR_ALERT | ERR_FATAL; |
| 1103 | } |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1108 | /* Parse the "on-error" server keyword */ |
| 1109 | static int srv_parse_on_error(char **args, int *cur_arg, |
| 1110 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1111 | { |
| 1112 | if (strcmp(args[*cur_arg + 1], "fastinter") == 0) |
| 1113 | newsrv->onerror = HANA_ONERR_FASTINTER; |
| 1114 | else if (strcmp(args[*cur_arg + 1], "fail-check") == 0) |
| 1115 | newsrv->onerror = HANA_ONERR_FAILCHK; |
| 1116 | else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0) |
| 1117 | newsrv->onerror = HANA_ONERR_SUDDTH; |
| 1118 | else if (strcmp(args[*cur_arg + 1], "mark-down") == 0) |
| 1119 | newsrv->onerror = HANA_ONERR_MARKDWN; |
| 1120 | else { |
| 1121 | memprintf(err, "'%s' expects one of 'fastinter', " |
| 1122 | "'fail-check', 'sudden-death' or 'mark-down' but got '%s'", |
| 1123 | args[*cur_arg], args[*cur_arg + 1]); |
| 1124 | return ERR_ALERT | ERR_FATAL; |
| 1125 | } |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | /* Parse the "on-marked-down" server keyword */ |
| 1131 | static int srv_parse_on_marked_down(char **args, int *cur_arg, |
| 1132 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1133 | { |
| 1134 | if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0) |
| 1135 | newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS; |
| 1136 | else { |
| 1137 | memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'", |
| 1138 | args[*cur_arg], args[*cur_arg + 1]); |
| 1139 | return ERR_ALERT | ERR_FATAL; |
| 1140 | } |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | /* Parse the "on-marked-up" server keyword */ |
| 1146 | static int srv_parse_on_marked_up(char **args, int *cur_arg, |
| 1147 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1148 | { |
| 1149 | if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0) |
| 1150 | newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS; |
| 1151 | else { |
| 1152 | memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'", |
| 1153 | args[*cur_arg], args[*cur_arg + 1]); |
| 1154 | return ERR_ALERT | ERR_FATAL; |
| 1155 | } |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Frédéric Lécaille | 1618623 | 2017-03-14 16:42:49 +0100 | [diff] [blame] | 1160 | /* Parse the "redir" server keyword */ |
| 1161 | static int srv_parse_redir(char **args, int *cur_arg, |
| 1162 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1163 | { |
| 1164 | char *arg; |
| 1165 | |
| 1166 | arg = args[*cur_arg + 1]; |
| 1167 | if (!*arg) { |
| 1168 | memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]); |
| 1169 | return ERR_ALERT | ERR_FATAL; |
| 1170 | } |
| 1171 | |
| 1172 | free(newsrv->rdr_pfx); |
| 1173 | newsrv->rdr_pfx = strdup(arg); |
| 1174 | newsrv->rdr_len = strlen(arg); |
| 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1179 | /* Parse the "resolvers" server keyword */ |
| 1180 | static int srv_parse_resolvers(char **args, int *cur_arg, |
| 1181 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1182 | { |
| 1183 | free(newsrv->resolvers_id); |
| 1184 | newsrv->resolvers_id = strdup(args[*cur_arg + 1]); |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | /* Parse the "resolve-net" server keyword */ |
| 1189 | static int srv_parse_resolve_net(char **args, int *cur_arg, |
| 1190 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1191 | { |
| 1192 | char *p, *e; |
| 1193 | unsigned char mask; |
| 1194 | struct resolv_options *opt; |
| 1195 | |
| 1196 | if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') { |
| 1197 | memprintf(err, "'%s' expects a list of networks.", |
| 1198 | args[*cur_arg]); |
| 1199 | return ERR_ALERT | ERR_FATAL; |
| 1200 | } |
| 1201 | |
| 1202 | opt = &newsrv->resolv_opts; |
| 1203 | |
| 1204 | /* Split arguments by comma, and convert it from ipv4 or ipv6 |
| 1205 | * string network in in_addr or in6_addr. |
| 1206 | */ |
| 1207 | p = args[*cur_arg + 1]; |
| 1208 | e = p; |
| 1209 | while (*p != '\0') { |
| 1210 | /* If no room available, return error. */ |
| 1211 | if (opt->pref_net_nb >= SRV_MAX_PREF_NET) { |
| 1212 | memprintf(err, "'%s' exceed %d networks.", |
| 1213 | args[*cur_arg], SRV_MAX_PREF_NET); |
| 1214 | return ERR_ALERT | ERR_FATAL; |
| 1215 | } |
| 1216 | /* look for end or comma. */ |
| 1217 | while (*e != ',' && *e != '\0') |
| 1218 | e++; |
| 1219 | if (*e == ',') { |
| 1220 | *e = '\0'; |
| 1221 | e++; |
| 1222 | } |
| 1223 | if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4, |
| 1224 | &opt->pref_net[opt->pref_net_nb].mask.in4)) { |
| 1225 | /* Try to convert input string from ipv4 or ipv6 network. */ |
| 1226 | opt->pref_net[opt->pref_net_nb].family = AF_INET; |
| 1227 | } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6, |
| 1228 | &mask)) { |
| 1229 | /* Try to convert input string from ipv6 network. */ |
| 1230 | len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6); |
| 1231 | opt->pref_net[opt->pref_net_nb].family = AF_INET6; |
| 1232 | } else { |
| 1233 | /* All network conversions fail, return error. */ |
| 1234 | memprintf(err, "'%s' invalid network '%s'.", |
| 1235 | args[*cur_arg], p); |
| 1236 | return ERR_ALERT | ERR_FATAL; |
| 1237 | } |
| 1238 | opt->pref_net_nb++; |
| 1239 | p = e; |
| 1240 | } |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | /* Parse the "resolve-opts" server keyword */ |
| 1246 | static int srv_parse_resolve_opts(char **args, int *cur_arg, |
| 1247 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1248 | { |
| 1249 | char *p, *end; |
| 1250 | |
| 1251 | for (p = args[*cur_arg + 1]; *p; p = end) { |
| 1252 | /* cut on next comma */ |
| 1253 | for (end = p; *end && *end != ','; end++); |
| 1254 | if (*end) |
| 1255 | *(end++) = 0; |
| 1256 | |
| 1257 | if (strcmp(p, "allow-dup-ip") == 0) { |
| 1258 | newsrv->resolv_opts.accept_duplicate_ip = 1; |
| 1259 | } |
| 1260 | else if (strcmp(p, "ignore-weight") == 0) { |
| 1261 | newsrv->resolv_opts.ignore_weight = 1; |
| 1262 | } |
| 1263 | else if (strcmp(p, "prevent-dup-ip") == 0) { |
| 1264 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
| 1265 | } |
| 1266 | else { |
| 1267 | memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.", |
| 1268 | args[*cur_arg], p); |
| 1269 | return ERR_ALERT | ERR_FATAL; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* Parse the "resolve-prefer" server keyword */ |
| 1277 | static int srv_parse_resolve_prefer(char **args, int *cur_arg, |
| 1278 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1279 | { |
| 1280 | if (strcmp(args[*cur_arg + 1], "ipv4") == 0) |
| 1281 | newsrv->resolv_opts.family_prio = AF_INET; |
| 1282 | else if (strcmp(args[*cur_arg + 1], "ipv6") == 0) |
| 1283 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 1284 | else { |
| 1285 | memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.", |
| 1286 | args[*cur_arg]); |
| 1287 | return ERR_ALERT | ERR_FATAL; |
| 1288 | } |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Frédéric Lécaille | 31045e4 | 2017-03-10 16:40:00 +0100 | [diff] [blame] | 1293 | /* Parse the "send-proxy" server keyword */ |
| 1294 | static int srv_parse_send_proxy(char **args, int *cur_arg, |
| 1295 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1296 | { |
| 1297 | return srv_enable_pp_flags(newsrv, SRV_PP_V1); |
| 1298 | } |
| 1299 | |
| 1300 | /* Parse the "send-proxy-v2" server keyword */ |
| 1301 | static int srv_parse_send_proxy_v2(char **args, int *cur_arg, |
| 1302 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1303 | { |
| 1304 | return srv_enable_pp_flags(newsrv, SRV_PP_V2); |
| 1305 | } |
| 1306 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1307 | /* Parse the "slowstart" server keyword */ |
| 1308 | static int srv_parse_slowstart(char **args, int *cur_arg, |
| 1309 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1310 | { |
| 1311 | /* slowstart is stored in seconds */ |
| 1312 | unsigned int val; |
| 1313 | const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS); |
| 1314 | |
| 1315 | if (time_err == PARSE_TIME_OVER) { |
| 1316 | memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).", |
| 1317 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1318 | return ERR_ALERT | ERR_FATAL; |
| 1319 | } |
| 1320 | else if (time_err == PARSE_TIME_UNDER) { |
| 1321 | memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.", |
| 1322 | args[*cur_arg+1], args[*cur_arg], newsrv->id); |
| 1323 | return ERR_ALERT | ERR_FATAL; |
| 1324 | } |
| 1325 | else if (time_err) { |
| 1326 | memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.", |
| 1327 | *time_err, newsrv->id); |
| 1328 | return ERR_ALERT | ERR_FATAL; |
| 1329 | } |
| 1330 | newsrv->slowstart = (val + 999) / 1000; |
| 1331 | |
| 1332 | return 0; |
| 1333 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1334 | |
| 1335 | /* Parse the "source" server keyword */ |
| 1336 | static int srv_parse_source(char **args, int *cur_arg, |
| 1337 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1338 | { |
| 1339 | char *errmsg; |
| 1340 | int port_low, port_high; |
| 1341 | struct sockaddr_storage *sk; |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1342 | |
| 1343 | errmsg = NULL; |
| 1344 | |
| 1345 | if (!*args[*cur_arg + 1]) { |
| 1346 | memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, " |
| 1347 | "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface"); |
| 1348 | goto err; |
| 1349 | } |
| 1350 | |
| 1351 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1352 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1353 | &errmsg, NULL, NULL, |
| 1354 | 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] | 1355 | if (!sk) { |
| 1356 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1357 | goto err; |
| 1358 | } |
| 1359 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1360 | newsrv->conn_src.opts |= CO_SRC_BIND; |
| 1361 | newsrv->conn_src.source_addr = *sk; |
| 1362 | |
| 1363 | if (port_low != port_high) { |
| 1364 | int i; |
| 1365 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1366 | 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] | 1367 | if (!newsrv->conn_src.sport_range) { |
| 1368 | ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]); |
| 1369 | goto err; |
| 1370 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1371 | for (i = 0; i < newsrv->conn_src.sport_range->size; i++) |
| 1372 | newsrv->conn_src.sport_range->ports[i] = port_low + i; |
| 1373 | } |
| 1374 | |
| 1375 | *cur_arg += 2; |
| 1376 | while (*(args[*cur_arg])) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1377 | 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] | 1378 | #if defined(CONFIG_HAP_TRANSPARENT) |
| 1379 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1380 | ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', " |
| 1381 | "or 'hdr_ip(name,#)' as argument.\n"); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1382 | goto err; |
| 1383 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1384 | if (strcmp(args[*cur_arg + 1], "client") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1385 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1386 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI; |
| 1387 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1388 | else if (strcmp(args[*cur_arg + 1], "clientip") == 0) { |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1389 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1390 | newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP; |
| 1391 | } |
| 1392 | else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) { |
| 1393 | char *name, *end; |
| 1394 | |
| 1395 | name = args[*cur_arg + 1] + 7; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1396 | while (isspace((unsigned char)*name)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1397 | name++; |
| 1398 | |
| 1399 | end = name; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1400 | while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')') |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1401 | end++; |
| 1402 | |
| 1403 | newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK; |
| 1404 | newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN; |
| 1405 | free(newsrv->conn_src.bind_hdr_name); |
| 1406 | 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] | 1407 | if (!newsrv->conn_src.bind_hdr_name) { |
| 1408 | ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]); |
| 1409 | goto err; |
| 1410 | } |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1411 | newsrv->conn_src.bind_hdr_len = end - name; |
| 1412 | memcpy(newsrv->conn_src.bind_hdr_name, name, end - name); |
| 1413 | newsrv->conn_src.bind_hdr_name[end - name] = '\0'; |
| 1414 | newsrv->conn_src.bind_hdr_occ = -1; |
| 1415 | |
| 1416 | /* now look for an occurrence number */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1417 | while (isspace((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1418 | end++; |
| 1419 | if (*end == ',') { |
| 1420 | end++; |
| 1421 | name = end; |
| 1422 | if (*end == '-') |
| 1423 | end++; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1424 | while (isdigit((unsigned char)*end)) |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1425 | end++; |
| 1426 | newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name); |
| 1427 | } |
| 1428 | |
| 1429 | if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1430 | ha_alert("usesrc hdr_ip(name,num) does not support negative" |
| 1431 | " occurrences values smaller than %d.\n", MAX_HDR_HISTORY); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1432 | goto err; |
| 1433 | } |
| 1434 | } |
| 1435 | else { |
| 1436 | struct sockaddr_storage *sk; |
| 1437 | int port1, port2; |
| 1438 | |
| 1439 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1440 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, |
| 1441 | &errmsg, NULL, NULL, |
| 1442 | 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] | 1443 | if (!sk) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1444 | 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] | 1445 | goto err; |
| 1446 | } |
| 1447 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1448 | newsrv->conn_src.tproxy_addr = *sk; |
| 1449 | newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR; |
| 1450 | } |
| 1451 | global.last_checks |= LSTCHK_NETADM; |
| 1452 | *cur_arg += 2; |
| 1453 | continue; |
| 1454 | #else /* no TPROXY support */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1455 | 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] | 1456 | goto err; |
| 1457 | #endif /* defined(CONFIG_HAP_TRANSPARENT) */ |
| 1458 | } /* "usesrc" */ |
| 1459 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1460 | 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] | 1461 | #ifdef SO_BINDTODEVICE |
| 1462 | if (!*args[*cur_arg + 1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1463 | ha_alert("'%s' : missing interface name.\n", args[0]); |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1464 | goto err; |
| 1465 | } |
| 1466 | free(newsrv->conn_src.iface_name); |
| 1467 | newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]); |
| 1468 | newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name); |
| 1469 | global.last_checks |= LSTCHK_NETADM; |
| 1470 | #else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1471 | 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] | 1472 | goto err; |
| 1473 | #endif |
| 1474 | *cur_arg += 2; |
| 1475 | continue; |
| 1476 | } |
| 1477 | /* this keyword in not an option of "source" */ |
| 1478 | break; |
| 1479 | } /* while */ |
| 1480 | |
| 1481 | return 0; |
| 1482 | |
| 1483 | err: |
| 1484 | free(errmsg); |
| 1485 | return ERR_ALERT | ERR_FATAL; |
| 1486 | } |
| 1487 | |
Frédéric Lécaille | f9bc1d6 | 2017-03-10 15:50:49 +0100 | [diff] [blame] | 1488 | /* Parse the "stick" server keyword */ |
| 1489 | static int srv_parse_stick(char **args, int *cur_arg, |
| 1490 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1491 | { |
| 1492 | newsrv->flags &= ~SRV_F_NON_STICK; |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1496 | /* Parse the "track" server keyword */ |
| 1497 | static int srv_parse_track(char **args, int *cur_arg, |
| 1498 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1499 | { |
| 1500 | char *arg; |
| 1501 | |
| 1502 | arg = args[*cur_arg + 1]; |
| 1503 | if (!*arg) { |
| 1504 | memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n"); |
| 1505 | return ERR_ALERT | ERR_FATAL; |
| 1506 | } |
| 1507 | |
| 1508 | free(newsrv->trackit); |
| 1509 | newsrv->trackit = strdup(arg); |
| 1510 | |
| 1511 | return 0; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* Parse the "socks4" server keyword */ |
| 1515 | static int srv_parse_socks4(char **args, int *cur_arg, |
| 1516 | struct proxy *curproxy, struct server *newsrv, char **err) |
| 1517 | { |
| 1518 | char *errmsg; |
| 1519 | int port_low, port_high; |
| 1520 | struct sockaddr_storage *sk; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1521 | |
| 1522 | errmsg = NULL; |
| 1523 | |
| 1524 | if (!*args[*cur_arg + 1]) { |
| 1525 | memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]); |
| 1526 | goto err; |
| 1527 | } |
| 1528 | |
| 1529 | /* 'sk' is statically allocated (no need to be freed). */ |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1530 | sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, |
| 1531 | &errmsg, NULL, NULL, |
| 1532 | 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] | 1533 | if (!sk) { |
| 1534 | memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg); |
| 1535 | goto err; |
| 1536 | } |
| 1537 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1538 | newsrv->flags |= SRV_F_SOCKS4_PROXY; |
| 1539 | newsrv->socks4_addr = *sk; |
| 1540 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1541 | return 0; |
| 1542 | |
| 1543 | err: |
| 1544 | free(errmsg); |
| 1545 | return ERR_ALERT | ERR_FATAL; |
Frédéric Lécaille | 67e0e61 | 2017-03-14 15:21:31 +0100 | [diff] [blame] | 1546 | } |
| 1547 | |
Frédéric Lécaille | dba9707 | 2017-03-17 15:33:50 +0100 | [diff] [blame] | 1548 | |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 1549 | /* parse the "tfo" server keyword */ |
| 1550 | static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1551 | { |
| 1552 | newsrv->flags |= SRV_F_FASTOPEN; |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Amaury Denoyelle | 587b71e | 2021-03-10 16:36:02 +0100 | [diff] [blame] | 1556 | /* parse the "usesrc" server keyword */ |
| 1557 | static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1558 | { |
| 1559 | memprintf(err, "'%s' only allowed after a '%s' statement.", |
| 1560 | "usesrc", "source"); |
| 1561 | return ERR_ALERT | ERR_FATAL; |
| 1562 | } |
| 1563 | |
| 1564 | /* parse the "weight" server keyword */ |
| 1565 | static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 1566 | { |
| 1567 | int w; |
| 1568 | |
| 1569 | w = atol(args[*cur_arg + 1]); |
| 1570 | if (w < 0 || w > SRV_UWGHT_MAX) { |
| 1571 | memprintf(err, "weight of server %s is not within 0 and %d (%d).", |
| 1572 | newsrv->id, SRV_UWGHT_MAX, w); |
| 1573 | return ERR_ALERT | ERR_FATAL; |
| 1574 | } |
| 1575 | newsrv->uweight = newsrv->iweight = w; |
| 1576 | |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
Aurelien DARRAGON | 4bc0909 | 2023-09-21 14:18:50 +0200 | [diff] [blame] | 1580 | /* Returns 1 if the server has streams pointing to it, and 0 otherwise. |
| 1581 | * |
| 1582 | * Must be called with the server lock held. |
| 1583 | */ |
| 1584 | static int srv_has_streams(struct server *srv) |
| 1585 | { |
| 1586 | int thr; |
| 1587 | |
| 1588 | for (thr = 0; thr < global.nbthread; thr++) |
| 1589 | if (!MT_LIST_ISEMPTY(&srv->per_thr[thr].streams)) |
| 1590 | return 1; |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1594 | /* Shutdown all connections of a server. The caller must pass a termination |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1595 | * 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] | 1596 | * shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1597 | * |
| 1598 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1599 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1600 | void srv_shutdown_streams(struct server *srv, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1601 | { |
Willy Tarreau | 751153e | 2021-02-17 13:33:24 +0100 | [diff] [blame] | 1602 | struct stream *stream; |
| 1603 | struct mt_list *elt1, elt2; |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1604 | int thr; |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1605 | |
Willy Tarreau | d4e78d8 | 2021-03-04 10:47:54 +0100 | [diff] [blame] | 1606 | for (thr = 0; thr < global.nbthread; thr++) |
| 1607 | mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2) |
| 1608 | if (stream->srv_conn == srv) |
| 1609 | stream_shutdown(stream, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* 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] | 1613 | * 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] | 1614 | * the reason for the shutdown. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1615 | * |
| 1616 | * Must be called with the server lock held. |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1617 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1618 | void srv_shutdown_backup_streams(struct proxy *px, int why) |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1619 | { |
| 1620 | struct server *srv; |
| 1621 | |
| 1622 | for (srv = px->srv; srv != NULL; srv = srv->next) |
| 1623 | if (srv->flags & SRV_F_BACKUP) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1624 | srv_shutdown_streams(srv, why); |
Willy Tarreau | 4aac7db | 2014-05-16 11:48:10 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1627 | 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] | 1628 | { |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1629 | switch (cause) { |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1630 | case SRV_OP_STCHGC_NONE: |
| 1631 | break; /* do nothing */ |
| 1632 | case SRV_OP_STCHGC_HEALTH: |
| 1633 | check_append_info(msg, &s->check); |
| 1634 | break; |
| 1635 | case SRV_OP_STCHGC_AGENT: |
| 1636 | check_append_info(msg, &s->agent); |
| 1637 | break; |
| 1638 | default: |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1639 | chunk_appendf(msg, ", %s", srv_op_st_chg_cause(cause)); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1640 | break; |
| 1641 | } |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1644 | 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] | 1645 | { |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1646 | if (cause) |
| 1647 | chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(cause)); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | /* Appends some information to a message string related to a server tracking |
| 1651 | * or requeued connections info. |
| 1652 | * |
| 1653 | * If <forced> is null and the server tracks another one, a "via" |
Emeric Brun | 5a13351 | 2017-10-19 14:42:30 +0200 | [diff] [blame] | 1654 | * If <xferred> is non-negative, some information about requeued sessions are |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1655 | * provided. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1656 | * |
| 1657 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1658 | */ |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1659 | static void srv_append_more(struct buffer *msg, struct server *s, |
| 1660 | int xferred, int forced) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1661 | { |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 1662 | if (!forced && s->track) { |
Willy Tarreau | bda9227 | 2014-05-20 21:55:30 +0200 | [diff] [blame] | 1663 | 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] | 1664 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1665 | |
| 1666 | if (xferred >= 0) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1667 | if (s->next_state == SRV_ST_STOPPED) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1668 | chunk_appendf(msg, ". %d active and %d backup servers left.%s" |
| 1669 | " %d sessions active, %d requeued, %d remaining in queue", |
| 1670 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1671 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1672 | s->cur_sess, xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1673 | else |
| 1674 | chunk_appendf(msg, ". %d active and %d backup servers online.%s" |
| 1675 | " %d sessions requeued, %d total in queue", |
| 1676 | s->proxy->srv_act, s->proxy->srv_bck, |
| 1677 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "", |
Willy Tarreau | a057045 | 2021-06-18 09:30:30 +0200 | [diff] [blame] | 1678 | xferred, s->queue.length); |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Aurelien DARRAGON | 10518c0 | 2023-04-19 10:33:02 +0200 | [diff] [blame] | 1682 | /* Marks server <s> down, regardless of its checks' statuses. The server |
| 1683 | * transfers queued streams whenever possible to other servers at a sync |
| 1684 | * point. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1685 | * |
| 1686 | * Must be called with the server lock held. |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1687 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1688 | 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] | 1689 | { |
| 1690 | struct server *srv; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1691 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1692 | 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] | 1693 | return; |
| 1694 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1695 | s->next_state = SRV_ST_STOPPED; |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1696 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1697 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1698 | srv_update_status(s, 0, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1699 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1700 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1701 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1702 | srv_set_stopped(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1703 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1704 | } |
Willy Tarreau | e7d1ef1 | 2014-05-20 22:25:12 +0200 | [diff] [blame] | 1705 | } |
| 1706 | |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1707 | /* 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] | 1708 | * in maintenance. The server tries to grab requests from the proxy at a sync |
| 1709 | * point. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1710 | * |
| 1711 | * Must be called with the server lock held. |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1712 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1713 | 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] | 1714 | { |
| 1715 | struct server *srv; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1716 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1717 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1718 | return; |
| 1719 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1720 | 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] | 1721 | return; |
| 1722 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1723 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1724 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1725 | if (s->slowstart <= 0) |
| 1726 | s->next_state = SRV_ST_RUNNING; |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1727 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1728 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1729 | srv_update_status(s, 0, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1730 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1731 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1732 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1733 | srv_set_running(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1734 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1735 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1736 | } |
| 1737 | |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1738 | /* Marks server <s> stopping regardless of its checks' statuses and provided it |
Aurelien DARRAGON | 10518c0 | 2023-04-19 10:33:02 +0200 | [diff] [blame] | 1739 | * isn't in maintenance. The server tries to redispatch pending requests |
| 1740 | * to the proxy. Maintenance servers are ignored. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1741 | * |
| 1742 | * Must be called with the server lock held. |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1743 | */ |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1744 | 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] | 1745 | { |
| 1746 | struct server *srv; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1747 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1748 | if (s->cur_admin & SRV_ADMF_MAINT) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1749 | return; |
| 1750 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1751 | if (s->next_state == SRV_ST_STOPPING) |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1752 | return; |
| 1753 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1754 | s->next_state = SRV_ST_STOPPING; |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1755 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1756 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1757 | srv_update_status(s, 0, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1758 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1759 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1760 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 1761 | srv_set_stopping(srv, SRV_OP_STCHGC_NONE); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1762 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1763 | } |
Willy Tarreau | 8eb7784 | 2014-05-21 13:54:57 +0200 | [diff] [blame] | 1764 | } |
Willy Tarreau | dbd5e78 | 2014-05-20 22:46:35 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1766 | /* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1767 | * enforce either maint mode or drain mode. It is not allowed to set more than |
| 1768 | * one flag at once. The equivalent "inherited" flag is propagated to all |
| 1769 | * tracking servers. Maintenance mode disables health checks (but not agent |
| 1770 | * 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] | 1771 | * is done. If <cause> is non-null, it will be displayed at the end of the log |
| 1772 | * lines to justify the state change. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1773 | * |
| 1774 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1775 | */ |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1776 | 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] | 1777 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1778 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1779 | |
| 1780 | if (!mode) |
| 1781 | return; |
| 1782 | |
| 1783 | /* 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] | 1784 | if (s->next_admin & mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1785 | return; |
| 1786 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1787 | s->next_admin |= mode; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 1788 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1789 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1790 | srv_update_status(s, 1, cause); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1791 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1792 | /* 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] | 1793 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) || |
| 1794 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1795 | return; |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1796 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1797 | /* compute the inherited flag to propagate */ |
| 1798 | if (mode & SRV_ADMF_MAINT) |
| 1799 | mode = SRV_ADMF_IMAINT; |
| 1800 | else if (mode & SRV_ADMF_DRAIN) |
| 1801 | mode = SRV_ADMF_IDRAIN; |
| 1802 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1803 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1804 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | 8b42848 | 2016-11-07 15:53:43 +0100 | [diff] [blame] | 1805 | srv_set_admin_flag(srv, mode, cause); |
Christopher Faulet | 8d01fd6 | 2018-01-24 21:49:41 +0100 | [diff] [blame] | 1806 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1807 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1808 | } |
| 1809 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1810 | /* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to |
| 1811 | * stop enforcing either maint mode or drain mode. It is not allowed to set more |
| 1812 | * than one flag at once. The equivalent "inherited" flag is propagated to all |
| 1813 | * tracking servers. Leaving maintenance mode re-enables health checks. When |
| 1814 | * 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] | 1815 | * |
| 1816 | * Must be called with the server lock held. |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1817 | */ |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1818 | void srv_clr_admin_flag(struct server *s, enum srv_admin mode) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1819 | { |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1820 | struct server *srv; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1821 | |
| 1822 | if (!mode) |
| 1823 | return; |
| 1824 | |
| 1825 | /* 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] | 1826 | if (!(s->next_admin & mode)) |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1827 | return; |
| 1828 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1829 | s->next_admin &= ~mode; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1830 | |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1831 | /* propagate changes */ |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1832 | srv_update_status(s, 1, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1833 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1834 | /* 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] | 1835 | if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) || |
| 1836 | ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN))) |
Willy Tarreau | eeba36b | 2018-08-21 08:22:26 +0200 | [diff] [blame] | 1837 | return; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1838 | |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1839 | if (mode & SRV_ADMF_MAINT) |
| 1840 | mode = SRV_ADMF_IMAINT; |
| 1841 | else if (mode & SRV_ADMF_DRAIN) |
| 1842 | mode = SRV_ADMF_IDRAIN; |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1843 | |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1844 | for (srv = s->trackers; srv; srv = srv->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1845 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Willy Tarreau | bfc7b7a | 2014-05-22 16:14:34 +0200 | [diff] [blame] | 1846 | srv_clr_admin_flag(srv, mode); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1847 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 9f0b458 | 2017-10-23 14:39:51 +0200 | [diff] [blame] | 1848 | } |
Willy Tarreau | a0066dd | 2014-05-16 11:25:16 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1851 | /* principle: propagate maint and drain to tracking servers. This is useful |
| 1852 | * upon startup so that inherited states are correct. |
| 1853 | */ |
| 1854 | static void srv_propagate_admin_state(struct server *srv) |
| 1855 | { |
| 1856 | struct server *srv2; |
| 1857 | |
| 1858 | if (!srv->trackers) |
| 1859 | return; |
| 1860 | |
| 1861 | for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1862 | HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1863 | if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT)) |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1864 | srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1865 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1866 | if (srv->next_admin & SRV_ADMF_DRAIN) |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 1867 | srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1868 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock); |
Willy Tarreau | 757478e | 2016-11-03 19:22:19 +0100 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /* Compute and propagate the admin states for all servers in proxy <px>. |
| 1873 | * Only servers *not* tracking another one are considered, because other |
| 1874 | * ones will be handled when the server they track is visited. |
| 1875 | */ |
| 1876 | void srv_compute_all_admin_states(struct proxy *px) |
| 1877 | { |
| 1878 | struct server *srv; |
| 1879 | |
| 1880 | for (srv = px->srv; srv; srv = srv->next) { |
| 1881 | if (srv->track) |
| 1882 | continue; |
| 1883 | srv_propagate_admin_state(srv); |
| 1884 | } |
| 1885 | } |
| 1886 | |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1887 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1888 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1889 | * all code contributors. |
| 1890 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1891 | * the config parser can report an appropriate error when a known keyword was |
| 1892 | * not enabled. |
Frédéric Lécaille | dfacd69 | 2017-04-16 17:14:14 +0200 | [diff] [blame] | 1893 | * 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] | 1894 | */ |
| 1895 | static struct srv_kw_list srv_kws = { "ALL", { }, { |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1896 | { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1897 | { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1898 | { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */ |
| 1899 | { "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] | 1900 | { "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] | 1901 | { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1902 | { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1903 | { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */ |
| 1904 | { "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] | 1905 | { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */ |
| 1906 | { "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] | 1907 | { "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] | 1908 | { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */ |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 1909 | { "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] | 1910 | { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */ |
| 1911 | { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */ |
| 1912 | { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */ |
| 1913 | { "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] | 1914 | { "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] | 1915 | { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */ |
| 1916 | { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */ |
| 1917 | { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */ |
| 1918 | { "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] | 1919 | { "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 */ |
| 1920 | { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */ |
| 1921 | { "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] | 1922 | { "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] | 1923 | { "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] | 1924 | { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */ |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 1925 | { "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] | 1926 | { "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] | 1927 | { "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] | 1928 | { "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] | 1929 | { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */ |
| 1930 | { "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] | 1931 | { "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] | 1932 | { "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] | 1933 | { "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] | 1934 | { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */ |
Amaury Denoyelle | fc465a5 | 2021-03-09 17:36:23 +0100 | [diff] [blame] | 1935 | { "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] | 1936 | { "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] | 1937 | { "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] | 1938 | { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */ |
| 1939 | { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */ |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1940 | { NULL, NULL, 0 }, |
| 1941 | }}; |
| 1942 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1943 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
Willy Tarreau | dff5543 | 2012-10-10 17:51:05 +0200 | [diff] [blame] | 1944 | |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1945 | /* 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] | 1946 | * 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] | 1947 | * state is automatically disabled if the time is elapsed. If <must_update> is |
| 1948 | * not zero, the update will be propagated immediately. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1949 | * |
| 1950 | * Must be called with the server lock held. |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1951 | */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1952 | void server_recalc_eweight(struct server *sv, int must_update) |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1953 | { |
| 1954 | struct proxy *px = sv->proxy; |
| 1955 | unsigned w; |
| 1956 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 1957 | if (ns_to_sec(now_ns) < sv->last_change || ns_to_sec(now_ns) >= sv->last_change + sv->slowstart) { |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1958 | /* go to full throttle if the slowstart interval is reached */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1959 | if (sv->next_state == SRV_ST_STARTING) |
| 1960 | sv->next_state = SRV_ST_RUNNING; |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* We must take care of not pushing the server to full throttle during slow starts. |
| 1964 | * It must also start immediately, at least at the minimal step when leaving maintenance. |
| 1965 | */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1966 | 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] | 1967 | 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] | 1968 | else |
| 1969 | w = px->lbprm.wdiv; |
| 1970 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 1971 | 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] | 1972 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 1973 | /* propagate changes only if needed (i.e. not recursively) */ |
Willy Tarreau | 49725a0 | 2018-08-21 19:54:09 +0200 | [diff] [blame] | 1974 | if (must_update) |
Aurelien DARRAGON | 76e2555 | 2023-04-18 11:00:17 +0200 | [diff] [blame] | 1975 | srv_update_status(sv, 0, SRV_OP_STCHGC_NONE); |
Willy Tarreau | 004e045 | 2013-11-21 11:22:01 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1978 | /* |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1979 | * Parses weight_str and configures sv accordingly. |
| 1980 | * Returns NULL on success, error message string otherwise. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 1981 | * |
| 1982 | * Must be called with the server lock held. |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1983 | */ |
| 1984 | const char *server_parse_weight_change_request(struct server *sv, |
| 1985 | const char *weight_str) |
| 1986 | { |
| 1987 | struct proxy *px; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1988 | long int w; |
| 1989 | char *end; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 1990 | |
| 1991 | px = sv->proxy; |
| 1992 | |
| 1993 | /* if the weight is terminated with '%', it is set relative to |
| 1994 | * the initial weight, otherwise it is absolute. |
| 1995 | */ |
| 1996 | if (!*weight_str) |
| 1997 | return "Require <weight> or <weight%>.\n"; |
| 1998 | |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 1999 | w = strtol(weight_str, &end, 10); |
| 2000 | if (end == weight_str) |
| 2001 | return "Empty weight string empty or preceded by garbage"; |
| 2002 | else if (end[0] == '%' && end[1] == '\0') { |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 2003 | if (w < 0) |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2004 | return "Relative weight must be positive.\n"; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 2005 | /* Avoid integer overflow */ |
| 2006 | if (w > 25600) |
| 2007 | w = 25600; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2008 | w = sv->iweight * w / 100; |
Simon Horman | 58b5d29 | 2013-02-12 10:45:52 +0900 | [diff] [blame] | 2009 | if (w > 256) |
| 2010 | w = 256; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2011 | } |
| 2012 | else if (w < 0 || w > 256) |
| 2013 | return "Absolute weight can only be between 0 and 256 inclusive.\n"; |
Simon Horman | b796afa | 2013-02-12 10:45:53 +0900 | [diff] [blame] | 2014 | else if (end[0] != '\0') |
| 2015 | return "Trailing garbage in weight string"; |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2016 | |
| 2017 | if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) |
| 2018 | return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; |
| 2019 | |
| 2020 | sv->uweight = w; |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 2021 | server_recalc_eweight(sv, 1); |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 2022 | |
| 2023 | return NULL; |
| 2024 | } |
| 2025 | |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2026 | /* |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 2027 | * Parses <addr_str> and configures <sv> accordingly. <from> precise |
| 2028 | * the source of the change in the associated message log. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2029 | * Returns: |
| 2030 | * - error string on error |
| 2031 | * - NULL on success |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 2032 | * |
| 2033 | * Must be called with the server lock held. |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2034 | */ |
| 2035 | const char *server_parse_addr_change_request(struct server *sv, |
Thierry Fournier | 09a9178 | 2016-02-24 08:25:39 +0100 | [diff] [blame] | 2036 | const char *addr_str, const char *updater) |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2037 | { |
| 2038 | unsigned char ip[INET6_ADDRSTRLEN]; |
| 2039 | |
| 2040 | if (inet_pton(AF_INET6, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2041 | srv_update_addr(sv, ip, AF_INET6, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2042 | return NULL; |
| 2043 | } |
| 2044 | if (inet_pton(AF_INET, addr_str, ip)) { |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2045 | srv_update_addr(sv, ip, AF_INET, updater); |
Baptiste Assmann | 3d8f831 | 2015-04-13 22:54:33 +0200 | [diff] [blame] | 2046 | return NULL; |
| 2047 | } |
| 2048 | |
| 2049 | return "Could not understand IP address format.\n"; |
| 2050 | } |
| 2051 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 2052 | /* |
| 2053 | * Must be called with the server lock held. |
| 2054 | */ |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 2055 | const char *server_parse_maxconn_change_request(struct server *sv, |
| 2056 | const char *maxconn_str) |
| 2057 | { |
| 2058 | long int v; |
| 2059 | char *end; |
| 2060 | |
| 2061 | if (!*maxconn_str) |
| 2062 | return "Require <maxconn>.\n"; |
| 2063 | |
| 2064 | v = strtol(maxconn_str, &end, 10); |
| 2065 | if (end == maxconn_str) |
| 2066 | return "maxconn string empty or preceded by garbage"; |
| 2067 | else if (end[0] != '\0') |
| 2068 | return "Trailing garbage in maxconn string"; |
| 2069 | |
| 2070 | if (sv->maxconn == sv->minconn) { // static maxconn |
| 2071 | sv->maxconn = sv->minconn = v; |
| 2072 | } else { // dynamic maxconn |
| 2073 | sv->maxconn = v; |
| 2074 | } |
| 2075 | |
| 2076 | if (may_dequeue_tasks(sv, sv->proxy)) |
Willy Tarreau | 9ab7829 | 2021-06-22 18:47:51 +0200 | [diff] [blame] | 2077 | process_srv_queue(sv); |
Nenad Merdanovic | 174dd37 | 2016-04-24 23:10:06 +0200 | [diff] [blame] | 2078 | |
| 2079 | return NULL; |
| 2080 | } |
| 2081 | |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2082 | static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px, |
| 2083 | const char *file, int linenum, char **err) |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2084 | { |
| 2085 | int idx; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2086 | const char *args[] = { |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2087 | srv->sni_expr, |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2088 | NULL, |
| 2089 | }; |
| 2090 | |
| 2091 | idx = 0; |
Olivier Houchard | 7d8e688 | 2017-04-20 18:21:17 +0200 | [diff] [blame] | 2092 | px->conf.args.ctx = ARGC_SRV; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2093 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2094 | 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] | 2095 | } |
| 2096 | |
William Lallemand | 0d05867 | 2022-03-16 15:44:42 +0100 | [diff] [blame] | 2097 | 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] | 2098 | { |
| 2099 | struct sample_expr *expr; |
| 2100 | |
| 2101 | 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] | 2102 | if (!expr) { |
| 2103 | memprintf(err, "error detected while parsing sni expression : %s", *err); |
| 2104 | return ERR_ALERT | ERR_FATAL; |
| 2105 | } |
| 2106 | |
| 2107 | if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) { |
| 2108 | memprintf(err, "error detected while parsing sni expression : " |
| 2109 | " fetch method '%s' extracts information from '%s', " |
Amaury Denoyelle | c008a63 | 2021-05-28 11:01:22 +0200 | [diff] [blame] | 2110 | "none of which is available here.", |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2111 | newsrv->sni_expr, sample_src_names(expr->fetch->use)); |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2112 | return ERR_ALERT | ERR_FATAL; |
| 2113 | } |
| 2114 | |
| 2115 | px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 2116 | release_sample_expr(newsrv->ssl_ctx.sni); |
| 2117 | newsrv->ssl_ctx.sni = expr; |
| 2118 | |
| 2119 | return 0; |
| 2120 | } |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2121 | |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2122 | 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] | 2123 | { |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2124 | char *msg = "error encountered while processing "; |
| 2125 | char *quote = "'"; |
| 2126 | char *token = args[cur_arg]; |
| 2127 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2128 | if (err && *err) { |
| 2129 | indent_msg(err, 2); |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2130 | msg = *err; |
| 2131 | quote = ""; |
| 2132 | token = ""; |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 2133 | } |
Gaetan Rivet | 10c4b4a | 2020-02-11 11:42:38 +0100 | [diff] [blame] | 2134 | |
| 2135 | if (err_code & ERR_WARN && !(err_code & ERR_ALERT)) |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2136 | 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] | 2137 | else |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2138 | 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] | 2139 | } |
| 2140 | |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2141 | 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] | 2142 | { |
| 2143 | int range_sz; |
| 2144 | |
| 2145 | range_sz = src->conn_src.sport_range->size; |
| 2146 | if (range_sz > 0) { |
| 2147 | srv->conn_src.sport_range = port_range_alloc_range(range_sz); |
| 2148 | if (srv->conn_src.sport_range != NULL) { |
| 2149 | int i; |
| 2150 | |
| 2151 | for (i = 0; i < range_sz; i++) { |
| 2152 | srv->conn_src.sport_range->ports[i] = |
| 2153 | src->conn_src.sport_range->ports[i]; |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * Copy <src> server connection source settings to <srv> server everything needed. |
| 2161 | */ |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2162 | 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] | 2163 | { |
| 2164 | srv->conn_src.opts = src->conn_src.opts; |
| 2165 | srv->conn_src.source_addr = src->conn_src.source_addr; |
| 2166 | |
| 2167 | /* Source port range copy. */ |
| 2168 | if (src->conn_src.sport_range != NULL) |
| 2169 | srv_conn_src_sport_range_cpy(srv, src); |
| 2170 | |
| 2171 | #ifdef CONFIG_HAP_TRANSPARENT |
| 2172 | if (src->conn_src.bind_hdr_name != NULL) { |
| 2173 | srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name); |
| 2174 | srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name); |
| 2175 | } |
| 2176 | srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ; |
| 2177 | srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr; |
| 2178 | #endif |
| 2179 | if (src->conn_src.iface_name != NULL) |
| 2180 | srv->conn_src.iface_name = strdup(src->conn_src.iface_name); |
| 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * Copy <src> server SSL settings to <srv> server allocating |
| 2185 | * everything needed. |
| 2186 | */ |
| 2187 | #if defined(USE_OPENSSL) |
Christopher Faulet | 0b365e3 | 2022-08-03 11:21:14 +0200 | [diff] [blame] | 2188 | 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] | 2189 | { |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2190 | /* <src> is the current proxy's default server and SSL is enabled */ |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2191 | BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */ |
| 2192 | |
Christopher Faulet | 4ab2679 | 2021-12-01 09:50:41 +0100 | [diff] [blame] | 2193 | if (src == &srv->proxy->defsrv && src->use_ssl == 1) |
| 2194 | srv->flags |= SRV_F_DEFSRV_USE_SSL; |
| 2195 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2196 | if (src->ssl_ctx.ca_file != NULL) |
| 2197 | srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file); |
| 2198 | if (src->ssl_ctx.crl_file != NULL) |
| 2199 | srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file); |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 2200 | if (src->ssl_ctx.client_crt != NULL) |
| 2201 | 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] | 2202 | |
| 2203 | srv->ssl_ctx.verify = src->ssl_ctx.verify; |
| 2204 | |
Remi Tricot-Le Breton | 0498fa4 | 2021-07-13 18:28:22 +0200 | [diff] [blame] | 2205 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2206 | if (src->ssl_ctx.verify_host != NULL) |
| 2207 | srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host); |
| 2208 | if (src->ssl_ctx.ciphers != NULL) |
| 2209 | srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers); |
Jerome Magnin | 2e8d52f | 2020-04-22 11:40:18 +0200 | [diff] [blame] | 2210 | if (src->ssl_ctx.options) |
| 2211 | srv->ssl_ctx.options = src->ssl_ctx.options; |
| 2212 | if (src->ssl_ctx.methods.flags) |
| 2213 | srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags; |
| 2214 | if (src->ssl_ctx.methods.min) |
| 2215 | srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min; |
| 2216 | if (src->ssl_ctx.methods.max) |
| 2217 | srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max; |
| 2218 | |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 2219 | if (src->ssl_ctx.ciphersuites != NULL) |
| 2220 | srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2221 | if (src->sni_expr != NULL) |
| 2222 | srv->sni_expr = strdup(src->sni_expr); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2223 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2224 | if (src->ssl_ctx.alpn_str) { |
| 2225 | srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len); |
| 2226 | if (srv->ssl_ctx.alpn_str) { |
| 2227 | memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str, |
| 2228 | src->ssl_ctx.alpn_len); |
| 2229 | srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len; |
| 2230 | } |
| 2231 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2232 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2233 | if (src->ssl_ctx.npn_str) { |
| 2234 | srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len); |
| 2235 | if (srv->ssl_ctx.npn_str) { |
| 2236 | memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str, |
| 2237 | src->ssl_ctx.npn_len); |
| 2238 | srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len; |
| 2239 | } |
| 2240 | } |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2241 | } |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2242 | |
| 2243 | /* Activate ssl on server <s>. |
| 2244 | * do nothing if there is no change to apply |
| 2245 | * |
| 2246 | * Must be called with the server lock held. |
| 2247 | */ |
| 2248 | void srv_set_ssl(struct server *s, int use_ssl) |
| 2249 | { |
| 2250 | if (s->use_ssl == use_ssl) |
| 2251 | return; |
| 2252 | |
| 2253 | s->use_ssl = use_ssl; |
| 2254 | if (s->use_ssl) |
| 2255 | s->xprt = xprt_get(XPRT_SSL); |
| 2256 | else |
William Dauchy | a087f87 | 2022-01-06 16:57:15 +0100 | [diff] [blame] | 2257 | s->xprt = xprt_get(XPRT_RAW); |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | #endif /* USE_OPENSSL */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2261 | |
| 2262 | /* |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2263 | * Prepare <srv> for hostname resolution. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2264 | * 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] | 2265 | * 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] | 2266 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 2267 | 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] | 2268 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2269 | char *hostname_dn; |
| 2270 | int hostname_len, hostname_dn_len; |
| 2271 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2272 | if (!hostname) |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2273 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2274 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2275 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2276 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 2277 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 2278 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2279 | if (hostname_dn_len == -1) |
| 2280 | goto err; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2281 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2282 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2283 | free(srv->hostname); |
| 2284 | free(srv->hostname_dn); |
| 2285 | srv->hostname = strdup(hostname); |
| 2286 | srv->hostname_dn = strdup(hostname_dn); |
| 2287 | srv->hostname_dn_len = hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2288 | if (!srv->hostname || !srv->hostname_dn) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2289 | goto err; |
| 2290 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2291 | return 0; |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2292 | |
| 2293 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2294 | ha_free(&srv->hostname); |
| 2295 | ha_free(&srv->hostname_dn); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 2296 | return -1; |
| 2297 | } |
| 2298 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2299 | /* |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2300 | * Copy <src> server settings to <srv> server allocating |
| 2301 | * everything needed. |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2302 | * This function is not supposed to be called at any time, but only |
| 2303 | * during server settings parsing or during server allocations from |
| 2304 | * a server template, and just after having calloc()'ed a new server. |
| 2305 | * So, <src> may only be a default server (when parsing server settings) |
| 2306 | * or a server template (during server allocations from a server template). |
| 2307 | * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template, |
| 2308 | * 0 if not). |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2309 | */ |
Christopher Faulet | b32cb9b | 2022-08-03 11:28:08 +0200 | [diff] [blame] | 2310 | 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] | 2311 | { |
| 2312 | /* Connection source settings copy */ |
| 2313 | srv_conn_src_cpy(srv, src); |
| 2314 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2315 | if (srv_tmpl) { |
| 2316 | srv->addr = src->addr; |
| 2317 | srv->svc_port = src->svc_port; |
| 2318 | } |
| 2319 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2320 | srv->pp_opts = src->pp_opts; |
| 2321 | if (src->rdr_pfx != NULL) { |
| 2322 | srv->rdr_pfx = strdup(src->rdr_pfx); |
| 2323 | srv->rdr_len = src->rdr_len; |
| 2324 | } |
| 2325 | if (src->cookie != NULL) { |
| 2326 | srv->cookie = strdup(src->cookie); |
| 2327 | srv->cklen = src->cklen; |
| 2328 | } |
| 2329 | srv->use_ssl = src->use_ssl; |
Willy Tarreau | 2444108 | 2019-12-11 15:43:45 +0100 | [diff] [blame] | 2330 | srv->check.addr = src->check.addr; |
| 2331 | srv->agent.addr = src->agent.addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2332 | srv->check.use_ssl = src->check.use_ssl; |
| 2333 | srv->check.port = src->check.port; |
Olivier Houchard | 2194401 | 2018-12-21 19:42:01 +0100 | [diff] [blame] | 2334 | srv->check.sni = src->check.sni; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 2335 | srv->check.alpn_str = src->check.alpn_str; |
Ilya Shipitsin | 0c50b1e | 2019-04-30 21:21:28 +0500 | [diff] [blame] | 2336 | srv->check.alpn_len = src->check.alpn_len; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2337 | /* Note: 'flags' field has potentially been already initialized. */ |
| 2338 | srv->flags |= src->flags; |
| 2339 | srv->do_check = src->do_check; |
| 2340 | srv->do_agent = src->do_agent; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2341 | srv->check.inter = src->check.inter; |
| 2342 | srv->check.fastinter = src->check.fastinter; |
| 2343 | srv->check.downinter = src->check.downinter; |
| 2344 | srv->agent.use_ssl = src->agent.use_ssl; |
| 2345 | srv->agent.port = src->agent.port; |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 2346 | |
| 2347 | if (src->agent.tcpcheck_rules) { |
| 2348 | srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules)); |
| 2349 | if (srv->agent.tcpcheck_rules) { |
| 2350 | srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags; |
| 2351 | srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list; |
| 2352 | LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars); |
| 2353 | dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars, |
| 2354 | &src->agent.tcpcheck_rules->preset_vars); |
| 2355 | } |
| 2356 | } |
| 2357 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2358 | srv->agent.inter = src->agent.inter; |
| 2359 | srv->agent.fastinter = src->agent.fastinter; |
| 2360 | srv->agent.downinter = src->agent.downinter; |
| 2361 | srv->maxqueue = src->maxqueue; |
Amaury Denoyelle | 9c3251d | 2021-10-18 14:39:57 +0200 | [diff] [blame] | 2362 | srv->ws = src->ws; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2363 | srv->minconn = src->minconn; |
| 2364 | srv->maxconn = src->maxconn; |
| 2365 | srv->slowstart = src->slowstart; |
| 2366 | srv->observe = src->observe; |
| 2367 | srv->onerror = src->onerror; |
| 2368 | srv->onmarkeddown = src->onmarkeddown; |
| 2369 | srv->onmarkedup = src->onmarkedup; |
| 2370 | if (src->trackit != NULL) |
| 2371 | srv->trackit = strdup(src->trackit); |
| 2372 | srv->consecutive_errors_limit = src->consecutive_errors_limit; |
| 2373 | srv->uweight = srv->iweight = src->iweight; |
| 2374 | |
| 2375 | srv->check.send_proxy = src->check.send_proxy; |
| 2376 | /* health: up, but will fall down at first failure */ |
| 2377 | srv->check.rise = srv->check.health = src->check.rise; |
| 2378 | srv->check.fall = src->check.fall; |
| 2379 | |
| 2380 | /* Here we check if 'disabled' is the default server state */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2381 | if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) { |
| 2382 | srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT; |
| 2383 | srv->next_state = SRV_ST_STOPPED; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2384 | srv->check.state |= CHK_ST_PAUSED; |
| 2385 | srv->check.health = 0; |
| 2386 | } |
| 2387 | |
| 2388 | /* health: up but will fall down at first failure */ |
| 2389 | srv->agent.rise = srv->agent.health = src->agent.rise; |
| 2390 | srv->agent.fall = src->agent.fall; |
| 2391 | |
| 2392 | if (src->resolvers_id != NULL) |
| 2393 | srv->resolvers_id = strdup(src->resolvers_id); |
Emeric Brun | 21fbeed | 2020-12-23 18:01:04 +0100 | [diff] [blame] | 2394 | srv->resolv_opts.family_prio = src->resolv_opts.family_prio; |
| 2395 | srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip; |
| 2396 | srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight; |
| 2397 | if (srv->resolv_opts.family_prio == AF_UNSPEC) |
| 2398 | srv->resolv_opts.family_prio = AF_INET6; |
| 2399 | memcpy(srv->resolv_opts.pref_net, |
| 2400 | src->resolv_opts.pref_net, |
| 2401 | sizeof srv->resolv_opts.pref_net); |
| 2402 | 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] | 2403 | |
| 2404 | srv->init_addr_methods = src->init_addr_methods; |
| 2405 | srv->init_addr = src->init_addr; |
| 2406 | #if defined(USE_OPENSSL) |
| 2407 | srv_ssl_settings_cpy(srv, src); |
| 2408 | #endif |
| 2409 | #ifdef TCP_USER_TIMEOUT |
| 2410 | srv->tcp_ut = src->tcp_ut; |
| 2411 | #endif |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2412 | srv->mux_proto = src->mux_proto; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 2413 | srv->pool_purge_delay = src->pool_purge_delay; |
Willy Tarreau | 2f3f4d3 | 2020-07-01 07:43:51 +0200 | [diff] [blame] | 2414 | srv->low_idle_conns = src->low_idle_conns; |
Olivier Houchard | 006e310 | 2018-12-10 18:30:32 +0100 | [diff] [blame] | 2415 | srv->max_idle_conns = src->max_idle_conns; |
Willy Tarreau | 9c538e0 | 2019-01-23 10:21:49 +0100 | [diff] [blame] | 2416 | srv->max_reuse = src->max_reuse; |
Christopher Faulet | 8ed0a3e | 2018-04-10 14:45:45 +0200 | [diff] [blame] | 2417 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2418 | if (srv_tmpl) |
| 2419 | srv->srvrq = src->srvrq; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2420 | |
Aurelien DARRAGON | 60d185d | 2023-06-14 09:53:32 +0200 | [diff] [blame] | 2421 | srv->netns = src->netns; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 2422 | srv->check.via_socks4 = src->check.via_socks4; |
| 2423 | srv->socks4_addr = src->socks4_addr; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2424 | } |
| 2425 | |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 2426 | /* allocate a server and attach it to the global servers_list. Returns |
| 2427 | * the server on success, otherwise NULL. |
| 2428 | */ |
William Lallemand | 313bfd1 | 2018-10-26 14:47:32 +0200 | [diff] [blame] | 2429 | struct server *new_server(struct proxy *proxy) |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2430 | { |
| 2431 | struct server *srv; |
| 2432 | |
| 2433 | srv = calloc(1, sizeof *srv); |
| 2434 | if (!srv) |
| 2435 | return NULL; |
| 2436 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2437 | srv_take(srv); |
| 2438 | |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2439 | srv->obj_type = OBJ_TYPE_SERVER; |
| 2440 | srv->proxy = proxy; |
Willy Tarreau | cdc83e0 | 2021-06-23 16:11:02 +0200 | [diff] [blame] | 2441 | queue_init(&srv->queue, proxy, srv); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2442 | LIST_APPEND(&servers_list, &srv->global_list); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2443 | LIST_INIT(&srv->srv_rec_item); |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 2444 | LIST_INIT(&srv->ip_rec_item); |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2445 | MT_LIST_INIT(&srv->prev_deleted); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2446 | event_hdl_sub_list_init(&srv->e_subs); |
Aurelien DARRAGON | 9a55efb | 2023-07-07 15:19:36 +0200 | [diff] [blame] | 2447 | srv->rid = 0; /* rid defaults to 0 */ |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 2448 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 2449 | srv->next_state = SRV_ST_RUNNING; /* early server setup */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 2450 | srv->last_change = ns_to_sec(now_ns); |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2451 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2452 | srv->check.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2453 | srv->check.status = HCHK_STATUS_INI; |
| 2454 | srv->check.server = srv; |
Olivier Houchard | c98aa1f | 2019-01-11 18:17:17 +0100 | [diff] [blame] | 2455 | srv->check.proxy = proxy; |
Christopher Faulet | 5d503fc | 2020-03-30 20:34:34 +0200 | [diff] [blame] | 2456 | srv->check.tcpcheck_rules = &proxy->tcpcheck_rules; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2457 | |
Christopher Faulet | 3829046 | 2020-04-21 11:46:40 +0200 | [diff] [blame] | 2458 | srv->agent.obj_type = OBJ_TYPE_CHECK; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2459 | srv->agent.status = HCHK_STATUS_INI; |
| 2460 | srv->agent.server = srv; |
Willy Tarreau | 1ba3203 | 2019-01-21 07:48:26 +0100 | [diff] [blame] | 2461 | srv->agent.proxy = proxy; |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2462 | srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW); |
Frédéric Lécaille | f46c10c | 2020-11-23 14:29:28 +0100 | [diff] [blame] | 2463 | #if defined(USE_QUIC) |
| 2464 | srv->cids = EB_ROOT_UNIQUE; |
| 2465 | #endif |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2466 | |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2467 | srv->extra_counters = NULL; |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 2468 | #ifdef USE_OPENSSL |
| 2469 | HA_RWLOCK_INIT(&srv->ssl_ctx.lock); |
| 2470 | #endif |
Amaury Denoyelle | 7f8f6cb | 2020-11-10 14:24:31 +0100 | [diff] [blame] | 2471 | |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2472 | /* please don't put default server settings here, they are set in |
Willy Tarreau | 144289b | 2021-02-12 08:19:01 +0100 | [diff] [blame] | 2473 | * proxy_preset_defaults(). |
Willy Tarreau | 975b155 | 2019-06-06 16:25:55 +0200 | [diff] [blame] | 2474 | */ |
Frédéric Lécaille | 58b207c | 2017-03-30 14:18:30 +0200 | [diff] [blame] | 2475 | return srv; |
| 2476 | } |
Frédéric Lécaille | 759ea98 | 2017-03-30 17:32:36 +0200 | [diff] [blame] | 2477 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2478 | /* Increment the server refcount. */ |
| 2479 | void srv_take(struct server *srv) |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2480 | { |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2481 | HA_ATOMIC_INC(&srv->refcount); |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2482 | } |
| 2483 | |
Aurelien DARRAGON | 899b547 | 2023-06-01 12:07:43 +0200 | [diff] [blame] | 2484 | /* deallocate common server parameters (may be used by default-servers) */ |
| 2485 | void srv_free_params(struct server *srv) |
| 2486 | { |
| 2487 | free(srv->cookie); |
Aurelien DARRAGON | d2d7fbd | 2023-09-15 00:42:55 +0200 | [diff] [blame] | 2488 | free(srv->rdr_pfx); |
Aurelien DARRAGON | 899b547 | 2023-06-01 12:07:43 +0200 | [diff] [blame] | 2489 | free(srv->hostname); |
| 2490 | free(srv->hostname_dn); |
| 2491 | free((char*)srv->conf.file); |
| 2492 | free(srv->per_thr); |
| 2493 | free(srv->per_tgrp); |
| 2494 | free(srv->curr_idle_thr); |
| 2495 | free(srv->resolvers_id); |
| 2496 | free(srv->addr_node.key); |
| 2497 | free(srv->lb_nodes); |
| 2498 | |
| 2499 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv) |
| 2500 | xprt_get(XPRT_SSL)->destroy_srv(srv); |
| 2501 | } |
| 2502 | |
Amaury Denoyelle | 13f2e2c | 2021-08-09 15:08:54 +0200 | [diff] [blame] | 2503 | /* Deallocate a server <srv> and its member. <srv> must be allocated. For |
| 2504 | * dynamic servers, its refcount is decremented first. The free operations are |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2505 | * conducted only if the refcount is nul. |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2506 | * |
| 2507 | * 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] | 2508 | * when calling srv_drop on the list of servers. |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2509 | */ |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 2510 | struct server *srv_drop(struct server *srv) |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2511 | { |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2512 | struct server *next = NULL; |
| 2513 | |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2514 | if (!srv) |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2515 | goto end; |
| 2516 | |
| 2517 | next = srv->next; |
William Lallemand | 4c395fc | 2021-08-20 10:10:15 +0200 | [diff] [blame] | 2518 | |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2519 | /* For dynamic servers, decrement the reference counter. Only free the |
| 2520 | * server when reaching zero. |
| 2521 | */ |
Aurelien DARRAGON | 32483ec | 2023-03-09 11:56:14 +0100 | [diff] [blame] | 2522 | if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1)) |
| 2523 | goto end; |
Amaury Denoyelle | d6b7080 | 2021-08-02 15:50:00 +0200 | [diff] [blame] | 2524 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 2525 | /* make sure we are removed from our 'next->prev_deleted' list |
| 2526 | * This doesn't require full thread isolation as we're using mt lists |
| 2527 | * However this could easily be turned into regular list if required |
| 2528 | * (with the proper use of thread isolation) |
| 2529 | */ |
| 2530 | MT_LIST_DELETE(&srv->prev_deleted); |
| 2531 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2532 | task_destroy(srv->warmup); |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 2533 | task_destroy(srv->srvrq_check); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2534 | |
| 2535 | free(srv->id); |
Aurelien DARRAGON | 899b547 | 2023-06-01 12:07:43 +0200 | [diff] [blame] | 2536 | srv_free_params(srv); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2537 | |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2538 | HA_SPIN_DESTROY(&srv->lock); |
| 2539 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2540 | LIST_DELETE(&srv->global_list); |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 2541 | event_hdl_sub_list_destroy(&srv->e_subs); |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2542 | |
| 2543 | EXTRA_COUNTERS_FREE(srv->extra_counters); |
| 2544 | |
Tim Duesterhus | 025b93e | 2021-11-04 21:03:52 +0100 | [diff] [blame] | 2545 | ha_free(&srv); |
Amaury Denoyelle | f5c1e12 | 2021-08-25 15:03:46 +0200 | [diff] [blame] | 2546 | |
| 2547 | end: |
| 2548 | return next; |
Amaury Denoyelle | 828adf0 | 2021-03-16 17:20:15 +0100 | [diff] [blame] | 2549 | } |
| 2550 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 2551 | /* Remove a server <srv> from a tracking list if <srv> is tracking another |
| 2552 | * server. No special care is taken if <srv> is tracked itself by another one : |
| 2553 | * this situation should be avoided by the caller. |
| 2554 | * |
| 2555 | * Not thread-safe. |
| 2556 | */ |
| 2557 | static void release_server_track(struct server *srv) |
| 2558 | { |
| 2559 | struct server *strack = srv->track; |
| 2560 | struct server **base; |
| 2561 | |
| 2562 | if (!strack) |
| 2563 | return; |
| 2564 | |
| 2565 | for (base = &strack->trackers; *base; base = &((*base)->tracknext)) { |
| 2566 | if (*base == srv) { |
| 2567 | *base = srv->tracknext; |
| 2568 | return; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | /* srv not found on the tracking list, this should never happen */ |
| 2573 | BUG_ON(!*base); |
| 2574 | } |
| 2575 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2576 | /* |
| 2577 | * Parse as much as possible such a range string argument: low[-high] |
| 2578 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2579 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2580 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2581 | * Returns 0 if succeeded, -1 if not. |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2582 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2583 | static int _srv_parse_tmpl_range(struct server *srv, const char *arg, |
| 2584 | int *nb_low, int *nb_high) |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2585 | { |
| 2586 | char *nb_high_arg; |
| 2587 | |
| 2588 | *nb_high = 0; |
| 2589 | chunk_printf(&trash, "%s", arg); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2590 | *nb_low = atoi(trash.area); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2591 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2592 | if ((nb_high_arg = strchr(trash.area, '-'))) { |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2593 | *nb_high_arg++ = '\0'; |
| 2594 | *nb_high = atoi(nb_high_arg); |
| 2595 | } |
| 2596 | else { |
| 2597 | *nb_high += *nb_low; |
| 2598 | *nb_low = 1; |
| 2599 | } |
| 2600 | |
| 2601 | if (*nb_low < 0 || *nb_high < *nb_low) |
| 2602 | return -1; |
| 2603 | |
| 2604 | return 0; |
| 2605 | } |
| 2606 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2607 | /* Parse as much as possible such a range string argument: low[-high] |
| 2608 | * Set <nb_low> and <nb_high> values so that they may be reused by this loop |
| 2609 | * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1. |
| 2610 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2611 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2612 | * initialize a new server on startup. |
| 2613 | * |
| 2614 | * Fails if 'low' < 0 or 'high' is present and not higher than 'low'. |
| 2615 | * Returns 0 if succeeded, -1 if not. |
| 2616 | */ |
| 2617 | static inline void _srv_parse_set_id_from_prefix(struct server *srv, |
| 2618 | const char *prefix, int nb) |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2619 | { |
| 2620 | chunk_printf(&trash, "%s%d", prefix, nb); |
| 2621 | free(srv->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2622 | srv->id = strdup(trash.area); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2623 | } |
| 2624 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2625 | /* 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] | 2626 | * Note that a server template is a special server with |
| 2627 | * a few different parameters than a server which has |
| 2628 | * been parsed mostly the same way as a server. |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2629 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2630 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2631 | * initialize a new server on startup. |
| 2632 | * |
Joseph Herlant | 4446682 | 2018-11-15 08:57:51 -0800 | [diff] [blame] | 2633 | * Returns the number of servers successfully allocated, |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2634 | * 'srv' template included. |
| 2635 | */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2636 | 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] | 2637 | { |
| 2638 | int i; |
| 2639 | struct server *newsrv; |
| 2640 | |
| 2641 | 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] | 2642 | newsrv = new_server(px); |
| 2643 | if (!newsrv) |
| 2644 | goto err; |
| 2645 | |
Christopher Faulet | 75bef00 | 2020-11-02 22:04:55 +0100 | [diff] [blame] | 2646 | newsrv->conf.file = strdup(srv->conf.file); |
| 2647 | newsrv->conf.line = srv->conf.line; |
| 2648 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2649 | srv_settings_cpy(newsrv, srv, 1); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2650 | srv_prepare_for_resolution(newsrv, srv->hostname); |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2651 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2652 | if (newsrv->sni_expr) { |
| 2653 | newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL); |
| 2654 | if (!newsrv->ssl_ctx.sni) |
| 2655 | goto err; |
| 2656 | } |
Willy Tarreau | 80527bc | 2021-10-06 14:48:37 +0200 | [diff] [blame] | 2657 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2658 | /* append to list of servers available to receive an hostname */ |
Emeric Brun | caef19e | 2021-06-14 10:02:18 +0200 | [diff] [blame] | 2659 | if (newsrv->srvrq) |
| 2660 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 2661 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2662 | /* Set this new server ID. */ |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2663 | _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] | 2664 | |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2665 | /* Linked backwards first. This will be restablished after parsing. */ |
| 2666 | newsrv->next = px->srv; |
| 2667 | px->srv = newsrv; |
| 2668 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2669 | _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] | 2670 | |
| 2671 | return i - srv->tmpl_info.nb_low; |
| 2672 | |
| 2673 | err: |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2674 | _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] | 2675 | if (newsrv) { |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2676 | release_sample_expr(newsrv->ssl_ctx.sni); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2677 | free_check(&newsrv->agent); |
| 2678 | free_check(&newsrv->check); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2679 | LIST_DELETE(&newsrv->global_list); |
Frédéric Lécaille | 72ed475 | 2017-04-14 13:28:00 +0200 | [diff] [blame] | 2680 | } |
| 2681 | free(newsrv); |
| 2682 | return i - srv->tmpl_info.nb_low; |
| 2683 | } |
| 2684 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2685 | /* Allocate a new server pointed by <srv> and try to parse the first arguments |
| 2686 | * in <args> as an address for a server or an address-range for a template or |
| 2687 | * nothing for a default-server. <cur_arg> is incremented to the next argument. |
| 2688 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2689 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2690 | * initialize a new server on startup. |
| 2691 | * |
| 2692 | * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case |
| 2693 | * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated, |
| 2694 | * <srv> will be set to NULL. |
| 2695 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2696 | static int _srv_parse_init(struct server **srv, char **args, int *cur_arg, |
| 2697 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2698 | int parse_flags) |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2699 | { |
| 2700 | struct server *newsrv = NULL; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2701 | const char *err = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2702 | int err_code = 0; |
Willy Tarreau | 07101d5 | 2015-09-08 16:16:35 +0200 | [diff] [blame] | 2703 | char *fqdn = NULL; |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2704 | int tmpl_range_low = 0, tmpl_range_high = 0; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2705 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2706 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2707 | *srv = NULL; |
| 2708 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2709 | /* There is no mandatory first arguments for default server. */ |
| 2710 | if (parse_flags & SRV_PARSE_PARSE_ADDR) { |
| 2711 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2712 | if (!*args[3]) { |
| 2713 | /* 'server-template' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2714 | ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n", |
| 2715 | args[0]); |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2716 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2717 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2718 | } |
| 2719 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2720 | err = invalid_prefix_char(args[1]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2721 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2722 | else { |
| 2723 | if (!*args[2]) { |
| 2724 | /* 'server' line number of argument check. */ |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2725 | ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 2726 | args[0]); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2727 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2728 | goto out; |
| 2729 | } |
| 2730 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2731 | err = invalid_char(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2732 | } |
| 2733 | |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2734 | if (err) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2735 | ha_alert("character '%c' is not permitted in %s %s '%s'.\n", |
| 2736 | *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2737 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2738 | goto out; |
| 2739 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2740 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2741 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2742 | *cur_arg = 2; |
| 2743 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2744 | /* Parse server-template <nb | range> arg. */ |
| 2745 | 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] | 2746 | ha_alert("Wrong %s number or range arg '%s'.\n", |
| 2747 | args[0], args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2748 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2749 | goto out; |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2750 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2751 | (*cur_arg)++; |
| 2752 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2753 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2754 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 2755 | struct sockaddr_storage *sk; |
| 2756 | int port1, port2, port; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2757 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2758 | *srv = newsrv = new_server(curproxy); |
| 2759 | if (!newsrv) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2760 | ha_alert("out of memory.\n"); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2761 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2762 | goto out; |
| 2763 | } |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 2764 | register_parsing_obj(&newsrv->obj_type); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2765 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2766 | if (parse_flags & SRV_PARSE_TEMPLATE) { |
| 2767 | newsrv->tmpl_info.nb_low = tmpl_range_low; |
| 2768 | newsrv->tmpl_info.nb_high = tmpl_range_high; |
| 2769 | } |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 2770 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2771 | if (parse_flags & SRV_PARSE_DYNAMIC) |
| 2772 | newsrv->flags |= SRV_F_DYNAMIC; |
| 2773 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2774 | /* Note: for a server template, its id is its prefix. |
| 2775 | * This is a temporary id which will be used for server allocations to come |
| 2776 | * after parsing. |
| 2777 | */ |
| 2778 | if (!(parse_flags & SRV_PARSE_TEMPLATE)) |
| 2779 | newsrv->id = strdup(args[1]); |
| 2780 | else |
| 2781 | newsrv->tmpl_info.prefix = strdup(args[1]); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2782 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2783 | /* several ways to check the port component : |
| 2784 | * - IP => port=+0, relative (IPv4 only) |
| 2785 | * - IP: => port=+0, relative |
| 2786 | * - IP:N => port=N, absolute |
| 2787 | * - IP:+N => port=+N, relative |
| 2788 | * - IP:-N => port=-N, relative |
| 2789 | */ |
| 2790 | if (!(parse_flags & SRV_PARSE_PARSE_ADDR)) |
| 2791 | goto skip_addr; |
Frédéric Lécaille | 355b203 | 2019-01-11 14:06:12 +0100 | [diff] [blame] | 2792 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2793 | sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2794 | &errmsg, NULL, &fqdn, |
Christopher Faulet | af27ada | 2023-06-02 14:10:36 +0200 | [diff] [blame] | 2795 | (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | |
| 2796 | (parse_flags & SRV_PARSE_IN_PEER_SECTION ? PA_O_PORT_MAND : PA_O_PORT_OFS) | |
| 2797 | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2798 | if (!sk) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2799 | ha_alert("%s\n", errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2800 | err_code |= ERR_ALERT | ERR_FATAL; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2801 | ha_free(&errmsg); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2802 | goto out; |
| 2803 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2804 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2805 | if (!port1 || !port2) { |
| 2806 | /* no port specified, +offset, -offset */ |
| 2807 | newsrv->flags |= SRV_F_MAPPORTS; |
| 2808 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2809 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2810 | /* save hostname and create associated name resolution */ |
| 2811 | if (fqdn) { |
| 2812 | if (fqdn[0] == '_') { /* SRV record */ |
| 2813 | /* Check if a SRV request already exists, and if not, create it */ |
| 2814 | if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL) |
| 2815 | newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn); |
| 2816 | if (newsrv->srvrq == NULL) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2817 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2818 | goto out; |
Baptiste Assmann | 4f91f7e | 2017-05-03 12:09:54 +0200 | [diff] [blame] | 2819 | } |
Christopher Faulet | 81ba74a | 2021-06-29 20:52:35 +0200 | [diff] [blame] | 2820 | LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 2821 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2822 | else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2823 | ha_alert("Can't create DNS resolution for server '%s'\n", |
| 2824 | newsrv->id); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2825 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2826 | goto out; |
| 2827 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2828 | } |
Frédéric Lécaille | 5c3cd97 | 2017-03-15 16:36:09 +0100 | [diff] [blame] | 2829 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2830 | newsrv->addr = *sk; |
| 2831 | newsrv->svc_port = port; |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 2832 | /* |
| 2833 | * we don't need to lock the server here, because |
| 2834 | * we are in the process of initializing. |
| 2835 | * |
| 2836 | * Note that the server is not attached into the proxy tree if |
| 2837 | * this is a dynamic server. |
| 2838 | */ |
| 2839 | srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC)); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2840 | |
Willy Tarreau | 14e7f29 | 2021-10-27 17:41:07 +0200 | [diff] [blame] | 2841 | if (!newsrv->srvrq && !newsrv->hostname && |
| 2842 | !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2843 | ha_alert("Unknown protocol family %d '%s'\n", |
| 2844 | newsrv->addr.ss_family, args[*cur_arg]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2845 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2846 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2847 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2848 | |
| 2849 | (*cur_arg)++; |
| 2850 | skip_addr: |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2851 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 2852 | /* Copy default server settings to new server */ |
| 2853 | srv_settings_cpy(newsrv, &curproxy->defsrv, 0); |
| 2854 | } else { |
| 2855 | /* Initialize dynamic server weight to 1 */ |
| 2856 | newsrv->uweight = newsrv->iweight = 1; |
| 2857 | |
| 2858 | /* A dynamic server is disabled on startup */ |
| 2859 | newsrv->next_admin = SRV_ADMF_FMAINT; |
| 2860 | newsrv->next_state = SRV_ST_STOPPED; |
| 2861 | server_recalc_eweight(newsrv, 0); |
Amaury Denoyelle | fca1817 | 2021-07-22 16:03:36 +0200 | [diff] [blame] | 2862 | |
| 2863 | /* Set default values for checks */ |
| 2864 | newsrv->check.inter = DEF_CHKINTR; |
| 2865 | newsrv->check.rise = DEF_RISETIME; |
| 2866 | newsrv->check.fall = DEF_FALLTIME; |
| 2867 | |
| 2868 | newsrv->agent.inter = DEF_CHKINTR; |
| 2869 | newsrv->agent.rise = DEF_AGENT_RISETIME; |
| 2870 | newsrv->agent.fall = DEF_AGENT_FALLTIME; |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2871 | } |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2872 | HA_SPIN_INIT(&newsrv->lock); |
| 2873 | } |
| 2874 | else { |
| 2875 | *srv = newsrv = &curproxy->defsrv; |
| 2876 | *cur_arg = 1; |
| 2877 | newsrv->resolv_opts.family_prio = AF_INET6; |
| 2878 | newsrv->resolv_opts.accept_duplicate_ip = 0; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2879 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2880 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2881 | free(fqdn); |
| 2882 | return 0; |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2883 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2884 | out: |
| 2885 | free(fqdn); |
| 2886 | return err_code; |
| 2887 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2888 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2889 | /* Parse the server keyword in <args>. |
| 2890 | * <cur_arg> is incremented beyond the keyword optional value. Note that this |
| 2891 | * might not be the case if an error is reported. |
| 2892 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2893 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2894 | * initialize a new server on startup. |
| 2895 | * |
| 2896 | * A mask of errors is returned. ERR_FATAL is set if the parsing should be |
| 2897 | * interrupted. |
| 2898 | */ |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2899 | static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg, |
| 2900 | struct proxy *curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2901 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2902 | { |
| 2903 | int err_code = 0; |
| 2904 | struct srv_kw *kw; |
| 2905 | const char *best; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2906 | char *errmsg = NULL; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2907 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2908 | kw = srv_find_kw(args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2909 | if (!kw) { |
| 2910 | best = srv_find_best_kw(args[*cur_arg]); |
| 2911 | if (best) |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2912 | ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n", |
| 2913 | args[*cur_arg], best, |
| 2914 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2915 | " Hint: no address was expected for this server."); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2916 | else |
Willy Tarreau | 245721b | 2022-05-31 09:25:34 +0200 | [diff] [blame] | 2917 | ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg], |
| 2918 | (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" : |
| 2919 | " Hint: no address was expected for this server."); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2920 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2921 | return ERR_ALERT | ERR_FATAL; |
| 2922 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2923 | |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2924 | if (!kw->parse) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2925 | ha_alert("'%s' option is not implemented in this version (check build options)\n", |
| 2926 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2927 | err_code = ERR_ALERT | ERR_FATAL; |
| 2928 | goto out; |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2929 | } |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 2930 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 2931 | if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2932 | ha_alert("'%s' option is not accepted in default-server sections\n", |
| 2933 | args[*cur_arg]); |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2934 | err_code = ERR_ALERT; |
| 2935 | goto out; |
| 2936 | } |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2937 | else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2938 | ha_alert("'%s' option is not accepted for dynamic server\n", |
| 2939 | args[*cur_arg]); |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 2940 | err_code |= ERR_ALERT; |
| 2941 | goto out; |
| 2942 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2943 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2944 | err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg); |
| 2945 | if (err_code) { |
| 2946 | display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg); |
| 2947 | free(errmsg); |
| 2948 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2949 | |
| 2950 | out: |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 2951 | if (kw->skip != -1) |
| 2952 | *cur_arg += 1 + kw->skip; |
| 2953 | |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 2954 | return err_code; |
| 2955 | } |
| 2956 | |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2957 | /* This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2958 | * initialize a new server on startup. |
| 2959 | */ |
| 2960 | static int _srv_parse_sni_expr_init(char **args, int cur_arg, |
| 2961 | struct server *srv, struct proxy *proxy, |
| 2962 | char **errmsg) |
| 2963 | { |
| 2964 | int ret; |
| 2965 | |
| 2966 | if (!srv->sni_expr) |
| 2967 | return 0; |
| 2968 | |
| 2969 | ret = server_parse_sni_expr(srv, proxy, errmsg); |
| 2970 | if (!ret) |
| 2971 | return 0; |
| 2972 | |
| 2973 | return ret; |
| 2974 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2975 | |
| 2976 | /* Server initializations finalization. |
| 2977 | * Initialize health check, agent check and SNI expression if enabled. |
| 2978 | * Must not be called for a default server instance. |
| 2979 | * |
Ilya Shipitsin | ba13f16 | 2021-03-19 22:21:44 +0500 | [diff] [blame] | 2980 | * This function is first intended to be used through parse_server to |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2981 | * initialize a new server on startup. |
| 2982 | */ |
| 2983 | static int _srv_parse_finalize(char **args, int cur_arg, |
| 2984 | struct server *srv, struct proxy *px, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2985 | int parse_flags) |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2986 | { |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2987 | int ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2988 | char *errmsg = NULL; |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2989 | |
| 2990 | if (srv->do_check && srv->trackit) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2991 | 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] | 2992 | return ERR_ALERT | ERR_FATAL; |
| 2993 | } |
| 2994 | |
| 2995 | if (srv->do_agent && !srv->agent.port) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 2996 | ha_alert("server %s does not have agent port. Agent check has been disabled.\n", |
| 2997 | srv->id); |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 2998 | return ERR_ALERT | ERR_FATAL; |
| 2999 | } |
| 3000 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3001 | if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) { |
| 3002 | if (errmsg) { |
| 3003 | ha_alert("%s\n", errmsg); |
| 3004 | free(errmsg); |
| 3005 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3006 | return ret; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3007 | } |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3008 | |
Amaury Denoyelle | 76e10e7 | 2021-03-08 17:08:01 +0100 | [diff] [blame] | 3009 | /* A dynamic server is disabled on startup. It must not be counted as |
| 3010 | * an active backend entry. |
| 3011 | */ |
| 3012 | if (!(parse_flags & SRV_PARSE_DYNAMIC)) { |
| 3013 | if (srv->flags & SRV_F_BACKUP) |
| 3014 | px->srv_bck++; |
| 3015 | else |
| 3016 | px->srv_act++; |
| 3017 | } |
| 3018 | |
Amaury Denoyelle | 7d27efe | 2021-03-17 14:25:39 +0100 | [diff] [blame] | 3019 | srv_lb_commit_status(srv); |
| 3020 | |
| 3021 | return 0; |
| 3022 | } |
| 3023 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3024 | int parse_server(const char *file, int linenum, char **args, |
| 3025 | struct proxy *curproxy, const struct proxy *defproxy, |
| 3026 | int parse_flags) |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 3027 | { |
| 3028 | struct server *newsrv = NULL; |
| 3029 | int err_code = 0; |
| 3030 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3031 | int cur_arg; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3032 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3033 | set_usermsgs_ctx(file, linenum, NULL); |
| 3034 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3035 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) { |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3036 | ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3037 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3038 | goto out; |
| 3039 | } |
| 3040 | else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) { |
| 3041 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3042 | goto out; |
| 3043 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 3044 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3045 | if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) == |
| 3046 | (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) { |
| 3047 | if (!*args[2]) |
| 3048 | return 0; |
| 3049 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3050 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3051 | err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3052 | parse_flags); |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 3053 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3054 | /* the servers are linked backwards first */ |
| 3055 | if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
| 3056 | newsrv->next = curproxy->srv; |
| 3057 | curproxy->srv = newsrv; |
| 3058 | } |
Amaury Denoyelle | a8f442e | 2021-03-08 10:29:33 +0100 | [diff] [blame] | 3059 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3060 | if (err_code & ERR_CODE) |
| 3061 | goto out; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3062 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3063 | newsrv->conf.file = strdup(file); |
| 3064 | newsrv->conf.line = linenum; |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3065 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3066 | while (*args[cur_arg]) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3067 | err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy, |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3068 | parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3069 | if (err_code & ERR_FATAL) |
| 3070 | goto out; |
| 3071 | } |
Amaury Denoyelle | 9394a94 | 2021-03-08 11:20:52 +0100 | [diff] [blame] | 3072 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3073 | if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 3074 | err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags); |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3075 | if (err_code & ERR_FATAL) |
| 3076 | goto out; |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3077 | } |
Amaury Denoyelle | cf58dd7 | 2021-03-08 16:35:54 +0100 | [diff] [blame] | 3078 | |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 3079 | if (parse_flags & SRV_PARSE_TEMPLATE) |
| 3080 | _srv_parse_tmpl_init(newsrv, curproxy); |
| 3081 | |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 3082 | /* If the server id is fixed, insert it in the proxy used_id tree. |
| 3083 | * This is needed to detect a later duplicate id via srv_parse_id. |
| 3084 | * |
| 3085 | * If no is specified, a dynamic one is generated in |
| 3086 | * check_config_validity. |
| 3087 | */ |
| 3088 | if (newsrv->flags & SRV_F_FORCED_ID) |
| 3089 | eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); |
| 3090 | |
Amaury Denoyelle | 24abb0c | 2021-05-07 15:13:51 +0200 | [diff] [blame] | 3091 | HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight, |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3092 | "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] | 3093 | |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3094 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3095 | return 0; |
| 3096 | |
| 3097 | out: |
Amaury Denoyelle | 0fc136c | 2021-05-28 11:00:18 +0200 | [diff] [blame] | 3098 | reset_usermsgs_ctx(); |
Willy Tarreau | 272adea | 2014-03-31 10:39:59 +0200 | [diff] [blame] | 3099 | return err_code; |
| 3100 | } |
| 3101 | |
Baptiste Assmann | 19a106d | 2015-07-08 22:03:56 +0200 | [diff] [blame] | 3102 | /* Returns a pointer to the first server matching either id <id>. |
| 3103 | * NULL is returned if no match is found. |
| 3104 | * the lookup is performed in the backend <bk> |
| 3105 | */ |
| 3106 | struct server *server_find_by_id(struct proxy *bk, int id) |
| 3107 | { |
| 3108 | struct eb32_node *eb32; |
| 3109 | struct server *curserver; |
| 3110 | |
| 3111 | if (!bk || (id ==0)) |
| 3112 | return NULL; |
| 3113 | |
| 3114 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3115 | if (!(bk->cap & PR_CAP_BE)) |
| 3116 | return NULL; |
| 3117 | |
| 3118 | curserver = NULL; |
| 3119 | |
| 3120 | eb32 = eb32_lookup(&bk->conf.used_server_id, id); |
| 3121 | if (eb32) |
| 3122 | curserver = container_of(eb32, struct server, conf.id); |
| 3123 | |
| 3124 | return curserver; |
| 3125 | } |
| 3126 | |
| 3127 | /* Returns a pointer to the first server matching either name <name>, or id |
| 3128 | * if <name> starts with a '#'. NULL is returned if no match is found. |
| 3129 | * the lookup is performed in the backend <bk> |
| 3130 | */ |
| 3131 | struct server *server_find_by_name(struct proxy *bk, const char *name) |
| 3132 | { |
| 3133 | struct server *curserver; |
| 3134 | |
| 3135 | if (!bk || !name) |
| 3136 | return NULL; |
| 3137 | |
| 3138 | /* <bk> has no backend capabilities, so it can't have a server */ |
| 3139 | if (!(bk->cap & PR_CAP_BE)) |
| 3140 | return NULL; |
| 3141 | |
| 3142 | curserver = NULL; |
| 3143 | if (*name == '#') { |
| 3144 | curserver = server_find_by_id(bk, atoi(name + 1)); |
| 3145 | if (curserver) |
| 3146 | return curserver; |
| 3147 | } |
| 3148 | else { |
| 3149 | curserver = bk->srv; |
| 3150 | |
| 3151 | while (curserver && (strcmp(curserver->id, name) != 0)) |
| 3152 | curserver = curserver->next; |
| 3153 | |
| 3154 | if (curserver) |
| 3155 | return curserver; |
| 3156 | } |
| 3157 | |
| 3158 | return NULL; |
| 3159 | } |
| 3160 | |
| 3161 | struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff) |
| 3162 | { |
| 3163 | struct server *byname; |
| 3164 | struct server *byid; |
| 3165 | |
| 3166 | if (!name && !id) |
| 3167 | return NULL; |
| 3168 | |
| 3169 | if (diff) |
| 3170 | *diff = 0; |
| 3171 | |
| 3172 | byname = byid = NULL; |
| 3173 | |
| 3174 | if (name) { |
| 3175 | byname = server_find_by_name(bk, name); |
| 3176 | if (byname && (!id || byname->puid == id)) |
| 3177 | return byname; |
| 3178 | } |
| 3179 | |
| 3180 | /* remaining possibilities : |
| 3181 | * - name not set |
| 3182 | * - name set but not found |
| 3183 | * - name found but ID doesn't match |
| 3184 | */ |
| 3185 | if (id) { |
| 3186 | byid = server_find_by_id(bk, id); |
| 3187 | if (byid) { |
| 3188 | if (byname) { |
| 3189 | /* use id only if forced by configuration */ |
| 3190 | if (byid->flags & SRV_F_FORCED_ID) { |
| 3191 | if (diff) |
| 3192 | *diff |= 2; |
| 3193 | return byid; |
| 3194 | } |
| 3195 | else { |
| 3196 | if (diff) |
| 3197 | *diff |= 1; |
| 3198 | return byname; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | /* remaining possibilities: |
| 3203 | * - name not set |
| 3204 | * - name set but not found |
| 3205 | */ |
| 3206 | if (name && diff) |
| 3207 | *diff |= 2; |
| 3208 | return byid; |
| 3209 | } |
| 3210 | |
| 3211 | /* id bot found */ |
| 3212 | if (byname) { |
| 3213 | if (diff) |
| 3214 | *diff |= 1; |
| 3215 | return byname; |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | return NULL; |
| 3220 | } |
| 3221 | |
Simon Horman | 7d09b9a | 2013-02-12 10:45:51 +0900 | [diff] [blame] | 3222 | /* |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3223 | * update a server's current IP address. |
| 3224 | * ip is a pointer to the new IP address, whose address family is ip_sin_family. |
| 3225 | * ip is in network format. |
| 3226 | * updater is a string which contains an information about the requester of the update. |
| 3227 | * updater is used if not NULL. |
| 3228 | * |
| 3229 | * 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] | 3230 | * |
| 3231 | * Must be called with the server lock held. |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3232 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3233 | 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] | 3234 | { |
Christopher Faulet | d6c6b5f | 2020-09-08 10:27:24 +0200 | [diff] [blame] | 3235 | /* save the new IP family & address if necessary */ |
| 3236 | switch (ip_sin_family) { |
| 3237 | case AF_INET: |
| 3238 | if (s->addr.ss_family == ip_sin_family && |
| 3239 | !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4)) |
| 3240 | return 0; |
| 3241 | break; |
| 3242 | case AF_INET6: |
| 3243 | if (s->addr.ss_family == ip_sin_family && |
| 3244 | !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16)) |
| 3245 | return 0; |
| 3246 | break; |
| 3247 | }; |
| 3248 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3249 | /* generates a log line and a warning on stderr */ |
| 3250 | if (1) { |
| 3251 | /* book enough space for both IPv4 and IPv6 */ |
| 3252 | char oldip[INET6_ADDRSTRLEN]; |
| 3253 | char newip[INET6_ADDRSTRLEN]; |
| 3254 | |
| 3255 | memset(oldip, '\0', INET6_ADDRSTRLEN); |
| 3256 | memset(newip, '\0', INET6_ADDRSTRLEN); |
| 3257 | |
| 3258 | /* copy old IP address in a string */ |
| 3259 | switch (s->addr.ss_family) { |
| 3260 | case AF_INET: |
| 3261 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN); |
| 3262 | break; |
| 3263 | case AF_INET6: |
| 3264 | inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN); |
| 3265 | break; |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3266 | default: |
Willy Tarreau | fc458ec | 2023-04-07 18:11:39 +0200 | [diff] [blame] | 3267 | strlcpy2(oldip, "(none)", sizeof(oldip)); |
Christopher Faulet | b0b7607 | 2020-09-08 10:38:40 +0200 | [diff] [blame] | 3268 | break; |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3269 | }; |
| 3270 | |
| 3271 | /* copy new IP address in a string */ |
| 3272 | switch (ip_sin_family) { |
| 3273 | case AF_INET: |
| 3274 | inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN); |
| 3275 | break; |
| 3276 | case AF_INET6: |
| 3277 | inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN); |
| 3278 | break; |
| 3279 | }; |
| 3280 | |
| 3281 | /* save log line into a buffer */ |
| 3282 | chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s", |
| 3283 | s->proxy->id, s->id, oldip, newip, updater); |
| 3284 | |
| 3285 | /* write the buffer on stderr */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3286 | ha_warning("%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3287 | |
| 3288 | /* send a log */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3289 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /* save the new IP family */ |
| 3293 | s->addr.ss_family = ip_sin_family; |
| 3294 | /* save the new IP address */ |
| 3295 | switch (ip_sin_family) { |
| 3296 | case AF_INET: |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3297 | memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3298 | break; |
| 3299 | case AF_INET6: |
| 3300 | memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16); |
| 3301 | break; |
| 3302 | }; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3303 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3304 | srv_set_addr_desc(s, 1); |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3305 | |
| 3306 | return 0; |
| 3307 | } |
| 3308 | |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 3309 | /* update agent health check address and port |
| 3310 | * addr can be ip4/ip6 or a hostname |
| 3311 | * if one error occurs, don't apply anything |
| 3312 | * must be called with the server lock held. |
| 3313 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3314 | 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] | 3315 | { |
| 3316 | struct sockaddr_storage sk; |
| 3317 | struct buffer *msg; |
| 3318 | int new_port; |
| 3319 | |
| 3320 | msg = get_trash_chunk(); |
| 3321 | chunk_reset(msg); |
| 3322 | |
| 3323 | if (!(s->agent.state & CHK_ST_ENABLED)) { |
| 3324 | chunk_strcat(msg, "agent checks are not enabled on this server"); |
| 3325 | goto out; |
| 3326 | } |
| 3327 | if (addr) { |
| 3328 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3329 | if (str2ip(addr, &sk) == NULL) { |
| 3330 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3331 | goto out; |
| 3332 | } |
| 3333 | } |
| 3334 | if (port) { |
| 3335 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3336 | chunk_appendf(msg, "provided port is not an integer"); |
| 3337 | goto out; |
| 3338 | } |
| 3339 | if (new_port < 0 || new_port > 65535) { |
| 3340 | chunk_appendf(msg, "provided port is invalid"); |
| 3341 | goto out; |
| 3342 | } |
| 3343 | } |
| 3344 | out: |
| 3345 | if (msg->data) |
| 3346 | return msg->area; |
| 3347 | else { |
| 3348 | if (addr) |
| 3349 | set_srv_agent_addr(s, &sk); |
| 3350 | if (port) |
| 3351 | set_srv_agent_port(s, new_port); |
| 3352 | } |
| 3353 | return NULL; |
| 3354 | } |
| 3355 | |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 3356 | /* update server health check address and port |
| 3357 | * addr must be ip4 or ip6, it won't be resolved |
| 3358 | * if one error occurs, don't apply anything |
| 3359 | * must be called with the server lock held. |
| 3360 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3361 | 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] | 3362 | { |
| 3363 | struct sockaddr_storage sk; |
| 3364 | struct buffer *msg; |
| 3365 | int new_port; |
| 3366 | |
| 3367 | msg = get_trash_chunk(); |
| 3368 | chunk_reset(msg); |
| 3369 | |
| 3370 | if (!(s->check.state & CHK_ST_ENABLED)) { |
| 3371 | chunk_strcat(msg, "health checks are not enabled on this server"); |
| 3372 | goto out; |
| 3373 | } |
| 3374 | if (addr) { |
| 3375 | memset(&sk, 0, sizeof(struct sockaddr_storage)); |
| 3376 | if (str2ip2(addr, &sk, 0) == NULL) { |
| 3377 | chunk_appendf(msg, "invalid addr '%s'", addr); |
| 3378 | goto out; |
| 3379 | } |
| 3380 | } |
| 3381 | if (port) { |
| 3382 | if (strl2irc(port, strlen(port), &new_port) != 0) { |
| 3383 | chunk_appendf(msg, "provided port is not an integer"); |
| 3384 | goto out; |
| 3385 | } |
| 3386 | if (new_port < 0 || new_port > 65535) { |
| 3387 | chunk_appendf(msg, "provided port is invalid"); |
| 3388 | goto out; |
| 3389 | } |
| 3390 | /* prevent the update of port to 0 if MAPPORTS are in use */ |
| 3391 | if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) { |
| 3392 | chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use"); |
| 3393 | goto out; |
| 3394 | } |
| 3395 | } |
| 3396 | out: |
| 3397 | if (msg->data) |
| 3398 | return msg->area; |
| 3399 | else { |
| 3400 | if (addr) |
| 3401 | s->check.addr = sk; |
| 3402 | if (port) |
| 3403 | s->check.port = new_port; |
| 3404 | } |
| 3405 | return NULL; |
| 3406 | } |
| 3407 | |
Baptiste Assmann | 14e4014 | 2015-04-14 01:13:07 +0200 | [diff] [blame] | 3408 | /* |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3409 | * This function update a server's addr and port only for AF_INET and AF_INET6 families. |
| 3410 | * |
| 3411 | * Caller can pass its name through <updater> to get it integrated in the response message |
| 3412 | * returned by the function. |
| 3413 | * |
| 3414 | * The function first does the following, in that order: |
| 3415 | * - validates the new addr and/or port |
| 3416 | * - checks if an update is required (new IP or port is different than current ones) |
| 3417 | * - checks the update is allowed: |
| 3418 | * - don't switch from/to a family other than AF_INET4 and AF_INET6 |
| 3419 | * - allow all changes if no CHECKS are configured |
| 3420 | * - if CHECK is configured: |
| 3421 | * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports |
| 3422 | * - applies required changes to both ADDR and PORT if both 'required' and 'allowed' |
| 3423 | * conditions are met |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3424 | * |
| 3425 | * Must be called with the server lock held. |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3426 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3427 | 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] | 3428 | { |
| 3429 | struct sockaddr_storage sa; |
| 3430 | int ret, port_change_required; |
| 3431 | char current_addr[INET6_ADDRSTRLEN]; |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3432 | uint16_t current_port, new_port; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3433 | struct buffer *msg; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3434 | int changed = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3435 | |
| 3436 | msg = get_trash_chunk(); |
| 3437 | chunk_reset(msg); |
| 3438 | |
| 3439 | if (addr) { |
| 3440 | memset(&sa, 0, sizeof(struct sockaddr_storage)); |
| 3441 | if (str2ip2(addr, &sa, 0) == NULL) { |
| 3442 | chunk_printf(msg, "Invalid addr '%s'", addr); |
| 3443 | goto out; |
| 3444 | } |
| 3445 | |
| 3446 | /* changes are allowed on AF_INET* families only */ |
| 3447 | if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) { |
| 3448 | chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file"); |
| 3449 | goto out; |
| 3450 | } |
| 3451 | |
| 3452 | /* collecting data currently setup */ |
| 3453 | memset(current_addr, '\0', sizeof(current_addr)); |
| 3454 | ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr)); |
| 3455 | /* changes are allowed on AF_INET* families only */ |
| 3456 | if ((ret != AF_INET) && (ret != AF_INET6)) { |
| 3457 | chunk_printf(msg, "Update for the current server address family is only supported through configuration file"); |
| 3458 | goto out; |
| 3459 | } |
| 3460 | |
| 3461 | /* applying ADDR changes if required and allowed |
| 3462 | * ipcmp returns 0 when both ADDR are the same |
| 3463 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3464 | if (ipcmp(&s->addr, &sa, 0) == 0) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3465 | chunk_appendf(msg, "no need to change the addr"); |
| 3466 | goto port; |
| 3467 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3468 | ipcpy(&sa, &s->addr); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3469 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3470 | |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3471 | /* update report for caller */ |
| 3472 | chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr); |
| 3473 | } |
| 3474 | |
| 3475 | port: |
| 3476 | if (port) { |
| 3477 | char sign = '\0'; |
| 3478 | char *endptr; |
| 3479 | |
| 3480 | if (addr) |
| 3481 | chunk_appendf(msg, ", "); |
| 3482 | |
| 3483 | /* collecting data currently setup */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3484 | current_port = s->svc_port; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3485 | |
| 3486 | /* check if PORT change is required */ |
| 3487 | port_change_required = 0; |
| 3488 | |
| 3489 | sign = *port; |
Ryabin Sergey | 77ee752 | 2017-01-11 19:39:55 +0400 | [diff] [blame] | 3490 | errno = 0; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3491 | new_port = strtol(port, &endptr, 10); |
| 3492 | if ((errno != 0) || (port == endptr)) { |
| 3493 | chunk_appendf(msg, "problem converting port '%s' to an int", port); |
| 3494 | goto out; |
| 3495 | } |
| 3496 | |
| 3497 | /* check if caller triggers a port mapped or offset */ |
| 3498 | if (sign == '-' || (sign == '+')) { |
| 3499 | /* check if server currently uses port map */ |
| 3500 | if (!(s->flags & SRV_F_MAPPORTS)) { |
| 3501 | /* switch from fixed port to port map mandatorily triggers |
| 3502 | * a port change */ |
| 3503 | port_change_required = 1; |
| 3504 | /* check is configured |
| 3505 | * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port |
| 3506 | * prevent PORT change if check doesn't have it's dedicated port while switching |
| 3507 | * to port mapping */ |
William Dauchy | 69f118d | 2021-02-03 22:30:07 +0100 | [diff] [blame] | 3508 | if (!s->check.port) { |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3509 | 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."); |
| 3510 | goto out; |
| 3511 | } |
| 3512 | } |
| 3513 | /* we're already using port maps */ |
| 3514 | else { |
| 3515 | port_change_required = current_port != new_port; |
| 3516 | } |
| 3517 | } |
| 3518 | /* fixed port */ |
| 3519 | else { |
| 3520 | port_change_required = current_port != new_port; |
| 3521 | } |
| 3522 | |
| 3523 | /* applying PORT changes if required and update response message */ |
| 3524 | if (port_change_required) { |
| 3525 | /* apply new port */ |
Willy Tarreau | 04276f3 | 2017-01-06 17:41:29 +0100 | [diff] [blame] | 3526 | s->svc_port = new_port; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3527 | changed = 1; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3528 | |
| 3529 | /* prepare message */ |
| 3530 | chunk_appendf(msg, "port changed from '"); |
| 3531 | if (s->flags & SRV_F_MAPPORTS) |
| 3532 | chunk_appendf(msg, "+"); |
| 3533 | chunk_appendf(msg, "%d' to '", current_port); |
| 3534 | |
| 3535 | if (sign == '-') { |
| 3536 | s->flags |= SRV_F_MAPPORTS; |
| 3537 | chunk_appendf(msg, "%c", sign); |
| 3538 | /* just use for result output */ |
| 3539 | new_port = -new_port; |
| 3540 | } |
| 3541 | else if (sign == '+') { |
| 3542 | s->flags |= SRV_F_MAPPORTS; |
| 3543 | chunk_appendf(msg, "%c", sign); |
| 3544 | } |
| 3545 | else { |
| 3546 | s->flags &= ~SRV_F_MAPPORTS; |
| 3547 | } |
| 3548 | |
| 3549 | chunk_appendf(msg, "%d'", new_port); |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3550 | } |
| 3551 | else { |
| 3552 | chunk_appendf(msg, "no need to change the port"); |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | out: |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3557 | if (changed) { |
| 3558 | /* force connection cleanup on the given server */ |
| 3559 | srv_cleanup_connections(s); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 3560 | srv_set_dyncookie(s); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 3561 | srv_set_addr_desc(s, 1); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 3562 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3563 | if (updater) |
| 3564 | chunk_appendf(msg, " by '%s'", updater); |
| 3565 | chunk_appendf(msg, "\n"); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3566 | return msg->area; |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3569 | /* |
| 3570 | * update server status based on result of SRV resolution |
| 3571 | * returns: |
| 3572 | * 0 if server status is updated |
| 3573 | * 1 if server status has not changed |
| 3574 | * |
| 3575 | * Must be called with the server lock held. |
| 3576 | */ |
| 3577 | int srvrq_update_srv_status(struct server *s, int has_no_ip) |
| 3578 | { |
| 3579 | if (!s->srvrq) |
| 3580 | return 1; |
| 3581 | |
| 3582 | /* since this server has an IP, it can go back in production */ |
| 3583 | if (has_no_ip == 0) { |
| 3584 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3585 | return 1; |
| 3586 | } |
| 3587 | |
| 3588 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3589 | return 1; |
| 3590 | |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3591 | 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] | 3592 | return 0; |
| 3593 | } |
Baptiste Assmann | d458adc | 2016-08-02 08:18:55 +0200 | [diff] [blame] | 3594 | |
| 3595 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3596 | * update server status based on result of name resolution |
| 3597 | * returns: |
| 3598 | * 0 if server status is updated |
| 3599 | * 1 if server status has not changed |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3600 | * |
| 3601 | * Must be called with the server lock held. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3602 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3603 | int snr_update_srv_status(struct server *s, int has_no_ip) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3604 | { |
Emeric Brun | 750fe79 | 2020-12-23 16:51:12 +0100 | [diff] [blame] | 3605 | struct resolvers *resolvers = s->resolvers; |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3606 | struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3607 | int exp; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3608 | |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3609 | /* If resolution is NULL we're dealing with SRV records Additional records */ |
Christopher Faulet | 5efdef2 | 2021-03-11 18:03:21 +0100 | [diff] [blame] | 3610 | if (resolution == NULL) |
| 3611 | return srvrq_update_srv_status(s, has_no_ip); |
Jerome Magnin | 012261a | 2020-07-28 13:38:22 +0200 | [diff] [blame] | 3612 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3613 | switch (resolution->status) { |
| 3614 | case RSLV_STATUS_NONE: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3615 | /* status when HAProxy has just (re)started. |
| 3616 | * Nothing to do, since the task is already automatically started */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3617 | break; |
| 3618 | |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3619 | case RSLV_STATUS_VALID: |
| 3620 | /* |
| 3621 | * resume health checks |
| 3622 | * server will be turned back on if health check is safe |
| 3623 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3624 | if (has_no_ip) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3625 | if (s->next_admin & SRV_ADMF_RMAINT) |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3626 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3627 | 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] | 3628 | return 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3629 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3630 | |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 3631 | if (!(s->next_admin & SRV_ADMF_RMAINT)) |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3632 | return 1; |
| 3633 | srv_clr_admin_flag(s, SRV_ADMF_RMAINT); |
| 3634 | chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer", |
| 3635 | s->proxy->id, s->id); |
| 3636 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3637 | ha_warning("%s.\n", trash.area); |
| 3638 | send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area); |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3639 | return 0; |
| 3640 | |
| 3641 | case RSLV_STATUS_NX: |
| 3642 | /* stop server if resolution is NX for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3643 | exp = tick_add(resolution->last_valid, resolvers->hold.nx); |
| 3644 | if (!tick_is_expired(exp, now_ms)) |
| 3645 | break; |
| 3646 | |
| 3647 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3648 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3649 | 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] | 3650 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3651 | |
| 3652 | case RSLV_STATUS_TIMEOUT: |
| 3653 | /* stop server if resolution is TIMEOUT for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3654 | exp = tick_add(resolution->last_valid, resolvers->hold.timeout); |
| 3655 | if (!tick_is_expired(exp, now_ms)) |
| 3656 | break; |
| 3657 | |
| 3658 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3659 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3660 | 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] | 3661 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3662 | |
| 3663 | case RSLV_STATUS_REFUSED: |
| 3664 | /* stop server if resolution is REFUSED for a long enough period */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3665 | exp = tick_add(resolution->last_valid, resolvers->hold.refused); |
| 3666 | if (!tick_is_expired(exp, now_ms)) |
| 3667 | break; |
| 3668 | |
| 3669 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3670 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3671 | 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] | 3672 | return 0; |
Baptiste Assmann | 3b9fe9f | 2016-11-02 22:58:18 +0100 | [diff] [blame] | 3673 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3674 | default: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3675 | /* stop server if resolution failed for a long enough period */ |
| 3676 | exp = tick_add(resolution->last_valid, resolvers->hold.other); |
| 3677 | if (!tick_is_expired(exp, now_ms)) |
| 3678 | break; |
| 3679 | |
| 3680 | if (s->next_admin & SRV_ADMF_RMAINT) |
| 3681 | return 1; |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 3682 | 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] | 3683 | return 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | return 1; |
| 3687 | } |
| 3688 | |
| 3689 | /* |
| 3690 | * Server Name Resolution valid response callback |
| 3691 | * It expects: |
| 3692 | * - <nameserver>: the name server which answered the valid response |
| 3693 | * - <response>: buffer containing a valid DNS response |
| 3694 | * - <response_len>: size of <response> |
| 3695 | * It performs the following actions: |
| 3696 | * - ignore response if current ip found and server family not met |
| 3697 | * - update with first new ip found if family is met and current IP is not found |
| 3698 | * returns: |
| 3699 | * 0 on error |
| 3700 | * 1 when no error or safe ignore |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3701 | * |
| 3702 | * Must be called with server lock held |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3703 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3704 | int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3705 | { |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3706 | struct server *s = NULL; |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3707 | struct resolv_resolution *resolution = NULL; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3708 | void *serverip, *firstip; |
| 3709 | short server_sin_family, firstip_sin_family; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3710 | int ret; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3711 | struct buffer *chk = get_trash_chunk(); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3712 | int has_no_ip = 0; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3713 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3714 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3715 | if (!s) |
| 3716 | return 1; |
| 3717 | |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3718 | if (s->srvrq) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3719 | /* If DNS resolution is disabled ignore it. |
| 3720 | * This is the case if the server was associated to |
| 3721 | * a SRV record and this record is now expired. |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3722 | */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3723 | if (s->flags & SRV_F_NO_RESOLUTION) |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3724 | return 1; |
| 3725 | } |
| 3726 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3727 | resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL); |
Christopher Faulet | 49531e8 | 2021-03-10 15:07:27 +0100 | [diff] [blame] | 3728 | if (!resolution) |
| 3729 | return 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3730 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3731 | /* initializing variables */ |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3732 | firstip = NULL; /* pointer to the first valid response found */ |
| 3733 | /* it will be used as the new IP if a change is required */ |
| 3734 | firstip_sin_family = AF_UNSPEC; |
| 3735 | serverip = NULL; /* current server IP address */ |
| 3736 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3737 | /* initializing server IP pointer */ |
| 3738 | server_sin_family = s->addr.ss_family; |
| 3739 | switch (server_sin_family) { |
| 3740 | case AF_INET: |
| 3741 | serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr; |
| 3742 | break; |
| 3743 | |
| 3744 | case AF_INET6: |
| 3745 | serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr; |
| 3746 | break; |
| 3747 | |
Willy Tarreau | 3acfcd1 | 2017-01-06 19:18:32 +0100 | [diff] [blame] | 3748 | case AF_UNSPEC: |
| 3749 | break; |
| 3750 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3751 | default: |
| 3752 | goto invalid; |
| 3753 | } |
| 3754 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3755 | ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts, |
| 3756 | serverip, server_sin_family, &firstip, |
| 3757 | &firstip_sin_family, s); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3758 | |
| 3759 | switch (ret) { |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3760 | case RSLV_UPD_NO: |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3761 | goto update_status; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3762 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3763 | case RSLV_UPD_SRVIP_NOT_FOUND: |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3764 | goto save_ip; |
| 3765 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3766 | case RSLV_UPD_NO_IP_FOUND: |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3767 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3768 | goto update_status; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 3769 | |
Emeric Brun | 456de77 | 2020-12-23 18:17:31 +0100 | [diff] [blame] | 3770 | case RSLV_UPD_NAME_ERROR: |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3771 | /* update resolution status to OTHER error type */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3772 | resolution->status = RSLV_STATUS_OTHER; |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3773 | has_no_ip = 1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3774 | goto update_status; |
Baptiste Assmann | fad0318 | 2015-10-28 02:03:32 +0100 | [diff] [blame] | 3775 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3776 | default: |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3777 | has_no_ip = 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3778 | goto invalid; |
| 3779 | |
| 3780 | } |
| 3781 | |
| 3782 | save_ip: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3783 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3784 | counters->app.resolver.update++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3785 | /* save the first ip we found */ |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3786 | chunk_printf(chk, "%s/%s", counters->pid, counters->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3787 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3788 | else |
| 3789 | chunk_printf(chk, "DNS cache"); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 3790 | srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3791 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3792 | update_status: |
Christopher Faulet | a8ce497 | 2021-06-24 15:26:03 +0200 | [diff] [blame] | 3793 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3794 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3795 | return 1; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3796 | |
| 3797 | invalid: |
Emeric Brun | 50c870e | 2021-01-04 10:40:46 +0100 | [diff] [blame] | 3798 | if (counters) { |
Emeric Brun | d174f0e | 2021-10-29 17:30:41 +0200 | [diff] [blame] | 3799 | counters->app.resolver.invalid++; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3800 | goto update_status; |
Christopher Faulet | 3bbd65b | 2017-09-15 11:55:45 +0200 | [diff] [blame] | 3801 | } |
Christopher Faulet | 07ecff5 | 2021-06-24 15:33:52 +0200 | [diff] [blame] | 3802 | if (!snr_update_srv_status(s, has_no_ip) && has_no_ip) |
| 3803 | memset(&s->addr, 0, sizeof(s->addr)); |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3804 | return 0; |
| 3805 | } |
| 3806 | |
| 3807 | /* |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3808 | * SRV record error management callback |
| 3809 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3810 | * 0 if we can trash answser items. |
| 3811 | * 1 when safely ignored and we must kept answer items |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3812 | * |
| 3813 | * Grabs the server's lock. |
| 3814 | */ |
| 3815 | int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code) |
| 3816 | { |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3817 | struct resolv_srvrq *srvrq; |
| 3818 | struct resolv_resolution *res; |
| 3819 | struct resolvers *resolvers; |
| 3820 | int exp; |
| 3821 | |
| 3822 | /* SRV records */ |
| 3823 | srvrq = objt_resolv_srvrq(requester->owner); |
| 3824 | if (!srvrq) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3825 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3826 | |
| 3827 | resolvers = srvrq->resolvers; |
| 3828 | res = requester->resolution; |
| 3829 | |
| 3830 | switch (res->status) { |
| 3831 | |
| 3832 | case RSLV_STATUS_NX: |
| 3833 | /* stop server if resolution is NX for a long enough period */ |
| 3834 | exp = tick_add(res->last_valid, resolvers->hold.nx); |
| 3835 | if (!tick_is_expired(exp, now_ms)) |
| 3836 | return 1; |
| 3837 | break; |
| 3838 | |
| 3839 | case RSLV_STATUS_TIMEOUT: |
| 3840 | /* stop server if resolution is TIMEOUT for a long enough period */ |
| 3841 | exp = tick_add(res->last_valid, resolvers->hold.timeout); |
| 3842 | if (!tick_is_expired(exp, now_ms)) |
| 3843 | return 1; |
| 3844 | break; |
| 3845 | |
| 3846 | case RSLV_STATUS_REFUSED: |
| 3847 | /* stop server if resolution is REFUSED for a long enough period */ |
| 3848 | exp = tick_add(res->last_valid, resolvers->hold.refused); |
| 3849 | if (!tick_is_expired(exp, now_ms)) |
| 3850 | return 1; |
| 3851 | break; |
| 3852 | |
| 3853 | default: |
| 3854 | /* stop server if resolution failed for a long enough period */ |
| 3855 | exp = tick_add(res->last_valid, resolvers->hold.other); |
| 3856 | if (!tick_is_expired(exp, now_ms)) |
| 3857 | return 1; |
| 3858 | } |
| 3859 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 3860 | /* Remove any associated server ref */ |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3861 | resolv_detach_from_resolution_answer_items(res, requester); |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3862 | |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3863 | return 0; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | /* |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3867 | * Server Name Resolution error management callback |
| 3868 | * returns: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3869 | * 0 if we can trash answser items. |
| 3870 | * 1 when safely ignored and we must kept answer items |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3871 | * |
| 3872 | * Grabs the server's lock. |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3873 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3874 | int snr_resolution_error_cb(struct resolv_requester *requester, int error_code) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3875 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3876 | struct server *s; |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3877 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3878 | s = objt_server(requester->owner); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3879 | if (!s) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3880 | return 0; |
| 3881 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3882 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3883 | if (!snr_update_srv_status(s, 1)) { |
Christopher Faulet | 5130c21 | 2021-03-10 20:31:40 +0100 | [diff] [blame] | 3884 | memset(&s->addr, 0, sizeof(s->addr)); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3885 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 3886 | resolv_detach_from_resolution_answer_items(requester->resolution, requester); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3887 | return 0; |
| 3888 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3889 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 3890 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 3891 | return 1; |
| 3892 | } |
| 3893 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3894 | /* |
| 3895 | * 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] | 3896 | * which owns <srv> and is up. |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3897 | * It returns a pointer to the first server found or NULL if <ip> is not yet |
| 3898 | * assigned. |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 3899 | * |
| 3900 | * Must be called with server lock held |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3901 | */ |
| 3902 | struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family) |
| 3903 | { |
| 3904 | struct server *tmpsrv; |
| 3905 | struct proxy *be; |
| 3906 | |
| 3907 | if (!srv) |
| 3908 | return NULL; |
| 3909 | |
| 3910 | be = srv->proxy; |
| 3911 | for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) { |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3912 | /* we found the current server is the same, ignore it */ |
| 3913 | if (srv == tmpsrv) |
| 3914 | continue; |
| 3915 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3916 | /* We want to compare the IP in the record with the IP of the servers in the |
| 3917 | * same backend, only if: |
| 3918 | * * DNS resolution is enabled on the server |
| 3919 | * * the hostname used for the resolution by our server is the same than the |
| 3920 | * one used for the server found in the backend |
| 3921 | * * the server found in the backend is not our current server |
| 3922 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3923 | HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3924 | if ((tmpsrv->hostname_dn == NULL) || |
| 3925 | (srv->hostname_dn_len != tmpsrv->hostname_dn_len) || |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 3926 | (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) || |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3927 | (srv->puid == tmpsrv->puid)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3928 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3929 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3930 | } |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3931 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3932 | /* If the server has been taken down, don't consider it */ |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3933 | if (tmpsrv->next_admin & SRV_ADMF_RMAINT) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3934 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3935 | continue; |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3936 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 3937 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3938 | /* At this point, we have 2 different servers using the same DNS hostname |
| 3939 | * for their respective resolution. |
| 3940 | */ |
| 3941 | if (*ip_family == tmpsrv->addr.ss_family && |
| 3942 | ((tmpsrv->addr.ss_family == AF_INET && |
| 3943 | memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) || |
| 3944 | (tmpsrv->addr.ss_family == AF_INET6 && |
| 3945 | memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3946 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3947 | return tmpsrv; |
| 3948 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3949 | HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock); |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
Emeric Brun | e9fd6b5 | 2017-11-02 17:20:39 +0100 | [diff] [blame] | 3952 | |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 3953 | return NULL; |
| 3954 | } |
| 3955 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3956 | /* Sets the server's address (srv->addr) from srv->hostname using the libc's |
| 3957 | * resolver. This is suited for initial address configuration. Returns 0 on |
| 3958 | * success otherwise a non-zero error code. In case of error, *err_code, if |
| 3959 | * not NULL, is filled up. |
| 3960 | */ |
| 3961 | int srv_set_addr_via_libc(struct server *srv, int *err_code) |
| 3962 | { |
| 3963 | if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) { |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3964 | if (err_code) |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 3965 | *err_code |= ERR_WARN; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 3966 | return 1; |
| 3967 | } |
| 3968 | return 0; |
| 3969 | } |
| 3970 | |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3971 | /* Set the server's FDQN (->hostname) from <hostname>. |
| 3972 | * Returns -1 if failed, 0 if not. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 3973 | * |
| 3974 | * Must be called with the server lock held. |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 3975 | */ |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3976 | 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] | 3977 | { |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3978 | struct resolv_resolution *resolution; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3979 | char *hostname_dn; |
| 3980 | int hostname_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3981 | |
Frédéric Lécaille | 5afb3cf | 2018-08-21 15:04:23 +0200 | [diff] [blame] | 3982 | /* Note that the server lock is already held. */ |
| 3983 | if (!srv->resolvers) |
| 3984 | return -1; |
| 3985 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 3986 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3987 | HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3988 | /* run time DNS/SRV resolution was not active for this server |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3989 | * and we can't enable it at run time for now. |
| 3990 | */ |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 3991 | if (!srv->resolv_requester && !srv->srvrq) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 3992 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 3993 | |
| 3994 | chunk_reset(&trash); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3995 | hostname_len = strlen(hostname); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3996 | hostname_dn = trash.area; |
Willy Tarreau | bf9498a | 2021-10-14 07:49:49 +0200 | [diff] [blame] | 3997 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len, |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 3998 | hostname_dn, trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 3999 | if (hostname_dn_len == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4000 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4001 | |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 4002 | resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4003 | if (resolution && |
| 4004 | resolution->hostname_dn && |
Christopher Faulet | 59b2925 | 2021-03-16 11:21:04 +0100 | [diff] [blame] | 4005 | resolution->hostname_dn_len == hostname_dn_len && |
| 4006 | strcasecmp(resolution->hostname_dn, hostname_dn) == 0) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4007 | goto end; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4008 | |
Willy Tarreau | 6878f80 | 2021-10-20 14:07:31 +0200 | [diff] [blame] | 4009 | resolv_unlink_resolution(srv->resolv_requester); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4010 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4011 | free(srv->hostname); |
| 4012 | free(srv->hostname_dn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 4013 | srv->hostname = strdup(hostname); |
| 4014 | srv->hostname_dn = strdup(hostname_dn); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4015 | srv->hostname_dn_len = hostname_dn_len; |
| 4016 | if (!srv->hostname || !srv->hostname_dn) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4017 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4018 | |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4019 | if (srv->flags & SRV_F_NO_RESOLUTION) |
| 4020 | goto end; |
| 4021 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 4022 | if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1) |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4023 | goto err; |
| 4024 | |
| 4025 | end: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4026 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4027 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 4028 | return 0; |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4029 | |
| 4030 | err: |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4031 | if (!resolv_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 4032 | HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 4033 | return -1; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4034 | } |
| 4035 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4036 | /* Sets the server's address (srv->addr) from srv->lastaddr which was filled |
| 4037 | * from the state file. This is suited for initial address configuration. |
| 4038 | * Returns 0 on success otherwise a non-zero error code. In case of error, |
| 4039 | * *err_code, if not NULL, is filled up. |
| 4040 | */ |
| 4041 | static int srv_apply_lastaddr(struct server *srv, int *err_code) |
| 4042 | { |
| 4043 | if (!str2ip2(srv->lastaddr, &srv->addr, 0)) { |
| 4044 | if (err_code) |
| 4045 | *err_code |= ERR_WARN; |
| 4046 | return 1; |
| 4047 | } |
| 4048 | return 0; |
| 4049 | } |
| 4050 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4051 | /* returns 0 if no error, otherwise a combination of ERR_* flags */ |
| 4052 | static int srv_iterate_initaddr(struct server *srv) |
| 4053 | { |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4054 | char *name = srv->hostname; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4055 | int return_code = 0; |
| 4056 | int err_code; |
| 4057 | unsigned int methods; |
| 4058 | |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4059 | /* If no addr and no hostname set, get the name from the DNS SRV request */ |
| 4060 | if (!name && srv->srvrq) |
| 4061 | name = srv->srvrq->name; |
| 4062 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4063 | methods = srv->init_addr_methods; |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4064 | if (!methods) { |
| 4065 | /* otherwise default to "last,libc" */ |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4066 | srv_append_initaddr(&methods, SRV_IADDR_LAST); |
| 4067 | srv_append_initaddr(&methods, SRV_IADDR_LIBC); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4068 | if (srv->resolvers_id) { |
| 4069 | /* dns resolution is configured, add "none" to not fail on startup */ |
| 4070 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4071 | } |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4072 | } |
| 4073 | |
Willy Tarreau | 3eed10e | 2016-11-07 21:03:16 +0100 | [diff] [blame] | 4074 | /* "-dr" : always append "none" so that server addresses resolution |
| 4075 | * failures are silently ignored, this is convenient to validate some |
| 4076 | * configs out of their environment. |
| 4077 | */ |
| 4078 | if (global.tune.options & GTUNE_RESOLVE_DONTFAIL) |
| 4079 | srv_append_initaddr(&methods, SRV_IADDR_NONE); |
| 4080 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4081 | while (methods) { |
| 4082 | err_code = 0; |
| 4083 | switch (srv_get_next_initaddr(&methods)) { |
| 4084 | case SRV_IADDR_LAST: |
| 4085 | if (!srv->lastaddr) |
| 4086 | continue; |
| 4087 | if (srv_apply_lastaddr(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4088 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4089 | return_code |= err_code; |
| 4090 | break; |
| 4091 | |
| 4092 | case SRV_IADDR_LIBC: |
| 4093 | if (!srv->hostname) |
| 4094 | continue; |
| 4095 | if (srv_set_addr_via_libc(srv, &err_code) == 0) |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4096 | goto out; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4097 | return_code |= err_code; |
| 4098 | break; |
| 4099 | |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4100 | case SRV_IADDR_NONE: |
Aurelien DARRAGON | 9b1ccd7 | 2023-04-03 17:40:28 +0200 | [diff] [blame] | 4101 | srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4102 | if (return_code) { |
Christopher Faulet | 521d99f | 2023-07-20 17:25:54 +0200 | [diff] [blame] | 4103 | ha_notice("could not resolve address '%s', disabling server.\n", |
| 4104 | name); |
Willy Tarreau | 465b6e5 | 2016-11-07 19:19:22 +0100 | [diff] [blame] | 4105 | } |
Willy Tarreau | 37ebe12 | 2016-11-04 15:17:58 +0100 | [diff] [blame] | 4106 | return return_code; |
| 4107 | |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4108 | case SRV_IADDR_IP: |
| 4109 | ipcpy(&srv->init_addr, &srv->addr); |
| 4110 | if (return_code) { |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4111 | ha_warning("could not resolve address '%s', falling back to configured address.\n", |
| 4112 | name); |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4113 | } |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4114 | goto out; |
Willy Tarreau | 4310d36 | 2016-11-02 15:05:56 +0100 | [diff] [blame] | 4115 | |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4116 | default: /* unhandled method */ |
| 4117 | break; |
| 4118 | } |
| 4119 | } |
| 4120 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4121 | if (!return_code) |
| 4122 | ha_alert("no method found to resolve address '%s'.\n", name); |
| 4123 | else |
| 4124 | ha_alert("could not resolve address '%s'.\n", name); |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4125 | |
| 4126 | return_code |= ERR_ALERT | ERR_FATAL; |
| 4127 | return return_code; |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4128 | out: |
| 4129 | srv_set_dyncookie(srv); |
Amaury Denoyelle | 8ff0434 | 2021-06-08 15:19:51 +0200 | [diff] [blame] | 4130 | srv_set_addr_desc(srv, 1); |
Olivier Houchard | 4e69404 | 2017-03-14 20:01:29 +0100 | [diff] [blame] | 4131 | return return_code; |
Willy Tarreau | 25e5152 | 2016-11-04 15:10:17 +0100 | [diff] [blame] | 4132 | } |
| 4133 | |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4134 | /* |
| 4135 | * This function parses all backends and all servers within each backend |
| 4136 | * and performs servers' addr resolution based on information provided by: |
| 4137 | * - configuration file |
| 4138 | * - server-state file (states provided by an 'old' haproxy process) |
| 4139 | * |
| 4140 | * Returns 0 if no error, otherwise, a combination of ERR_ flags. |
| 4141 | */ |
| 4142 | int srv_init_addr(void) |
| 4143 | { |
| 4144 | struct proxy *curproxy; |
| 4145 | int return_code = 0; |
| 4146 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 4147 | curproxy = proxies_list; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4148 | while (curproxy) { |
| 4149 | struct server *srv; |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4150 | |
| 4151 | /* servers are in backend only */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4152 | 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] | 4153 | goto srv_init_addr_next; |
| 4154 | |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4155 | for (srv = curproxy->srv; srv; srv = srv->next) { |
| 4156 | set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type); |
Christopher Faulet | ac1c60fd | 2020-10-26 10:31:17 +0100 | [diff] [blame] | 4157 | if (srv->hostname || srv->srvrq) |
Willy Tarreau | 3d609a7 | 2017-09-06 14:22:45 +0200 | [diff] [blame] | 4158 | return_code |= srv_iterate_initaddr(srv); |
Amaury Denoyelle | 9d0138a | 2021-05-28 11:01:52 +0200 | [diff] [blame] | 4159 | reset_usermsgs_ctx(); |
| 4160 | } |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 4161 | |
| 4162 | srv_init_addr_next: |
| 4163 | curproxy = curproxy->next; |
| 4164 | } |
| 4165 | |
| 4166 | return return_code; |
| 4167 | } |
| 4168 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4169 | /* |
| 4170 | * Must be called with the server lock held. |
| 4171 | */ |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4172 | 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] | 4173 | { |
| 4174 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4175 | struct buffer *msg; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4176 | |
| 4177 | msg = get_trash_chunk(); |
| 4178 | chunk_reset(msg); |
| 4179 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4180 | if (server->hostname && strcmp(fqdn, server->hostname) == 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4181 | chunk_appendf(msg, "no need to change the FDQN"); |
| 4182 | goto out; |
| 4183 | } |
| 4184 | |
| 4185 | if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) { |
| 4186 | chunk_appendf(msg, "invalid fqdn '%s'", fqdn); |
| 4187 | goto out; |
| 4188 | } |
| 4189 | |
| 4190 | chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s", |
| 4191 | server->proxy->id, server->id, server->hostname, fqdn); |
| 4192 | |
Emeric Brun | 08622d3 | 2020-12-23 17:41:43 +0100 | [diff] [blame] | 4193 | if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) { |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4194 | chunk_reset(msg); |
| 4195 | chunk_appendf(msg, "could not update %s/%s FQDN", |
| 4196 | server->proxy->id, server->id); |
| 4197 | goto out; |
| 4198 | } |
| 4199 | |
| 4200 | /* Flag as FQDN set from stats socket. */ |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 4201 | server->next_admin |= SRV_ADMF_HMAINT; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4202 | |
| 4203 | out: |
| 4204 | if (updater) |
| 4205 | chunk_appendf(msg, " by '%s'", updater); |
| 4206 | chunk_appendf(msg, "\n"); |
| 4207 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4208 | return msg->area; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4212 | /* Expects to find a backend and a server in <arg> under the form <backend>/<server>, |
| 4213 | * and returns the pointer to the server. Otherwise, display adequate error messages |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 4214 | * 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] | 4215 | * used for CLI commands requiring a server name. |
| 4216 | * Important: the <arg> is modified to remove the '/'. |
| 4217 | */ |
| 4218 | struct server *cli_find_server(struct appctx *appctx, char *arg) |
| 4219 | { |
| 4220 | struct proxy *px; |
| 4221 | struct server *sv; |
| 4222 | char *line; |
| 4223 | |
| 4224 | /* split "backend/server" and make <line> point to server */ |
| 4225 | for (line = arg; *line; line++) |
| 4226 | if (*line == '/') { |
| 4227 | *line++ = '\0'; |
| 4228 | break; |
| 4229 | } |
| 4230 | |
| 4231 | if (!*line || !*arg) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4232 | cli_err(appctx, "Require 'backend/server'.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4233 | return NULL; |
| 4234 | } |
| 4235 | |
| 4236 | if (!get_backend_server(arg, line, &px, &sv)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4237 | cli_err(appctx, px ? "No such server.\n" : "No such backend.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4238 | return NULL; |
| 4239 | } |
| 4240 | |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 4241 | if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4242 | cli_err(appctx, "Proxy is disabled.\n"); |
Willy Tarreau | 21b069d | 2016-11-23 17:15:08 +0100 | [diff] [blame] | 4243 | return NULL; |
| 4244 | } |
| 4245 | |
| 4246 | return sv; |
| 4247 | } |
| 4248 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4249 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4250 | /* grabs the server lock */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4251 | 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] | 4252 | { |
| 4253 | struct server *sv; |
| 4254 | const char *warning; |
| 4255 | |
| 4256 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4257 | return 1; |
| 4258 | |
| 4259 | sv = cli_find_server(appctx, args[2]); |
| 4260 | if (!sv) |
| 4261 | return 1; |
| 4262 | |
| 4263 | if (strcmp(args[3], "weight") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4264 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4265 | warning = server_parse_weight_change_request(sv, args[4]); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4266 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4267 | if (warning) |
| 4268 | cli_err(appctx, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4269 | } |
| 4270 | else if (strcmp(args[3], "state") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4271 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4272 | if (strcmp(args[4], "ready") == 0) |
| 4273 | srv_adm_set_ready(sv); |
| 4274 | else if (strcmp(args[4], "drain") == 0) |
| 4275 | srv_adm_set_drain(sv); |
| 4276 | else if (strcmp(args[4], "maint") == 0) |
| 4277 | srv_adm_set_maint(sv); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4278 | else |
| 4279 | 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] | 4280 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4281 | } |
| 4282 | else if (strcmp(args[3], "health") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4283 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4284 | if (sv->track) |
| 4285 | cli_err(appctx, "cannot change health on a tracking server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4286 | else if (strcmp(args[4], "up") == 0) { |
| 4287 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4288 | srv_set_running(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4289 | } |
| 4290 | else if (strcmp(args[4], "stopping") == 0) { |
| 4291 | sv->check.health = sv->check.rise + sv->check.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4292 | srv_set_stopping(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4293 | } |
| 4294 | else if (strcmp(args[4], "down") == 0) { |
| 4295 | sv->check.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4296 | srv_set_stopped(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4297 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4298 | else |
| 4299 | 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] | 4300 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4301 | } |
| 4302 | else if (strcmp(args[3], "agent") == 0) { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4303 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4304 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4305 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4306 | else if (strcmp(args[4], "up") == 0) { |
| 4307 | sv->agent.health = sv->agent.rise + sv->agent.fall - 1; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4308 | srv_set_running(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4309 | } |
| 4310 | else if (strcmp(args[4], "down") == 0) { |
| 4311 | sv->agent.health = 0; |
Aurelien DARRAGON | 1746b56 | 2023-04-04 10:17:40 +0200 | [diff] [blame] | 4312 | srv_set_stopped(sv, SRV_OP_STCHGC_CLI); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4313 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4314 | else |
| 4315 | cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4316 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4317 | } |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4318 | else if (strcmp(args[3], "agent-addr") == 0) { |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4319 | char *addr = NULL; |
| 4320 | char *port = NULL; |
| 4321 | if (strlen(args[4]) == 0) { |
| 4322 | cli_err(appctx, "set server <b>/<s> agent-addr requires" |
| 4323 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4324 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4325 | } |
| 4326 | addr = args[4]; |
| 4327 | if (strcmp(args[5], "port") == 0) |
| 4328 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4329 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4330 | warning = srv_update_agent_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4331 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4332 | if (warning) |
| 4333 | cli_msg(appctx, LOG_WARNING, warning); |
| 4334 | } |
| 4335 | else if (strcmp(args[3], "agent-port") == 0) { |
| 4336 | char *port = NULL; |
| 4337 | if (strlen(args[4]) == 0) { |
| 4338 | cli_err(appctx, "set server <b>/<s> agent-port requires" |
| 4339 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4340 | goto out; |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4341 | } |
| 4342 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4343 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4344 | warning = srv_update_agent_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4345 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | 7cabc06 | 2021-02-11 22:51:24 +0100 | [diff] [blame] | 4346 | if (warning) |
| 4347 | cli_msg(appctx, LOG_WARNING, warning); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4348 | } |
| 4349 | else if (strcmp(args[3], "agent-send") == 0) { |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4350 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4351 | if (!(sv->agent.state & CHK_ST_ENABLED)) |
| 4352 | cli_err(appctx, "agent checks are not enabled on this server.\n"); |
| 4353 | else { |
Christopher Faulet | 0ae3d1d | 2020-04-06 17:54:24 +0200 | [diff] [blame] | 4354 | if (!set_srv_agent_send(sv, args[4])) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4355 | cli_err(appctx, "cannot allocate memory for new string.\n"); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4356 | } |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4357 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Misiek | 2da082d | 2017-01-09 09:40:42 +0100 | [diff] [blame] | 4358 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4359 | else if (strcmp(args[3], "check-addr") == 0) { |
| 4360 | char *addr = NULL; |
| 4361 | char *port = NULL; |
| 4362 | if (strlen(args[4]) == 0) { |
| 4363 | cli_err(appctx, "set server <b>/<s> check-addr requires" |
| 4364 | " an address and optionally a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4365 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4366 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4367 | addr = args[4]; |
| 4368 | if (strcmp(args[5], "port") == 0) |
| 4369 | port = args[6]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4370 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4371 | warning = srv_update_check_addr_port(sv, addr, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4372 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4373 | if (warning) |
| 4374 | cli_msg(appctx, LOG_WARNING, warning); |
| 4375 | } |
| 4376 | else if (strcmp(args[3], "check-port") == 0) { |
| 4377 | char *port = NULL; |
| 4378 | if (strlen(args[4]) == 0) { |
| 4379 | cli_err(appctx, "set server <b>/<s> check-port requires" |
| 4380 | " a port.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4381 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4382 | } |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4383 | port = args[4]; |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4384 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4385 | warning = srv_update_check_addr_port(sv, NULL, port); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4386 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | b456e1f | 2021-02-11 22:51:23 +0100 | [diff] [blame] | 4387 | if (warning) |
| 4388 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4389 | } |
| 4390 | else if (strcmp(args[3], "addr") == 0) { |
| 4391 | char *addr = NULL; |
| 4392 | char *port = NULL; |
| 4393 | if (strlen(args[4]) == 0) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4394 | 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] | 4395 | goto out; |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4396 | } |
| 4397 | else { |
| 4398 | addr = args[4]; |
| 4399 | } |
| 4400 | if (strcmp(args[5], "port") == 0) { |
| 4401 | port = args[6]; |
| 4402 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4403 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 4404 | warning = srv_update_addr_port(sv, addr, port, "stats socket command"); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4405 | if (warning) |
| 4406 | cli_msg(appctx, LOG_WARNING, warning); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4407 | srv_clr_admin_flag(sv, SRV_ADMF_RMAINT); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4408 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4409 | } |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4410 | else if (strcmp(args[3], "fqdn") == 0) { |
| 4411 | if (!*args[4]) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4412 | cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4413 | goto out; |
| 4414 | } |
| 4415 | if (!sv->resolvers) { |
| 4416 | cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n"); |
| 4417 | goto out; |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4418 | } |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4419 | if (sv->srvrq) { |
| 4420 | 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] | 4421 | goto out; |
Christopher Faulet | a386e78 | 2021-06-15 11:37:40 +0200 | [diff] [blame] | 4422 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4423 | HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock); |
| 4424 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 4425 | /* ensure runtime resolver will process this new fqdn */ |
| 4426 | if (sv->flags & SRV_F_NO_RESOLUTION) { |
| 4427 | sv->flags &= ~SRV_F_NO_RESOLUTION; |
| 4428 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4429 | warning = srv_update_fqdn(sv, args[4], "stats socket command", 1); |
Christopher Faulet | 0ba54bb | 2021-06-18 08:47:14 +0200 | [diff] [blame] | 4430 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4431 | HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4432 | if (warning) |
| 4433 | cli_msg(appctx, LOG_WARNING, warning); |
Frédéric Lécaille | b418c12 | 2017-04-26 11:24:02 +0200 | [diff] [blame] | 4434 | } |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4435 | else if (strcmp(args[3], "ssl") == 0) { |
| 4436 | #ifdef USE_OPENSSL |
Amaury Denoyelle | b89d3d3 | 2021-05-19 15:00:54 +0200 | [diff] [blame] | 4437 | if (sv->flags & SRV_F_DYNAMIC) { |
| 4438 | cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n"); |
| 4439 | goto out; |
| 4440 | } |
| 4441 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4442 | if (sv->ssl_ctx.ctx == NULL) { |
| 4443 | cli_err(appctx, "'set server <srv> ssl' cannot be set. " |
| 4444 | " default-server should define ssl settings\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4445 | goto out; |
| 4446 | } |
| 4447 | |
| 4448 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4449 | if (strcmp(args[4], "on") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4450 | srv_set_ssl(sv, 1); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4451 | } else if (strcmp(args[4], "off") == 0) { |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 4452 | srv_set_ssl(sv, 0); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4453 | } else { |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4454 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4455 | cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n"); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4456 | goto out; |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4457 | } |
| 4458 | srv_cleanup_connections(sv); |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4459 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 4460 | cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n"); |
| 4461 | #else |
| 4462 | cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n"); |
| 4463 | #endif |
| 4464 | } else { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4465 | cli_err(appctx, |
William Dauchy | 3f4ec7d | 2021-02-15 17:22:16 +0100 | [diff] [blame] | 4466 | "usage: set server <backend>/<server> " |
| 4467 | "addr | agent | agent-addr | agent-port | agent-send | " |
| 4468 | "check-addr | check-port | fqdn | health | ssl | " |
| 4469 | "state | weight\n"); |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4470 | } |
Christopher Faulet | c7b391a | 2021-06-15 12:01:29 +0200 | [diff] [blame] | 4471 | out: |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 4472 | return 1; |
| 4473 | } |
| 4474 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4475 | 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] | 4476 | { |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4477 | struct proxy *px; |
| 4478 | struct server *sv; |
| 4479 | char *line; |
| 4480 | |
| 4481 | |
| 4482 | /* split "backend/server" and make <line> point to server */ |
| 4483 | for (line = args[2]; *line; line++) |
| 4484 | if (*line == '/') { |
| 4485 | *line++ = '\0'; |
| 4486 | break; |
| 4487 | } |
| 4488 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4489 | if (!*line) |
| 4490 | return cli_err(appctx, "Require 'backend/server'.\n"); |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4491 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4492 | if (!get_backend_server(args[2], line, &px, &sv)) |
| 4493 | 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] | 4494 | |
| 4495 | /* return server's effective weight at the moment */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4496 | snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight, |
| 4497 | sv->iweight); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 4498 | if (applet_putstr(appctx, trash.area) == -1) |
Christopher Faulet | 90b5abe | 2016-12-05 14:25:08 +0100 | [diff] [blame] | 4499 | return 0; |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4500 | return 1; |
| 4501 | } |
| 4502 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4503 | /* Parse a "set weight" command. |
| 4504 | * |
| 4505 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4506 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4507 | 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] | 4508 | { |
| 4509 | struct server *sv; |
| 4510 | const char *warning; |
| 4511 | |
| 4512 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4513 | return 1; |
| 4514 | |
| 4515 | sv = cli_find_server(appctx, args[2]); |
| 4516 | if (!sv) |
| 4517 | return 1; |
| 4518 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4519 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4520 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4521 | warning = server_parse_weight_change_request(sv, args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4522 | if (warning) |
| 4523 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4524 | |
| 4525 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4526 | |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4527 | return 1; |
| 4528 | } |
| 4529 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4530 | /* parse a "set maxconn server" command. It always returns 1. |
| 4531 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4532 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4533 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4534 | 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] | 4535 | { |
| 4536 | struct server *sv; |
| 4537 | const char *warning; |
| 4538 | |
| 4539 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4540 | return 1; |
| 4541 | |
| 4542 | sv = cli_find_server(appctx, args[3]); |
| 4543 | if (!sv) |
| 4544 | return 1; |
| 4545 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4546 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
| 4547 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4548 | warning = server_parse_maxconn_change_request(sv, args[4]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4549 | if (warning) |
| 4550 | cli_err(appctx, warning); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4551 | |
Amaury Denoyelle | 0274286 | 2021-06-18 11:11:36 +0200 | [diff] [blame] | 4552 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
| 4553 | |
Willy Tarreau | b802627 | 2016-11-23 11:26:56 +0100 | [diff] [blame] | 4554 | return 1; |
| 4555 | } |
William Lallemand | 6b16094 | 2016-11-22 12:34:35 +0100 | [diff] [blame] | 4556 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4557 | /* parse a "disable agent" command. It always returns 1. |
| 4558 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4559 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4560 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4561 | 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] | 4562 | { |
| 4563 | struct server *sv; |
| 4564 | |
| 4565 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4566 | return 1; |
| 4567 | |
| 4568 | sv = cli_find_server(appctx, args[2]); |
| 4569 | if (!sv) |
| 4570 | return 1; |
| 4571 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4572 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4573 | sv->agent.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4574 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4575 | return 1; |
| 4576 | } |
| 4577 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4578 | /* parse a "disable health" command. It always returns 1. |
| 4579 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4580 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4581 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4582 | 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] | 4583 | { |
| 4584 | struct server *sv; |
| 4585 | |
| 4586 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4587 | return 1; |
| 4588 | |
| 4589 | sv = cli_find_server(appctx, args[2]); |
| 4590 | if (!sv) |
| 4591 | return 1; |
| 4592 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4593 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4594 | sv->check.state &= ~CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4595 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4596 | return 1; |
| 4597 | } |
| 4598 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4599 | /* parse a "disable server" command. It always returns 1. |
| 4600 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4601 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4602 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4603 | 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] | 4604 | { |
| 4605 | struct server *sv; |
| 4606 | |
| 4607 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4608 | return 1; |
| 4609 | |
| 4610 | sv = cli_find_server(appctx, args[2]); |
| 4611 | if (!sv) |
| 4612 | return 1; |
| 4613 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4614 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4615 | srv_adm_set_maint(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4616 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4617 | return 1; |
| 4618 | } |
| 4619 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4620 | /* parse a "enable agent" command. It always returns 1. |
| 4621 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4622 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4623 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4624 | 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] | 4625 | { |
| 4626 | struct server *sv; |
| 4627 | |
| 4628 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4629 | return 1; |
| 4630 | |
| 4631 | sv = cli_find_server(appctx, args[2]); |
| 4632 | if (!sv) |
| 4633 | return 1; |
| 4634 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 4635 | if (!(sv->agent.state & CHK_ST_CONFIGURED)) |
| 4636 | 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] | 4637 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4638 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4639 | sv->agent.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4640 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 58d9cb7 | 2016-11-24 12:56:01 +0100 | [diff] [blame] | 4641 | return 1; |
| 4642 | } |
| 4643 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4644 | /* parse a "enable health" command. It always returns 1. |
| 4645 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4646 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4647 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4648 | 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] | 4649 | { |
| 4650 | struct server *sv; |
| 4651 | |
| 4652 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4653 | return 1; |
| 4654 | |
| 4655 | sv = cli_find_server(appctx, args[2]); |
| 4656 | if (!sv) |
| 4657 | return 1; |
| 4658 | |
Amaury Denoyelle | 0f456d5 | 2021-09-21 10:29:09 +0200 | [diff] [blame] | 4659 | if (!(sv->check.state & CHK_ST_CONFIGURED)) |
| 4660 | return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n"); |
| 4661 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4662 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4663 | sv->check.state |= CHK_ST_ENABLED; |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4664 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | 2c04eda | 2016-11-24 12:51:04 +0100 | [diff] [blame] | 4665 | return 1; |
| 4666 | } |
| 4667 | |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4668 | /* parse a "enable server" command. It always returns 1. |
| 4669 | * |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4670 | * Grabs the server lock. |
Willy Tarreau | 46b7f53 | 2018-08-21 11:54:26 +0200 | [diff] [blame] | 4671 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 4672 | 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] | 4673 | { |
| 4674 | struct server *sv; |
| 4675 | |
| 4676 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4677 | return 1; |
| 4678 | |
| 4679 | sv = cli_find_server(appctx, args[2]); |
| 4680 | if (!sv) |
| 4681 | return 1; |
| 4682 | |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4683 | HA_SPIN_LOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4684 | srv_adm_set_ready(sv); |
Olivier Houchard | e9bad0a | 2018-01-17 17:39:34 +0100 | [diff] [blame] | 4685 | if (!(sv->flags & SRV_F_COOKIESET) |
| 4686 | && (sv->proxy->ck_opts & PR_CK_DYNAMIC) && |
| 4687 | sv->cookie) |
| 4688 | srv_check_for_dup_dyncookie(sv); |
Willy Tarreau | 3bcc269 | 2018-08-21 15:35:31 +0200 | [diff] [blame] | 4689 | HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock); |
Willy Tarreau | ffb4d58 | 2016-11-24 12:47:00 +0100 | [diff] [blame] | 4690 | return 1; |
| 4691 | } |
| 4692 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4693 | /* Allocates data structure related to load balancing for the server <sv>. It |
| 4694 | * is only required for dynamic servers. |
| 4695 | * |
| 4696 | * At the moment, the server lock is not used as this function is only called |
| 4697 | * for a dynamic server not yet registered. |
| 4698 | * |
| 4699 | * Returns 1 on success, 0 on allocation failure. |
| 4700 | */ |
| 4701 | static int srv_alloc_lb(struct server *sv, struct proxy *be) |
| 4702 | { |
| 4703 | int node; |
| 4704 | |
| 4705 | sv->lb_tree = (sv->flags & SRV_F_BACKUP) ? |
| 4706 | &be->lbprm.chash.bck : &be->lbprm.chash.act; |
| 4707 | sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE; |
| 4708 | sv->lb_nodes_now = 0; |
| 4709 | |
Willy Tarreau | dcb121f | 2021-04-20 11:37:45 +0200 | [diff] [blame] | 4710 | if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) || |
| 4711 | ((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] | 4712 | sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes)); |
| 4713 | |
| 4714 | if (!sv->lb_nodes) |
| 4715 | return 0; |
| 4716 | |
| 4717 | for (node = 0; node < sv->lb_nodes_tot; node++) { |
| 4718 | sv->lb_nodes[node].server = sv; |
| 4719 | sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node); |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | return 1; |
| 4724 | } |
| 4725 | |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4726 | /* updates the server's weight during a warmup stage. Once the final weight is |
| 4727 | * reached, the task automatically stops. Note that any server status change |
| 4728 | * must have updated s->last_change accordingly. |
| 4729 | */ |
| 4730 | static struct task *server_warmup(struct task *t, void *context, unsigned int state) |
| 4731 | { |
| 4732 | struct server *s = context; |
| 4733 | |
| 4734 | /* by default, plan on stopping the task */ |
| 4735 | t->expire = TICK_ETERNITY; |
| 4736 | if ((s->next_admin & SRV_ADMF_MAINT) || |
| 4737 | (s->next_state != SRV_ST_STARTING)) |
| 4738 | return t; |
| 4739 | |
| 4740 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 4741 | |
| 4742 | /* recalculate the weights and update the state */ |
| 4743 | server_recalc_eweight(s, 1); |
| 4744 | |
| 4745 | /* probably that we can refill this server with a bit more connections */ |
| 4746 | pendconn_grab_from_px(s); |
| 4747 | |
| 4748 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 4749 | |
| 4750 | /* get back there in 1 second or 1/20th of the slowstart interval, |
| 4751 | * whichever is greater, resulting in small 5% steps. |
| 4752 | */ |
| 4753 | if (s->next_state == SRV_ST_STARTING) |
| 4754 | t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))); |
| 4755 | return t; |
| 4756 | } |
| 4757 | |
| 4758 | /* Allocate the slowstart task if the server is configured with a slowstart |
| 4759 | * timer. If server next_state is SRV_ST_STARTING, the task is scheduled. |
| 4760 | * |
| 4761 | * Returns 0 on success else non-zero. |
| 4762 | */ |
| 4763 | static int init_srv_slowstart(struct server *srv) |
| 4764 | { |
| 4765 | struct task *t; |
| 4766 | |
| 4767 | if (srv->slowstart) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4768 | if ((t = task_new_anywhere()) == NULL) { |
Amaury Denoyelle | 29d1ac1 | 2021-09-21 11:51:29 +0200 | [diff] [blame] | 4769 | ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id); |
| 4770 | return ERR_ALERT | ERR_FATAL; |
| 4771 | } |
| 4772 | |
| 4773 | /* We need a warmup task that will be called when the server |
| 4774 | * state switches from down to up. |
| 4775 | */ |
| 4776 | srv->warmup = t; |
| 4777 | t->process = server_warmup; |
| 4778 | t->context = srv; |
| 4779 | |
| 4780 | /* server can be in this state only because of */ |
| 4781 | if (srv->next_state == SRV_ST_STARTING) { |
| 4782 | task_schedule(srv->warmup, |
| 4783 | tick_add(now_ms, |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 4784 | 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] | 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | return ERR_NONE; |
| 4789 | } |
| 4790 | REGISTER_POST_SERVER_CHECK(init_srv_slowstart); |
| 4791 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4792 | /* Memory allocation and initialization of the per_thr field. |
| 4793 | * Returns 0 if the field has been successfully initialized, -1 on failure. |
| 4794 | */ |
| 4795 | int srv_init_per_thr(struct server *srv) |
| 4796 | { |
| 4797 | int i; |
| 4798 | |
| 4799 | srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr)); |
Willy Tarreau | c21a187 | 2022-11-21 14:14:06 +0100 | [diff] [blame] | 4800 | srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp)); |
| 4801 | if (!srv->per_thr || !srv->per_tgrp) |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4802 | return -1; |
| 4803 | |
| 4804 | for (i = 0; i < global.nbthread; i++) { |
| 4805 | srv->per_thr[i].idle_conns = EB_ROOT; |
| 4806 | srv->per_thr[i].safe_conns = EB_ROOT; |
| 4807 | srv->per_thr[i].avail_conns = EB_ROOT; |
| 4808 | MT_LIST_INIT(&srv->per_thr[i].streams); |
| 4809 | } |
| 4810 | |
| 4811 | return 0; |
| 4812 | } |
| 4813 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4814 | /* Parse a "add server" command |
| 4815 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 4816 | */ |
| 4817 | static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 4818 | { |
| 4819 | struct proxy *be; |
| 4820 | struct server *srv; |
| 4821 | char *be_name, *sv_name; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4822 | int errcode, argc; |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4823 | int next_id; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4824 | const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR; |
| 4825 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4826 | usermsgs_clr("CLI"); |
| 4827 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4828 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 4829 | return 1; |
| 4830 | |
| 4831 | ++args; |
| 4832 | |
| 4833 | sv_name = be_name = args[1]; |
| 4834 | /* split backend/server arg */ |
| 4835 | while (*sv_name && *(++sv_name)) { |
| 4836 | if (*sv_name == '/') { |
| 4837 | *sv_name = '\0'; |
| 4838 | ++sv_name; |
| 4839 | break; |
| 4840 | } |
| 4841 | } |
| 4842 | |
| 4843 | if (!*sv_name) |
| 4844 | return cli_err(appctx, "Require 'backend/server'."); |
| 4845 | |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4846 | be = proxy_be_by_name(be_name); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4847 | if (!be) |
| 4848 | return cli_err(appctx, "No such backend."); |
| 4849 | |
| 4850 | if (!(be->lbprm.algo & BE_LB_PROP_DYN)) { |
Amaury Denoyelle | eafd701 | 2021-04-29 14:59:42 +0200 | [diff] [blame] | 4851 | 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] | 4852 | return 1; |
| 4853 | } |
| 4854 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4855 | /* At this point, some operations might not be thread-safe anymore. This |
| 4856 | * might be the case for parsing handlers which were designed to run |
| 4857 | * only at the starting stage on single-thread mode. |
| 4858 | * |
| 4859 | * Activate thread isolation to ensure thread-safety. |
| 4860 | */ |
| 4861 | thread_isolate(); |
| 4862 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4863 | args[1] = sv_name; |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4864 | errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags); |
| 4865 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4866 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4867 | |
| 4868 | while (*args[argc]) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4869 | errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4870 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4871 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4872 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4875 | errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags); |
| 4876 | if (errcode) |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4877 | goto out; |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4878 | |
Amaury Denoyelle | efbf35c | 2021-06-10 17:34:10 +0200 | [diff] [blame] | 4879 | /* A dynamic server does not currently support resolution. |
| 4880 | * |
| 4881 | * Initialize it explicitly to the "none" method to ensure no |
| 4882 | * resolution will ever be executed. |
| 4883 | */ |
| 4884 | srv->init_addr_methods = SRV_IADDR_NONE; |
| 4885 | |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4886 | if (srv->mux_proto) { |
Aurelien DARRAGON | d0086f7 | 2023-10-19 16:15:50 +0200 | [diff] [blame^] | 4887 | int proto_mode = conn_pr_mode_to_proto_mode(be->mode); |
| 4888 | const struct mux_proto_list *mux_ent; |
| 4889 | |
| 4890 | mux_ent = conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, proto_mode); |
| 4891 | |
| 4892 | if (!mux_ent || !isteq(mux_ent->token, srv->mux_proto->token)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4893 | ha_alert("MUX protocol is not usable for server.\n"); |
Amaury Denoyelle | 3046723 | 2021-03-12 18:03:27 +0100 | [diff] [blame] | 4894 | goto out; |
| 4895 | } |
| 4896 | } |
| 4897 | |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 4898 | if (srv_init_per_thr(srv) == -1) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4899 | ha_alert("failed to allocate per-thread lists for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4900 | goto out; |
| 4901 | } |
| 4902 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4903 | if (srv->max_idle_conns != 0) { |
| 4904 | srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr)); |
| 4905 | if (!srv->curr_idle_thr) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4906 | ha_alert("failed to allocate counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4907 | goto out; |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | if (!srv_alloc_lb(srv, be)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4912 | ha_alert("Failed to initialize load-balancing data.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4913 | goto out; |
| 4914 | } |
| 4915 | |
| 4916 | if (!stats_allocate_proxy_counters_internal(&srv->extra_counters, |
| 4917 | COUNTERS_SV, |
| 4918 | STATS_PX_CAP_SRV)) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4919 | ha_alert("failed to allocate extra counters for server.\n"); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4920 | goto out; |
| 4921 | } |
| 4922 | |
Aurelien DARRAGON | 86207e7 | 2023-02-08 11:55:08 +0100 | [diff] [blame] | 4923 | /* ensure minconn/maxconn consistency */ |
| 4924 | srv_minmax_conn_apply(srv); |
| 4925 | |
Amaury Denoyelle | 79b90e8 | 2021-09-20 15:15:19 +0200 | [diff] [blame] | 4926 | if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) || |
| 4927 | srv->check.use_ssl == 1) { |
Amaury Denoyelle | 34897d2 | 2021-05-19 09:49:41 +0200 | [diff] [blame] | 4928 | if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 4929 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) |
| 4930 | goto out; |
| 4931 | } |
| 4932 | } |
| 4933 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 4934 | if (srv->trackit) { |
| 4935 | if (srv_apply_track(srv, be)) |
| 4936 | goto out; |
| 4937 | } |
| 4938 | |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4939 | /* Init check/agent if configured. The check is manually disabled |
| 4940 | * because a dynamic server is started in a disable state. It must be |
| 4941 | * manually activated via a "enable health/agent" command. |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4942 | */ |
| 4943 | if (srv->do_check) { |
| 4944 | if (init_srv_check(srv)) |
| 4945 | goto out; |
| 4946 | |
| 4947 | srv->check.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4948 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 4949 | |
| 4950 | if (srv->do_agent) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4951 | if (init_srv_agent_check(srv)) |
| 4952 | goto out; |
| 4953 | |
| 4954 | srv->agent.state &= ~CHK_ST_ENABLED; |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 4955 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 4956 | |
Amaury Denoyelle | cd8a6f2 | 2021-09-21 11:51:54 +0200 | [diff] [blame] | 4957 | /* Init slowstart if needed. */ |
| 4958 | if (init_srv_slowstart(srv)) |
| 4959 | goto out; |
| 4960 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4961 | /* Attach the server to the end of the proxy linked list. Note that this |
| 4962 | * operation is not thread-safe so this is executed under thread |
| 4963 | * isolation. |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4964 | * |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 4965 | * 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] | 4966 | */ |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4967 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4968 | /* TODO use a double-linked list for px->srv */ |
| 4969 | if (be->srv) { |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4970 | struct server *next = be->srv; |
| 4971 | |
| 4972 | while (1) { |
| 4973 | /* check for duplicate server */ |
Tim Duesterhus | c5aa113 | 2021-10-16 17:48:15 +0200 | [diff] [blame] | 4974 | if (strcmp(srv->id, next->id) == 0) { |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 4975 | 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] | 4976 | goto out; |
| 4977 | } |
| 4978 | |
| 4979 | if (!next->next) |
| 4980 | break; |
| 4981 | |
| 4982 | next = next->next; |
| 4983 | } |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 4984 | |
| 4985 | next->next = srv; |
| 4986 | } |
| 4987 | else { |
| 4988 | srv->next = be->srv; |
| 4989 | be->srv = srv; |
| 4990 | } |
Amaury Denoyelle | cece918 | 2021-04-20 17:09:08 +0200 | [diff] [blame] | 4991 | |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 4992 | /* generate the server id if not manually specified */ |
| 4993 | if (!srv->puid) { |
| 4994 | next_id = get_next_id(&be->conf.used_server_id, 1); |
| 4995 | if (!next_id) { |
| 4996 | ha_alert("Cannot attach server : no id left in proxy\n"); |
| 4997 | goto out; |
| 4998 | } |
| 4999 | |
| 5000 | srv->conf.id.key = srv->puid = next_id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5001 | } |
Christopher Faulet | 70f8948 | 2021-12-07 18:49:44 +0100 | [diff] [blame] | 5002 | srv->conf.name.key = srv->id; |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5003 | |
| 5004 | /* insert the server in the backend trees */ |
Amaury Denoyelle | 1613b4a | 2021-06-08 17:00:20 +0200 | [diff] [blame] | 5005 | eb32_insert(&be->conf.used_server_id, &srv->conf.id); |
| 5006 | ebis_insert(&be->conf.used_server_name, &srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5007 | /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */ |
| 5008 | if (srv->addr_node.key) |
| 5009 | ebis_insert(&be->used_server_addr, &srv->addr_node); |
Amaury Denoyelle | 406aaef | 2021-06-09 09:58:47 +0200 | [diff] [blame] | 5010 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5011 | /* check if LSB bit (odd bit) is set for reuse_cnt */ |
| 5012 | if (srv_id_reuse_cnt & 1) { |
| 5013 | /* cnt must be increased */ |
| 5014 | srv_id_reuse_cnt++; |
| 5015 | } |
| 5016 | /* srv_id_reuse_cnt is always even at this stage, divide by 2 to |
| 5017 | * save some space |
| 5018 | * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt)) |
| 5019 | */ |
| 5020 | srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0; |
| 5021 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 5022 | /* adding server cannot fail when we reach this: |
| 5023 | * publishing EVENT_HDL_SUB_SERVER_ADD |
| 5024 | */ |
| 5025 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1); |
| 5026 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5027 | thread_release(); |
| 5028 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5029 | /* Start the check task. The server must be fully initialized. |
| 5030 | * |
| 5031 | * <srvpos> and <nbcheck> parameters are set to 1 as there should be no |
| 5032 | * need to randomly spread the task interval for dynamic servers. |
| 5033 | */ |
| 5034 | if (srv->check.state & CHK_ST_CONFIGURED) { |
| 5035 | if (!start_check_task(&srv->check, 0, 1, 1)) |
| 5036 | ha_alert("System might be unstable, consider to execute a reload"); |
| 5037 | } |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5038 | if (srv->agent.state & CHK_ST_CONFIGURED) { |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5039 | if (!start_check_task(&srv->agent, 0, 1, 1)) |
| 5040 | ha_alert("System might be unstable, consider to execute a reload"); |
| 5041 | } |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5042 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5043 | ha_notice("New server registered.\n"); |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 5044 | cli_umsg(appctx, LOG_INFO); |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5045 | |
| 5046 | return 0; |
| 5047 | |
| 5048 | out: |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 5049 | if (srv) { |
| 5050 | if (srv->track) |
| 5051 | release_server_track(srv); |
| 5052 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5053 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5054 | free_check(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5055 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5056 | free_check(&srv->agent); |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5057 | |
Amaury Denoyelle | bd8dd84 | 2021-08-04 11:20:05 +0200 | [diff] [blame] | 5058 | /* remove the server from the proxy linked list */ |
| 5059 | if (be->srv == srv) { |
| 5060 | be->srv = srv->next; |
| 5061 | } |
| 5062 | else { |
| 5063 | struct server *prev; |
| 5064 | for (prev = be->srv; prev && prev->next != srv; prev = prev->next) |
| 5065 | ; |
| 5066 | if (prev) |
| 5067 | prev->next = srv->next; |
| 5068 | } |
| 5069 | |
| 5070 | } |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 5071 | |
Amaury Denoyelle | 31ddd76 | 2021-06-10 15:26:44 +0200 | [diff] [blame] | 5072 | thread_release(); |
| 5073 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5074 | if (!usermsgs_empty()) |
Amaury Denoyelle | 30fc6da | 2022-11-10 15:16:49 +0100 | [diff] [blame] | 5075 | cli_umsgerr(appctx); |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5076 | |
Amaury Denoyelle | 08be72b | 2021-07-28 10:06:52 +0200 | [diff] [blame] | 5077 | if (srv) |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5078 | srv_drop(srv); |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5079 | |
Amaury Denoyelle | f99f77a | 2021-03-08 17:13:32 +0100 | [diff] [blame] | 5080 | return 1; |
| 5081 | } |
| 5082 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5083 | /* Parse a "del server" command |
| 5084 | * Returns 0 if the server has been successfully initialized, 1 on failure. |
| 5085 | */ |
| 5086 | static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private) |
| 5087 | { |
| 5088 | struct proxy *be; |
| 5089 | struct server *srv; |
| 5090 | char *be_name, *sv_name; |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5091 | struct server *prev_del; |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5092 | |
| 5093 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 5094 | return 1; |
| 5095 | |
| 5096 | ++args; |
| 5097 | |
| 5098 | sv_name = be_name = args[1]; |
| 5099 | /* split backend/server arg */ |
| 5100 | while (*sv_name && *(++sv_name)) { |
| 5101 | if (*sv_name == '/') { |
| 5102 | *sv_name = '\0'; |
| 5103 | ++sv_name; |
| 5104 | break; |
| 5105 | } |
| 5106 | } |
| 5107 | |
| 5108 | if (!*sv_name) |
| 5109 | return cli_err(appctx, "Require 'backend/server'."); |
| 5110 | |
| 5111 | /* 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] | 5112 | * requires thread isolation. In addition, any place referencing the |
| 5113 | * server about to be deleted would be unsafe after our operation, so |
| 5114 | * we must be certain to be alone so that no other thread has even |
| 5115 | * started to grab a temporary reference to this server. |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5116 | */ |
Willy Tarreau | ba3ab79 | 2021-08-04 14:42:37 +0200 | [diff] [blame] | 5117 | thread_isolate_full(); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5118 | |
| 5119 | get_backend_server(be_name, sv_name, &be, &srv); |
| 5120 | if (!be) { |
| 5121 | cli_err(appctx, "No such backend."); |
| 5122 | goto out; |
| 5123 | } |
| 5124 | |
| 5125 | if (!srv) { |
| 5126 | cli_err(appctx, "No such server."); |
| 5127 | goto out; |
| 5128 | } |
| 5129 | |
Amaury Denoyelle | 14c3c5c | 2021-08-23 14:10:51 +0200 | [diff] [blame] | 5130 | if (srv->flags & SRV_F_NON_PURGEABLE) { |
| 5131 | 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] | 5132 | goto out; |
| 5133 | } |
| 5134 | |
| 5135 | /* Only servers in maintenance can be deleted. This ensures that the |
| 5136 | * server is not present anymore in the lb structures (through |
| 5137 | * lbprm.set_server_status_down). |
| 5138 | */ |
| 5139 | if (!(srv->cur_admin & SRV_ADMF_MAINT)) { |
| 5140 | cli_err(appctx, "Only servers in maintenance mode can be deleted."); |
| 5141 | goto out; |
| 5142 | } |
| 5143 | |
| 5144 | /* Ensure that there is no active/idle/pending connection on the server. |
| 5145 | * |
| 5146 | * TODO idle connections should not prevent server deletion. A proper |
| 5147 | * cleanup function should be implemented to be used here. |
| 5148 | */ |
| 5149 | if (srv->cur_sess || srv->curr_idle_conns || |
Aurelien DARRAGON | 4bc0909 | 2023-09-21 14:18:50 +0200 | [diff] [blame] | 5150 | !eb_is_empty(&srv->queue.head) || srv_has_streams(srv)) { |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5151 | cli_err(appctx, "Server still has connections attached to it, cannot remove it."); |
| 5152 | goto out; |
| 5153 | } |
| 5154 | |
Aurelien DARRAGON | 129ecf4 | 2022-11-17 10:37:58 +0100 | [diff] [blame] | 5155 | /* removing cannot fail anymore when we reach this: |
| 5156 | * publishing EVENT_HDL_SUB_SERVER_DEL |
| 5157 | */ |
| 5158 | srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1); |
| 5159 | |
Amaury Denoyelle | 56eb8ed | 2021-07-13 10:36:03 +0200 | [diff] [blame] | 5160 | /* remove srv from tracking list */ |
| 5161 | if (srv->track) |
| 5162 | release_server_track(srv); |
| 5163 | |
Amaury Denoyelle | 2fc4d39 | 2021-07-22 16:04:59 +0200 | [diff] [blame] | 5164 | /* stop the check task if running */ |
| 5165 | if (srv->check.state & CHK_ST_CONFIGURED) |
| 5166 | check_purge(&srv->check); |
Amaury Denoyelle | 3eb42f9 | 2021-08-10 16:24:36 +0200 | [diff] [blame] | 5167 | if (srv->agent.state & CHK_ST_CONFIGURED) |
Amaury Denoyelle | b65f4ca | 2021-08-04 11:33:14 +0200 | [diff] [blame] | 5168 | check_purge(&srv->agent); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5169 | |
| 5170 | /* detach the server from the proxy linked list |
| 5171 | * The proxy servers list is currently not protected by a lock, so this |
| 5172 | * requires thread_isolate/release. |
| 5173 | */ |
| 5174 | |
| 5175 | /* be->srv cannot be empty since we have already found the server with |
| 5176 | * get_backend_server */ |
| 5177 | BUG_ON(!be->srv); |
| 5178 | if (be->srv == srv) { |
| 5179 | be->srv = srv->next; |
| 5180 | } |
| 5181 | else { |
| 5182 | struct server *next; |
Amaury Denoyelle | d6b4b6d | 2021-04-21 11:50:26 +0200 | [diff] [blame] | 5183 | for (next = be->srv; srv != next->next; next = next->next) { |
| 5184 | /* srv cannot be not found since we have already found |
| 5185 | * it with get_backend_server */ |
| 5186 | BUG_ON(!next); |
| 5187 | } |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5188 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5189 | next->next = srv->next; |
| 5190 | } |
| 5191 | |
Aurelien DARRAGON | f175b08 | 2023-02-01 17:22:32 +0100 | [diff] [blame] | 5192 | /* Some deleted servers could still point to us using their 'next', |
| 5193 | * update them as needed |
| 5194 | * Please note the small race between the POP and APPEND, although in |
| 5195 | * this situation this is not an issue as we are under full thread |
| 5196 | * isolation |
| 5197 | */ |
| 5198 | while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) { |
| 5199 | /* update its 'next' ptr */ |
| 5200 | prev_del->next = srv->next; |
| 5201 | if (srv->next) { |
| 5202 | /* now it is our 'next' responsibility */ |
| 5203 | MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted); |
| 5204 | } |
| 5205 | } |
| 5206 | |
| 5207 | /* we ourselves need to inform our 'next' that we will still point it */ |
| 5208 | if (srv->next) |
| 5209 | MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted); |
| 5210 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5211 | /* remove srv from addr_node tree */ |
Amaury Denoyelle | 82d7f77 | 2021-06-09 16:00:43 +0200 | [diff] [blame] | 5212 | eb32_delete(&srv->conf.id); |
| 5213 | ebpt_delete(&srv->conf.name); |
Aurelien DARRAGON | 8d0ff28 | 2022-09-16 14:27:53 +0200 | [diff] [blame] | 5214 | if (srv->addr_node.key) |
| 5215 | ebpt_delete(&srv->addr_node); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5216 | |
| 5217 | /* remove srv from idle_node tree for idle conn cleanup */ |
| 5218 | eb32_delete(&srv->idle_node); |
| 5219 | |
Aurelien DARRAGON | 75b9d1c | 2023-01-24 14:40:01 +0100 | [diff] [blame] | 5220 | /* flag the server as deleted |
| 5221 | * (despite the server being removed from primary server list, |
| 5222 | * one could still access the server data from a valid ptr) |
| 5223 | * Deleted flag helps detecting when a server is in transient removal |
| 5224 | * state. |
| 5225 | * ie: removed from the list but not yet freed/purged from memory. |
| 5226 | */ |
| 5227 | srv->flags |= SRV_F_DELETED; |
| 5228 | |
Aurelien DARRAGON | 61e3894 | 2022-11-17 16:10:35 +0100 | [diff] [blame] | 5229 | /* set LSB bit (odd bit) for reuse_cnt */ |
| 5230 | srv_id_reuse_cnt |= 1; |
| 5231 | |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5232 | thread_release(); |
| 5233 | |
Amaury Denoyelle | 5e560e8 | 2021-05-28 16:35:05 +0200 | [diff] [blame] | 5234 | ha_notice("Server deleted.\n"); |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 5235 | srv_drop(srv); |
Amaury Denoyelle | e558043 | 2021-04-15 14:41:20 +0200 | [diff] [blame] | 5236 | |
| 5237 | cli_msg(appctx, LOG_INFO, "Server deleted."); |
| 5238 | |
| 5239 | return 0; |
| 5240 | |
| 5241 | out: |
| 5242 | thread_release(); |
| 5243 | |
| 5244 | return 1; |
| 5245 | } |
| 5246 | |
William Lallemand | 222baf2 | 2016-11-19 02:00:33 +0100 | [diff] [blame] | 5247 | /* register cli keywords */ |
| 5248 | static struct cli_kw_list cli_kws = {{ },{ |
Amaury Denoyelle | c755efd | 2021-08-03 18:05:37 +0200 | [diff] [blame] | 5249 | { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL }, |
| 5250 | { { "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] | 5251 | { { "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] | 5252 | { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL }, |
| 5253 | { { "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] | 5254 | { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL }, |
| 5255 | { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL }, |
| 5256 | { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server }, |
| 5257 | { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight }, |
| 5258 | { { "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] | 5259 | { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL }, |
| 5260 | { { "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] | 5261 | {{},} |
| 5262 | }}; |
| 5263 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5264 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
Baptiste Assmann | 83cbaa5 | 2016-11-02 15:34:05 +0100 | [diff] [blame] | 5265 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5266 | /* Prepare a server <srv> to track check status of another one. <srv>.<trackit> |
| 5267 | * field is used to retrieve the identifier of the tracked server, either with |
| 5268 | * the format "proxy/server" or just "server". <curproxy> must point to the |
| 5269 | * backend owning <srv>; if no proxy is specified in <trackit>, it will be used |
| 5270 | * to find the tracked server. |
| 5271 | * |
| 5272 | * Returns 0 if the server track has been activated else non-zero. |
| 5273 | * |
| 5274 | * Not thread-safe. |
| 5275 | */ |
| 5276 | int srv_apply_track(struct server *srv, struct proxy *curproxy) |
| 5277 | { |
| 5278 | struct proxy *px; |
| 5279 | struct server *strack, *loop; |
| 5280 | char *pname, *sname; |
| 5281 | |
| 5282 | if (!srv->trackit) |
| 5283 | return 1; |
| 5284 | |
| 5285 | pname = srv->trackit; |
| 5286 | sname = strrchr(pname, '/'); |
| 5287 | |
| 5288 | if (sname) { |
| 5289 | *sname++ = '\0'; |
| 5290 | } |
| 5291 | else { |
| 5292 | sname = pname; |
| 5293 | pname = NULL; |
| 5294 | } |
| 5295 | |
| 5296 | if (pname) { |
| 5297 | px = proxy_be_by_name(pname); |
| 5298 | if (!px) { |
| 5299 | ha_alert("unable to find required proxy '%s' for tracking.\n", |
| 5300 | pname); |
| 5301 | return 1; |
| 5302 | } |
| 5303 | } |
| 5304 | else { |
| 5305 | px = curproxy; |
| 5306 | } |
| 5307 | |
| 5308 | strack = findserver(px, sname); |
| 5309 | if (!strack) { |
| 5310 | ha_alert("unable to find required server '%s' for tracking.\n", |
| 5311 | sname); |
| 5312 | return 1; |
| 5313 | } |
| 5314 | |
Amaury Denoyelle | 79f68be | 2021-07-13 10:35:50 +0200 | [diff] [blame] | 5315 | if (strack->flags & SRV_F_DYNAMIC) { |
| 5316 | ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n", |
| 5317 | px->id, strack->id); |
| 5318 | return 1; |
| 5319 | } |
| 5320 | |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5321 | if (!strack->do_check && !strack->do_agent && !strack->track && |
| 5322 | !strack->trackit) { |
| 5323 | ha_alert("unable to use %s/%s for " |
| 5324 | "tracking as it does not have any check nor agent enabled.\n", |
| 5325 | px->id, strack->id); |
| 5326 | return 1; |
| 5327 | } |
| 5328 | |
| 5329 | for (loop = strack->track; loop && loop != srv; loop = loop->track) |
| 5330 | ; |
| 5331 | |
| 5332 | if (srv == strack || loop) { |
| 5333 | ha_alert("unable to track %s/%s as it " |
| 5334 | "belongs to a tracking chain looping back to %s/%s.\n", |
| 5335 | px->id, strack->id, px->id, |
| 5336 | srv == strack ? strack->id : loop->id); |
| 5337 | return 1; |
| 5338 | } |
| 5339 | |
| 5340 | if (curproxy != px && |
| 5341 | (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) { |
| 5342 | ha_alert("unable to use %s/%s for" |
| 5343 | "tracking: disable-on-404 option inconsistency.\n", |
| 5344 | px->id, strack->id); |
| 5345 | return 1; |
| 5346 | } |
| 5347 | |
| 5348 | srv->track = strack; |
| 5349 | srv->tracknext = strack->trackers; |
| 5350 | strack->trackers = srv; |
Amaury Denoyelle | 0626961 | 2021-08-23 14:05:07 +0200 | [diff] [blame] | 5351 | strack->flags |= SRV_F_NON_PURGEABLE; |
Amaury Denoyelle | 669b620 | 2021-07-13 10:35:23 +0200 | [diff] [blame] | 5352 | |
| 5353 | ha_free(&srv->trackit); |
| 5354 | |
| 5355 | return 0; |
| 5356 | } |
| 5357 | |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5358 | /* This function propagates srv state change to lb algorithms */ |
| 5359 | static void srv_lb_propagate(struct server *s) |
| 5360 | { |
| 5361 | struct proxy *px = s->proxy; |
| 5362 | |
| 5363 | if (px->lbprm.update_server_eweight) |
| 5364 | px->lbprm.update_server_eweight(s); |
| 5365 | else if (srv_willbe_usable(s)) { |
| 5366 | if (px->lbprm.set_server_status_up) |
| 5367 | px->lbprm.set_server_status_up(s); |
| 5368 | } |
| 5369 | else { |
| 5370 | if (px->lbprm.set_server_status_down) |
| 5371 | px->lbprm.set_server_status_down(s); |
| 5372 | } |
| 5373 | } |
| 5374 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5375 | /* directly update server state based on an operational change |
| 5376 | * (compare current and next state to know which transition to apply) |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5377 | * |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5378 | * The function returns the number of requeued sessions (either taken by |
| 5379 | * the server or redispatched to others servers) due to the server state |
| 5380 | * change. |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5381 | */ |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5382 | 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] | 5383 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5384 | struct buffer *tmptrash = NULL; |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5385 | int log_level; |
| 5386 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 5387 | int xferred = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5388 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5389 | if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) { |
| 5390 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5391 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5392 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5393 | srv_shutdown_streams(s, SF_ERR_DOWN); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5394 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5395 | /* we might have streams queued on this server and waiting for |
| 5396 | * a connection. Those which are redispatchable will be queued |
| 5397 | * to another server or to the proxy itself. |
| 5398 | */ |
| 5399 | xferred = pendconn_redistribute(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5400 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5401 | tmptrash = alloc_trash_chunk(); |
| 5402 | if (tmptrash) { |
| 5403 | chunk_printf(tmptrash, |
| 5404 | "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5405 | s->proxy->id, s->id); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5406 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5407 | srv_append_op_chg_cause(tmptrash, s, cause); |
| 5408 | srv_append_more(tmptrash, s, xferred, 0); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5409 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5410 | ha_warning("%s.\n", tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5411 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5412 | /* we don't send an alert if the server was previously paused */ |
| 5413 | log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT; |
| 5414 | send_log(s->proxy, log_level, "%s.\n", |
| 5415 | tmptrash->area); |
| 5416 | send_email_alert(s, log_level, "%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5417 | tmptrash->area); |
Aurelien DARRAGON | 0d2f1ac | 2023-05-15 18:03:35 +0200 | [diff] [blame] | 5418 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5419 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5420 | } |
| 5421 | else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) { |
| 5422 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5423 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5424 | /* we might have streams queued on this server and waiting for |
| 5425 | * a connection. Those which are redispatchable will be queued |
| 5426 | * to another server or to the proxy itself. |
| 5427 | */ |
| 5428 | xferred = pendconn_redistribute(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5429 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5430 | tmptrash = alloc_trash_chunk(); |
| 5431 | if (tmptrash) { |
| 5432 | chunk_printf(tmptrash, |
| 5433 | "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5434 | s->proxy->id, s->id); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5435 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5436 | srv_append_op_chg_cause(tmptrash, s, cause); |
| 5437 | srv_append_more(tmptrash, s, xferred, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5438 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5439 | ha_warning("%s.\n", tmptrash->area); |
| 5440 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5441 | tmptrash->area); |
| 5442 | free_trash_chunk(tmptrash); |
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 | } |
| 5445 | else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING)) |
| 5446 | || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5447 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5448 | if (s->next_state == SRV_ST_STARTING && s->warmup) |
| 5449 | 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] | 5450 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5451 | server_recalc_eweight(s, 0); |
| 5452 | /* now propagate the status change to any LB algorithms */ |
| 5453 | srv_lb_propagate(s); |
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 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5456 | * and it's not a backup server and its effective weight is > 0, |
| 5457 | * then it can accept new connections, so we shut down all streams |
| 5458 | * on all backup servers. |
| 5459 | */ |
| 5460 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5461 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5462 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5463 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5464 | /* check if we can handle some connections queued at the proxy. We |
| 5465 | * will take as many as we can handle. |
| 5466 | */ |
| 5467 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5468 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5469 | tmptrash = alloc_trash_chunk(); |
| 5470 | if (tmptrash) { |
| 5471 | chunk_printf(tmptrash, |
| 5472 | "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5473 | s->proxy->id, s->id); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5474 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5475 | srv_append_op_chg_cause(tmptrash, s, cause); |
| 5476 | srv_append_more(tmptrash, s, xferred, 0); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5477 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5478 | ha_warning("%s.\n", tmptrash->area); |
| 5479 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5480 | tmptrash->area); |
| 5481 | send_email_alert(s, LOG_NOTICE, "%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5482 | tmptrash->area); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5483 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5484 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5485 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5486 | else if (s->cur_eweight != s->next_eweight) { |
| 5487 | /* now propagate the status change to any LB algorithms */ |
| 5488 | srv_lb_propagate(s); |
| 5489 | } |
| 5490 | return xferred; |
| 5491 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5492 | |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5493 | /* deduct and update server state from an administrative change |
| 5494 | * (use current and next admin to deduct the administrative transition that |
| 5495 | * may result in server state update) |
| 5496 | * |
| 5497 | * The function returns the number of requeued sessions (either taken by |
| 5498 | * the server or redispatched to others servers) due to the server state |
| 5499 | * change. |
| 5500 | */ |
| 5501 | static int _srv_update_status_adm(struct server *s, enum srv_adm_st_chg_cause cause) |
| 5502 | { |
| 5503 | struct buffer *tmptrash = NULL; |
| 5504 | int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN); |
| 5505 | int xferred = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5506 | |
| 5507 | /* Maintenance must also disable health checks */ |
| 5508 | if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) { |
| 5509 | if (s->check.state & CHK_ST_ENABLED) { |
| 5510 | s->check.state |= CHK_ST_PAUSED; |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame] | 5511 | s->check.health = 0; |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */ |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5515 | tmptrash = alloc_trash_chunk(); |
| 5516 | if (tmptrash) { |
| 5517 | chunk_printf(tmptrash, |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5518 | "%sServer %s/%s was DOWN and now enters maintenance", |
| 5519 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5520 | srv_append_adm_chg_cause(tmptrash, s, cause); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5521 | srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5522 | |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5523 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5524 | ha_warning("%s.\n", tmptrash->area); |
| 5525 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5526 | tmptrash->area); |
Olivier Houchard | 796a2b3 | 2017-10-24 17:42:47 +0200 | [diff] [blame] | 5527 | } |
| 5528 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5529 | } |
| 5530 | } |
| 5531 | else { /* server was still running */ |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame] | 5532 | s->check.health = 0; /* failure */ |
Emeric Brun | e311480 | 2017-12-21 14:42:26 +0100 | [diff] [blame] | 5533 | |
| 5534 | s->next_state = SRV_ST_STOPPED; |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5535 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5536 | |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5537 | if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS) |
| 5538 | srv_shutdown_streams(s, SF_ERR_DOWN); |
| 5539 | |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5540 | /* force connection cleanup on the given server */ |
| 5541 | srv_cleanup_connections(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5542 | /* we might have streams queued on this server and waiting for |
| 5543 | * a connection. Those which are redispatchable will be queued |
| 5544 | * to another server or to the proxy itself. |
| 5545 | */ |
| 5546 | xferred = pendconn_redistribute(s); |
| 5547 | |
| 5548 | tmptrash = alloc_trash_chunk(); |
| 5549 | if (tmptrash) { |
| 5550 | chunk_printf(tmptrash, |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5551 | "%sServer %s/%s is going DOWN for maintenance", |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5552 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5553 | s->proxy->id, s->id); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5554 | srv_append_adm_chg_cause(tmptrash, s, cause); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5555 | srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5556 | |
| 5557 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5558 | ha_warning("%s.\n", tmptrash->area); |
| 5559 | send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", |
| 5560 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5561 | } |
| 5562 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5563 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5564 | } |
| 5565 | } |
| 5566 | else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) { |
| 5567 | /* OK here we're leaving maintenance, we have many things to check, |
| 5568 | * because the server might possibly be coming back up depending on |
| 5569 | * its state. In practice, leaving maintenance means that we should |
| 5570 | * immediately turn to UP (more or less the slowstart) under the |
| 5571 | * following conditions : |
| 5572 | * - server is neither checked nor tracked |
| 5573 | * - server tracks another server which is not checked |
| 5574 | * - server tracks another server which is already up |
| 5575 | * Which sums up as something simpler : |
| 5576 | * "either the tracking server is up or the server's checks are disabled |
| 5577 | * or up". Otherwise we only re-enable health checks. There's a special |
| 5578 | * case associated to the stopping state which can be inherited. Note |
| 5579 | * that the server might still be in drain mode, which is naturally dealt |
| 5580 | * with by the lower level functions. |
| 5581 | */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5582 | if (s->check.state & CHK_ST_ENABLED) { |
| 5583 | s->check.state &= ~CHK_ST_PAUSED; |
Aurelien DARRAGON | c54b98a | 2023-04-18 12:08:18 +0200 | [diff] [blame] | 5584 | s->check.health = s->check.rise; /* start OK but check immediately */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | if ((!s->track || s->track->next_state != SRV_ST_STOPPED) && |
| 5588 | (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) && |
| 5589 | (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) { |
| 5590 | if (s->track && s->track->next_state == SRV_ST_STOPPING) { |
| 5591 | s->next_state = SRV_ST_STOPPING; |
| 5592 | } |
| 5593 | else { |
| 5594 | s->next_state = SRV_ST_STARTING; |
Willy Tarreau | 1e690bb | 2020-10-22 11:30:59 +0200 | [diff] [blame] | 5595 | if (s->slowstart > 0) { |
| 5596 | if (s->warmup) |
| 5597 | task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)))); |
| 5598 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5599 | else |
| 5600 | s->next_state = SRV_ST_RUNNING; |
| 5601 | } |
| 5602 | |
| 5603 | } |
| 5604 | |
| 5605 | tmptrash = alloc_trash_chunk(); |
| 5606 | if (tmptrash) { |
| 5607 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5608 | chunk_printf(tmptrash, |
| 5609 | "%sServer %s/%s is %s/%s (leaving forced maintenance)", |
| 5610 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5611 | s->proxy->id, s->id, |
| 5612 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5613 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5614 | } |
| 5615 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5616 | chunk_printf(tmptrash, |
| 5617 | "%sServer %s/%s ('%s') is %s/%s (resolves again)", |
| 5618 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5619 | s->proxy->id, s->id, s->hostname, |
| 5620 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5621 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5622 | } |
| 5623 | if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5624 | chunk_printf(tmptrash, |
| 5625 | "%sServer %s/%s is %s/%s (leaving maintenance)", |
| 5626 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5627 | s->proxy->id, s->id, |
| 5628 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP", |
| 5629 | (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY"); |
| 5630 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5631 | ha_warning("%s.\n", tmptrash->area); |
| 5632 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5633 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5634 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5635 | } |
| 5636 | |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5637 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5638 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5639 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5640 | |
Willy Tarreau | 6a78e61 | 2018-08-07 10:14:53 +0200 | [diff] [blame] | 5641 | /* If the server is set with "on-marked-up shutdown-backup-sessions", |
| 5642 | * and it's not a backup server and its effective weight is > 0, |
| 5643 | * then it can accept new connections, so we shut down all streams |
| 5644 | * on all backup servers. |
| 5645 | */ |
| 5646 | if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) && |
| 5647 | !(s->flags & SRV_F_BACKUP) && s->next_eweight) |
| 5648 | srv_shutdown_backup_streams(s->proxy, SF_ERR_UP); |
| 5649 | |
| 5650 | /* check if we can handle some connections queued at the proxy. We |
| 5651 | * will take as many as we can handle. |
| 5652 | */ |
| 5653 | xferred = pendconn_grab_from_px(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5654 | } |
| 5655 | else if (s->next_admin & SRV_ADMF_MAINT) { |
| 5656 | /* remaining in maintenance mode, let's inform precisely about the |
| 5657 | * situation. |
| 5658 | */ |
| 5659 | if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) { |
| 5660 | tmptrash = alloc_trash_chunk(); |
| 5661 | if (tmptrash) { |
| 5662 | chunk_printf(tmptrash, |
| 5663 | "%sServer %s/%s is leaving forced maintenance but remains in maintenance", |
| 5664 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5665 | s->proxy->id, s->id); |
| 5666 | |
| 5667 | if (s->track) /* normally it's mandatory here */ |
| 5668 | chunk_appendf(tmptrash, " via %s/%s", |
| 5669 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5670 | ha_warning("%s.\n", tmptrash->area); |
| 5671 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5672 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5673 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5674 | } |
| 5675 | } |
| 5676 | if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) { |
| 5677 | tmptrash = alloc_trash_chunk(); |
| 5678 | if (tmptrash) { |
| 5679 | chunk_printf(tmptrash, |
| 5680 | "%sServer %s/%s ('%s') resolves again but remains in maintenance", |
| 5681 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5682 | s->proxy->id, s->id, s->hostname); |
| 5683 | |
| 5684 | if (s->track) /* normally it's mandatory here */ |
| 5685 | chunk_appendf(tmptrash, " via %s/%s", |
| 5686 | s->track->proxy->id, s->track->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5687 | ha_warning("%s.\n", tmptrash->area); |
| 5688 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5689 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5690 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5691 | } |
| 5692 | } |
| 5693 | else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) { |
| 5694 | tmptrash = alloc_trash_chunk(); |
| 5695 | if (tmptrash) { |
| 5696 | chunk_printf(tmptrash, |
| 5697 | "%sServer %s/%s remains in forced maintenance", |
| 5698 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5699 | s->proxy->id, s->id); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5700 | ha_warning("%s.\n", tmptrash->area); |
| 5701 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5702 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5703 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5704 | } |
| 5705 | } |
| 5706 | /* don't report anything when leaving drain mode and remaining in maintenance */ |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5707 | } |
| 5708 | |
| 5709 | if (!(s->next_admin & SRV_ADMF_MAINT)) { |
| 5710 | if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) { |
| 5711 | /* drain state is applied only if not yet in maint */ |
| 5712 | |
Aurelien DARRAGON | 85b9137 | 2023-04-18 12:02:48 +0200 | [diff] [blame] | 5713 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5714 | |
| 5715 | /* we might have streams queued on this server and waiting for |
| 5716 | * a connection. Those which are redispatchable will be queued |
| 5717 | * to another server or to the proxy itself. |
| 5718 | */ |
| 5719 | xferred = pendconn_redistribute(s); |
| 5720 | |
| 5721 | tmptrash = alloc_trash_chunk(); |
| 5722 | if (tmptrash) { |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5723 | chunk_printf(tmptrash, "%sServer %s/%s enters drain state", |
| 5724 | s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5725 | srv_append_adm_chg_cause(tmptrash, s, cause); |
Aurelien DARRAGON | f3b48a8 | 2023-04-14 18:07:09 +0200 | [diff] [blame] | 5726 | srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN)); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5727 | |
| 5728 | if (!(global.mode & MODE_STARTING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5729 | ha_warning("%s.\n", tmptrash->area); |
| 5730 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5731 | tmptrash->area); |
| 5732 | send_email_alert(s, LOG_NOTICE, "%s", |
| 5733 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5734 | } |
| 5735 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5736 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5737 | } |
| 5738 | else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) { |
| 5739 | /* OK completely leaving drain mode */ |
Willy Tarreau | 3ff577e | 2018-08-02 11:48:52 +0200 | [diff] [blame] | 5740 | server_recalc_eweight(s, 0); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5741 | |
| 5742 | tmptrash = alloc_trash_chunk(); |
| 5743 | if (tmptrash) { |
Aurelien DARRAGON | 9a1df02 | 2023-03-27 10:17:31 +0200 | [diff] [blame] | 5744 | if (s->cur_admin & SRV_ADMF_FDRAIN) { |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5745 | chunk_printf(tmptrash, |
| 5746 | "%sServer %s/%s is %s (leaving forced drain)", |
| 5747 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5748 | s->proxy->id, s->id, |
| 5749 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5750 | } |
| 5751 | else { |
| 5752 | chunk_printf(tmptrash, |
| 5753 | "%sServer %s/%s is %s (leaving drain)", |
| 5754 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5755 | s->proxy->id, s->id, |
| 5756 | (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP"); |
| 5757 | if (s->track) /* normally it's mandatory here */ |
| 5758 | chunk_appendf(tmptrash, " via %s/%s", |
| 5759 | s->track->proxy->id, s->track->id); |
| 5760 | } |
| 5761 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5762 | ha_warning("%s.\n", tmptrash->area); |
| 5763 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5764 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5765 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | /* now propagate the status change to any LB algorithms */ |
Aurelien DARRAGON | 8bbe643 | 2023-04-17 13:53:56 +0200 | [diff] [blame] | 5769 | srv_lb_propagate(s); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5770 | } |
| 5771 | else if ((s->next_admin & SRV_ADMF_DRAIN)) { |
| 5772 | /* remaining in drain mode after removing one of its flags */ |
| 5773 | |
| 5774 | tmptrash = alloc_trash_chunk(); |
| 5775 | if (tmptrash) { |
| 5776 | if (!(s->next_admin & SRV_ADMF_FDRAIN)) { |
| 5777 | chunk_printf(tmptrash, |
Aurelien DARRAGON | 977688b | 2023-05-03 10:36:54 +0200 | [diff] [blame] | 5778 | "%sServer %s/%s remains in drain mode", |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5779 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5780 | s->proxy->id, s->id); |
| 5781 | |
| 5782 | if (s->track) /* normally it's mandatory here */ |
| 5783 | chunk_appendf(tmptrash, " via %s/%s", |
| 5784 | s->track->proxy->id, s->track->id); |
| 5785 | } |
| 5786 | else { |
| 5787 | chunk_printf(tmptrash, |
| 5788 | "%sServer %s/%s remains in forced drain mode", |
| 5789 | s->flags & SRV_F_BACKUP ? "Backup " : "", |
| 5790 | s->proxy->id, s->id); |
| 5791 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5792 | ha_warning("%s.\n", tmptrash->area); |
| 5793 | send_log(s->proxy, LOG_NOTICE, "%s.\n", |
| 5794 | tmptrash->area); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5795 | free_trash_chunk(tmptrash); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5796 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5797 | } |
| 5798 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5799 | return xferred; |
| 5800 | } |
| 5801 | |
| 5802 | /* |
| 5803 | * This function applies server's status changes. |
| 5804 | * |
| 5805 | * Must be called with the server lock held. This may also be called at init |
| 5806 | * time as the result of parsing the state file, in which case no lock will be |
| 5807 | * held, and the server's warmup task can be null. |
| 5808 | * <type> should be 0 for operational and 1 for administrative |
| 5809 | * <cause> must be srv_op_st_chg_cause enum for operational and |
| 5810 | * srv_adm_st_chg_cause enum for administrative |
| 5811 | */ |
| 5812 | static void srv_update_status(struct server *s, int type, int cause) |
| 5813 | { |
| 5814 | int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act; |
| 5815 | enum srv_state srv_prev_state = s->cur_state; |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5816 | union { |
| 5817 | struct event_hdl_cb_data_server_state state; |
Aurelien DARRAGON | a163d65 | 2023-04-21 18:06:58 +0200 | [diff] [blame] | 5818 | struct event_hdl_cb_data_server_admin admin; |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5819 | struct event_hdl_cb_data_server common; |
| 5820 | } cb_data; |
| 5821 | int requeued; |
| 5822 | |
| 5823 | /* prepare common server event data */ |
| 5824 | _srv_event_hdl_prepare(&cb_data.common, s, 0); |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5825 | |
Aurelien DARRAGON | a163d65 | 2023-04-21 18:06:58 +0200 | [diff] [blame] | 5826 | if (type) { |
| 5827 | cb_data.admin.safe.cause = cause; |
| 5828 | cb_data.admin.safe.old_admin = s->cur_admin; |
| 5829 | cb_data.admin.safe.new_admin = s->next_admin; |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5830 | requeued = _srv_update_status_adm(s, cause); |
Aurelien DARRAGON | a163d65 | 2023-04-21 18:06:58 +0200 | [diff] [blame] | 5831 | cb_data.admin.safe.requeued = requeued; |
| 5832 | /* publish admin change */ |
| 5833 | _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADMIN, cb_data.admin, s); |
| 5834 | } |
Aurelien DARRAGON | f71e064 | 2023-04-19 16:19:58 +0200 | [diff] [blame] | 5835 | else |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5836 | requeued = _srv_update_status_op(s, cause); |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5837 | |
Aurelien DARRAGON | 22151c7 | 2023-04-17 17:45:58 +0200 | [diff] [blame] | 5838 | /* explicitly commit state changes (even if it was already applied implicitly |
| 5839 | * by some lb state change function), so we don't miss anything |
| 5840 | */ |
| 5841 | srv_lb_commit_status(s); |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5842 | |
Aurelien DARRAGON | 9f5853f | 2023-04-18 13:52:27 +0200 | [diff] [blame] | 5843 | /* check if server stats must be updated due the the server state change */ |
| 5844 | if (srv_prev_state != s->cur_state) { |
| 5845 | if (srv_prev_state == SRV_ST_STOPPED) { |
| 5846 | /* server was down and no longer is */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 5847 | if (s->last_change < ns_to_sec(now_ns)) // ignore negative times |
| 5848 | s->down_time += ns_to_sec(now_ns) - s->last_change; |
Aurelien DARRAGON | c249f6d | 2023-04-19 22:34:01 +0200 | [diff] [blame] | 5849 | _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] | 5850 | } |
Aurelien DARRAGON | 5f80f8b | 2023-04-19 18:22:21 +0200 | [diff] [blame] | 5851 | else if (s->cur_state == SRV_ST_STOPPED) { |
| 5852 | /* server was up and is currently down */ |
| 5853 | s->counters.down_trans++; |
Aurelien DARRAGON | c249f6d | 2023-04-19 22:34:01 +0200 | [diff] [blame] | 5854 | _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] | 5855 | } |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 5856 | s->last_change = ns_to_sec(now_ns); |
Aurelien DARRAGON | e3eea29 | 2023-04-04 21:28:07 +0200 | [diff] [blame] | 5857 | |
| 5858 | /* publish the state change */ |
| 5859 | _srv_event_hdl_prepare_state(&cb_data.state, |
| 5860 | s, type, cause, srv_prev_state, requeued); |
| 5861 | _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] | 5862 | } |
| 5863 | |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5864 | /* check if backend stats must be updated due to the server state change */ |
| 5865 | if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) |
| 5866 | set_backend_down(s->proxy); /* backend going down */ |
| 5867 | else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) { |
| 5868 | /* backend was down and is back up again: |
| 5869 | * no helper function, updating last_change and backend downtime stats |
| 5870 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 5871 | if (s->proxy->last_change < ns_to_sec(now_ns)) // ignore negative times |
| 5872 | s->proxy->down_time += ns_to_sec(now_ns) - s->proxy->last_change; |
| 5873 | s->proxy->last_change = ns_to_sec(now_ns); |
Aurelien DARRAGON | e80ddb1 | 2023-04-17 15:30:11 +0200 | [diff] [blame] | 5874 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5875 | } |
Emeric Brun | 64cc49c | 2017-10-03 14:46:45 +0200 | [diff] [blame] | 5876 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 5877 | 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] | 5878 | { |
| 5879 | struct connection *conn; |
| 5880 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5881 | while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns, |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5882 | struct connection *, toremove_list)) != NULL) { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 5883 | conn->mux->destroy(conn->ctx); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | return task; |
| 5887 | } |
| 5888 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5889 | /* Move toremove_nb connections from idle_tree to toremove_list, -1 means |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5890 | * moving them all. |
| 5891 | * Returns the number of connections moved. |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5892 | * |
| 5893 | * Must be called with idle_conns_lock held. |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5894 | */ |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5895 | 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] | 5896 | { |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5897 | struct eb_node *node, *next; |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5898 | struct conn_hash_node *hash_node; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5899 | int i = 0; |
| 5900 | |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5901 | node = eb_first(idle_tree); |
| 5902 | while (node) { |
| 5903 | next = eb_next(node); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5904 | if (toremove_nb != -1 && i >= toremove_nb) |
| 5905 | break; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5906 | |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 5907 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5908 | eb_delete(node); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5909 | MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5910 | i++; |
Amaury Denoyelle | f232cb3 | 2021-01-06 16:14:12 +0100 | [diff] [blame] | 5911 | |
| 5912 | node = next; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 5913 | } |
| 5914 | return i; |
| 5915 | } |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5916 | /* cleanup connections for a given server |
| 5917 | * might be useful when going on forced maintenance or live changing ip/port |
| 5918 | */ |
William Dauchy | 707ad32 | 2020-05-04 13:52:40 +0200 | [diff] [blame] | 5919 | static void srv_cleanup_connections(struct server *srv) |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5920 | { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5921 | int did_remove; |
| 5922 | int i; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5923 | |
Amaury Denoyelle | 10d5c31 | 2021-01-06 14:28:51 +0100 | [diff] [blame] | 5924 | /* nothing to do if pool-max-conn is null */ |
| 5925 | if (!srv->max_idle_conns) |
| 5926 | return; |
| 5927 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5928 | /* check all threads starting with ours */ |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5929 | for (i = tid;;) { |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5930 | did_remove = 0; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5931 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5932 | 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] | 5933 | did_remove = 1; |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 5934 | 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] | 5935 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 5936 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5937 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 5938 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 5939 | |
| 5940 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 5941 | break; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 5942 | } |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | /* removes an idle conn after updating the server idle conns counters */ |
| 5946 | void srv_release_conn(struct server *srv, struct connection *conn) |
| 5947 | { |
| 5948 | if (conn->flags & CO_FL_LIST_MASK) { |
| 5949 | /* The connection is currently in the server's idle list, so tell it |
| 5950 | * there's one less connection available in that list. |
| 5951 | */ |
| 5952 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 5953 | _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb); |
| 5954 | _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]); |
| 5955 | } |
| 5956 | else { |
| 5957 | /* The connection is not private and not in any server's idle |
| 5958 | * list, so decrement the current number of used connections |
| 5959 | */ |
| 5960 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 5961 | } |
| 5962 | |
| 5963 | /* Remove the connection from any tree (safe, idle or available) */ |
| 5964 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5965 | conn_delete_from_tree(&conn->hash_node->node); |
Christopher Faulet | 5e1b0e7 | 2023-02-28 15:39:38 +0100 | [diff] [blame] | 5966 | conn->flags &= ~CO_FL_LIST_MASK; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5967 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 5968 | } |
| 5969 | |
| 5970 | /* retrieve a connection from its <hash> in <tree> |
| 5971 | * returns NULL if no connection found |
| 5972 | */ |
| 5973 | struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash) |
| 5974 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5975 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5976 | struct connection *conn = NULL; |
| 5977 | struct conn_hash_node *hash_node = NULL; |
| 5978 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5979 | node = eb64_lookup(tree, hash); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5980 | if (node) { |
| 5981 | hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 5982 | conn = hash_node->conn; |
| 5983 | } |
| 5984 | |
| 5985 | return conn; |
| 5986 | } |
| 5987 | |
| 5988 | /* retrieve the next connection sharing the same hash as <conn> |
| 5989 | * returns NULL if no connection found |
| 5990 | */ |
| 5991 | struct connection *srv_lookup_conn_next(struct connection *conn) |
| 5992 | { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5993 | struct eb64_node *node = NULL; |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5994 | struct connection *next_conn = NULL; |
| 5995 | struct conn_hash_node *hash_node = NULL; |
| 5996 | |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5997 | node = eb64_next_dup(&conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 5998 | if (node) { |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 5999 | hash_node = eb64_entry(node, struct conn_hash_node, node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6000 | next_conn = hash_node->conn; |
| 6001 | } |
| 6002 | |
| 6003 | return next_conn; |
| 6004 | } |
| 6005 | |
| 6006 | /* This adds an idle connection to the server's list if the connection is |
| 6007 | * reusable, not held by any owner anymore, but still has available streams. |
| 6008 | */ |
| 6009 | int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe) |
| 6010 | { |
| 6011 | /* we try to keep the connection in the server's idle list |
| 6012 | * if we don't have too many FD in use, and if the number of |
| 6013 | * idle+current conns is lower than what was observed before |
| 6014 | * last purge, or if we already don't have idle conns for the |
| 6015 | * current thread and we don't exceed last count by global.nbthread. |
| 6016 | */ |
| 6017 | if (!(conn->flags & CO_FL_PRIVATE) && |
| 6018 | srv && srv->pool_purge_delay > 0 && |
| 6019 | ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) && |
| 6020 | ha_used_fds < global.tune.pool_high_count && |
| 6021 | (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) && |
| 6022 | ((eb_is_empty(&srv->per_thr[tid].safe_conns) && |
| 6023 | (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) || |
| 6024 | (ha_used_fds < global.tune.pool_low_count && |
| 6025 | (srv->curr_used_conns + srv->curr_idle_conns <= |
| 6026 | MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) && |
| 6027 | !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) { |
| 6028 | int retadd; |
| 6029 | |
| 6030 | retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1); |
| 6031 | if (retadd > srv->max_idle_conns) { |
| 6032 | _HA_ATOMIC_DEC(&srv->curr_idle_conns); |
| 6033 | return 0; |
| 6034 | } |
| 6035 | _HA_ATOMIC_DEC(&srv->curr_used_conns); |
| 6036 | |
| 6037 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6038 | conn_delete_from_tree(&conn->hash_node->node); |
| 6039 | |
| 6040 | if (is_safe) { |
| 6041 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6042 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6043 | _HA_ATOMIC_INC(&srv->curr_safe_nb); |
| 6044 | } else { |
| 6045 | conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST; |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6046 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Willy Tarreau | 260f324 | 2021-10-06 18:30:04 +0200 | [diff] [blame] | 6047 | _HA_ATOMIC_INC(&srv->curr_idle_nb); |
| 6048 | } |
| 6049 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6050 | _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]); |
| 6051 | |
| 6052 | __ha_barrier_full(); |
| 6053 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 6054 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6055 | if ((volatile void *)srv->idle_node.node.leaf_p == NULL) { |
| 6056 | srv->idle_node.key = tick_add(srv->pool_purge_delay, |
| 6057 | now_ms); |
| 6058 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
| 6059 | if (!task_in_wq(idle_conn_task) && ! |
| 6060 | task_in_rq(idle_conn_task)) { |
| 6061 | task_schedule(idle_conn_task, |
| 6062 | srv->idle_node.key); |
| 6063 | } |
| 6064 | |
| 6065 | } |
| 6066 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6067 | } |
| 6068 | return 1; |
| 6069 | } |
| 6070 | return 0; |
William Dauchy | 6318d33 | 2020-05-02 21:52:36 +0200 | [diff] [blame] | 6071 | } |
| 6072 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6073 | 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] | 6074 | { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6075 | struct server *srv; |
| 6076 | struct eb32_node *eb; |
| 6077 | int i; |
| 6078 | unsigned int next_wakeup; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 6079 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6080 | next_wakeup = TICK_ETERNITY; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6081 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6082 | while (1) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6083 | int exceed_conns; |
| 6084 | int to_kill; |
| 6085 | int curr_idle; |
Olivier Houchard | b7b3faa | 2018-12-14 18:15:36 +0100 | [diff] [blame] | 6086 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6087 | eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK); |
| 6088 | if (!eb) { |
| 6089 | /* we might have reached the end of the tree, typically because |
| 6090 | * <now_ms> is in the first half and we're first scanning the last |
| 6091 | * half. Let's loop back to the beginning of the tree now. |
| 6092 | */ |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6093 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6094 | eb = eb32_first(&idle_conn_srv); |
| 6095 | if (likely(!eb)) |
| 6096 | break; |
| 6097 | } |
| 6098 | if (tick_is_lt(now_ms, eb->key)) { |
| 6099 | /* timer not expired yet, revisit it later */ |
| 6100 | next_wakeup = eb->key; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6101 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6102 | } |
| 6103 | srv = eb32_entry(eb, struct server, idle_node); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6104 | |
| 6105 | /* Calculate how many idle connections we want to kill : |
| 6106 | * we want to remove half the difference between the total |
| 6107 | * of established connections (used or idle) and the max |
| 6108 | * number of used connections. |
| 6109 | */ |
| 6110 | curr_idle = srv->curr_idle_conns; |
| 6111 | if (curr_idle == 0) |
| 6112 | goto remove; |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6113 | 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] | 6114 | exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1); |
Willy Tarreau | bdb86bd | 2020-06-29 15:56:35 +0200 | [diff] [blame] | 6115 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6116 | 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] | 6117 | if (srv->est_need_conns < srv->max_used_conns) |
| 6118 | srv->est_need_conns = srv->max_used_conns; |
| 6119 | |
Willy Tarreau | 9dc231a | 2022-11-21 14:32:33 +0100 | [diff] [blame] | 6120 | HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns); |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6121 | |
Willy Tarreau | 18ed789 | 2020-07-02 19:05:30 +0200 | [diff] [blame] | 6122 | if (exceed_conns <= 0) |
| 6123 | goto remove; |
| 6124 | |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6125 | /* check all threads starting with ours */ |
| 6126 | for (i = tid;;) { |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6127 | int max_conn; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6128 | int j; |
| 6129 | int did_remove = 0; |
| 6130 | |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6131 | max_conn = (exceed_conns * srv->curr_idle_thr[i]) / |
| 6132 | curr_idle + 1; |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6133 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6134 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6135 | 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] | 6136 | if (j > 0) |
| 6137 | did_remove = 1; |
| 6138 | if (max_conn - j > 0 && |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 6139 | 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] | 6140 | did_remove = 1; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6141 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock); |
Olivier Houchard | ff1d092 | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 6142 | |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6143 | if (did_remove) |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 6144 | task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER); |
Willy Tarreau | c35bcfc | 2020-06-29 14:43:16 +0200 | [diff] [blame] | 6145 | |
| 6146 | if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid) |
| 6147 | break; |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6148 | } |
Olivier Houchard | 079cb9a | 2020-03-30 00:23:57 +0200 | [diff] [blame] | 6149 | remove: |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6150 | eb32_delete(&srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6151 | |
| 6152 | if (srv->curr_idle_conns) { |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6153 | /* There are still more idle connections, add the |
| 6154 | * server back in the tree. |
| 6155 | */ |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6156 | srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6157 | eb32_insert(&idle_conn_srv, &srv->idle_node); |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6158 | next_wakeup = tick_first(next_wakeup, srv->idle_node.key); |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6159 | } |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6160 | } |
Olivier Houchard | 9ea5d36 | 2019-02-14 18:29:09 +0100 | [diff] [blame] | 6161 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock); |
| 6162 | |
Willy Tarreau | 21b9ff5 | 2020-11-05 09:12:20 +0100 | [diff] [blame] | 6163 | task->expire = next_wakeup; |
Olivier Houchard | 0c18a6f | 2018-12-02 14:11:41 +0100 | [diff] [blame] | 6164 | return task; |
| 6165 | } |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6166 | |
Amaury Denoyelle | 3109ccf | 2021-04-29 17:30:05 +0200 | [diff] [blame] | 6167 | /* Close remaining idle connections. This functions is designed to be run on |
| 6168 | * process shutdown. This guarantees a proper socket shutdown to avoid |
| 6169 | * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is |
| 6170 | * bypassed. |
| 6171 | * |
| 6172 | * This function is not thread-safe so it must only be called via a global |
| 6173 | * deinit function. |
| 6174 | */ |
| 6175 | static void srv_close_idle_conns(struct server *srv) |
| 6176 | { |
| 6177 | struct eb_root **cleaned_tree; |
| 6178 | int i; |
| 6179 | |
| 6180 | for (i = 0; i < global.nbthread; ++i) { |
| 6181 | struct eb_root *conn_trees[] = { |
| 6182 | &srv->per_thr[i].idle_conns, |
| 6183 | &srv->per_thr[i].safe_conns, |
| 6184 | &srv->per_thr[i].avail_conns, |
| 6185 | NULL |
| 6186 | }; |
| 6187 | |
| 6188 | for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) { |
| 6189 | while (!eb_is_empty(*cleaned_tree)) { |
| 6190 | struct ebmb_node *node = ebmb_first(*cleaned_tree); |
| 6191 | struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node); |
| 6192 | struct connection *conn = conn_hash_node->conn; |
| 6193 | |
| 6194 | if (conn->ctrl->ctrl_close) |
| 6195 | conn->ctrl->ctrl_close(conn); |
| 6196 | ebmb_delete(node); |
| 6197 | } |
| 6198 | } |
| 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | REGISTER_SERVER_DEINIT(srv_close_idle_conns); |
| 6203 | |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6204 | /* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */ |
| 6205 | 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] | 6206 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6207 | char **err) |
| 6208 | { |
| 6209 | if (too_many_args(1, args, err, NULL)) |
| 6210 | return -1; |
| 6211 | |
| 6212 | if (strcmp(args[1], "on") == 0) |
| 6213 | global.tune.options |= GTUNE_IDLE_POOL_SHARED; |
| 6214 | else if (strcmp(args[1], "off") == 0) |
| 6215 | global.tune.options &= ~GTUNE_IDLE_POOL_SHARED; |
| 6216 | else { |
| 6217 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 6218 | return -1; |
| 6219 | } |
| 6220 | return 0; |
| 6221 | } |
| 6222 | |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6223 | /* config parser for global "tune.pool-{low,high}-fd-ratio" */ |
| 6224 | 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] | 6225 | const struct proxy *defpx, const char *file, int line, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6226 | char **err) |
| 6227 | { |
| 6228 | int arg = -1; |
| 6229 | |
| 6230 | if (too_many_args(1, args, err, NULL)) |
| 6231 | return -1; |
| 6232 | |
| 6233 | if (*(args[1]) != 0) |
| 6234 | arg = atoi(args[1]); |
| 6235 | |
| 6236 | if (arg < 0 || arg > 100) { |
| 6237 | memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]); |
| 6238 | return -1; |
| 6239 | } |
| 6240 | |
| 6241 | if (args[0][10] == 'h') |
| 6242 | global.tune.pool_high_ratio = arg; |
| 6243 | else |
| 6244 | global.tune.pool_low_ratio = arg; |
| 6245 | return 0; |
| 6246 | } |
| 6247 | |
| 6248 | /* config keyword parsers */ |
| 6249 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 76cc699 | 2020-07-01 18:49:24 +0200 | [diff] [blame] | 6250 | { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared }, |
Olivier Houchard | 88698d9 | 2019-04-16 19:07:22 +0200 | [diff] [blame] | 6251 | { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6252 | { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio }, |
| 6253 | { 0, NULL, NULL } |
| 6254 | }}; |
| 6255 | |
| 6256 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 6257 | |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 6258 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6259 | * Local variables: |
| 6260 | * c-indent-level: 8 |
| 6261 | * c-basic-offset: 8 |
| 6262 | * End: |
| 6263 | */ |