Code Coverage: vinyl-cache/vmod/vmod_debug_transport_vai.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * Copyright 2024 UPLEX - Nils Goroll Systemoptimierung
4
 * All rights reserved.
5
 *
6
 * Authors: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *          Nils Goroll <slink@uplex.de>
8
 *
9
 * SPDX-License-Identifier: BSD-2-Clause
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include "config.h"
34
35
#include "cache/cache_int.h"
36
37
#include "cache/cache_filter.h"
38
#include "cache/cache_transport.h"
39
#include "http1/cache_http1.h"
40
41
#include "vtim.h"
42
43
#include "vmod_debug.h"
44
45
/* VDP hello: prepend a string to the start of the response
46
 */
47
48
#define HELLO "hello "
49
50
static int v_matchproto_(vdpio_init_f)
51
vdpio_hello_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity)
52 32
{
53
54 32
        (void)ctx;
55 32
        (void)priv;
56
57 32
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
58 32
        AN(vdc->clen);
59
60 32
        if (*vdc->clen < 0)
61 9
                return (capacity);
62
63 23
        *vdc->clen += vstrlen(HELLO);
64 23
        http_Unset(vdc->hp, H_Content_Length);
65 23
        http_PrintfHeader(vdc->hp, "Content-Length: %jd", *vdc->clen);
66 23
        return (capacity);
67 32
}
68
69
static int v_matchproto_(vdpio_lease_f)
70
vdpio_hello_lease(struct vdp_ctx *vdc, struct vdp_entry *this,
71
    struct vscarab *scarab)
72 32
{
73 32
        int r;
74
75 32
        VSCARAB_CHECK_NOTNULL(scarab);
76 32
        if (scarab->used == scarab->capacity)
77 0
                return (0);
78
        //lint -e{446} side effects in initializer - uh?
79 32
        VSCARAB_ADD_IOV_NORET(scarab, ((struct iovec)
80 32
            {.iov_base = TRUST_ME(HELLO), .iov_len = vstrlen(HELLO)}));
81 32
        r = vdpio_pull(vdc, this, scarab);
82
83 32
        (void) VDPIO_Close1(vdc, this);
84
85
        // return error from pull
86 32
        if (r < 0)
87 0
                r = 1;
88 32
        else
89 32
                r += 1;
90
91 32
        return (r);
92 32
}
93
94
static const struct vdp vdp_hello = {
95
        .name = "hello",
96
        .io_init = vdpio_hello_init,
97
        .io_lease = vdpio_hello_lease
98
};
99
100
/* VDP reluctant state */
101
102
struct vdp_reluctant_state {
103
        unsigned                magic;
104
#define VDPRS_MAGIC             0xeaf5bc82
105
        unsigned                been_reluctant;
106
        struct pool_task        task;
107
};
108
109
/* helper for VDP reluctant, similar to sml_ai_later*, but acting from the other
110
 * end of the API
111
 *
112
 * Using a task which sleeps is extraordinarily inefficient, but this
113
 * is vmod_debug after all
114
 */
115
116
static const vtim_dur reluctant_delay = 0.05;
117
118
static void
119
vdpio_reluctant_later_task(struct worker *wrk, void *priv)
120 128
{
121
122 128
        VTIM_sleep(reluctant_delay);
123 128
        VDPIO_Notify(wrk, priv);
124 128
}
125
126
static void
127
vdpio_reluctant_later(struct vdp_ctx *vdc, struct vdp_reluctant_state *vdprs)
128 128
{
129 128
        struct worker *wrk;
130
131 128
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
132 128
        wrk = vdc->wrk;
133 128
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
134 128
        CHECK_OBJ_NOTNULL(vdprs, VDPRS_MAGIC);
135 128
        vdprs->task.func = vdpio_reluctant_later_task;
136 128
        vdprs->task.priv = vdc;
137 128
        AZ(Pool_Task(wrk->pool, &vdprs->task, TASK_QUEUE_BO));
138 128
}
139
140
/* VDP relunctant: always needs to be asked twice and takes 50ms inbetween
141
 */
