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 236
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 236
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
55 236
        AN(addr);
56 236
        AN(*addr);
57 236
        assert(tmo >= 0.0);
58
59 236
        if (ctx->vsl != NULL)
60 236
                VSLb(ctx->vsl, SLT_Debug, "barrier_sync(\"%s\")", addr);
61
        else
62 0
                VSL(SLT_Debug, NO_VXID, "barrier_sync(\"%s\")", addr);
63
64 236
        sock = VTCP_open(addr, NULL, 0., &err);
65 236
        if (sock < 0) {
66 0
                VRT_fail(ctx, "Barrier connection failed: %s", err);
67 0
                return;
68
        }
69
70 236
        sz = VTCP_read(sock, buf, sizeof buf, tmo);
71 236
        i = errno;
72 236
        closefd(&sock);
73 236
        if (sz < 0)
74 0
                VRT_fail(ctx, "Barrier read failed: %s (errno=%d)",
75 0
                    strerror(i), i);
76 236
        if (sz > 0)
77 0
                VRT_fail(ctx, "Barrier unexpected data (%zdB)", sz);
78 236
}
79
80
/*--------------------------------------------------------------------*/
81
82
VCL_BACKEND v_matchproto_(td_vtc_no_backend)
83 6
vmod_no_backend(VRT_CTX)
84
{
85
86 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
87 6
        return (NULL);
88
}
89
90
VCL_STEVEDORE v_matchproto_(td_vtc_no_stevedore)
91 3
vmod_no_stevedore(VRT_CTX)
92
{
93
94 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
95 3
        return (NULL);
96
}
97
98
VCL_IP v_matchproto_(td_vtc_no_ip)
99 3
vmod_no_ip(VRT_CTX)
100
{
101
102 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
103 3
        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 75
vmod_sleep(VRT_CTX, VCL_DURATION t)
123
{
124
125 75
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
126 75
        VTIM_sleep(t);
127 75
}
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 1617
vtc_ws_find(VRT_CTX, VCL_ENUM which)
136
{
137
138 1617
        if (which == VENUM(client))
139 774
                return (ctx->ws);
140 843
        if (which == VENUM(backend))
141 756
                return (ctx->bo->ws);
142 87
        if (which == VENUM(session))
143 81
                return (ctx->req->sp->ws);
144 6
        if (which == VENUM(thread) && ctx->req != NULL)
145 6
                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 1617
}
150
151
VCL_VOID v_matchproto_(td_vtc_workspace_alloc)
152 1473
vmod_workspace_alloc(VRT_CTX, VCL_ENUM which, VCL_INT size)
153
{
154
        struct ws *ws;
155
        void *p;
156
157 1473
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
158
159 1473
        ws = vtc_ws_find(ctx, which);
160 1473
        if (ws == NULL)
161 0
                return;
162 1473
        WS_Assert(ws);
163
164 1473
        if (size < 0) {
165 1425
                size += WS_ReserveAll(ws);
166 1425
                WS_Release(ws, 0);
167 1425
        }
168 1473
        if (size <= 0) {
169 6
                VRT_fail(ctx, "Attempted negative WS allocation");
170 6
                return;
171
        }
172 1467
        p = WS_Alloc(ws, size);
173 1467
        if (p == NULL)
174 3
                VRT_fail(ctx, "vtc.workspace_alloc");
175
        else
176 1464
                memset(p, '\0', size);
177 1473
}
178
179
VCL_BYTES v_matchproto_(td_vtc_workspace_reserve)
180 36
vmod_workspace_reserve(VRT_CTX, VCL_ENUM which, VCL_INT size)
181
{
182
        struct ws *ws;
183
        unsigned r;
184
185 36
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
186
187 36
        ws = vtc_ws_find(ctx, which);
188 36
        if (ws == NULL)
189 0
                return (0);
190 36
        WS_Assert(ws);
191
192 36
        if (size < 0) {
193 21
                size += WS_ReserveAll(ws);
194 21
                WS_Release(ws, 0);
195 21
        }
196 36
        if (size <= 0) {
197 0
                VRT_fail(ctx, "Attempted negative WS reservation");
198 0
                return (0);
199
        }
200 36
        r = WS_ReserveSize(ws, size);
201 36
        if (r == 0)
202 3
                return (0);
203 33
        memset(WS_Reservation(ws), 0, r);
204 33
        WS_Release(ws, 0);
205 33
        return (r);
206 36
}
207
208
VCL_INT v_matchproto_(td_vtc_workspace_free)
209 48
vmod_workspace_free(VRT_CTX, VCL_ENUM which)
210
{
211
        struct ws *ws;
212
        unsigned u;
213
214 48
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
215
216 48
        ws = vtc_ws_find(ctx, which);
217 48
        if (ws == NULL)
218 0
                return (-1);
219 48
        WS_Assert(ws);
220
221 48
        u = WS_ReserveAll(ws);
222 48
        WS_Release(ws, 0);
223 48
        return (u);
224 48
}
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 3
VTC_WS_OP(VOID, , snapshot, (vtc_ws_snapshot = WS_Snapshot(ws)))
242 3
VTC_WS_OP(VOID, , reset, WS_Reset(ws, vtc_ws_snapshot))
243 3
VTC_WS_OP(VOID, , overflow, WS_MarkOverflow(ws))
244 15
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 36
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 36
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
259 36
        AN(where);
260
261 36
        ws = vtc_ws_find(ctx, which);
262 36
        if (ws == NULL)
263 0
                return (NULL);
264 36
        WS_Assert(ws);
265
266 36
        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 36
        l = WS_Dump(ws, *where, off, buf, len);
273 36
        assert(l <= WS_DUMP_MAXLEN);
274
275 36
        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 36
        p = WS_Copy(ctx->ws, buf, (int)l);
287 36
        if (p == NULL) {
288 0
                VRT_fail(ctx, "workspace_dump: copy failed");
289 0
                return (NULL);
290
        }
291 36
        return (VRT_blob(ctx, "workspace_dump", p, l, 0xd000d000));
292 36
}
293
294
#undef WS_DUMP_MAXLEN
295
296
/*--------------------------------------------------------------------*/
297
298
VCL_INT v_matchproto_(td_vtc_typesize)
299 75
vmod_typesize(VRT_CTX, VCL_STRING s)
300
{
301 75
        size_t i = 0, l, a, p = 0;
302
303 75
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
304 75
        AN(s);
305 75
        AN(*s);
306
307 336
        for (; *s; s++) {
308 264
                switch (*s) {
309
#define VTC_TYPESIZE(c, t) case c: l = sizeof(t); break;
310 33
                VTC_TYPESIZE('c', char)
311 9
                VTC_TYPESIZE('d', double)
312 3
                VTC_TYPESIZE('f', float)
313 9
                VTC_TYPESIZE('i', int)
314 3
                VTC_TYPESIZE('j', intmax_t)
315 3
                VTC_TYPESIZE('l', long)
316 3
                VTC_TYPESIZE('o', off_t)
317 48
                VTC_TYPESIZE('p', void *)
318 9
                VTC_TYPESIZE('s', short)
319 87
                VTC_TYPESIZE('u', unsigned)
320 54
                VTC_TYPESIZE('z', size_t)
321
#undef VTC_TYPESIZE
322 3
                default:        return (-1);
323
                }
324 261
                if (l > p)
325 126
                        p = l;
326 261
                a = i % l;
327 261
                if (a != 0)
328 15
                        i += (l - a); /* align */
329 261
                i += l;
330 261
        }
331 72
        AN(p);
332 72
        a = i % p;
333 72
        if (a != 0)
334 18
                i += (p - a); /* pad */
335 72
        return ((VCL_INT)i);
336 75
}
337
338
/*--------------------------------------------------------------------*/
339
340
#define BLOB_VMOD_PROXY_HEADER_TYPE     0xc8f34f78
341
342
VCL_BLOB v_matchproto_(td_vtc_proxy_header)
343 6
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 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
352
353 6
        if (venum == VENUM(v1))
