vinyl-cache/vmod/vmod_debug_transport_vai.c
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 96
vdpio_hello_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity)
52
{
53
54 96
        (void)ctx;
55 96
        (void)priv;
56
57 96
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
58 96
        AN(vdc->clen);
59
60 96
        if (*vdc->clen < 0)
61 27
                return (capacity);
62
63 69
        *vdc->clen += vstrlen(HELLO);
64 69
        http_Unset(vdc->hp, H_Content_Length);
65 69
        http_PrintfHeader(vdc->hp, "Content-Length: %jd", *vdc->clen);
66 69
        return (capacity);
67 96
}
68
69
static int v_matchproto_(vdpio_lease_f)
70 96
vdpio_hello_lease(struct vdp_ctx *vdc, struct vdp_entry *this,
71
    struct vscarab *scarab)
72
{
73
        int r;
74
75 96
        VSCARAB_CHECK_NOTNULL(scarab);
76 96
        if (scarab->used == scarab->capacity)
77 0
                return (0);
78
        //lint -e{446} side effects in initializer - uh?
79 96
        VSCARAB_ADD_IOV_NORET(scarab, ((struct iovec)
80
            {.iov_base = TRUST_ME(HELLO), .iov_len = vstrlen(HELLO)}));
81 96
        r = vdpio_pull(vdc, this, scarab);
82
83 96
        (void) VDPIO_Close1(vdc, this);
84
85
        // return error from pull
86 96
        if (r < 0)
87 0
                r = 1;
88
        else
89 96
                r += 1;
90
91 96
        return (r);
92 96
}
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 384
vdpio_reluctant_later_task(struct worker *wrk, void *priv)
120
{
121
122 384
        VTIM_sleep(reluctant_delay);
123 384
        VDPIO_Notify(wrk, priv);
124 384
}
125
126
static void
127 384
vdpio_reluctant_later(struct vdp_ctx *vdc, struct vdp_reluctant_state *vdprs)
128
{
129
        struct worker *wrk;
130
131 384
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
132 384
        wrk = vdc->wrk;
133 384
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
134 384
        CHECK_OBJ_NOTNULL(vdprs, VDPRS_MAGIC);
135 384
        vdprs->task.func = vdpio_reluctant_later_task;
136 384
        vdprs->task.priv = vdc;
137 384
        AZ(Pool_Task(wrk->pool, &vdprs->task, TASK_QUEUE_BO));
138 384
}
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 192
vdpio_reluctant_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity)
145
{
146
        struct vdp_reluctant_state *vdprs;
147
148 192
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
149 192
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
150 192
        AN(priv);
151
152 384
        WS_TASK_ALLOC_OBJ(ctx, vdprs, VDPRS_MAGIC);
153 192
        if (vdprs == NULL)
154 0
                return (-1);
155
156 192
        *priv = vdprs;
157 192
        return (capacity);
158 192
}
159
160
static int v_matchproto_(vdpio_lease_f)
161 767
vdpio_reluctant_lease(struct vdp_ctx *vdc, struct vdp_entry *this,
162
    struct vscarab *scarab)