142
143
static int v_matchproto_(vdpio_init_f)
144
vdpio_reluctant_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity)
145 64
{
146 64
        struct vdp_reluctant_state *vdprs;
147
148 64
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
149 64
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
150 64
        AN(priv);
151
152 64
        WS_TASK_ALLOC_OBJ(ctx, vdprs, VDPRS_MAGIC);
153 64
        if (vdprs == NULL)
154 0
                return (-1);
155
156 64
        *priv = vdprs;
157 64
        return (capacity);
158 64
}
159
160
static int v_matchproto_(vdpio_lease_f)
161
vdpio_reluctant_lease(struct vdp_ctx *vdc, struct vdp_entry *this,
162
    struct vscarab *scarab)
163 256
{
164 256
        struct vdp_reluctant_state *vdprs;
165
166 256
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
167 256
        CHECK_OBJ_NOTNULL(this, VDP_ENTRY_MAGIC);
168 256
        CAST_OBJ_NOTNULL(vdprs, this->priv, VDPRS_MAGIC);
169
170 256
        if (vdprs->been_reluctant) {
171 128
                vdprs->been_reluctant = 0;
172 128
                return (vdpio_pull(vdc, this, scarab));
173 128
        }
174
175 128
        vdprs->been_reluctant = 1;
176 128
        vdpio_reluctant_later(vdc, vdprs);
177 128
        return (-EAGAIN);
178 256
}
179
180
static void v_matchproto_(vdpio_fini_f)
181
vdpio_reluctant_fini(struct vdp_ctx *vdc, void **priv)
182 64
{
183 64
        struct vdp_reluctant_state *vdprs;
184
185 64
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
186 64
        TAKE_OBJ_NOTNULL(vdprs, priv, VDPRS_MAGIC);
187
        // we need to make sure that no notifications come in after we return,
188
        // because the vai handle will get destroyed.
189
        //
190
        // This a is cheap and wrong (for real code) way to wait for any pending
191
        // notification task to complete - but after all this is vmod_debug
192 64
        VTIM_sleep(reluctant_delay * 10);
193 64
}
194
195
static const struct vdp vdp_reluctant = {
196
        .name = "reluctant",
197
        .io_init = vdpio_reluctant_init,
198
        .io_lease = vdpio_reluctant_lease,
199
        .io_fini = vdpio_reluctant_fini
200
};
201
202
203
static void
204
dbg_vai_error(struct req *req, struct v1l **v1lp, const char *msg)
205 0
{
206
207 0
        (void)req;
208 0
        (void)v1lp;
209 0
        (void)msg;
210 0
        INCOMPL();
211 0
}
212
213
static void dbg_vai_deliver_finish(struct req *req, struct v1l **v1lp, int err);
214
static void dbg_vai_deliverobj(struct worker *wrk, void *arg);
215
static void dbg_vai_lease(struct worker *wrk, void *arg);
216
217
static task_func_t *hack_http1_req = NULL;
218
219
// copied from cache_http_deliver.c, then split & modified
220
static enum vtr_deliver_e v_matchproto_(vtr_deliver_f)
221
dbg_vai_deliver(struct req *req, int sendbody)
222 40
{
223 40
        struct vrt_ctx ctx[1];
224 40
        struct v1l *v1l;
225 40
        int cap = 0;
226
227 40
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
228 40
        CHECK_OBJ_ORNULL(req->boc, BOC_MAGIC);
229 40
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
230
231 40
        req->doclose = http_EnsureConnection(req->resp, req->doclose);
232
233 40
        CHECK_OBJ_NOTNULL(req->wrk, WORKER_MAGIC);
234
235 40
        v1l = V1L_Open(req->ws, &req->sp->fd, req->vsl,
236 40
            req->t_prev + SESS_TMO(req->sp, send_timeout),
237 40
            cache_param->http1_iovs);
238
239 40
        if (v1l == NULL) {
240 0
                dbg_vai_error(req, &v1l, "Failure to init v1d "
241 0
                    "(workspace_thread overflow)");
242 0
                return (VTR_D_DONE);
243 0
        }
244
245
        // Do not roll back req->ws upon V1L_Close()
246 40
        V1L_NoRollback(v1l);
247
248 40
        while (sendbody) {
249 40
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
250 9
                        if (req->http->protover == 11) {
251 9
                                http_SetHeader(req->resp,
252 9
                                    "Transfer-Encoding: chunked");
253 9
                        } else {
254 0
                                req->doclose = SC_TX_EOF;
255 0
                        }
256 9
                }
257 40
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
258 40
                VCL_Req2Ctx(ctx, req);
259 40
                cap = VDPIO_Upgrade(ctx, req->vdc);
260 40
                if (cap <= 0) {
261 8
                        if (VDP_Push(ctx, req->vdc, req->ws, VDP_v1l, v1l)) {
262 0
                                dbg_vai_error(req, &v1l, "Failure to push v1d");
263 0
                                return (VTR_D_DONE);
264 0
                        }
265 8
                        break;
266 8
                }
267 32
                cap = VDPIO_Push(ctx, req->vdc, req->ws, &vdp_reluctant, NULL);
268 32
                if (cap < 1) {
269 0
                        dbg_vai_error(req, &v1l, "Failure to push reluctant");
270 0
                        return (VTR_D_DONE);
271 0
                }
272 32
                cap = VDPIO_Push(ctx, req->vdc, req->ws, &vdp_hello, NULL);
273 32
                if (cap < 1) {
274 0
                        dbg_vai_error(req, &v1l, "Failure to push hello");
275 0
                        return (VTR_D_DONE);
276 0
                }
277 32
                cap = VDPIO_Push(ctx, req->vdc, req->ws, &vdp_reluctant, NULL);
278 32
                if (cap < 1) {
279 0
                        dbg_vai_error(req, &v1l, "Failure to push reluctant");
280 0
                        return (VTR_D_DONE);
281 0
                }
282 32
                cap = VDPIO_Push(ctx, req->vdc, req->ws, VDP_v1l, v1l);
283 32
                if (cap < 1) {
284 0
                        dbg_vai_error(req, &v1l, "Failure to push v1d (vdpio)");
285 0
                        return (VTR_D_DONE);
286 0
                }
287 32
                break;
288 32
        }
