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