163
{
164
        struct vdp_reluctant_state *vdprs;
165
166 767
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
167 767
        CHECK_OBJ_NOTNULL(this, VDP_ENTRY_MAGIC);
168 767
        CAST_OBJ_NOTNULL(vdprs, this->priv, VDPRS_MAGIC);
169
170 767
        if (vdprs->been_reluctant) {
171 383
                vdprs->been_reluctant = 0;
172 383
                return (vdpio_pull(vdc, this, scarab));
173
        }
174
175 384
        vdprs->been_reluctant = 1;
176 384
        vdpio_reluctant_later(vdc, vdprs);
177 384
        return (-EAGAIN);
178 767
}
179
180
static void v_matchproto_(vdpio_fini_f)
181 192
vdpio_reluctant_fini(struct vdp_ctx *vdc, void **priv)
182
{
183
        struct vdp_reluctant_state *vdprs;
184
185 192
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
186 192
        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 192
        VTIM_sleep(reluctant_delay * 10);
193 192
}
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 0
dbg_vai_error(struct req *req, struct v1l **v1lp, const char *msg)
205
{
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 120
dbg_vai_deliver(struct req *req, int sendbody)
222
{
223
        struct vrt_ctx ctx[1];
224
        struct v1l *v1l;
225 120
        int cap = 0;
226
227 120
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
228 120
        CHECK_OBJ_ORNULL(req->boc, BOC_MAGIC);
229 120
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
230
231 120
        if (req->doclose == SC_NULL &&
232 120
            http_HdrIs(req->resp, H_Connection, "close")) {
233 0
                req->doclose = SC_RESP_CLOSE;
234 120
        } else if (req->doclose != SC_NULL) {
235 0
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
236 0
                        http_Unset(req->resp, H_Connection);
237 0
                        http_SetHeader(req->resp, "Connection: close");
238 0
                }
239 120
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
240 120
                http_SetHeader(req->resp, "Connection: keep-alive");
241
242 120
        CHECK_OBJ_NOTNULL(req->wrk, WORKER_MAGIC);
243
244 240
        v1l = V1L_Open(req->ws, &req->sp->fd, req->vsl,
245 120
            req->t_prev + SESS_TMO(req->sp, send_timeout),
246 120
            cache_param->http1_iovs);
247
248 120
        if (v1l == NULL) {
249 0
                dbg_vai_error(req, &v1l, "Failure to init v1d "
250
                    "(workspace_thread overflow)");
251 0
                return (VTR_D_DONE);
252
        }
253
254
        // Do not roll back req->ws upon V1L_Close()
255 120
        V1L_NoRollback(v1l);
256
257 120
        while (sendbody) {
258 120
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
259 27
                        if (req->http->protover == 11) {
260 27
                                http_SetHeader(req->resp,
261
                                    "Transfer-Encoding: chunked");
262 27
                        } else {
263 0
                                req->doclose = SC_TX_EOF;
264
                        }
265 27
                }
266 120
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
267 120
                VCL_Req2Ctx(ctx, req);
268 120
                cap = VDPIO_Upgrade(ctx, req->vdc);
269 120
                if (cap <= 0) {
270 24
                        if (VDP_Push(ctx, req->vdc, req->ws, VDP_v1l, v1l)) {
271 0
                                dbg_vai_error(req, &v1l, "Failure to push v1d");
272 0
                                return (VTR_D_DONE);
273
                        }
274 24
                        break;
275
                }
276 96
                cap = VDPIO_Push(ctx, req->vdc, req->ws, &vdp_reluctant, NULL);
277 96
                if (cap < 1) {
278 0
                        dbg_vai_error(req, &v1l, "Failure to push reluctant");
279 0
                        return (VTR_D_DONE);
280
                }
281 96
                cap = VDPIO_Push(ctx, req->vdc, req->ws, &vdp_hello, NULL);
282 96
                if (cap < 1) {
283 0
                        dbg_vai_error(req, &v1l, "Failure to push hello");
284 0
                        return (VTR_D_DONE);
285
                }
286 96
                cap = VDPIO_Push(ctx, req->vdc, req->ws, &vdp_reluctant, NULL);
287 96
                if (cap < 1) {
288 0
                        dbg_vai_error(req, &v1l, "Failure to push reluctant");
289 0
                        return (VTR_D_DONE);
290
                }
291 96
                cap = VDPIO_Push(ctx, req->vdc, req->ws, VDP_v1l, v1l);
292 96
                if (cap < 1) {
293 0
                        dbg_vai_error(req, &v1l, "Failure to push v1d (vdpio)");
294 0
                        return (VTR_D_DONE);
295
                }
296 96
                break;
297
        }
298
299 120
        if (WS_Overflowed(req->ws)) {
300 0
                dbg_vai_error(req, &v1l, "workspace_client overflow");
301 0
                return (VTR_D_DONE);
302
        }
303
304 120
        if (WS_Overflowed(req->sp->ws)) {
305 0
                dbg_vai_error(req, &v1l, "workspace_session overflow");
306 0
                return (VTR_D_DONE);
307
        }
308
309 120
        if (WS_Overflowed(req->wrk->aws)) {
310 0
                dbg_vai_error(req, &v1l, "workspace_thread overflow");
311 0
                return (VTR_D_DONE);
312
        }
313
314 120
        req->acct.resp_hdrbytes += HTTP1_Write(v1l, req->resp, HTTP1_Resp);
315
316 120
        if (! sendbody) {
317 0
                dbg_vai_deliver_finish(req, &v1l, 0);
318 0
                return (VTR_D_DONE);
319
        }
320
321 120
        (void)V1L_Flush(v1l);
322
323 120
        if (hack_http1_req == NULL)
324 3
                hack_http1_req = req->task->func;
325 120
        AN(hack_http1_req);
326
327 120
        if (cap > 0) {
328 96
                VSLb(req->vsl, SLT_Debug, "w=%p scheduling dbg_vai_lease cap %d", req->wrk, cap);
329 96
                req->task->func = dbg_vai_lease;
330 96
        }
331
        else {
332 24
                VSLb(req->vsl, SLT_Debug, "w=%p scheduling dbg_vai_deliverobj", req->wrk);
333 24
                req->task->func = dbg_vai_deliverobj;
334
        }
335 120
        req->task->priv = req;
336
337 120
        req->wrk = NULL;
338 120
        req->vdc->wrk = NULL;
339 120
        req->transport_priv = v1l;
340
341 120
        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
342 120
        return (VTR_D_DISEMBARK);
343 120
}
344
345
static void v_matchproto_(task_func_t)
346 24
dbg_vai_deliverobj(struct worker *wrk, void *arg)
347
{
348
        struct req *req;
349
        struct v1l *v1l;
350
        const char *p;
351
        int err, chunked;
352
353 24
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
354 24
        CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC);