289
290 40
        if (WS_Overflowed(req->ws)) {
291 0
                dbg_vai_error(req, &v1l, "workspace_client overflow");
292 0
                return (VTR_D_DONE);
293 0
        }
294
295 40
        if (WS_Overflowed(req->sp->ws)) {
296 0
                dbg_vai_error(req, &v1l, "workspace_session overflow");
297 0
                return (VTR_D_DONE);
298 0
        }
299
300 40
        if (WS_Overflowed(req->wrk->aws)) {
301 0
                dbg_vai_error(req, &v1l, "workspace_thread overflow");
302 0
                return (VTR_D_DONE);
303 0
        }
304
305 40
        req->acct.resp_hdrbytes += HTTP1_Write(v1l, req->resp, HTTP1_Resp);
306
307 40
        if (! sendbody) {
308 0
                dbg_vai_deliver_finish(req, &v1l, 0);
309 0
                return (VTR_D_DONE);
310 0
        }
311
312 40
        (void)V1L_Flush(v1l);
313
314 40
        if (hack_http1_req == NULL)
315 1
                hack_http1_req = req->task->func;
316 40
        AN(hack_http1_req);
317
318 40
        if (cap > 0) {
319 32
                VSLb(req->vsl, SLT_Debug, "w=%p scheduling dbg_vai_lease cap %d", req->wrk, cap);
320 32
                req->task->func = dbg_vai_lease;
321 32
        }
322 8
        else {
323 8
                VSLb(req->vsl, SLT_Debug, "w=%p scheduling dbg_vai_deliverobj", req->wrk);
324 8
                req->task->func = dbg_vai_deliverobj;
325 8
        }
326 40
        req->task->priv = req;
327
328 40
        req->wrk = NULL;
329 40
        req->vdc->wrk = NULL;
330 40
        req->transport_priv = v1l;
331
332 40
        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
333 40
        return (VTR_D_DISEMBARK);
334 40
}
335
336
static void v_matchproto_(task_func_t)
337
dbg_vai_deliverobj(struct worker *wrk, void *arg)
338 8
{
339 8
        struct req *req;
340 8
        struct v1l *v1l;
341 8
        const char *p;
342 8
        int err, chunked;
343
344 8
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
345 8
        CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC);
