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