354 3
                version = 1;
355 3
        else if (venum == VENUM(v2))
356 3
                version = 2;
357
        else
358 0
                WRONG(venum);
359
360 6
        vsb = VSB_new_auto();
361 6
        AN(vsb);
362 6
        VRT_Format_Proxy(vsb, version, client, server, authority);
363 6
        l = VSB_len(vsb);
364 6
        h = WS_Copy(ctx->ws, VSB_data(vsb), l);
365 6
        VSB_destroy(&vsb);
366
367 6
        if (h == NULL) {
368 0
                VRT_fail(ctx, "proxy_header: out of workspace");
369 0
                return (NULL);
370
        }
371
372 6
        return (VRT_blob(ctx, "proxy_header", h, l,
373
            BLOB_VMOD_PROXY_HEADER_TYPE));
374 6
}
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 189897
vsl_tagcmp(const void *aa, const void *bb)
392
{
393 189897
        const struct vsl_tag2enum *a = aa, *b = bb;
394
395
        // ref vsl2rst.c ptag_cmp
396 189897
        if (a->string == NULL && b->string != NULL)
397 438
                return (1);
398 189459
        else if (a->string != NULL && b->string == NULL)
399 21297
                return (-1);
400 168162
        else if (a->string == NULL && b->string == NULL)
401 36792
                return (0);
402 131370
        return (vstrcmp(a->string, b->string));
403 189897
}
404
405
/*lint -esym(528, init_vsl_tag2enum) */
406
static void __attribute__((constructor))
407 219
init_vsl_tag2enum(void)
408
{
409 219
        qsort(vsl_tag2enum, SLT__MAX, sizeof *vsl_tag2enum, vsl_tagcmp);
410 219
}
411
412
413
VCL_VOID
414 183
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 183
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
420
421 183
        key.string = tag_s;