346 8
        v1l = req->transport_priv;
347 8
        req->transport_priv = NULL;
348 8
        AN(v1l);
349
350 8
        THR_SetRequest(req);
351 8
        VSLb(req->vsl, SLT_Debug, "w=%p enter dbg_vai_deliverobj", wrk);
352 8
        AZ(req->wrk);
353 8
        CNT_Embark(wrk, req);
354 8
        req->vdc->wrk = wrk;    // move to CNT_Embark?
355
356 8
        chunked = http_GetHdr(req->resp, H_Transfer_Encoding, &p) &&
357 8
            vstrcmp(p, "chunked") == 0;
358 8
        if (chunked)
359 0
                V1L_Chunked(v1l);
360 8
        err = VDP_DeliverObj(req->vdc, req->objcore);
361 8
        if (!err && chunked)
362 0
                V1L_EndChunk(v1l);
363 8
        dbg_vai_deliver_finish(req, &v1l, err);
364
365 8
        VSLb(req->vsl, SLT_Debug, "w=%p resuming http1_req", wrk);
366 8
        wrk->task->func = hack_http1_req;
367 8
        wrk->task->priv = req;
368 8
}
369
370
/*
371
 * copied from sml_notfiy
372
 */
373
struct dbg_vai_notify {
374
        unsigned                magic;
375
#define DBG_VAI_NOTIFY_MAGIC    0xa0154ed5
376
        unsigned                hasmore;
377
        pthread_mutex_t         mtx;
378
        pthread_cond_t          cond;
379
};
380
381
static void
382
dbg_vai_notify_init(struct dbg_vai_notify *sn)
383 32
{
384
385 32
        INIT_OBJ(sn, DBG_VAI_NOTIFY_MAGIC);
386 32
        PTOK(pthread_mutex_init(&sn->mtx, NULL));
387 32
        PTOK(pthread_cond_init(&sn->cond, NULL));
388 32
}
389
390
static void
391
dbg_vai_notify_fini(struct dbg_vai_notify *sn)
392 32
{
393
394 32
        CHECK_OBJ_NOTNULL(sn, DBG_VAI_NOTIFY_MAGIC);
395 32
        PTOK(pthread_mutex_destroy(&sn->mtx));
396 32
        PTOK(pthread_cond_destroy(&sn->cond));
397 32
}
398
399
static void v_matchproto_(vai_notify_cb)
400
dbg_vai_notify(vai_hdl hdl, void *priv)
401 128
{
402 128
        struct dbg_vai_notify *sn;
403
404 128
        (void) hdl;
405 128
        CAST_OBJ_NOTNULL(sn, priv, DBG_VAI_NOTIFY_MAGIC);
406 128
        PTOK(pthread_mutex_lock(&sn->mtx));
407 128
        sn->hasmore = 1;
408 128
        PTOK(pthread_cond_signal(&sn->cond));
409 128
        PTOK(pthread_mutex_unlock(&sn->mtx));
410
411 128
}
412
413
static void
414
dbg_vai_notify_wait(struct dbg_vai_notify *sn)
415 128
{
416
417 128
        CHECK_OBJ_NOTNULL(sn, DBG_VAI_NOTIFY_MAGIC);
418 128
        PTOK(pthread_mutex_lock(&sn->mtx));
419 256
        while (sn->hasmore == 0)
420 128
                PTOK(pthread_cond_wait(&sn->cond, &sn->mtx));
421 128
        AN(sn->hasmore);
422 128
        sn->hasmore = 0;
423 128
        PTOK(pthread_mutex_unlock(&sn->mtx));
424 128
}
425
426
static void
427
dbg_vai_lease_done(struct worker *wrk, struct req *req)
428 32
{
429 32
        VSLb(req->vsl, SLT_Debug, "w=%p resuming http1_req", wrk);
430 32
        wrk->task->func = hack_http1_req;
431 32
        wrk->task->priv = req;
432 32
}
433
434
static void v_matchproto_(task_func_t)
435
dbg_vai_lease(struct worker *wrk, void *arg)
436 32
{
437 32
        struct req *req;
438 32
        struct v1l *v1l;
439 32
        const char *p;
440 32
        unsigned flags = 0;
441 32
        int r, cap, err, chunked;
442
443 32
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
444 32
        CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC);