355 24
        v1l = req->transport_priv;
356 24
        req->transport_priv = NULL;
357 24
        AN(v1l);
358
359 24
        THR_SetRequest(req);
360 24
        VSLb(req->vsl, SLT_Debug, "w=%p enter dbg_vai_deliverobj", wrk);
361 24
        AZ(req->wrk);
362 24
        CNT_Embark(wrk, req);
363 24
        req->vdc->wrk = wrk;    // move to CNT_Embark?
364
365 24
        chunked = http_GetHdr(req->resp, H_Transfer_Encoding, &p) &&
366 0
            vstrcmp(p, "chunked") == 0;
367 24
        if (chunked)
368 0
                V1L_Chunked(v1l);
369 24
        err = VDP_DeliverObj(req->vdc, req->objcore);
370 24
        if (!err && chunked)
371 0
                V1L_EndChunk(v1l);
372 24
        dbg_vai_deliver_finish(req, &v1l, err);
373
374 24
        VSLb(req->vsl, SLT_Debug, "w=%p resuming http1_req", wrk);
375 24
        wrk->task->func = hack_http1_req;
376 24
        wrk->task->priv = req;
377 24
}
378
379
/*
380
 * copied from sml_notfiy
381
 */
382
struct dbg_vai_notify {
383
        unsigned                magic;
384
#define DBG_VAI_NOTIFY_MAGIC    0xa0154ed5
385
        unsigned                hasmore;
386
        pthread_mutex_t         mtx;
387
        pthread_cond_t          cond;
388
};
389
390
static void
391 96
dbg_vai_notify_init(struct dbg_vai_notify *sn)
392
{
393
394 96
        INIT_OBJ(sn, DBG_VAI_NOTIFY_MAGIC);
395 96
        PTOK(pthread_mutex_init(&sn->mtx, NULL));
396 96
        PTOK(pthread_cond_init(&sn->cond, NULL));
397 96
}
398
399
static void
400 96
dbg_vai_notify_fini(struct dbg_vai_notify *sn)
401
{
402
403 96
        CHECK_OBJ_NOTNULL(sn, DBG_VAI_NOTIFY_MAGIC);
404 96
        PTOK(pthread_mutex_destroy(&sn->mtx));
405 96
        PTOK(pthread_cond_destroy(&sn->cond));
406 96
}
407
408
static void v_matchproto_(vai_notify_cb)
409 384
dbg_vai_notify(vai_hdl hdl, void *priv)
410
{
411
        struct dbg_vai_notify *sn;
412
413 384
        (void) hdl;
414 384
        CAST_OBJ_NOTNULL(sn, priv, DBG_VAI_NOTIFY_MAGIC);
415 384
        PTOK(pthread_mutex_lock(&sn->mtx));
416 384
        sn->hasmore = 1;
417 384
        PTOK(pthread_cond_signal(&sn->cond));
418 384
        PTOK(pthread_mutex_unlock(&sn->mtx));
419
420 384
}
421
422
static void
423 384
dbg_vai_notify_wait(struct dbg_vai_notify *sn)
424
{
425
426 384
        CHECK_OBJ_NOTNULL(sn, DBG_VAI_NOTIFY_MAGIC);
427 384
        PTOK(pthread_mutex_lock(&sn->mtx));
428 767
        while (sn->hasmore == 0)
429 383
                PTOK(pthread_cond_wait(&sn->cond, &sn->mtx));
430 384
        AN(sn->hasmore);
431 384
        sn->hasmore = 0;
432 384
        PTOK(pthread_mutex_unlock(&sn->mtx));
433 384
}
434
435
static void
436 96
dbg_vai_lease_done(struct worker *wrk, struct req *req)
437
{
438 96
        VSLb(req->vsl, SLT_Debug, "w=%p resuming http1_req", wrk);
439 96
        wrk->task->func = hack_http1_req;
440 96
        wrk->task->priv = req;
441 96
}
442
443
static void v_matchproto_(task_func_t)
444 96
dbg_vai_lease(struct worker *wrk, void *arg)
445
{
446
        struct req *req;
447
        struct v1l *v1l;
448
        const char *p;
449 96
        unsigned flags = 0;
450
        int r, cap, err, chunked;
451
452 96
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
453 96
        CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC);
