vinyl-cache/vmod/vmod_vtc.c
0
/*-
1
 * Copyright (c) 2012-2017 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
5
 * Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
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
31
#include "config.h"
32
33
#include <stdlib.h>
34
#include <stdio.h>
35
#include <string.h>
36
#include <unistd.h>
37
38
#include "cache/cache.h"
39
40
#include "vsb.h"
41
#include "vtcp.h"
42
#include "vtim.h"
43
44
#include "vcc_vtc_if.h"
45
46
VCL_VOID v_matchproto_(td_vtc_barrier_sync)
47 316
vmod_barrier_sync(VRT_CTX, VCL_STRING addr, VCL_DURATION tmo)
48
{
49
        const char *err;
50
        char buf[32];
51
        int sock, i;
52
        ssize_t sz;
53
54 316
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
55 316
        AN(addr);
56 316
        AN(*addr);
57 316
        assert(tmo >= 0.0);
58
59 316
        if (ctx->vsl != NULL)
60 316
                VSLb(ctx->vsl, SLT_Debug, "barrier_sync(\"%s\")", addr);
61
        else
62 0
                VSL(SLT_Debug, NO_VXID, "barrier_sync(\"%s\")", addr);
63
64 316
        sock = VTCP_open(addr, NULL, 0., &err);
65 316
        if (sock < 0) {
66 0
                VRT_fail(ctx, "Barrier connection failed: %s", err);
67 0
                return;
68
        }
69
70 316
        sz = VTCP_read(sock, buf, sizeof buf, tmo);
71 316
        i = errno;
72 316
        closefd(&sock);
73 316
        if (sz < 0)
74 0
                VRT_fail(ctx, "Barrier read failed: %s (errno=%d)",
75 0
                    strerror(i), i);
76 316
        if (sz > 0)
77 0
                VRT_fail(ctx, "Barrier unexpected data (%zdB)", sz);
78 316
}
79
80
/*--------------------------------------------------------------------*/
81
82
VCL_BACKEND v_matchproto_(td_vtc_no_backend)
83 8
vmod_no_backend(VRT_CTX)
84
{
85
86 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
87 8
        return (NULL);
88
}
89
90
VCL_STEVEDORE v_matchproto_(td_vtc_no_stevedore)
91 4
vmod_no_stevedore(VRT_CTX)
92
{
93
94 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
95 4
        return (NULL);
96
}
97
98
VCL_IP v_matchproto_(td_vtc_no_ip)
99 4
vmod_no_ip(VRT_CTX)
100
{
101
102 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
103 4
        return (NULL);
104
}
105
106
/*--------------------------------------------------------------------*/
107
108
VCL_VOID v_noreturn_ v_matchproto_(td_vtc_panic)
109 0
vmod_panic(VRT_CTX, VCL_STRANDS str)
110
{
111
        const char *b;
112
113 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
114
115 0
        b = VRT_StrandsWS(ctx->ws, "PANIC:", str);
116 0
        VAS_Fail("VCL", "", 0, b, VAS_VCL);
117
}
118
119
/*--------------------------------------------------------------------*/
120
121
VCL_VOID v_matchproto_(td_vtc_sleep)
122 100
vmod_sleep(VRT_CTX, VCL_DURATION t)
123
{
124
125 100
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
126 100
        VTIM_sleep(t);
127 100
}
128
129
/*--------------------------------------------------------------------*/
130
131
// XXX this really should be PRIV_TASK state
132
static uintptr_t vtc_ws_snapshot;
133
134
static struct ws *
135 2168
vtc_ws_find(VRT_CTX, VCL_ENUM which)
136
{
137
138 2168
        if (which == VENUM(client))
139 1044
                return (ctx->ws);
140 1124
        if (which == VENUM(backend))
141 1008
                return (ctx->bo->ws);
142 116
        if (which == VENUM(session))
143 108
                return (ctx->req->sp->ws);
144 8
        if (which == VENUM(thread) && ctx->req != NULL)
145 8
                return (ctx->req->wrk->aws);
146 0
        if (which == VENUM(thread) && ctx->bo != NULL)
147 0
                return (ctx->bo->wrk->aws);
148 0
        WRONG("vtc_ws_find Illegal enum");
149 2168
}
150
151
VCL_VOID v_matchproto_(td_vtc_workspace_alloc)
152 1976
vmod_workspace_alloc(VRT_CTX, VCL_ENUM which, VCL_INT size)
153
{
154
        struct ws *ws;
155
        void *p;
156
157 1976
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
158
159 1976
        ws = vtc_ws_find(ctx, which);
160 1976
        if (ws == NULL)
161 0
                return;
162 1976
        WS_Assert(ws);
163
164 1976
        if (size < 0) {
165 1912
                size += WS_ReserveAll(ws);
166 1912
                WS_Release(ws, 0);
167 1912
        }
168 1976
        if (size <= 0) {
169 8
                VRT_fail(ctx, "Attempted negative WS allocation");
170 8
                return;
171
        }
172 1968
        p = WS_Alloc(ws, size);
173 1968
        if (p == NULL)
174 4
                VRT_fail(ctx, "vtc.workspace_alloc");
175
        else
176 1964
                memset(p, '\0', size);
177 1976
}
178
179
VCL_BYTES v_matchproto_(td_vtc_workspace_reserve)
180 48
vmod_workspace_reserve(VRT_CTX, VCL_ENUM which, VCL_INT size)
181
{
182
        struct ws *ws;
183
        unsigned r;
184
185 48
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
186
187 48
        ws = vtc_ws_find(ctx, which);
188 48
        if (ws == NULL)
189 0
                return (0);
190 48
        WS_Assert(ws);
191
192 48
        if (size < 0) {
193 28
                size += WS_ReserveAll(ws);
194 28
                WS_Release(ws, 0);
195 28
        }
196 48
        if (size <= 0) {
197 0
                VRT_fail(ctx, "Attempted negative WS reservation");
198 0
                return (0);
199
        }
200 48
        r = WS_ReserveSize(ws, size);
201 48
        if (r == 0)
202 4
                return (0);
203 44
        memset(WS_Reservation(ws), 0, r);
204 44
        WS_Release(ws, 0);
205 44
        return (r);
206 48
}
207
208
VCL_INT v_matchproto_(td_vtc_workspace_free)
209 64
vmod_workspace_free(VRT_CTX, VCL_ENUM which)
210
{
211
        struct ws *ws;
212
        unsigned u;
213
214 64
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
215
216 64
        ws = vtc_ws_find(ctx, which);
217 64
        if (ws == NULL)
218 0
                return (-1);
219 64
        WS_Assert(ws);
220
221 64
        u = WS_ReserveAll(ws);
222 64
        WS_Release(ws, 0);
223 64
        return (u);
224 64
}
225
226
#define VTC_WS_OP(type, def, name, op)                  \
227
VCL_##type v_matchproto_(td_vtc_workspace_##name)       \
228
vmod_workspace_##name(VRT_CTX, VCL_ENUM which)          \
229
{                                                       \
230
        struct ws *ws;                                  \
231
                                                        \
232
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);          \
233
                                                        \
