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