Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Server-state management functions. |
| 3 | * |
| 4 | * Copyright (C) 2021 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | |
| 15 | #include <import/eb64tree.h> |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 16 | #include <import/ebistree.h> |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 17 | |
| 18 | #include <haproxy/api.h> |
| 19 | #include <haproxy/backend.h> |
| 20 | #include <haproxy/cfgparse.h> |
| 21 | #include <haproxy/check.h> |
| 22 | #include <haproxy/errors.h> |
| 23 | #include <haproxy/global.h> |
| 24 | #include <haproxy/log.h> |
| 25 | #include <haproxy/port_range.h> |
| 26 | #include <haproxy/proxy.h> |
| 27 | #include <haproxy/resolvers.h> |
| 28 | #include <haproxy/server.h> |
Willy Tarreau | bf1ae1a | 2021-05-08 13:08:34 +0200 | [diff] [blame] | 29 | #include <haproxy/tools.h> |
Tim Duesterhus | d5fc8fc | 2021-09-11 17:51:13 +0200 | [diff] [blame] | 30 | #include <haproxy/xxhash.h> |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* Update a server state using the parameters available in the params list. |
| 34 | * The caller must provide a supported version |
| 35 | * Grabs the server lock during operation. |
| 36 | */ |
| 37 | static void srv_state_srv_update(struct server *srv, int version, char **params) |
| 38 | { |
| 39 | char *p; |
| 40 | struct buffer *msg; |
| 41 | const char *warning; |
| 42 | |
| 43 | /* fields since version 1 |
| 44 | * and common to all other upcoming versions |
| 45 | */ |
| 46 | enum srv_state srv_op_state; |
| 47 | enum srv_admin srv_admin_state; |
| 48 | unsigned srv_uweight, srv_iweight; |
| 49 | unsigned long srv_last_time_change; |
| 50 | short srv_check_status; |
| 51 | enum chk_result srv_check_result; |
| 52 | int srv_check_health; |
| 53 | int srv_check_state, srv_agent_state; |
| 54 | int bk_f_forced_id; |
| 55 | int srv_f_forced_id; |
| 56 | int fqdn_set_by_cli; |
| 57 | const char *fqdn; |
| 58 | const char *port_st; |
| 59 | unsigned int port_svc; |
| 60 | char *srvrecord; |
| 61 | char *addr; |
| 62 | int partial_apply = 0; |
| 63 | #ifdef USE_OPENSSL |
| 64 | int use_ssl; |
| 65 | #endif |
| 66 | |
| 67 | fqdn = NULL; |
| 68 | port_svc = 0; |
| 69 | msg = alloc_trash_chunk(); |
| 70 | if (!msg) |
| 71 | goto end; |
| 72 | |
| 73 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 74 | |
| 75 | /* Only version 1 supported for now, don't check it. Fields are : |
| 76 | * srv_addr: params[0] |
| 77 | * srv_op_state: params[1] |
| 78 | * srv_admin_state: params[2] |
| 79 | * srv_uweight: params[3] |
| 80 | * srv_iweight: params[4] |
| 81 | * srv_last_time_change: params[5] |
| 82 | * srv_check_status: params[6] |
| 83 | * srv_check_result: params[7] |
| 84 | * srv_check_health: params[8] |
| 85 | * srv_check_state: params[9] |
| 86 | * srv_agent_state: params[10] |
| 87 | * bk_f_forced_id: params[11] |
| 88 | * srv_f_forced_id: params[12] |
| 89 | * srv_fqdn: params[13] |
| 90 | * srv_port: params[14] |
| 91 | * srvrecord: params[15] |
| 92 | * srv_use_ssl: params[16] |
| 93 | * srv_check_port: params[17] |
| 94 | * srv_check_addr: params[18] |
| 95 | * srv_agent_addr: params[19] |
| 96 | * srv_agent_port: params[20] |
| 97 | */ |
| 98 | |
| 99 | /* validating srv_op_state */ |
| 100 | p = NULL; |
| 101 | errno = 0; |
| 102 | srv_op_state = strtol(params[1], &p, 10); |
| 103 | if ((p == params[1]) || errno == EINVAL || errno == ERANGE || |
| 104 | (srv_op_state != SRV_ST_STOPPED && |
| 105 | srv_op_state != SRV_ST_STARTING && |
| 106 | srv_op_state != SRV_ST_RUNNING && |
| 107 | srv_op_state != SRV_ST_STOPPING)) { |
| 108 | chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]); |
| 109 | } |
| 110 | |
| 111 | /* validating srv_admin_state */ |
| 112 | p = NULL; |
| 113 | errno = 0; |
| 114 | srv_admin_state = strtol(params[2], &p, 10); |
| 115 | fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT); |
| 116 | |
| 117 | /* inherited statuses will be recomputed later. |
| 118 | * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn). |
| 119 | */ |
| 120 | srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT & ~SRV_ADMF_RMAINT; |
| 121 | |
| 122 | if ((p == params[2]) || errno == EINVAL || errno == ERANGE || |
| 123 | (srv_admin_state != 0 && |
| 124 | srv_admin_state != SRV_ADMF_FMAINT && |
| 125 | srv_admin_state != SRV_ADMF_CMAINT && |
| 126 | srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) && |
| 127 | srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) && |
| 128 | srv_admin_state != SRV_ADMF_FDRAIN)) { |
| 129 | chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]); |
| 130 | } |
| 131 | |
| 132 | /* validating srv_uweight */ |
| 133 | p = NULL; |
| 134 | errno = 0; |
| 135 | srv_uweight = strtol(params[3], &p, 10); |
| 136 | if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX)) |
| 137 | chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]); |
| 138 | |
| 139 | /* validating srv_iweight */ |
| 140 | p = NULL; |
| 141 | errno = 0; |
| 142 | srv_iweight = strtol(params[4], &p, 10); |
| 143 | if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX)) |
| 144 | chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]); |
| 145 | |
| 146 | /* validating srv_last_time_change */ |
| 147 | p = NULL; |
| 148 | errno = 0; |
| 149 | srv_last_time_change = strtol(params[5], &p, 10); |
| 150 | if ((p == params[5]) || errno == EINVAL || errno == ERANGE) |
| 151 | chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]); |
| 152 | |
| 153 | /* validating srv_check_status */ |
| 154 | p = NULL; |
| 155 | errno = 0; |
| 156 | srv_check_status = strtol(params[6], &p, 10); |
| 157 | if (p == params[6] || errno == EINVAL || errno == ERANGE || |
| 158 | (srv_check_status >= HCHK_STATUS_SIZE)) |
| 159 | chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]); |
| 160 | |
| 161 | /* validating srv_check_result */ |
| 162 | p = NULL; |
| 163 | errno = 0; |
| 164 | srv_check_result = strtol(params[7], &p, 10); |
| 165 | if ((p == params[7]) || errno == EINVAL || errno == ERANGE || |
| 166 | (srv_check_result != CHK_RES_UNKNOWN && |
| 167 | srv_check_result != CHK_RES_NEUTRAL && |
| 168 | srv_check_result != CHK_RES_FAILED && |
| 169 | srv_check_result != CHK_RES_PASSED && |
| 170 | srv_check_result != CHK_RES_CONDPASS)) { |
| 171 | chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]); |
| 172 | } |
| 173 | |
| 174 | /* validating srv_check_health */ |
| 175 | p = NULL; |
| 176 | errno = 0; |
| 177 | srv_check_health = strtol(params[8], &p, 10); |
| 178 | if (p == params[8] || errno == EINVAL || errno == ERANGE) |
| 179 | chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]); |
| 180 | |
| 181 | /* validating srv_check_state */ |
| 182 | p = NULL; |
| 183 | errno = 0; |
| 184 | srv_check_state = strtol(params[9], &p, 10); |
| 185 | if (p == params[9] || errno == EINVAL || errno == ERANGE || |
| 186 | (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT))) |
| 187 | chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]); |
| 188 | |
| 189 | /* validating srv_agent_state */ |
| 190 | p = NULL; |
| 191 | errno = 0; |
| 192 | srv_agent_state = strtol(params[10], &p, 10); |
| 193 | if (p == params[10] || errno == EINVAL || errno == ERANGE || |
| 194 | (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT))) |
| 195 | chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]); |
| 196 | |
| 197 | /* validating bk_f_forced_id */ |
| 198 | p = NULL; |
| 199 | errno = 0; |
| 200 | bk_f_forced_id = strtol(params[11], &p, 10); |
| 201 | if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1))) |
| 202 | chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]); |
| 203 | |
| 204 | /* validating srv_f_forced_id */ |
| 205 | p = NULL; |
| 206 | errno = 0; |
| 207 | srv_f_forced_id = strtol(params[12], &p, 10); |
| 208 | if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1))) |
| 209 | chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]); |
| 210 | |
| 211 | /* validating srv_fqdn */ |
| 212 | fqdn = params[13]; |
| 213 | if (fqdn && *fqdn == '-') |
| 214 | fqdn = NULL; |
| 215 | if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) { |
| 216 | chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]); |
| 217 | fqdn = NULL; |
| 218 | } |
| 219 | |
| 220 | port_st = params[14]; |
| 221 | if (port_st) { |
| 222 | port_svc = strl2uic(port_st, strlen(port_st)); |
| 223 | if (port_svc > USHRT_MAX) { |
| 224 | chunk_appendf(msg, ", invalid srv_port value '%s'", port_st); |
| 225 | port_st = NULL; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* SRV record |
| 230 | * NOTE: in HAProxy, SRV records must start with an underscore '_' |
| 231 | */ |
| 232 | srvrecord = params[15]; |
| 233 | if (srvrecord && *srvrecord != '_') |
| 234 | srvrecord = NULL; |
| 235 | |
| 236 | /* don't apply anything if one error has been detected */ |
| 237 | if (msg->data) |
| 238 | goto out; |
| 239 | partial_apply = 1; |
| 240 | |
| 241 | /* recover operational state and apply it to this server |
| 242 | * and all servers tracking this one */ |
| 243 | srv->check.health = srv_check_health; |
| 244 | switch (srv_op_state) { |
| 245 | case SRV_ST_STOPPED: |
| 246 | srv->check.health = 0; |
| 247 | srv_set_stopped(srv, "changed from server-state after a reload", NULL); |
| 248 | break; |
| 249 | case SRV_ST_STARTING: |
| 250 | /* If rise == 1 there is no STARTING state, let's switch to |
| 251 | * RUNNING |
| 252 | */ |
| 253 | if (srv->check.rise == 1) { |
| 254 | srv->check.health = srv->check.rise + srv->check.fall - 1; |
| 255 | srv_set_running(srv, "", NULL); |
| 256 | break; |
| 257 | } |
| 258 | if (srv->check.health < 1 || srv->check.health >= srv->check.rise) |
| 259 | srv->check.health = srv->check.rise - 1; |
| 260 | srv->next_state = srv_op_state; |
| 261 | break; |
| 262 | case SRV_ST_STOPPING: |
| 263 | /* If fall == 1 there is no STOPPING state, let's switch to |
| 264 | * STOPPED |
| 265 | */ |
| 266 | if (srv->check.fall == 1) { |
| 267 | srv->check.health = 0; |
| 268 | srv_set_stopped(srv, "changed from server-state after a reload", NULL); |
| 269 | break; |
| 270 | } |
| 271 | if (srv->check.health < srv->check.rise || |
| 272 | srv->check.health > srv->check.rise + srv->check.fall - 2) |
| 273 | srv->check.health = srv->check.rise; |
| 274 | srv_set_stopping(srv, "changed from server-state after a reload", NULL); |
| 275 | break; |
| 276 | case SRV_ST_RUNNING: |
| 277 | srv->check.health = srv->check.rise + srv->check.fall - 1; |
| 278 | srv_set_running(srv, "", NULL); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | /* When applying server state, the following rules apply: |
| 283 | * - in case of a configuration change, we apply the setting from the new |
| 284 | * configuration, regardless of old running state |
| 285 | * - if no configuration change, we apply old running state only if old running |
| 286 | * state is different from new configuration state |
| 287 | */ |
| 288 | /* configuration has changed */ |
| 289 | if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) { |
| 290 | if (srv->next_admin & SRV_ADMF_CMAINT) |
| 291 | srv_adm_set_maint(srv); |
| 292 | else |
| 293 | srv_adm_set_ready(srv); |
| 294 | } |
| 295 | /* configuration is the same, let's compate old running state and new conf state */ |
| 296 | else { |
| 297 | if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT)) |
| 298 | srv_adm_set_maint(srv); |
| 299 | else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT)) |
| 300 | srv_adm_set_ready(srv); |
| 301 | } |
| 302 | /* apply drain mode if server is currently enabled */ |
| 303 | if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) { |
| 304 | /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0 |
| 305 | * (srv->iweight is the weight set up in configuration). |
| 306 | * There are two possible reasons for FDRAIN to have been present : |
| 307 | * - previous config weight was zero |
| 308 | * - "set server b/s drain" was sent to the CLI |
| 309 | * |
| 310 | * In the first case, we simply want to drop this drain state |
| 311 | * if the new weight is not zero anymore, meaning the administrator |
| 312 | * has intentionally turned the weight back to a positive value to |
| 313 | * enable the server again after an operation. In the second case, |
| 314 | * the drain state was forced on the CLI regardless of the config's |
| 315 | * weight so we don't want a change to the config weight to lose this |
| 316 | * status. What this means is : |
| 317 | * - if previous weight was 0 and new one is >0, drop the DRAIN state. |
| 318 | * - if the previous weight was >0, keep it. |
| 319 | */ |
| 320 | if (srv_iweight > 0 || srv->iweight == 0) |
| 321 | srv_adm_set_drain(srv); |
| 322 | } |
| 323 | |
| 324 | srv->last_change = date.tv_sec - srv_last_time_change; |
| 325 | srv->check.status = srv_check_status; |
| 326 | srv->check.result = srv_check_result; |
| 327 | |
| 328 | /* Only case we want to apply is removing ENABLED flag which could have been |
| 329 | * done by the "disable health" command over the stats socket |
| 330 | */ |
| 331 | if ((srv->check.state & CHK_ST_CONFIGURED) && |
| 332 | (srv_check_state & CHK_ST_CONFIGURED) && |
| 333 | !(srv_check_state & CHK_ST_ENABLED)) |
| 334 | srv->check.state &= ~CHK_ST_ENABLED; |
| 335 | |
| 336 | /* Only case we want to apply is removing ENABLED flag which could have been |
| 337 | * done by the "disable agent" command over the stats socket |
| 338 | */ |
| 339 | if ((srv->agent.state & CHK_ST_CONFIGURED) && |
| 340 | (srv_agent_state & CHK_ST_CONFIGURED) && |
| 341 | !(srv_agent_state & CHK_ST_ENABLED)) |
| 342 | srv->agent.state &= ~CHK_ST_ENABLED; |
| 343 | |
| 344 | /* We want to apply the previous 'running' weight (srv_uweight) only if there |
| 345 | * was no change in the configuration: both previous and new iweight are equals |
| 346 | * |
| 347 | * It means that a configuration file change has precedence over a unix socket change |
| 348 | * for server's weight |
| 349 | * |
| 350 | * by default, HAProxy applies the following weight when parsing the configuration |
| 351 | * srv->uweight = srv->iweight |
| 352 | */ |
| 353 | if (srv_iweight == srv->iweight) { |
| 354 | srv->uweight = srv_uweight; |
| 355 | } |
| 356 | server_recalc_eweight(srv, 1); |
| 357 | |
| 358 | /* load server IP address */ |
| 359 | if (strcmp(params[0], "-") != 0) |
| 360 | srv->lastaddr = strdup(params[0]); |
| 361 | |
| 362 | if (fqdn && srv->hostname) { |
| 363 | if (strcmp(srv->hostname, fqdn) == 0) { |
| 364 | /* Here we reset the 'set from stats socket FQDN' flag |
| 365 | * to support such transitions: |
| 366 | * Let's say initial FQDN value is foo1 (in configuration file). |
| 367 | * - FQDN changed from stats socket, from foo1 to foo2 value, |
| 368 | * - FQDN changed again from file configuration (with the same previous value |
| 369 | set from stats socket, from foo1 to foo2 value), |
| 370 | * - reload for any other reason than a FQDN modification, |
| 371 | * the configuration file FQDN matches the fqdn server state file value. |
| 372 | * So we must reset the 'set from stats socket FQDN' flag to be consistent with |
| 373 | * any further FQDN modification. |
| 374 | */ |
| 375 | srv->next_admin &= ~SRV_ADMF_HMAINT; |
| 376 | } |
| 377 | else { |
| 378 | /* If the FDQN has been changed from stats socket, |
| 379 | * apply fqdn state file value (which is the value set |
| 380 | * from stats socket). |
| 381 | * Also ensure the runtime resolver will process this resolution. |
| 382 | */ |
| 383 | if (fqdn_set_by_cli) { |
| 384 | srv_set_fqdn(srv, fqdn, 0); |
| 385 | srv->flags &= ~SRV_F_NO_RESOLUTION; |
| 386 | srv->next_admin |= SRV_ADMF_HMAINT; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | /* If all the conditions below are validated, this means |
| 391 | * we're evaluating a server managed by SRV resolution |
| 392 | */ |
| 393 | else if (fqdn && !srv->hostname && srvrecord) { |
| 394 | int res; |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 395 | int i; |
| 396 | char *tmp; |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 397 | |
| 398 | /* we can't apply previous state if SRV record has changed */ |
Christopher Faulet | 85af93b | 2021-06-10 16:59:53 +0200 | [diff] [blame] | 399 | if (!srv->srvrq) { |
| 400 | chunk_appendf(msg, ", no SRV resolution for server '%s'. Previous state not applied", srv->id); |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 401 | goto out; |
| 402 | } |
Christopher Faulet | 85af93b | 2021-06-10 16:59:53 +0200 | [diff] [blame] | 403 | if (strcmp(srv->srvrq->name, srvrecord) != 0) { |
| 404 | chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id); |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | /* prepare DNS resolution for this server */ |
| 409 | res = srv_prepare_for_resolution(srv, fqdn); |
| 410 | if (res == -1) { |
| 411 | chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id); |
| 412 | goto out; |
| 413 | } |
| 414 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 415 | /* Remove from available list and insert in tree |
| 416 | * since this server has an hostname |
| 417 | */ |
| 418 | LIST_DEL_INIT(&srv->srv_rec_item); |
| 419 | srv->host_dn.key = tmp = strdup(srv->hostname_dn); |
| 420 | |
| 421 | /* convert the key in lowercase because tree |
| 422 | * lookup is case sensitive but we don't care |
| 423 | */ |
| 424 | for (i = 0; tmp[i]; i++) |
| 425 | tmp[i] = tolower(tmp[i]); |
| 426 | |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 427 | /* insert in tree and set the srvrq expiration date */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 428 | ebis_insert(&srv->srvrq->named_servers, &srv->host_dn); |
Christopher Faulet | dcac418 | 2021-06-15 16:17:17 +0200 | [diff] [blame] | 429 | task_schedule(srv->srvrq_check, tick_add(now_ms, srv->srvrq->resolvers->hold.timeout)); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 430 | |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 431 | /* Unset SRV_F_MAPPORTS for SRV records. |
| 432 | * SRV_F_MAPPORTS is unfortunately set by parse_server() |
| 433 | * because no ports are provided in the configuration file. |
| 434 | * This is because HAProxy will use the port found into the SRV record. |
| 435 | */ |
| 436 | srv->flags &= ~SRV_F_MAPPORTS; |
| 437 | } |
| 438 | |
| 439 | if (port_st) |
| 440 | srv->svc_port = port_svc; |
| 441 | |
| 442 | |
| 443 | if (params[16]) { |
| 444 | #ifdef USE_OPENSSL |
| 445 | use_ssl = strtol(params[16], &p, 10); |
| 446 | |
| 447 | /* configure ssl if connection has been initiated at startup */ |
| 448 | if (srv->ssl_ctx.ctx != NULL) |
Willy Tarreau | a8a72c6 | 2021-10-06 11:48:34 +0200 | [diff] [blame] | 449 | srv_set_ssl(srv, use_ssl); |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 450 | #endif |
| 451 | } |
| 452 | |
| 453 | port_st = NULL; |
| 454 | if (params[17] && strcmp(params[17], "0") != 0) |
| 455 | port_st = params[17]; |
| 456 | addr = NULL; |
| 457 | if (params[18] && strcmp(params[18], "-") != 0) |
| 458 | addr = params[18]; |
| 459 | if (addr || port_st) { |
| 460 | warning = srv_update_check_addr_port(srv, addr, port_st); |
| 461 | if (warning) { |
| 462 | chunk_appendf(msg, ", %s", warning); |
| 463 | goto out; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | port_st = NULL; |
| 468 | if (params[20] && strcmp(params[20], "0") != 0) |
| 469 | port_st = params[20]; |
| 470 | addr = NULL; |
| 471 | if (params[19] && strcmp(params[19], "-") != 0) |
| 472 | addr = params[19]; |
| 473 | if (addr || port_st) { |
| 474 | warning = srv_update_agent_addr_port(srv, addr, port_st); |
| 475 | if (warning) { |
| 476 | chunk_appendf(msg, ", %s", warning); |
| 477 | goto out; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | out: |
| 482 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 483 | if (msg->data) { |
| 484 | if (partial_apply == 1) |
| 485 | ha_warning("server-state partially applied for server '%s/%s'%s\n", |
| 486 | srv->proxy->id, srv->id, msg->area); |
| 487 | else |
| 488 | ha_warning("server-state application failed for server '%s/%s'%s\n", |
| 489 | srv->proxy->id, srv->id, msg->area); |
| 490 | } |
| 491 | end: |
| 492 | free_trash_chunk(msg); |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Loop on the proxy's servers and try to load its state from <st_tree> using |
| 497 | * srv_state_srv_update(). The proxy name and the server name are concatenated |
| 498 | * to form the key. If found the entry is removed from the tree. |
| 499 | */ |
| 500 | static void srv_state_px_update(const struct proxy *px, int vsn, struct eb_root *st_tree) |
| 501 | { |
| 502 | struct server_state_line *st_line; |
| 503 | struct eb64_node *node; |
| 504 | struct server *srv; |
| 505 | unsigned long key; |
| 506 | |
| 507 | for (srv = px->srv; srv; srv = srv->next) { |
| 508 | chunk_printf(&trash, "%s %s", px->id, srv->id); |
| 509 | key = XXH3(trash.area, trash.data, 0); |
| 510 | node = eb64_lookup(st_tree, key); |
| 511 | if (!node) |
| 512 | continue; /* next server */ |
| 513 | st_line = eb64_entry(node, typeof(*st_line), node); |
| 514 | srv_state_srv_update(srv, vsn, st_line->params+4); |
| 515 | |
| 516 | /* the node may be released now */ |
| 517 | eb64_delete(node); |
| 518 | free(st_line->line); |
| 519 | free(st_line); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * read next line from file <f> and return the server state version if one found. |
| 525 | * If no version is found, then 0 is returned |
| 526 | * Note that this should be the first read on <f> |
| 527 | */ |
| 528 | static int srv_state_get_version(FILE *f) { |
| 529 | char mybuf[SRV_STATE_LINE_MAXLEN]; |
| 530 | char *endptr; |
| 531 | long int vsn; |
| 532 | |
| 533 | /* first character of first line of the file must contain the version of the export */ |
| 534 | if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) |
| 535 | return 0; |
| 536 | |
| 537 | vsn = strtol(mybuf, &endptr, 10); |
| 538 | if (endptr == mybuf || *endptr != '\n') { |
| 539 | /* Empty or truncated line */ |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | if (vsn < SRV_STATE_FILE_VERSION_MIN || vsn > SRV_STATE_FILE_VERSION_MAX) { |
| 544 | /* Wrong version number */ |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | return vsn; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | /* |
| 553 | * parses server state line stored in <buf> and supposedly in version <version>. |
| 554 | * Set <params> accordingly on success. It returns 1 on success, 0 if the line |
| 555 | * must be ignored and -1 on error. |
| 556 | * The caller must provide a supported version |
| 557 | */ |
| 558 | static int srv_state_parse_line(char *buf, const int version, char **params) |
| 559 | { |
| 560 | int buflen, arg, ret; |
| 561 | char *cur; |
| 562 | |
| 563 | buflen = strlen(buf); |
| 564 | cur = buf; |
| 565 | ret = 1; /* be optimistic and pretend a success */ |
| 566 | |
| 567 | /* we need at least one character and a non-truncated line */ |
| 568 | if (buflen == 0 || buf[buflen - 1] != '\n') { |
| 569 | ret = -1; |
| 570 | goto out; |
| 571 | } |
| 572 | |
| 573 | /* skip blank characters at the beginning of the line */ |
| 574 | while (isblank((unsigned char)*cur)) |
| 575 | ++cur; |
| 576 | |
| 577 | /* ignore empty or commented lines */ |
| 578 | if (!*cur || *cur == '\n' || *cur == '#') { |
| 579 | ret = 0; |
| 580 | goto out; |
| 581 | } |
| 582 | |
| 583 | /* Removes trailing '\n' to ease parsing */ |
| 584 | buf[buflen - 1] = '\0'; |
| 585 | |
| 586 | /* we're now ready to move the line into <params> */ |
| 587 | memset(params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*params)); |
| 588 | arg = 0; |
| 589 | while (*cur) { |
| 590 | /* first of all, stop if there are too many fields */ |
| 591 | if (arg >= SRV_STATE_FILE_MAX_FIELDS) |
| 592 | break; |
| 593 | |
| 594 | /* then skip leading spaces */ |
| 595 | while (*cur && isblank((unsigned char)*cur)) { |
| 596 | ++cur; |
| 597 | if (!*cur) |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * idx: |
| 603 | * be_id: params[0] |
| 604 | * be_name: params[1] |
| 605 | * srv_id: params[2] |
| 606 | * srv_name: params[3] |
| 607 | * v1 |
| 608 | * srv_addr: params[4] |
| 609 | * srv_op_state: params[5] |
| 610 | * srv_admin_state: params[6] |
| 611 | * srv_uweight: params[7] |
| 612 | * srv_iweight: params[8] |
| 613 | * srv_last_time_change: params[9] |
| 614 | * srv_check_status: params[10] |
| 615 | * srv_check_result: params[11] |
| 616 | * srv_check_health: params[12] |
| 617 | * srv_check_state: params[13] |
| 618 | * srv_agent_state: params[14] |
| 619 | * bk_f_forced_id: params[15] |
| 620 | * srv_f_forced_id: params[16] |
| 621 | * srv_fqdn: params[17] |
| 622 | * srv_port: params[18] |
| 623 | * srvrecord: params[19] |
| 624 | * |
| 625 | * srv_use_ssl: params[20] (optional field) |
| 626 | * srv_check_port: params[21] (optional field) |
| 627 | * srv_check_addr: params[22] (optional field) |
| 628 | * srv_agent_addr: params[23] (optional field) |
| 629 | * srv_agent_port: params[24] (optional field) |
| 630 | * |
| 631 | */ |
| 632 | params[arg++] = cur; |
| 633 | |
| 634 | /* look for the end of the current field */ |
| 635 | while (*cur && !isblank((unsigned char)*cur)) { |
| 636 | ++cur; |
| 637 | if (!*cur) |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | /* otherwise, cut the field and move to the next one */ |
| 642 | *cur++ = '\0'; |
| 643 | } |
| 644 | |
| 645 | /* if the number of fields does not match the version, then return an error */ |
| 646 | if (version == 1 && |
| 647 | (arg < SRV_STATE_FILE_MIN_FIELDS_VERSION_1 || |
| 648 | arg > SRV_STATE_FILE_MAX_FIELDS_VERSION_1)) |
| 649 | ret = -1; |
| 650 | |
| 651 | out: |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /* |
| 657 | * parses a server state line using srv_state_parse_line() and store the result |
| 658 | * in <st_tree>. If an error occurred during the parsing, the line is |
| 659 | * ignored. if <px> is defined, it is used to check the backend id/name against |
| 660 | * the parsed params and to compute the key of the line. |
| 661 | */ |
| 662 | static int srv_state_parse_and_store_line(char *line, int vsn, struct eb_root *st_tree, |
| 663 | struct proxy *px) |
| 664 | { |
| 665 | struct server_state_line *st_line; |
| 666 | int ret = 0; |
| 667 | |
| 668 | /* store line in tree and duplicate the line */ |
| 669 | st_line = calloc(1, sizeof(*st_line)); |
| 670 | if (st_line == NULL) |
| 671 | goto skip_line; |
| 672 | st_line->line = strdup(line); |
| 673 | if (st_line->line == NULL) |
| 674 | goto skip_line; |
| 675 | |
| 676 | ret = srv_state_parse_line(st_line->line, vsn, st_line->params); |
| 677 | if (ret <= 0) |
| 678 | goto skip_line; |
| 679 | |
| 680 | /* Check backend name against params if <px> is defined */ |
| 681 | if (px) { |
| 682 | int check_id = (atoi(st_line->params[0]) == px->uuid); |
| 683 | int check_name = (strcmp(px->id, st_line->params[1]) == 0); |
| 684 | int bk_f_forced_id = (atoi(st_line->params[15]) & PR_O_FORCED_ID); |
| 685 | |
| 686 | |
| 687 | if (!check_id && !check_name) { |
| 688 | /* backend does not match at all: skip the line */ |
| 689 | goto skip_line; |
| 690 | } |
| 691 | else if (!check_id) { |
| 692 | /* Id mismatch: warn but continue */ |
| 693 | ha_warning("Proxy '%s': backend ID mismatch: from server state file: '%s', from running config '%d'\n", |
| 694 | px->id, st_line->params[0], px->uuid); |
| 695 | send_log(px, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", |
| 696 | st_line->params[0], px->uuid); |
| 697 | } |
| 698 | else if (!check_name) { |
| 699 | /* Name mismatch: warn and skip the line, except if the backend id was forced |
| 700 | * in the previous configuration */ |
| 701 | ha_warning("Proxy '%s': backend name mismatch: from server state file: '%s', from running config '%s'\n", |
| 702 | px->id, st_line->params[1], px->id); |
| 703 | send_log(px, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", |
| 704 | st_line->params[1], px->id); |
| 705 | if (!bk_f_forced_id) |
| 706 | goto skip_line; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * The key: "be_name srv_name" |
| 712 | * if <px> is defined: be_name == px->id |
| 713 | * otherwise: be_name == params[1] |
| 714 | */ |
| 715 | chunk_printf(&trash, "%s %s", (px ? px->id : st_line->params[1]), st_line->params[3]); |
| 716 | st_line->node.key = XXH3(trash.area, trash.data, 0); |
| 717 | if (eb64_insert(st_tree, &st_line->node) != &st_line->node) { |
| 718 | /* this is a duplicate key, probably a hand-crafted file, drop it! */ |
| 719 | goto skip_line; |
| 720 | } |
| 721 | |
| 722 | return ret; |
| 723 | |
| 724 | skip_line: |
| 725 | /* free up memory in case of error during the processing of the line */ |
| 726 | if (st_line) { |
| 727 | free(st_line->line); |
| 728 | free(st_line); |
| 729 | } |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | /* Helper function to get the server-state file path. |
| 734 | * If <filename> starts with a '/', it is considered as an absolute path. In |
| 735 | * this case or if <global.server_state_base> is not set, <filename> only is |
| 736 | * considered. Otherwise, the <global.server_state_base> is concatenated to |
| 737 | * <filename> to produce the file path and copied to <dst_path>. in both cases, |
| 738 | * the result must not exceeds <maxpathlen>. |
| 739 | * |
| 740 | * The len is returned on success or -1 if the path is too long. On error, the |
| 741 | * caller must not rely on <dst_path>. |
| 742 | */ |
| 743 | static inline int srv_state_get_filepath(char *dst_path, int maxpathlen, const char *filename) |
| 744 | { |
Willy Tarreau | 6d4173e | 2021-03-12 13:57:19 +0100 | [diff] [blame] | 745 | char *sep; |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 746 | int len = 0; |
| 747 | |
| 748 | /* create the globalfilepath variable */ |
| 749 | if (*filename == '/' || !global.server_state_base) { |
| 750 | /* absolute path or no base directory provided */ |
Willy Tarreau | 47a30c4 | 2021-03-12 14:09:10 +0100 | [diff] [blame] | 751 | len = strlcpy2(dst_path, filename, maxpathlen); |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 752 | } |
| 753 | else { |
| 754 | /* concat base directory and global server-state file */ |
Willy Tarreau | 6d4173e | 2021-03-12 13:57:19 +0100 | [diff] [blame] | 755 | sep = (global.server_state_base[strlen(global.server_state_base)-1] != '/' ? "/": ""); |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 756 | len = snprintf(dst_path, maxpathlen, "%s%s%s", global.server_state_base, sep, filename); |
| 757 | } |
| 758 | return (len < maxpathlen ? len: -1); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /* This function parses all the proxies and only take care of the backends (since we're looking for server) |
| 763 | * For each proxy, it does the following: |
| 764 | * - opens its server state file (either one or local one) |
| 765 | * - read whole file, line by line |
| 766 | * - analyse each line to check if it matches our current backend: |
| 767 | * - backend name matches |
| 768 | * - backend id matches if id is forced and name doesn't match |
| 769 | * - if the server pointed by the line is found, then state is applied |
| 770 | * |
| 771 | * If the running backend uuid or id differs from the state file, then HAProxy reports |
| 772 | * a warning. |
| 773 | * |
| 774 | * Grabs the server's lock via srv_state_srv_update(). |
| 775 | */ |
| 776 | void apply_server_state(void) |
| 777 | { |
| 778 | /* tree where global state_file is loaded */ |
| 779 | struct eb_root global_state_tree = EB_ROOT_UNIQUE; |
| 780 | struct proxy *curproxy; |
| 781 | struct server_state_line *st_line; |
| 782 | struct eb64_node *node, *next_node; |
| 783 | FILE *f; |
| 784 | char mybuf[SRV_STATE_LINE_MAXLEN]; |
| 785 | char file[MAXPATHLEN]; |
| 786 | int local_vsn, global_vsn, len, linenum; |
| 787 | |
| 788 | global_vsn = 0; /* no global file */ |
| 789 | if (!global.server_state_file) |
| 790 | goto no_globalfile; |
| 791 | len = srv_state_get_filepath(file, MAXPATHLEN, global.server_state_file); |
| 792 | if (len == -1) { |
| 793 | ha_warning("config: Can't load global server state file: file too long.\n"); |
| 794 | goto no_globalfile; |
| 795 | } |
| 796 | |
| 797 | /* Load global server state in a tree */ |
| 798 | errno = 0; |
| 799 | f = fopen(file, "r"); |
| 800 | if (!f) { |
| 801 | ha_warning("config: Can't open global server state file '%s': %s\n", file, strerror(errno)); |
| 802 | goto no_globalfile; |
| 803 | } |
| 804 | |
| 805 | global_vsn = srv_state_get_version(f); |
| 806 | if (global_vsn == 0) { |
| 807 | ha_warning("config: Can't get version of the global server state file '%s'.\n", |
| 808 | file); |
| 809 | goto close_globalfile; |
| 810 | } |
| 811 | |
| 812 | for (linenum = 1; fgets(mybuf, SRV_STATE_LINE_MAXLEN, f); linenum++) { |
| 813 | int ret; |
| 814 | |
| 815 | ret = srv_state_parse_and_store_line(mybuf, global_vsn, &global_state_tree, NULL); |
| 816 | if (ret == -1) { |
| 817 | ha_warning("config: corrupted global server state file '%s' at line %d.\n", |
| 818 | file, linenum); |
| 819 | global_vsn = 0; |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | close_globalfile: |
| 825 | fclose(f); |
| 826 | |
| 827 | no_globalfile: |
| 828 | /* parse all proxies and load states form tree (global file) or from local file */ |
| 829 | for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) { |
| 830 | struct eb_root local_state_tree = EB_ROOT_UNIQUE; |
| 831 | |
Christopher Faulet | 6f69110 | 2021-03-04 16:35:26 +0100 | [diff] [blame] | 832 | /* Must be an enabled backend with at least a server */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 833 | if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) || !curproxy->srv) |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 834 | continue; /* next proxy */ |
| 835 | |
Christopher Faulet | 6f69110 | 2021-03-04 16:35:26 +0100 | [diff] [blame] | 836 | /* Mode must be specified */ |
| 837 | BUG_ON(curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_UNSPEC); |
| 838 | |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 839 | /* No server-state file for this proxy */ |
| 840 | if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_NONE) |
| 841 | continue; /* next proxy */ |
| 842 | |
| 843 | if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) { |
| 844 | /* when global file is used, we get data from the tree |
| 845 | * Note that in such case we don't check backend name neither uuid. |
| 846 | * Backend name can't be wrong since it's used as a key to retrieve the server state |
| 847 | * line from the tree. |
| 848 | */ |
| 849 | if (global_vsn) |
| 850 | srv_state_px_update(curproxy, global_vsn, &global_state_tree); |
| 851 | continue; /* next proxy */ |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Here we load a local server state-file |
| 856 | */ |
| 857 | |
| 858 | /* create file variable */ |
| 859 | len = srv_state_get_filepath(file, MAXPATHLEN, curproxy->server_state_file_name); |
| 860 | if (len == -1) { |
| 861 | ha_warning("Proxy '%s': Can't load local server state file: file too long.\n", curproxy->id); |
| 862 | continue; /* next proxy */ |
| 863 | } |
| 864 | |
| 865 | /* Load local server state in a tree */ |
| 866 | errno = 0; |
| 867 | f = fopen(file, "r"); |
| 868 | if (!f) { |
| 869 | ha_warning("Proxy '%s': Can't open server state file '%s': %s.\n", |
| 870 | curproxy->id, file, strerror(errno)); |
| 871 | continue; /* next proxy */ |
| 872 | } |
| 873 | |
| 874 | /* first character of first line of the file must contain the version of the export */ |
| 875 | local_vsn = srv_state_get_version(f); |
| 876 | if (local_vsn == 0) { |
| 877 | ha_warning("Proxy '%s': Can't get version of the server state file '%s'.\n", |
| 878 | curproxy->id, file); |
| 879 | goto close_localfile; |
| 880 | } |
| 881 | |
| 882 | /* First, parse lines of the local server-state file and store them in a eb-tree */ |
| 883 | for (linenum = 1; fgets(mybuf, SRV_STATE_LINE_MAXLEN, f); linenum++) { |
| 884 | int ret; |
| 885 | |
| 886 | ret = srv_state_parse_and_store_line(mybuf, local_vsn, &local_state_tree, curproxy); |
| 887 | if (ret == -1) { |
| 888 | ha_warning("Proxy '%s': corrupted server state file '%s' at line %d.\n", |
| 889 | curproxy->id, file, linenum); |
| 890 | local_vsn = 0; |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | if (local_vsn) |
| 896 | srv_state_px_update(curproxy, local_vsn, &local_state_tree); |
| 897 | |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 898 | /* Remove unused server-state lines */ |
Christopher Faulet | 3e3d3be | 2021-02-16 13:31:30 +0100 | [diff] [blame] | 899 | node = eb64_first(&local_state_tree); |
| 900 | while (node) { |
| 901 | st_line = eb64_entry(node, typeof(*st_line), node); |
| 902 | next_node = eb64_next(node); |
| 903 | eb64_delete(node); |
| 904 | |
| 905 | if (local_vsn) { |
| 906 | /* if no server found, then warn */ |
| 907 | ha_warning("Proxy '%s': can't find server '%s' in backend '%s'\n", |
| 908 | curproxy->id, st_line->params[3], curproxy->id); |
| 909 | send_log(curproxy, LOG_NOTICE, "can't find server '%s' in backend '%s'\n", |
| 910 | st_line->params[3], curproxy->id); |
| 911 | } |
| 912 | |
| 913 | free(st_line->line); |
| 914 | free(st_line); |
| 915 | node = next_node; |
| 916 | } |
| 917 | |
| 918 | close_localfile: |
| 919 | fclose(f); |
| 920 | } |
| 921 | |
| 922 | node = eb64_first(&global_state_tree); |
| 923 | while (node) { |
| 924 | st_line = eb64_entry(node, typeof(*st_line), node); |
| 925 | next_node = eb64_next(node); |
| 926 | eb64_delete(node); |
| 927 | free(st_line->line); |
| 928 | free(st_line); |
| 929 | node = next_node; |
| 930 | } |
| 931 | } |