234
        ws = vtc_ws_find(ctx, which);                   \
235
        if (ws == NULL)                                 \
236
                return def ;                            \
237
        WS_Assert(ws);                                  \
238
                                                        \
239
        op;                                             \
240
}
241 4
VTC_WS_OP(VOID, , snapshot, (vtc_ws_snapshot = WS_Snapshot(ws)))
242 4
VTC_WS_OP(VOID, , reset, WS_Reset(ws, vtc_ws_snapshot))
243 4
VTC_WS_OP(VOID, , overflow, WS_MarkOverflow(ws))
244 20
VTC_WS_OP(BOOL, (0), overflowed, return (WS_Overflowed(ws)))
245
#undef VTC_WS_OP
246
247
#define WS_DUMP_MAXLEN  1023
248
249
VCL_BLOB v_matchproto_(td_vtc_workspace_dump)
250 48
vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
251
    VCL_BYTES off, VCL_BYTES len)
252
{
253
        struct ws *ws;
254
        unsigned l;
255
        unsigned char buf[WS_DUMP_MAXLEN];
256
        const char *p, *err;
257
258 48
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
259 48
        AN(where);
260
261 48
        ws = vtc_ws_find(ctx, which);
262 48
        if (ws == NULL)
263 0
                return (NULL);
264 48
        WS_Assert(ws);
265
266 48
        if (len > WS_DUMP_MAXLEN) {
267 0
                VRT_fail(ctx, "workspace_dump: max length is %jd",
268
                    (intmax_t)WS_DUMP_MAXLEN);
269 0
                return (NULL);
270
        }
271
272 48
        l = WS_Dump(ws, *where, off, buf, len);
273 48
        assert(l <= WS_DUMP_MAXLEN);
274
275 48
        if (l == 0) {
276 0
                switch (errno) {
277 0
                case EINVAL: WRONG(where); break;
278 0
                case EAGAIN: err = "NULL"; break;
279 0
                case EFAULT: err = "off limit"; break;
280 0
                default: err = "unknown error"; break;
281
                }
282 0
                VRT_fail(ctx, "workspace_dump: %s", err);
283 0
                return (NULL);
284
        }
285
286 48
        p = WS_Copy(ctx->ws, buf, (int)l);
287 48
        if (p == NULL) {
288 0
                VRT_fail(ctx, "workspace_dump: copy failed");
289 0
                return (NULL);
290
        }
291 48
        return (VRT_blob(ctx, "workspace_dump", p, l, 0xd000d000));
292 48
}
293
294
#undef WS_DUMP_MAXLEN
295
296
/*--------------------------------------------------------------------*/
297
298
VCL_INT v_matchproto_(td_vtc_typesize)
299 100
vmod_typesize(VRT_CTX, VCL_STRING s)
300
{
301 100
        size_t i = 0, l, a, p = 0;
302
303 100
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
304 100
        AN(s);
305 100
        AN(*s);
306
307 448
        for (; *s; s++) {
308 352
                switch (*s) {
309
#define VTC_TYPESIZE(c, t) case c: l = sizeof(t); break;
310 44
                VTC_TYPESIZE('c', char)
311 12
                VTC_TYPESIZE('d', double)
312 4
                VTC_TYPESIZE('f', float)
313 12
                VTC_TYPESIZE('i', int)
314 4
                VTC_TYPESIZE('j', intmax_t)
315 4
                VTC_TYPESIZE('l', long)
316 4
                VTC_TYPESIZE('o', off_t)
317 64
                VTC_TYPESIZE('p', void *)
318 12
                VTC_TYPESIZE('s', short)
319 116
                VTC_TYPESIZE('u', unsigned)
320 72
                VTC_TYPESIZE('z', size_t)
321
#undef VTC_TYPESIZE
322 4
                default:        return (-1);
323
                }
324 348
                if (l > p)
325 168
                        p = l;
326 348
                a = i % l;
327 348
                if (a != 0)
328 20
                        i += (l - a); /* align */
329 348
                i += l;
330 348
        }
331 96
        AN(p);
332 96
        a = i % p;
333 96
        if (a != 0)
334 24
                i += (p - a); /* pad */
335 96
        return ((VCL_INT)i);
336 100
}
337
338
/*--------------------------------------------------------------------*/
339
340
#define BLOB_VMOD_PROXY_HEADER_TYPE     0xc8f34f78
341
342
VCL_BLOB v_matchproto_(td_vtc_proxy_header)
343 8
vmod_proxy_header(VRT_CTX, VCL_ENUM venum, VCL_IP client, VCL_IP server,
344
    VCL_STRING authority)