422 183
        te = bsearch(&key, vsl_tag2enum, SLT__MAX,
423
            sizeof *vsl_tag2enum, vsl_tagcmp);
424
425 183
        if (te == NULL) {
426 0
                VRT_fail(ctx, "No such tag: %s", tag_s);
427 0
                return;
428
        }
429
430 183
        if (id < 0 || id > VRT_INTEGER_MAX) {
431 0
                VRT_fail(ctx, "id out of bounds");
432 0
                return;
433
        }
434
435 183
        vxid.vxid = id & VSL_IDENTMASK;
436 183
        if (side == VENUM(c))
437 177
                vxid.vxid |= VSL_CLIENTMARKER;
438 6
        else if (side == VENUM(b))
439 6
                vxid.vxid |= VSL_BACKENDMARKER;
440
        else
441 0
                WRONG("side");
442
443 183
        VSLs(te->tag, vxid, s);
444 183
}
445
446
static void
447 177
vsl_line(VRT_CTX, char *str)
448
{
449
        VCL_INT id;
450
        VCL_ENUM side;
451
        VCL_STRANDS s;
452 177
        const char *tag, *delim = " \t\r\n";
453
        char *e, *save;
454
455 177
        if (*str == '*') {
456
                // vinyltest
457 165
                str = strstr(str, "vsl|");
458 165
                if (str == NULL)
459 0
                        return;
460 165
                str += 4;
461 165
        }
462 177
        if ((str = strtok_r(str, delim, &save)) == NULL)
463 12
                return;
464 165
        id = strtoll(str, &e, 10);
465 165
        if (e == str)
466 0
                return;
467
468 165
        if ((str = strtok_r(NULL, delim, &save)) == NULL)
469 0
                return;
470 165
        tag = str;
471
472 165
        if ((str = strtok_r(NULL, delim, &save)) == NULL)
473 0
                return;
474 165
        if (*str == 'c')
475 159
                side = VENUM(c);
476 6
        else if (*str == 'b')
477 6
                side = VENUM(b);
478
        else
479 0
                return;
480
481 165
        str = strtok_r(NULL, "\r\n", &save);
482
        // needs to be assigned here because of the compound literal lifetime
483 165
        s = TOSTRAND(str);
484 165
        if (str == NULL)
485 24
                s = vrt_null_strands;
486
487 165
        vmod_vsl(ctx, id, tag, side, s);
488 177
}
489
490
VCL_VOID
491 6
vmod_vsl_replay(VRT_CTX, VCL_STRANDS s)
492
{
493
        struct vsb cp[1];
494
        const char *p, *pp;
495
        size_t l;
496 6
        int i, err = 0;
497
498 6
        CHECK_OBJ_ORNULL(s, STRANDS_MAGIC);
499 6
        if (s == NULL || s->n == 0)
500 0
                return;
501
502 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
503 6
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
504 6
        WS_VSB_new(cp, ctx->ws);
505
506 12
        for (i = 0; i < s->n; i++) {
507 6
                p = s->p[i];
508 6
                if (p == NULL || *p == '\0')
509 0
                        continue;
510 6
                pp = strpbrk(p, "\r\n");
511 177
                while (pp != NULL) {
512 171
                        l = pp - p;
513 171
                        if (VSB_bcat(cp, p, l) || VSB_finish(cp)) {
514 0
                                err = 1;
515 0
                                break;
516
                        }
517 171
                        vsl_line(ctx, VSB_data(cp));
518 171
                        VSB_clear(cp);
519 171
                        p = pp + 1;
520 171
                        pp = strpbrk(p, "\r\n");
521
                }
522 6
                if (err || VSB_cat(cp, p)) {
523 0
                        err = 1;
524 0
                        break;
525
                }
526 6
        }
527 6
        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 6
        vsl_line(ctx, VSB_data(cp));
533 6
        VSB_clear(cp);
534 6
        AN(WS_VSB_finish(cp, ctx->ws, NULL));
535 6
}