445 32
        v1l = req->transport_priv;
446 32
        req->transport_priv = NULL;
447 32
        AN(v1l);
448
449 32
        THR_SetRequest(req);
450 32
        VSLb(req->vsl, SLT_Debug, "w=%p enter dbg_vai_lease", wrk);
451 32
        AZ(req->wrk);
452 32
        CNT_Embark(wrk, req);
453 32
        req->vdc->wrk = wrk;    // move to CNT_Embark?
454
455 32
        cap = req->vdc->retval;
456 32
        req->vdc->retval = 0;
457 32
        assert(cap > 0);
458
459 32
        VSCARAB_LOCAL(scarab, cap);
460 32
        VSCARET_LOCAL(scaret, cap);
461
462 32
        chunked = http_GetHdr(req->resp, H_Transfer_Encoding, &p) &&
463 32
            vstrcmp(p, "chunked") == 0;
464 32
        if (chunked)
465 9
                V1L_Chunked(v1l);
466
467 32
        struct dbg_vai_notify notify;
468 32
        dbg_vai_notify_init(&notify);
469
470 32
        if (VDPIO_Init(req->vdc, req->objcore, req->ws, dbg_vai_notify, &notify, scaret)) {
471 0
                dbg_vai_notify_fini(&notify);
472 0
                dbg_vai_deliver_finish(req, &v1l, 1);
473 0
                dbg_vai_lease_done(wrk, req);
474 0
                return;
475 0
        }
476
477 32
        err = 0;
478 192
        do {
479 192
                r = vdpio_pull(req->vdc, NULL, scarab);
480 192
                flags = scarab->flags; // because vdpio_return_vscarab
481 192
                VSLb(req->vsl, SLT_Debug, "%d = vdpio_pull()", r);
482 192
                (void)V1L_Flush(v1l);
483 192
                vdpio_return_vscarab(req->vdc, scarab);
484
485 192
                if (r == -ENOBUFS || r == -EAGAIN) {
486 128
                        VDPIO_Return(req->vdc);
487 128
                        dbg_vai_notify_wait(&notify);
488 128
                }
489 64
                else if (r < 0) {
490 0
                        err = r;
491 0
                        break;
492 0
                }
493 192
        } while ((flags & VSCARAB_F_END) == 0);
494
495 32
        if (!err && chunked)
496 9
                V1L_EndChunk(v1l);
497 32
        dbg_vai_deliver_finish(req, &v1l, err);
498 32
        VDPIO_Fini(req->vdc);
499 32
        dbg_vai_notify_fini(&notify);
500 32
        dbg_vai_lease_done(wrk, req);
501 32
}
502
503
static void
504
dbg_vai_deliver_finish(struct req *req, struct v1l **v1lp, int err)
505 40
{
506 40
        stream_close_t sc;
507 40
        uint64_t bytes;
508
509 40
        sc = V1L_Close(v1lp, &bytes);
510
511 40
        if (req->vdc->vai_hdl != NULL)
512 32
                req->acct.resp_bodybytes += VDPIO_Close(req->vdc, req->objcore, req->boc);
513 40
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc);
514
515 40
        if (sc == SC_NULL && err && req->sp->fd >= 0)
516 0
                sc = SC_REM_CLOSE;
517 40
        if (sc != SC_NULL)
518 0
                Req_Fail(req, sc);
519 40
}
520
521
static struct transport DBG_transport;
522
523
void
524
debug_transport_vai_init(void)
525 114
{
526 114
        DBG_transport = HTTP1_transport;
527 114
        DBG_transport.name = "DBG VAI";
528 114
        DBG_transport.deliver = dbg_vai_deliver;
529 114
}
530
531
void
532
debug_transport_vai_use(VRT_CTX)
533 40
{
534 40
        struct req *req;
535
536 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
537 40
        req = ctx->req;
538 40
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
539
540 40
        if (req->transport != &HTTP1_transport) {
541 0
                VRT_fail(ctx, "Only works on built-in http1 transport");
542 0
                return;
543 0
        }
544 40
        AZ(req->transport_priv);
545 40
        req->transport = &DBG_transport;
546 40
}