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