345
{
346
        struct vsb *vsb;
347
        const void *h;
348
        int version;
349
        size_t l;
350
351 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
352
353 8
        if (venum == VENUM(v1))
354 4
                version = 1;
355 4
        else if (venum == VENUM(v2))
356 4
                version = 2;
357
        else
358 0
                WRONG(venum);
359
360 8
        vsb = VSB_new_auto();
361 8
        AN(vsb);
362 8
        VRT_Format_Proxy(vsb, version, client, server, authority);
363 8
        l = VSB_len(vsb);
364 8
        h = WS_Copy(ctx->ws, VSB_data(vsb), l);
365 8
        VSB_destroy(&vsb);
366
367 8
        if (h == NULL) {
368 0
                VRT_fail(ctx, "proxy_header: out of workspace");
369 0
                return (NULL);
370
        }
371
372 8
        return (VRT_blob(ctx, "proxy_header", h, l,
373
            BLOB_VMOD_PROXY_HEADER_TYPE));
374 8
}
375
376
// ref vsl.c
377
struct vsl_tag2enum {
378
        const char      *string;
379
        enum VSL_tag_e  tag;
380
};
381
382
static struct vsl_tag2enum vsl_tag2enum[SLT__MAX] = {
383
#define SLTM(name,flags,sdesc,ldesc) [SLT_ ## name] = { \
384
                .string = #name,                                \
385
                .tag = SLT_ ## name                             \
386
        },
