| | varnish-cache/bin/varnishd/cache/cache_backend.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions |
| 11 |
|
* are met: |
| 12 |
|
* 1. Redistributions of source code must retain the above copyright |
| 13 |
|
* notice, this list of conditions and the following disclaimer. |
| 14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
|
* notice, this list of conditions and the following disclaimer in the |
| 16 |
|
* documentation and/or other materials provided with the distribution. |
| 17 |
|
* |
| 18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
|
* SUCH DAMAGE. |
| 29 |
|
* |
| 30 |
|
* The director implementation for VCL backends. |
| 31 |
|
* |
| 32 |
|
*/ |
| 33 |
|
|
| 34 |
|
#include "config.h" |
| 35 |
|
|
| 36 |
|
#include <stdlib.h> |
| 37 |
|
|
| 38 |
|
#include "cache_varnishd.h" |
| 39 |
|
#include "cache_director.h" |
| 40 |
|
|
| 41 |
|
#include "vtcp.h" |
| 42 |
|
#include "vtim.h" |
| 43 |
|
#include "vsa.h" |
| 44 |
|
|
| 45 |
|
#include "cache_backend.h" |
| 46 |
|
#include "cache_conn_pool.h" |
| 47 |
|
#include "cache_transport.h" |
| 48 |
|
#include "cache_vcl.h" |
| 49 |
|
#include "http1/cache_http1.h" |
| 50 |
|
#include "proxy/cache_proxy.h" |
| 51 |
|
|
| 52 |
|
#include "VSC_vbe.h" |
| 53 |
|
|
| 54 |
|
/*--------------------------------------------------------------------*/ |
| 55 |
|
|
| 56 |
|
enum connwait_e { |
| 57 |
|
CW_DO_CONNECT = 1, |
| 58 |
|
CW_QUEUED, |
| 59 |
|
CW_DEQUEUED, |
| 60 |
|
CW_BE_BUSY, |
| 61 |
|
}; |
| 62 |
|
|
| 63 |
|
struct connwait { |
| 64 |
|
unsigned magic; |
| 65 |
|
#define CONNWAIT_MAGIC 0x75c7a52b |
| 66 |
|
enum connwait_e cw_state; |
| 67 |
|
VTAILQ_ENTRY(connwait) cw_list; |
| 68 |
|
pthread_cond_t cw_cond; |
| 69 |
|
}; |
| 70 |
|
|
| 71 |
|
static const char * const vbe_proto_ident = "HTTP Backend"; |
| 72 |
|
|
| 73 |
|
static struct lock backends_mtx; |
| 74 |
|
|
| 75 |
|
/*--------------------------------------------------------------------*/ |
| 76 |
|
|
| 77 |
|
void |
| 78 |
1878 |
VBE_Connect_Error(struct VSC_vbe *vsc, int err) |
| 79 |
|
{ |
| 80 |
|
|
| 81 |
1878 |
switch(err) { |
| 82 |
|
case 0: |
| 83 |
|
/* |
| 84 |
|
* This is kind of brittle, but zero is the only |
| 85 |
|
* value of errno we can trust to have no meaning. |
| 86 |
|
*/ |
| 87 |
812 |
vsc->helddown++; |
| 88 |
812 |
break; |
| 89 |
|
case EACCES: |
| 90 |
|
case EPERM: |
| 91 |
0 |
vsc->fail_eacces++; |
| 92 |
0 |
break; |
| 93 |
|
case EADDRNOTAVAIL: |
| 94 |
0 |
vsc->fail_eaddrnotavail++; |
| 95 |
0 |
break; |
| 96 |
|
case ECONNREFUSED: |
| 97 |
1066 |
vsc->fail_econnrefused++; |
| 98 |
1066 |
break; |
| 99 |
|
case ENETUNREACH: |
| 100 |
0 |
vsc->fail_enetunreach++; |
| 101 |
0 |
break; |
| 102 |
|
case ETIMEDOUT: |
| 103 |
0 |
vsc->fail_etimedout++; |
| 104 |
0 |
break; |
| 105 |
|
default: |
| 106 |
0 |
vsc->fail_other++; |
| 107 |
0 |
} |
| 108 |
1878 |
} |
| 109 |
|
|
| 110 |
|
/*--------------------------------------------------------------------*/ |
| 111 |
|
|
| 112 |
|
#define FIND_TMO(tmx, dst, bo, be) \ |
| 113 |
|
do { \ |
| 114 |
|
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); \ |
| 115 |
|
dst = bo->tmx; \ |
| 116 |
|
if (isnan(dst) && be->tmx >= 0.0) \ |
| 117 |
|
dst = be->tmx; \ |
| 118 |
|
if (isnan(dst)) \ |
| 119 |
|
dst = cache_param->tmx; \ |
| 120 |
|
} while (0) |
| 121 |
|
|
| 122 |
|
#define FIND_BE_SPEC(tmx, dst, be, def) \ |
| 123 |
|
do { \ |
| 124 |
|
CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC); \ |
| 125 |
|
dst = be->tmx; \ |
| 126 |
|
if (dst == def) \ |
| 127 |
|
dst = cache_param->tmx; \ |
| 128 |
|
} while (0) |
| 129 |
|
|
| 130 |
|
#define FIND_BE_PARAM(tmx, dst, be) \ |
| 131 |
|
FIND_BE_SPEC(tmx, dst, be, 0) |
| 132 |
|
|
| 133 |
|
#define FIND_BE_TMO(tmx, dst, be) \ |
| 134 |
|
FIND_BE_SPEC(tmx, dst, be, -1.0) |
| 135 |
|
|
| 136 |
|
#define BE_BUSY(be) \ |
| 137 |
|
(be->max_connections > 0 && be->n_conn >= be->max_connections) |
| 138 |
|
|
| 139 |
|
/*--------------------------------------------------------------------*/ |
| 140 |
|
|
| 141 |
|
static void |
| 142 |
2842 |
vbe_connwait_broadcast(const struct backend *bp) |
| 143 |
|
{ |
| 144 |
|
struct connwait *cw; |
| 145 |
|
|
| 146 |
2842 |
CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC); |
| 147 |
|
|
| 148 |
2842 |
Lck_Lock(bp->director->mtx); |
| 149 |
3202 |
VTAILQ_FOREACH(cw, &bp->cw_head, cw_list) { |
| 150 |
360 |
CHECK_OBJ(cw, CONNWAIT_MAGIC); |
| 151 |
360 |
assert(cw->cw_state == CW_QUEUED); |
| 152 |
360 |
PTOK(pthread_cond_signal(&cw->cw_cond)); |
| 153 |
360 |
} |
| 154 |
2842 |
Lck_Unlock(bp->director->mtx); |
| 155 |
2842 |
} |
| 156 |
|
|
| 157 |
|
static void |
| 158 |
89040 |
vbe_connwait_signal_locked(const struct backend *bp) |
| 159 |
|
{ |
| 160 |
|
struct connwait *cw; |
| 161 |
|
|
| 162 |
89040 |
Lck_AssertHeld(bp->director->mtx); |
| 163 |
|
|
| 164 |
89040 |
if (bp->n_conn < bp->max_connections) { |
| 165 |
440 |
cw = VTAILQ_FIRST(&bp->cw_head); |
| 166 |
440 |
if (cw != NULL) { |
| 167 |
40 |
CHECK_OBJ(cw, CONNWAIT_MAGIC); |
| 168 |
40 |
assert(cw->cw_state == CW_QUEUED); |
| 169 |
40 |
PTOK(pthread_cond_signal(&cw->cw_cond)); |
| 170 |
40 |
} |
| 171 |
440 |
} |
| 172 |
89040 |
} |
| 173 |
|
|
| 174 |
|
static void |
| 175 |
89598 |
vbe_connwait_fini(struct connwait *cw) |
| 176 |
|
{ |
| 177 |
89598 |
CHECK_OBJ_NOTNULL(cw, CONNWAIT_MAGIC); |
| 178 |
89598 |
assert(cw->cw_state != CW_QUEUED); |
| 179 |
89598 |
PTOK(pthread_cond_destroy(&cw->cw_cond)); |
| 180 |
89598 |
FINI_OBJ(cw); |
| 181 |
89598 |
} |
| 182 |
|
|
| 183 |
|
/*-------------------------------------------------------------------- |
| 184 |
|
* Get a connection to the backend |
| 185 |
|
* |
| 186 |
|
* note: wrk is a separate argument because it differs for pipe vs. fetch |
| 187 |
|
*/ |
| 188 |
|
|
| 189 |
|
static struct pfd * |
| 190 |
89800 |
vbe_dir_getfd(VRT_CTX, struct worker *wrk, VCL_BACKEND dir, struct backend *bp, |
| 191 |
|
unsigned force_fresh) |
| 192 |
|
{ |
| 193 |
|
struct busyobj *bo; |
| 194 |
|
struct pfd *pfd; |
| 195 |
|
int *fdp, err; |
| 196 |
|
vtim_dur tmod; |
| 197 |
|
char abuf1[VTCP_ADDRBUFSIZE], abuf2[VTCP_ADDRBUFSIZE]; |
| 198 |
|
char pbuf1[VTCP_PORTBUFSIZE], pbuf2[VTCP_PORTBUFSIZE]; |
| 199 |
|
unsigned wait_limit; |
| 200 |
|
vtim_dur wait_tmod; |
| 201 |
|
vtim_dur wait_end; |
| 202 |
|
struct connwait cw[1]; |
| 203 |
|
|
| 204 |
89800 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 205 |
89800 |
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC); |
| 206 |
89800 |
bo = ctx->bo; |
| 207 |
89800 |
CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC); |
| 208 |
89800 |
AN(bp->vsc); |
| 209 |
|
|
| 210 |
89800 |
if (!VRT_Healthy(ctx, dir, NULL)) { |
| 211 |
400 |
VSLb(bo->vsl, SLT_FetchError, |
| 212 |
200 |
"backend %s: unhealthy", VRT_BACKEND_string(dir)); |
| 213 |
200 |
bp->vsc->unhealthy++; |
| 214 |
200 |
VSC_C_main->backend_unhealthy++; |
| 215 |
200 |
return (NULL); |
| 216 |
|
} |
| 217 |
89600 |
INIT_OBJ(cw, CONNWAIT_MAGIC); |
| 218 |
89600 |
PTOK(pthread_cond_init(&cw->cw_cond, NULL)); |
| 219 |
89600 |
Lck_Lock(bp->director->mtx); |
| 220 |
89600 |
FIND_BE_PARAM(backend_wait_limit, wait_limit, bp); |
| 221 |
89600 |
FIND_BE_TMO(backend_wait_timeout, wait_tmod, bp); |
| 222 |
89600 |
cw->cw_state = CW_DO_CONNECT; |
| 223 |
89600 |
if (!VTAILQ_EMPTY(&bp->cw_head) || BE_BUSY(bp)) |
| 224 |
560 |
cw->cw_state = CW_BE_BUSY; |
| 225 |
|
|
| 226 |
90080 |
if (cw->cw_state == CW_BE_BUSY && wait_limit > 0 && |
| 227 |
480 |
wait_tmod > 0.0 && bp->cw_count < wait_limit) { |
| 228 |
480 |
VTAILQ_INSERT_TAIL(&bp->cw_head, cw, cw_list); |
| 229 |
480 |
bp->cw_count++; |
| 230 |
480 |
VSC_C_main->backend_wait++; |
| 231 |
480 |
cw->cw_state = CW_QUEUED; |
| 232 |
480 |
wait_end = VTIM_real() + wait_tmod; |
| 233 |
480 |
do { |
| 234 |
960 |
err = Lck_CondWaitUntil(&cw->cw_cond, bp->director->mtx, |
| 235 |
480 |
wait_end); |
| 236 |
480 |
} while (err == EINTR); |
| 237 |
480 |
assert(cw->cw_state == CW_QUEUED); |
| 238 |
480 |
VTAILQ_REMOVE(&bp->cw_head, cw, cw_list); |
| 239 |
480 |
cw->cw_state = CW_DEQUEUED; |
| 240 |
480 |
bp->cw_count--; |
| 241 |
480 |
if ((err != 0 && BE_BUSY(bp)) || !VRT_Healthy(ctx, dir, NULL)) { |
| 242 |
440 |
VSC_C_main->backend_wait_fail++; |
| 243 |
440 |
cw->cw_state = CW_BE_BUSY; |
| 244 |
440 |
} |
| 245 |
480 |
} |
| 246 |
89600 |
if (cw->cw_state != CW_BE_BUSY) |
| 247 |
89079 |
bp->n_conn++; |
| 248 |
|
|
| 249 |
89600 |
if (!VTAILQ_EMPTY(&bp->cw_head) && !BE_BUSY(bp)) { |
| 250 |
|
/* Signal the new head of the waiting queue */ |
| 251 |
0 |
vbe_connwait_signal_locked(bp); |
| 252 |
0 |
} |
| 253 |
|
|
| 254 |
89600 |
Lck_Unlock(bp->director->mtx); |
| 255 |
|
|
| 256 |
89600 |
if (cw->cw_state == CW_BE_BUSY) { |
| 257 |
1040 |
VSLb(bo->vsl, SLT_FetchError, |
| 258 |
520 |
"backend %s: busy", VRT_BACKEND_string(dir)); |
| 259 |
520 |
bp->vsc->busy++; |
| 260 |
520 |
VSC_C_main->backend_busy++; |
| 261 |
520 |
vbe_connwait_fini(cw); |
| 262 |
520 |
return (NULL); |
| 263 |
|
} |
| 264 |
|
|
| 265 |
89080 |
AZ(bo->htc); |
| 266 |
89080 |
bo->htc = WS_Alloc(bo->ws, sizeof *bo->htc); |
| 267 |
|
/* XXX: we may want to detect the ws overflow sooner */ |
| 268 |
89080 |
if (bo->htc == NULL) { |
| 269 |
1240 |
VSLb(bo->vsl, SLT_FetchError, "out of workspace"); |
| 270 |
|
/* XXX: counter ? */ |
| 271 |
1240 |
Lck_Lock(bp->director->mtx); |
| 272 |
1240 |
bp->n_conn--; |
| 273 |
1240 |
vbe_connwait_signal_locked(bp); |
| 274 |
1240 |
Lck_Unlock(bp->director->mtx); |
| 275 |
1240 |
vbe_connwait_fini(cw); |
| 276 |
1240 |
return (NULL); |
| 277 |
|
} |
| 278 |
87840 |
bo->htc->doclose = SC_NULL; |
| 279 |
87840 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 280 |
|
|
| 281 |
87840 |
FIND_TMO(connect_timeout, tmod, bo, bp); |
| 282 |
87840 |
pfd = VCP_Get(bp->conn_pool, tmod, wrk, force_fresh, &err); |
| 283 |
87840 |
if (pfd == NULL) { |
| 284 |
1240 |
Lck_Lock(bp->director->mtx); |
| 285 |
1240 |
VBE_Connect_Error(bp->vsc, err); |
| 286 |
1240 |
bp->n_conn--; |
| 287 |
1240 |
vbe_connwait_signal_locked(bp); |
| 288 |
1240 |
Lck_Unlock(bp->director->mtx); |
| 289 |
2480 |
VSLb(bo->vsl, SLT_FetchError, |
| 290 |
|
"backend %s: fail errno %d (%s)", |
| 291 |
1240 |
VRT_BACKEND_string(dir), err, VAS_errtxt(err)); |
| 292 |
1240 |
VSC_C_main->backend_fail++; |
| 293 |
1240 |
bo->htc = NULL; |
| 294 |
1240 |
vbe_connwait_fini(cw); |
| 295 |
1240 |
return (NULL); |
| 296 |
|
} |
| 297 |
|
|
| 298 |
86600 |
VSLb_ts_busyobj(bo, "Connected", W_TIM_real(wrk)); |
| 299 |
86600 |
fdp = PFD_Fd(pfd); |
| 300 |
86600 |
AN(fdp); |
| 301 |
86600 |
assert(*fdp >= 0); |
| 302 |
|
|
| 303 |
86600 |
Lck_Lock(bp->director->mtx); |
| 304 |
86600 |
bp->vsc->conn++; |
| 305 |
86600 |
bp->vsc->req++; |
| 306 |
86600 |
Lck_Unlock(bp->director->mtx); |
| 307 |
|
|
| 308 |
86600 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 309 |
|
|
| 310 |
86600 |
err = 0; |
| 311 |
86600 |
if (bp->proxy_header != 0) |
| 312 |
200 |
err += VPX_Send_Proxy(*fdp, bp->proxy_header, bo->sp); |
| 313 |
86600 |
if (err < 0) { |
| 314 |
0 |
VSLb(bo->vsl, SLT_FetchError, |
| 315 |
|
"backend %s: proxy write errno %d (%s)", |
| 316 |
0 |
VRT_BACKEND_string(dir), |
| 317 |
0 |
errno, VAS_errtxt(errno)); |
| 318 |
|
// account as if connect failed - good idea? |
| 319 |
0 |
VSC_C_main->backend_fail++; |
| 320 |
0 |
bo->htc = NULL; |
| 321 |
0 |
VCP_Close(&pfd); |
| 322 |
0 |
AZ(pfd); |
| 323 |
0 |
Lck_Lock(bp->director->mtx); |
| 324 |
0 |
bp->n_conn--; |
| 325 |
0 |
bp->vsc->conn--; |
| 326 |
0 |
bp->vsc->req--; |
| 327 |
0 |
vbe_connwait_signal_locked(bp); |
| 328 |
0 |
Lck_Unlock(bp->director->mtx); |
| 329 |
0 |
vbe_connwait_fini(cw); |
| 330 |
0 |
return (NULL); |
| 331 |
|
} |
| 332 |
86600 |
bo->acct.bereq_hdrbytes += err; |
| 333 |
|
|
| 334 |
86600 |
PFD_LocalName(pfd, abuf1, sizeof abuf1, pbuf1, sizeof pbuf1); |
| 335 |
86600 |
PFD_RemoteName(pfd, abuf2, sizeof abuf2, pbuf2, sizeof pbuf2); |
| 336 |
173200 |
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s %s", |
| 337 |
86600 |
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1, |
| 338 |
86600 |
PFD_State(pfd) == PFD_STATE_STOLEN ? "reuse" : "connect"); |
| 339 |
|
|
| 340 |
86600 |
INIT_OBJ(bo->htc, HTTP_CONN_MAGIC); |
| 341 |
86600 |
bo->htc->priv = pfd; |
| 342 |
86600 |
bo->htc->rfd = fdp; |
| 343 |
86600 |
bo->htc->doclose = SC_NULL; |
| 344 |
86600 |
FIND_TMO(first_byte_timeout, |
| 345 |
|
bo->htc->first_byte_timeout, bo, bp); |
| 346 |
86600 |
FIND_TMO(between_bytes_timeout, |
| 347 |
|
bo->htc->between_bytes_timeout, bo, bp); |
| 348 |
86600 |
vbe_connwait_fini(cw); |
| 349 |
86600 |
return (pfd); |
| 350 |
89800 |
} |
| 351 |
|
|
| 352 |
|
static void v_matchproto_(vdi_finish_f) |
| 353 |
86556 |
vbe_dir_finish(VRT_CTX, VCL_BACKEND d) |
| 354 |
|
{ |
| 355 |
|
struct backend *bp; |
| 356 |
|
struct busyobj *bo; |
| 357 |
|
struct pfd *pfd; |
| 358 |
|
|
| 359 |
86556 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 360 |
86556 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 361 |
86556 |
bo = ctx->bo; |
| 362 |
86556 |
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); |
| 363 |
86556 |
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC); |
| 364 |
|
|
| 365 |
86556 |
CHECK_OBJ_NOTNULL(bo->htc, HTTP_CONN_MAGIC); |
| 366 |
86556 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 367 |
|
|
| 368 |
86556 |
pfd = bo->htc->priv; |
| 369 |
86556 |
bo->htc->priv = NULL; |
| 370 |
86556 |
if (bo->htc->doclose != SC_NULL || bp->proxy_header != 0) { |
| 371 |
31286 |
VSLb(bo->vsl, SLT_BackendClose, "%d %s close %s", *PFD_Fd(pfd), |
| 372 |
15643 |
VRT_BACKEND_string(d), bo->htc->doclose->name); |
| 373 |
15643 |
VCP_Close(&pfd); |
| 374 |
15643 |
AZ(pfd); |
| 375 |
15643 |
Lck_Lock(bp->director->mtx); |
| 376 |
15643 |
} else { |
| 377 |
70913 |
assert (PFD_State(pfd) == PFD_STATE_USED); |
| 378 |
141826 |
VSLb(bo->vsl, SLT_BackendClose, "%d %s recycle", *PFD_Fd(pfd), |
| 379 |
70913 |
VRT_BACKEND_string(d)); |
| 380 |
70913 |
Lck_Lock(bp->director->mtx); |
| 381 |
70913 |
VSC_C_main->backend_recycle++; |
| 382 |
70913 |
VCP_Recycle(bo->wrk, &pfd); |
| 383 |
|
} |
| 384 |
86556 |
assert(bp->n_conn > 0); |
| 385 |
86556 |
bp->n_conn--; |
| 386 |
86556 |
AN(bp->vsc); |
| 387 |
86556 |
bp->vsc->conn--; |
| 388 |
|
#define ACCT(foo) bp->vsc->foo += bo->acct.foo; |
| 389 |
|
#include "tbl/acct_fields_bereq.h" |
| 390 |
|
vbe_connwait_signal_locked(bp); |
| 391 |
|
Lck_Unlock(bp->director->mtx); |
| 392 |
|
bo->htc = NULL; |
| 393 |
|
} |
| 394 |
|
|
| 395 |
|
static int v_matchproto_(vdi_gethdrs_f) |
| 396 |
88638 |
vbe_dir_gethdrs(VRT_CTX, VCL_BACKEND d) |
| 397 |
|
{ |
| 398 |
88638 |
int i, extrachance = 1; |
| 399 |
|
struct backend *bp; |
| 400 |
|
struct pfd *pfd; |
| 401 |
|
struct busyobj *bo; |
| 402 |
|
struct worker *wrk; |
| 403 |
|
|
| 404 |
88638 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 405 |
88638 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 406 |
88638 |
bo = ctx->bo; |
| 407 |
88638 |
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); |
| 408 |
88638 |
CHECK_OBJ_NOTNULL(bo->bereq, HTTP_MAGIC); |
| 409 |
88638 |
if (bo->htc != NULL) |
| 410 |
0 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 411 |
88638 |
wrk = ctx->bo->wrk; |
| 412 |
88638 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 413 |
88638 |
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC); |
| 414 |
|
|
| 415 |
|
/* |
| 416 |
|
* Now that we know our backend, we can set a default Host: |
| 417 |
|
* header if one is necessary. This cannot be done in the VCL |
| 418 |
|
* because the backend may be chosen by a director. |
| 419 |
|
*/ |
| 420 |
88638 |
if (!http_GetHdr(bo->bereq, H_Host, NULL) && bp->hosthdr != NULL) |
| 421 |
2720 |
http_PrintfHeader(bo->bereq, "Host: %s", bp->hosthdr); |
| 422 |
|
|
| 423 |
88638 |
do { |
| 424 |
88842 |
if (bo->htc != NULL) |
| 425 |
0 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 426 |
88842 |
pfd = vbe_dir_getfd(ctx, wrk, d, bp, extrachance == 0 ? 1 : 0); |
| 427 |
88842 |
if (pfd == NULL) |
| 428 |
3160 |
return (-1); |
| 429 |
85682 |
AN(bo->htc); |
| 430 |
85682 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 431 |
85682 |
if (PFD_State(pfd) != PFD_STATE_STOLEN) |
| 432 |
53404 |
extrachance = 0; |
| 433 |
|
|
| 434 |
171364 |
i = V1F_SendReq(wrk, bo, &bo->acct.bereq_hdrbytes, |
| 435 |
85682 |
&bo->acct.bereq_bodybytes); |
| 436 |
|
|
| 437 |
85682 |
if (i == 0 && PFD_State(pfd) != PFD_STATE_USED) { |
| 438 |
96705 |
if (VCP_Wait(wrk, pfd, VTIM_real() + |
| 439 |
64470 |
bo->htc->first_byte_timeout) != 0) { |
| 440 |
40 |
bo->htc->doclose = SC_RX_TIMEOUT; |
| 441 |
40 |
VSLb(bo->vsl, SLT_FetchError, |
| 442 |
|
"first byte timeout (reused connection)"); |
| 443 |
40 |
extrachance = 0; |
| 444 |
40 |
} |
| 445 |
32235 |
} |
| 446 |
|
|
| 447 |
85682 |
if (bo->htc->doclose == SC_NULL) { |
| 448 |
84322 |
assert(PFD_State(pfd) == PFD_STATE_USED); |
| 449 |
84322 |
if (i == 0) |
| 450 |
84318 |
i = V1F_FetchRespHdr(bo); |
| 451 |
84322 |
if (i == 0) { |
| 452 |
81278 |
AN(bo->htc->priv); |
| 453 |
81278 |
http_VSL_log(bo->beresp); |
| 454 |
81278 |
return (0); |
| 455 |
|
} |
| 456 |
3040 |
} |
| 457 |
4400 |
CHECK_OBJ_NOTNULL(bo->htc->doclose, STREAM_CLOSE_MAGIC); |
| 458 |
|
|
| 459 |
|
/* |
| 460 |
|
* If we recycled a backend connection, there is a finite chance |
| 461 |
|
* that the backend closed it before we got the bereq to it. |
| 462 |
|
* In that case do a single automatic retry if req.body allows. |
| 463 |
|
*/ |
| 464 |
4400 |
vbe_dir_finish(ctx, d); |
| 465 |
4400 |
AZ(bo->htc); |
| 466 |
4400 |
if (i < 0 || extrachance == 0) |
| 467 |
4200 |
break; |
| 468 |
200 |
if (bo->no_retry != NULL) |
| 469 |
0 |
break; |
| 470 |
200 |
VSC_C_main->backend_retry++; |
| 471 |
200 |
} while (extrachance--); |
| 472 |
4204 |
return (-1); |
| 473 |
88642 |
} |
| 474 |
|
|
| 475 |
|
static VCL_IP v_matchproto_(vdi_getip_f) |
| 476 |
40 |
vbe_dir_getip(VRT_CTX, VCL_BACKEND d) |
| 477 |
|
{ |
| 478 |
|
struct pfd *pfd; |
| 479 |
|
|
| 480 |
40 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 481 |
40 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 482 |
40 |
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC); |
| 483 |
40 |
CHECK_OBJ_NOTNULL(ctx->bo->htc, HTTP_CONN_MAGIC); |
| 484 |
40 |
pfd = ctx->bo->htc->priv; |
| 485 |
|
|
| 486 |
40 |
return (VCP_GetIp(pfd)); |
| 487 |
|
} |
| 488 |
|
|
| 489 |
|
/*--------------------------------------------------------------------*/ |
| 490 |
|
|
| 491 |
|
static stream_close_t v_matchproto_(vdi_http1pipe_f) |
| 492 |
960 |
vbe_dir_http1pipe(VRT_CTX, VCL_BACKEND d) |
| 493 |
|
{ |
| 494 |
|
int i; |
| 495 |
|
stream_close_t retval; |
| 496 |
|
struct backend *bp; |
| 497 |
|
struct v1p_acct v1a; |
| 498 |
|
struct pfd *pfd; |
| 499 |
|
vtim_real deadline; |
| 500 |
|
|
| 501 |
960 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 502 |
960 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 503 |
960 |
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); |
| 504 |
960 |
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC); |
| 505 |
960 |
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC); |
| 506 |
|
|
| 507 |
960 |
memset(&v1a, 0, sizeof v1a); |
| 508 |
|
|
| 509 |
|
/* This is hackish... */ |
| 510 |
960 |
v1a.req = ctx->req->acct.req_hdrbytes; |
| 511 |
960 |
ctx->req->acct.req_hdrbytes = 0; |
| 512 |
|
|
| 513 |
960 |
ctx->req->res_pipe = 1; |
| 514 |
|
|
| 515 |
960 |
retval = SC_TX_ERROR; |
| 516 |
960 |
pfd = vbe_dir_getfd(ctx, ctx->req->wrk, d, bp, 0); |
| 517 |
|
|
| 518 |
960 |
if (pfd != NULL) { |
| 519 |
920 |
CHECK_OBJ_NOTNULL(ctx->bo->htc, HTTP_CONN_MAGIC); |
| 520 |
1840 |
i = V1F_SendReq(ctx->req->wrk, ctx->bo, |
| 521 |
920 |
&v1a.bereq, &v1a.out); |
| 522 |
920 |
VSLb_ts_req(ctx->req, "Pipe", W_TIM_real(ctx->req->wrk)); |
| 523 |
920 |
if (i == 0) { |
| 524 |
920 |
deadline = ctx->bo->task_deadline; |
| 525 |
920 |
if (isnan(deadline)) |
| 526 |
880 |
deadline = cache_param->pipe_task_deadline; |
| 527 |
920 |
if (deadline > 0.) |
| 528 |
120 |
deadline += ctx->req->sp->t_idle; |
| 529 |
1840 |
retval = V1P_Process(ctx->req, *PFD_Fd(pfd), &v1a, |
| 530 |
920 |
deadline); |
| 531 |
920 |
} |
| 532 |
920 |
VSLb_ts_req(ctx->req, "PipeSess", W_TIM_real(ctx->req->wrk)); |
| 533 |
920 |
ctx->bo->htc->doclose = retval; |
| 534 |
920 |
vbe_dir_finish(ctx, d); |
| 535 |
920 |
} |
| 536 |
960 |
V1P_Charge(ctx->req, &v1a, bp->vsc); |
| 537 |
960 |
CHECK_OBJ_NOTNULL(retval, STREAM_CLOSE_MAGIC); |
| 538 |
960 |
return (retval); |
| 539 |
|
} |
| 540 |
|
|
| 541 |
|
/*--------------------------------------------------------------------*/ |
| 542 |
|
|
| 543 |
|
static void |
| 544 |
62603 |
vbe_dir_event(const struct director *d, enum vcl_event_e ev) |
| 545 |
|
{ |
| 546 |
|
struct backend *bp; |
| 547 |
|
|
| 548 |
62603 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 549 |
62603 |
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC); |
| 550 |
|
|
| 551 |
62603 |
if (ev == VCL_EVENT_WARM) { |
| 552 |
53400 |
VRT_VSC_Reveal(bp->vsc_seg); |
| 553 |
53400 |
if (bp->probe != NULL) |
| 554 |
1480 |
VBP_Control(bp, 1); |
| 555 |
62603 |
} else if (ev == VCL_EVENT_COLD) { |
| 556 |
3300 |
if (bp->probe != NULL) |
| 557 |
280 |
VBP_Control(bp, 0); |
| 558 |
3300 |
VRT_VSC_Hide(bp->vsc_seg); |
| 559 |
9203 |
} else if (ev == VCL_EVENT_DISCARD) { |
| 560 |
2661 |
VRT_DelDirector(&bp->director); |
| 561 |
5903 |
} else if (ev == VDI_EVENT_SICK) { |
| 562 |
3242 |
const struct vdi_ahealth *ah = d->vdir->admin_health; |
| 563 |
|
|
| 564 |
3242 |
if (ah == VDI_AH_SICK || (ah == VDI_AH_AUTO && bp->sick)) |
| 565 |
2842 |
vbe_connwait_broadcast(bp); |
| 566 |
3242 |
} |
| 567 |
62603 |
} |
| 568 |
|
|
| 569 |
|
/*---------------------------------------------------------------------*/ |
| 570 |
|
|
| 571 |
|
static void |
| 572 |
3221 |
vbe_free(struct backend *be) |
| 573 |
|
{ |
| 574 |
|
|
| 575 |
3221 |
CHECK_OBJ_NOTNULL(be, BACKEND_MAGIC); |
| 576 |
|
|
| 577 |
3221 |
if (be->probe != NULL) |
| 578 |
240 |
VBP_Remove(be); |
| 579 |
|
|
| 580 |
3221 |
VSC_vbe_Destroy(&be->vsc_seg); |
| 581 |
3221 |
Lck_Lock(&backends_mtx); |
| 582 |
3221 |
VSC_C_main->n_backend--; |
| 583 |
3221 |
Lck_Unlock(&backends_mtx); |
| 584 |
3221 |
VCP_Rel(&be->conn_pool); |
| 585 |
|
|
| 586 |
|
#define DA(x) do { if (be->x != NULL) free(be->x); } while (0) |
| 587 |
|
#define DN(x) /**/ |
| 588 |
3221 |
VRT_BACKEND_HANDLE(); |
| 589 |
|
#undef DA |
| 590 |
|
#undef DN |
| 591 |
3221 |
free(be->endpoint); |
| 592 |
|
|
| 593 |
3221 |
assert(VTAILQ_EMPTY(&be->cw_head)); |
| 594 |
3221 |
FREE_OBJ(be); |
| 595 |
3221 |
} |
| 596 |
|
|
| 597 |
|
static void v_matchproto_(vdi_destroy_f) |
| 598 |
3221 |
vbe_destroy(const struct director *d) |
| 599 |
|
{ |
| 600 |
|
struct backend *be; |
| 601 |
|
|
| 602 |
3221 |
CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC); |
| 603 |
3221 |
vbe_free(be); |
| 604 |
3221 |
} |
| 605 |
|
|
| 606 |
|
/*--------------------------------------------------------------------*/ |
| 607 |
|
|
| 608 |
|
static void |
| 609 |
240 |
vbe_panic(const struct director *d, struct vsb *vsb) |
| 610 |
|
{ |
| 611 |
|
struct backend *bp; |
| 612 |
|
|
| 613 |
240 |
PAN_CheckMagic(vsb, d, DIRECTOR_MAGIC); |
| 614 |
240 |
bp = d->priv; |
| 615 |
240 |
PAN_CheckMagic(vsb, bp, BACKEND_MAGIC); |
| 616 |
|
|
| 617 |
240 |
VCP_Panic(vsb, bp->conn_pool); |
| 618 |
240 |
VSB_printf(vsb, "hosthdr = %s,\n", bp->hosthdr); |
| 619 |
240 |
VSB_printf(vsb, "n_conn = %u,\n", bp->n_conn); |
| 620 |
240 |
} |
| 621 |
|
|
| 622 |
|
/*-------------------------------------------------------------------- |
| 623 |
|
*/ |
| 624 |
|
|
| 625 |
|
static void v_matchproto_(vdi_list_f) |
| 626 |
46480 |
vbe_list(VRT_CTX, const struct director *d, struct vsb *vsb, int pflag, |
| 627 |
|
int jflag) |
| 628 |
|
{ |
| 629 |
|
char buf[VTCP_ADDRBUFSIZE]; |
| 630 |
|
struct backend *bp; |
| 631 |
|
struct vrt_endpoint *vep; |
| 632 |
|
|
| 633 |
46480 |
(void)ctx; |
| 634 |
|
|
| 635 |
46480 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 636 |
46480 |
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC); |
| 637 |
46480 |
CHECK_OBJ_NOTNULL(bp->endpoint, VRT_ENDPOINT_MAGIC); |
| 638 |
|
|
| 639 |
46480 |
vep = bp->endpoint; |
| 640 |
|
|
| 641 |
46480 |
if (bp->probe != NULL) |
| 642 |
3120 |
VBP_Status(vsb, bp, pflag, jflag); |
| 643 |
43360 |
else if (jflag && pflag) |
| 644 |
120 |
VSB_cat(vsb, "{},\n"); |
| 645 |
43240 |
else if (jflag) |
| 646 |
440 |
VSB_cat(vsb, "[0, 0, \"healthy\"]"); |
| 647 |
42800 |
else if (pflag) |
| 648 |
360 |
return; |
| 649 |
|
else |
| 650 |
42440 |
VSB_cat(vsb, "0/0\thealthy"); |
| 651 |
|
|
| 652 |
46120 |
if (jflag && pflag) { |
| 653 |
200 |
if (vep->ipv4 != NULL) { |
| 654 |
200 |
VTCP_name(vep->ipv4, buf, sizeof buf, NULL, 0); |
| 655 |
200 |
VSB_printf(vsb, "\"ipv4\": \"%s\",\n", buf); |
| 656 |
200 |
} |
| 657 |
200 |
if (vep->ipv6 != NULL) { |
| 658 |
0 |
VTCP_name(vep->ipv6, buf, sizeof buf, NULL, 0); |
| 659 |
0 |
VSB_printf(vsb, "\"ipv6\": \"%s\",\n", buf); |
| 660 |
0 |
} |
| 661 |
200 |
} |
| 662 |
46480 |
} |
| 663 |
|
|
| 664 |
|
/*-------------------------------------------------------------------- |
| 665 |
|
*/ |
| 666 |
|
|
| 667 |
|
static VCL_BOOL v_matchproto_(vdi_healthy_f) |
| 668 |
2679 |
vbe_healthy(VRT_CTX, VCL_BACKEND d, VCL_TIME *t) |
| 669 |
|
{ |
| 670 |
|
struct backend *bp; |
| 671 |
|
|
| 672 |
2679 |
(void)ctx; |
| 673 |
2679 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 674 |
2679 |
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC); |
| 675 |
|
|
| 676 |
2679 |
if (t != NULL) |
| 677 |
1680 |
*t = bp->changed; |
| 678 |
|
|
| 679 |
2679 |
return (!bp->sick); |
| 680 |
|
} |
| 681 |
|
|
| 682 |
|
/*-------------------------------------------------------------------- |
| 683 |
|
*/ |
| 684 |
|
|
| 685 |
|
static const struct vdi_methods vbe_methods[1] = {{ |
| 686 |
|
.magic = VDI_METHODS_MAGIC, |
| 687 |
|
.type = "backend", |
| 688 |
|
.http1pipe = vbe_dir_http1pipe, |
| 689 |
|
.gethdrs = vbe_dir_gethdrs, |
| 690 |
|
.getip = vbe_dir_getip, |
| 691 |
|
.finish = vbe_dir_finish, |
| 692 |
|
.event = vbe_dir_event, |
| 693 |
|
.destroy = vbe_destroy, |
| 694 |
|
.panic = vbe_panic, |
| 695 |
|
.list = vbe_list, |
| 696 |
|
.healthy = vbe_healthy |
| 697 |
|
}}; |
| 698 |
|
|
| 699 |
|
static const struct vdi_methods vbe_methods_noprobe[1] = {{ |
| 700 |
|
.magic = VDI_METHODS_MAGIC, |
| 701 |
|
.type = "backend", |
| 702 |
|
.http1pipe = vbe_dir_http1pipe, |
| 703 |
|
.gethdrs = vbe_dir_gethdrs, |
| 704 |
|
.getip = vbe_dir_getip, |
| 705 |
|
.finish = vbe_dir_finish, |
| 706 |
|
.event = vbe_dir_event, |
| 707 |
|
.destroy = vbe_destroy, |
| 708 |
|
.panic = vbe_panic, |
| 709 |
|
.list = vbe_list |
| 710 |
|
}}; |
| 711 |
|
|
| 712 |
|
/*-------------------------------------------------------------------- |
| 713 |
|
* Create a new static or dynamic director::backend instance. |
| 714 |
|
*/ |
| 715 |
|
|
| 716 |
|
size_t |
| 717 |
49400 |
VRT_backend_vsm_need(VRT_CTX) |
| 718 |
|
{ |
| 719 |
49400 |
(void)ctx; |
| 720 |
49400 |
return (VRT_VSC_Overhead(VSC_vbe_size)); |
| 721 |
|
} |
| 722 |
|
|
| 723 |
|
/* |
| 724 |
|
* The new_backend via parameter is a VCL_BACKEND, but we need a (struct |
| 725 |
|
* backend) |
| 726 |
|
* |
| 727 |
|
* For now, we resolve it when creating the backend, which implies no redundancy |
| 728 |
|
* / load balancing across the via director if it is more than a simple backend. |
| 729 |
|
*/ |
| 730 |
|
|
| 731 |
|
static const struct backend * |
| 732 |
320 |
via_resolve(VRT_CTX, const struct vrt_endpoint *vep, VCL_BACKEND via) |
| 733 |
|
{ |
| 734 |
320 |
const struct backend *viabe = NULL; |
| 735 |
|
|
| 736 |
320 |
CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC); |
| 737 |
320 |
CHECK_OBJ_NOTNULL(via, DIRECTOR_MAGIC); |
| 738 |
|
|
| 739 |
320 |
if (vep->uds_path) { |
| 740 |
0 |
VRT_fail(ctx, "Via is only supported for IP addresses"); |
| 741 |
0 |
return (NULL); |
| 742 |
|
} |
| 743 |
|
|
| 744 |
320 |
via = VRT_DirectorResolve(ctx, via); |
| 745 |
|
|
| 746 |
320 |
if (via == NULL) { |
| 747 |
0 |
VRT_fail(ctx, "Via resolution failed"); |
| 748 |
0 |
return (NULL); |
| 749 |
|
} |
| 750 |
|
|
| 751 |
320 |
CHECK_OBJ(via, DIRECTOR_MAGIC); |
| 752 |
320 |
CHECK_OBJ_NOTNULL(via->vdir, VCLDIR_MAGIC); |
| 753 |
|
|
| 754 |
320 |
if (via->vdir->methods == vbe_methods || |
| 755 |
320 |
via->vdir->methods == vbe_methods_noprobe) |
| 756 |
320 |
CAST_OBJ_NOTNULL(viabe, via->priv, BACKEND_MAGIC); |
| 757 |
|
|
| 758 |
320 |
if (viabe == NULL) |
| 759 |
0 |
VRT_fail(ctx, "Via does not resolve to a backend"); |
| 760 |
|
|
| 761 |
320 |
return (viabe); |
| 762 |
320 |
} |
| 763 |
|
|
| 764 |
|
/* |
| 765 |
|
* construct a new endpoint identical to vep with sa in a proxy header |
| 766 |
|
*/ |
| 767 |
|
static struct vrt_endpoint * |
| 768 |
320 |
via_endpoint(const struct vrt_endpoint *vep, const struct suckaddr *sa, |
| 769 |
|
const char *auth) |
| 770 |
|
{ |
| 771 |
|
struct vsb *preamble; |
| 772 |
|
struct vrt_blob blob[1]; |
| 773 |
|
struct vrt_endpoint *nvep, *ret; |
| 774 |
|
const struct suckaddr *client_bogo; |
| 775 |
|
|
| 776 |
320 |
CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC); |
| 777 |
320 |
AN(sa); |
| 778 |
|
|
| 779 |
320 |
nvep = VRT_Endpoint_Clone(vep); |
| 780 |
320 |
CHECK_OBJ_NOTNULL(nvep, VRT_ENDPOINT_MAGIC); |
| 781 |
|
|
| 782 |
320 |
if (VSA_Get_Proto(sa) == AF_INET6) |
| 783 |
0 |
client_bogo = bogo_ip6; |
| 784 |
|
else |
| 785 |
320 |
client_bogo = bogo_ip; |
| 786 |
|
|
| 787 |
320 |
preamble = VSB_new_auto(); |
| 788 |
320 |
AN(preamble); |
| 789 |
320 |
VPX_Format_Proxy(preamble, 2, client_bogo, sa, auth); |
| 790 |
320 |
INIT_OBJ(blob, VRT_BLOB_MAGIC); |
| 791 |
320 |
blob->blob = VSB_data(preamble); |
| 792 |
320 |
blob->len = VSB_len(preamble); |
| 793 |
320 |
nvep->preamble = blob; |
| 794 |
320 |
ret = VRT_Endpoint_Clone(nvep); |
| 795 |
320 |
CHECK_OBJ_NOTNULL(ret, VRT_ENDPOINT_MAGIC); |
| 796 |
320 |
VSB_destroy(&preamble); |
| 797 |
320 |
FREE_OBJ(nvep); |
| 798 |
|
|
| 799 |
320 |
return (ret); |
| 800 |
|
} |
| 801 |
|
|
| 802 |
|
VCL_BACKEND |
| 803 |
53680 |
VRT_new_backend_clustered(VRT_CTX, struct vsmw_cluster *vc, |
| 804 |
|
const struct vrt_backend *vrt, VCL_BACKEND via) |
| 805 |
|
{ |
| 806 |
|
struct backend *be; |
| 807 |
|
struct vcl *vcl; |
| 808 |
|
const struct vrt_backend_probe *vbp; |
| 809 |
|
const struct vrt_endpoint *vep; |
| 810 |
|
const struct vdi_methods *m; |
| 811 |
53680 |
const struct suckaddr *sa = NULL; |
| 812 |
|
char abuf[VTCP_ADDRBUFSIZE]; |
| 813 |
53680 |
const struct backend *viabe = NULL; |
| 814 |
|
|
| 815 |
53680 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 816 |
53680 |
CHECK_OBJ_NOTNULL(vrt, VRT_BACKEND_MAGIC); |
| 817 |
53680 |
vep = vrt->endpoint; |
| 818 |
53680 |
CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC); |
| 819 |
53680 |
if (vep->uds_path == NULL) { |
| 820 |
51840 |
if (vep->ipv4 == NULL && vep->ipv6 == NULL) { |
| 821 |
0 |
VRT_fail(ctx, "%s: Illegal IP", __func__); |
| 822 |
0 |
return (NULL); |
| 823 |
|
} |
| 824 |
51840 |
} else { |
| 825 |
1840 |
assert(vep->ipv4== NULL && vep->ipv6== NULL); |
| 826 |
|
} |
| 827 |
|
|
| 828 |
53680 |
if (via != NULL) { |
| 829 |
320 |
viabe = via_resolve(ctx, vep, via); |
| 830 |
320 |
if (viabe == NULL) |
| 831 |
0 |
return (NULL); |
| 832 |
320 |
} |
| 833 |
|
|
| 834 |
53680 |
vcl = ctx->vcl; |
| 835 |
53680 |
AN(vcl); |
| 836 |
53680 |
AN(vrt->vcl_name); |
| 837 |
|
|
| 838 |
|
/* Create new backend */ |
| 839 |
53680 |
ALLOC_OBJ(be, BACKEND_MAGIC); |
| 840 |
53680 |
if (be == NULL) |
| 841 |
0 |
return (NULL); |
| 842 |
53680 |
VTAILQ_INIT(&be->cw_head); |
| 843 |
|
|
| 844 |
|
#define DA(x) do { if (vrt->x != NULL) REPLACE((be->x), (vrt->x)); } while (0) |
| 845 |
|
#define DN(x) do { be->x = vrt->x; } while (0) |
| 846 |
53680 |
VRT_BACKEND_HANDLE(); |
| 847 |
|
#undef DA |
| 848 |
|
#undef DN |
| 849 |
|
|
| 850 |
|
#define CPTMO(a, b, x) do { \ |
| 851 |
|
if ((a)->x < 0.0 || isnan((a)->x)) \ |
| 852 |
|
(a)->x = (b)->x; \ |
| 853 |
|
} while(0) |
| 854 |
|
|
| 855 |
53600 |
if (viabe != NULL) { |
| 856 |
320 |
CPTMO(be, viabe, connect_timeout); |
| 857 |
320 |
CPTMO(be, viabe, first_byte_timeout); |
| 858 |
320 |
CPTMO(be, viabe, between_bytes_timeout); |
| 859 |
320 |
} |
| 860 |
|
#undef CPTMO |
| 861 |
|
|
| 862 |
53600 |
if (viabe || be->hosthdr == NULL) { |
| 863 |
360 |
if (vrt->endpoint->uds_path != NULL) |
| 864 |
40 |
sa = bogo_ip; |
| 865 |
320 |
else if (cache_param->prefer_ipv6 && vep->ipv6 != NULL) |
| 866 |
0 |
sa = vep->ipv6; |
| 867 |
320 |
else if (vep->ipv4!= NULL) |
| 868 |
320 |
sa = vep->ipv4; |
| 869 |
|
else |
| 870 |
0 |
sa = vep->ipv6; |
| 871 |
360 |
if (be->hosthdr == NULL) { |
| 872 |
40 |
VTCP_name(sa, abuf, sizeof abuf, NULL, 0); |
| 873 |
40 |
REPLACE(be->hosthdr, abuf); |
| 874 |
40 |
} |
| 875 |
360 |
} |
| 876 |
|
|
| 877 |
107200 |
be->vsc = VSC_vbe_New(vc, &be->vsc_seg, |
| 878 |
53600 |
"%s.%s", VCL_Name(ctx->vcl), vrt->vcl_name); |
| 879 |
53600 |
AN(be->vsc); |
| 880 |
53600 |
if (! vcl->temp->is_warm) |
| 881 |
53080 |
VRT_VSC_Hide(be->vsc_seg); |
| 882 |
|
|
| 883 |
53600 |
if (viabe) |
| 884 |
560 |
vep = be->endpoint = via_endpoint(viabe->endpoint, sa, |
| 885 |
280 |
be->authority); |
| 886 |
|
else |
| 887 |
53320 |
vep = be->endpoint = VRT_Endpoint_Clone(vep); |
| 888 |
|
|
| 889 |
53600 |
AN(vep); |
| 890 |
53600 |
be->conn_pool = VCP_Ref(vep, vbe_proto_ident); |
| 891 |
53600 |
AN(be->conn_pool); |
| 892 |
|
|
| 893 |
53600 |
vbp = vrt->probe; |
| 894 |
53600 |
if (vbp == NULL) |
| 895 |
52640 |
vbp = VCL_DefaultProbe(vcl); |
| 896 |
|
|
| 897 |
53600 |
if (vbp != NULL) { |
| 898 |
1440 |
VBP_Insert(be, vbp, be->conn_pool); |
| 899 |
1440 |
m = vbe_methods; |
| 900 |
1440 |
} else { |
| 901 |
52160 |
be->sick = 0; |
| 902 |
52160 |
be->vsc->happy = UINT64_MAX; |
| 903 |
52160 |
m = vbe_methods_noprobe; |
| 904 |
|
} |
| 905 |
|
|
| 906 |
53600 |
Lck_Lock(&backends_mtx); |
| 907 |
53600 |
VSC_C_main->n_backend++; |
| 908 |
53600 |
Lck_Unlock(&backends_mtx); |
| 909 |
|
|
| 910 |
53600 |
be->director = VRT_AddDirector(ctx, m, be, "%s", vrt->vcl_name); |
| 911 |
|
|
| 912 |
53600 |
if (be->director == NULL) { |
| 913 |
0 |
vbe_free(be); |
| 914 |
0 |
return (NULL); |
| 915 |
|
} |
| 916 |
|
/* for cold VCL, update initial director state */ |
| 917 |
53600 |
if (be->probe != NULL) |
| 918 |
1480 |
VBP_Update_Backend(be->probe); |
| 919 |
53600 |
return (be->director); |
| 920 |
53600 |
} |
| 921 |
|
|
| 922 |
|
VCL_BACKEND |
| 923 |
1280 |
VRT_new_backend(VRT_CTX, const struct vrt_backend *vrt, VCL_BACKEND via) |
| 924 |
|
{ |
| 925 |
|
|
| 926 |
1280 |
CHECK_OBJ_NOTNULL(vrt, VRT_BACKEND_MAGIC); |
| 927 |
1280 |
CHECK_OBJ_NOTNULL(vrt->endpoint, VRT_ENDPOINT_MAGIC); |
| 928 |
1280 |
return (VRT_new_backend_clustered(ctx, NULL, vrt, via)); |
| 929 |
|
} |
| 930 |
|
|
| 931 |
|
/*-------------------------------------------------------------------- |
| 932 |
|
* Delete a dynamic director::backend instance. Undeleted dynamic and |
| 933 |
|
* static instances are GC'ed when the VCL is discarded (in cache_vcl.c) |
| 934 |
|
*/ |
| 935 |
|
|
| 936 |
|
void |
| 937 |
3141 |
VRT_delete_backend(VRT_CTX, VCL_BACKEND *dp) |
| 938 |
|
{ |
| 939 |
|
|
| 940 |
3141 |
(void)ctx; |
| 941 |
3141 |
CHECK_OBJ_NOTNULL(*dp, DIRECTOR_MAGIC); |
| 942 |
3141 |
VRT_DisableDirector(*dp); |
| 943 |
3141 |
VRT_Assign_Backend(dp, NULL); |
| 944 |
3141 |
} |
| 945 |
|
|
| 946 |
|
/*---------------------------------------------------------------------*/ |
| 947 |
|
|
| 948 |
|
void |
| 949 |
38034 |
VBE_InitCfg(void) |
| 950 |
|
{ |
| 951 |
|
|
| 952 |
38034 |
Lck_New(&backends_mtx, lck_vbe); |
| 953 |
38034 |
} |