454 96
        v1l = req->transport_priv;
455 96
        req->transport_priv = NULL;
456 96
        AN(v1l);
457
458 96
        THR_SetRequest(req);
459 96
        VSLb(req->vsl, SLT_Debug, "w=%p enter dbg_vai_lease", wrk);
460 96
        AZ(req->wrk);
461 96
        CNT_Embark(wrk, req);
462 96
        req->vdc->wrk = wrk;    // move to CNT_Embark?
463
464 96
        cap = req->vdc->retval;
465 96
        req->vdc->retval = 0;
466 96
        assert(cap > 0);
467
468 96
        VSCARAB_LOCAL(scarab, cap);
469 96
        VSCARET_LOCAL(scaret, cap);
470
471 123
        chunked = http_GetHdr(req->resp, H_Transfer_Encoding, &p) &&
472 27
            vstrcmp(p, "chunked") == 0;
473 96
        if (chunked)
474 27
                V1L_Chunked(v1l);
475
476
        struct dbg_vai_notify notify;
477 96
        dbg_vai_notify_init(&notify);
478
479 96
        if (VDPIO_Init(req->vdc, req->objcore, req->ws, dbg_vai_notify, &notify, scaret)) {
480 0
                dbg_vai_notify_fini(&notify);
481 0
                dbg_vai_deliver_finish(req, &v1l, 1);
482 0
                dbg_vai_lease_done(wrk, req);
483 0
                return;
484
        }
485
486 96
        err = 0;
487 96
        do {
488 576
                r = vdpio_pull(req->vdc, NULL, scarab);
489 576
                flags = scarab->flags; // because vdpio_return_vscarab
490 576
                VSLb(req->vsl, SLT_Debug, "%d = vdpio_pull()", r);
491 576
                (void)V1L_Flush(v1l);
492 576
                vdpio_return_vscarab(req->vdc, scarab);
493
494 576
                if (r == -ENOBUFS || r == -EAGAIN) {
495 384
                        VDPIO_Return(req->vdc);
496 384
                        dbg_vai_notify_wait(&notify);
497 384
                }
498 192
                else if (r < 0) {
499 0
                        err = r;
500 0
                        break;
501
                }
502 576
        } while ((flags & VSCARAB_F_END) == 0);
503
504 96
        if (!err && chunked)
505 27
                V1L_EndChunk(v1l);
506 96
        dbg_vai_deliver_finish(req, &v1l, err);
507 96
        VDPIO_Fini(req->vdc);
508 96
        dbg_vai_notify_fini(&notify);
509 96
        dbg_vai_lease_done(wrk, req);
510 96
}
511
512
static void
513 120
dbg_vai_deliver_finish(struct req *req, struct v1l **v1lp, int err)
514
{
515
        stream_close_t sc;
516
        uint64_t bytes;
517
518 120
        sc = V1L_Close(v1lp, &bytes);
519
520 120
        if (req->vdc->vai_hdl != NULL)
521 96
                req->acct.resp_bodybytes += VDPIO_Close(req->vdc, req->objcore, req->boc);
522 120
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc);
523
524 120
        if (sc == SC_NULL && err && req->sp->fd >= 0)
525 0
                sc = SC_REM_CLOSE;
526 120
        if (sc != SC_NULL)
527 0
                Req_Fail(req, sc);
528 120
}
529
530
static struct transport DBG_transport;
531
532
void
533 312
debug_transport_vai_init(void)
534
{
535 312
        DBG_transport = HTTP1_transport;
536 312
        DBG_transport.name = "DBG VAI";
537 312
        DBG_transport.deliver = dbg_vai_deliver;
538 312
}
539
540
void
541 120
debug_transport_vai_use(VRT_CTX)
542
{
543
        struct req *req;
544
545 120
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
546 120
        req = ctx->req;
547 120
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
548
549 120
        if (req->transport != &HTTP1_transport) {
550 0
                VRT_fail(ctx, "Only works on built-in http1 transport");
551 0
                return;
552
        }
553 120
        AZ(req->transport_priv);
554 120
        req->transport = &DBG_transport;
555 120
}