387
#include "tbl/vsl_tags.h"
388
};
389
390
static int
391 256640
vsl_tagcmp(const void *aa, const void *bb)
392
{
393 256640
        const struct vsl_tag2enum *a = aa, *b = bb;
394
395
        // ref vsl2rst.c ptag_cmp
396 256640
        if (a->string == NULL && b->string != NULL)
397 592
                return (1);
398 256048
        else if (a->string != NULL && b->string == NULL)
399 28780
                return (-1);
400 227268
        else if (a->string == NULL && b->string == NULL)
401 49728
                return (0);
402 177540
        return (vstrcmp(a->string, b->string));
403 256640
}
404
405
/*lint -esym(528, init_vsl_tag2enum) */
406
static void __attribute__((constructor))
407 296
init_vsl_tag2enum(void)
408
{
409 296
        qsort(vsl_tag2enum, SLT__MAX, sizeof *vsl_tag2enum, vsl_tagcmp);
410 296
}
411
412
413
VCL_VOID
414 244
vmod_vsl(VRT_CTX, VCL_INT id, VCL_STRING tag_s, VCL_ENUM side, VCL_STRANDS s)
415
{
416
        struct vsl_tag2enum *te, key;
417
        vxid_t vxid;
418
419 244
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
420
421 244
        key.string = tag_s;
422 244
        te = bsearch(&key, vsl_tag2enum, SLT__MAX,
423
            sizeof *vsl_tag2enum, vsl_tagcmp);
424
425 244
        if (te == NULL) {
426 0
                VRT_fail(ctx, "No such tag: %s", tag_s);
427 0
                return;
428
        }
429
430 244
        if (id < 0 || id > VRT_INTEGER_MAX) {
431 0
                VRT_fail(ctx, "id out of bounds");
432 0
                return;
433
        }
434
435 244
        vxid.vxid = id & VSL_IDENTMASK;
436 244
        if (side == VENUM(c))
437 236
                vxid.vxid |= VSL_CLIENTMARKER;
438 8
        else if (side == VENUM(b))
439 8
                vxid.vxid |= VSL_BACKENDMARKER;
440
        else
441 0
                WRONG("side");
442
443 244
        VSLs(te->tag, vxid, s);
444 244
}
445
446
static void
447 236
vsl_line(VRT_CTX, char *str)
448
{
449
        VCL_INT id;
450
        VCL_ENUM side;
451
        VCL_STRANDS s;
452 236
        const char *tag, *delim = " \t\r\n";
453
        char *e, *save;
454
455 236
        if (*str == '*') {
456
                // vinyltest
457 220
                str = strstr(str, "vsl|");
458 220
                if (str == NULL)
459 0
                        return;
460 220
                str += 4;
461 220
        }
462 236
        if ((str = strtok_r(str, delim, &save)) == NULL)
463 16
                return;
464 220
        id = strtoll(str, &e, 10);
465 220
        if (e == str)
466 0
                return;
467
468 220
        if ((str = strtok_r(NULL, delim, &save)) == NULL)
469 0
                return;
470 220
        tag = str;
471
472 220
        if ((str = strtok_r(NULL, delim, &save)) == NULL)
473 0
                return;
474 220
        if (*str == 'c')
475 212
                side = VENUM(c);
476 8
        else if (*str == 'b')
477 8
                side = VENUM(b);
478
        else
479 0
                return;
480
481 220
        str = strtok_r(NULL, "\r\n", &save);
482
        // needs to be assigned here because of the compound literal lifetime
483 220
        s = TOSTRAND(str);
484 220
        if (str == NULL)
485 32
                s = vrt_null_strands;
486
487 220
        vmod_vsl(ctx, id, tag, side, s);
488 236
}
489
490
VCL_VOID
491 8
vmod_vsl_replay(VRT_CTX, VCL_STRANDS s)
492
{
493
        struct vsb cp[1];
494
        const char *p, *pp;
495
        size_t l;
496 8
        int i, err = 0;
497
498 8
        CHECK_OBJ_ORNULL(s, STRANDS_MAGIC);
499 8
        if (s == NULL || s->n == 0)
500 0
                return;
501
502 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
503 8
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
504 8
        WS_VSB_new(cp, ctx->ws);
505
506 16
        for (i = 0; i < s->n; i++) {
507 8
                p = s->p[i];
508 8
                if (p == NULL || *p == '\0')
509 0
                        continue;
510 8
                pp = strpbrk(p, "\r\n");
511 236
                while (pp != NULL) {
512 228
                        l = pp - p;
513 228
                        if (VSB_bcat(cp, p, l) || VSB_finish(cp)) {
514 0
                                err = 1;
515 0
                                break;
516
                        }
517 228
                        vsl_line(ctx, VSB_data(cp));
518 228
                        VSB_clear(cp);
519 228
                        p = pp + 1;
520 228
                        pp = strpbrk(p, "\r\n");
521
                }
522 8
                if (err || VSB_cat(cp, p)) {
523 0
                        err = 1;
524 0
                        break;
525
                }
526 8
        }
527 8
        if (err || VSB_finish(cp)) {
528 0
                AZ(WS_VSB_finish(cp, ctx->ws, NULL));
529 0
                VRT_fail(ctx, "out of workspace");
530 0
                return;
531
        }
532 8
        vsl_line(ctx, VSB_data(cp));
533 8
        VSB_clear(cp);
534 8
        AN(WS_VSB_finish(cp, ctx->ws, NULL));
535 8
}