varnish-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 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 2156
vtc_ws_find(VRT_CTX, VCL_ENUM which)
136
{
137
138 2156
        if (which == VENUM(client))
139 1032
                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 2156
}
150
151
VCL_VOID v_matchproto_(td_vtc_workspace_alloc)
152 1964
vmod_workspace_alloc(VRT_CTX, VCL_ENUM which, VCL_INT size)
153
{
154
        struct ws *ws;
155
        void *p;
156
157 1964
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
158
159 1964
        ws = vtc_ws_find(ctx, which);
160 1964
        if (ws == NULL)
161 0
                return;
162 1964
        WS_Assert(ws);
163
164 1964
        if (size < 0) {
165 1900
                size += WS_ReserveAll(ws);
166 1900
                WS_Release(ws, 0);
167 1900
        }
168 1964
        if (size <= 0) {
169 8
                VRT_fail(ctx, "Attempted negative WS allocation");
170 8
                return;
171
        }
172 1956
        p = WS_Alloc(ws, size);
173 1956
        if (p == NULL)
174 4
                VRT_fail(ctx, "vtc.workspace_alloc");
175
        else
176 1952
                memset(p, '\0', size);
177 1964
}
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
VCL_BLOB v_matchproto_(td_vtc_workspace_dump)
248 48
vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
249
    VCL_BYTES off, VCL_BYTES len)
250
{
251
        struct ws *ws;
252
        unsigned l;
253 48
        const unsigned maxlen = 1024;
254 48
        unsigned char buf[maxlen];
255
        const char *p, *err;
256
257 48
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
258 48
        AN(where);
259
260 48
        ws = vtc_ws_find(ctx, which);
261 48
        if (ws == NULL)
262 0
                return (NULL);
263 48
        WS_Assert(ws);
264
265 48
        if (len > maxlen) {
266 0
                VRT_fail(ctx, "workspace_dump: max length is %jd",
267
                    (intmax_t)maxlen);
268 0
                return (NULL);
269
        }
270
271 48
        l = WS_Dump(ws, *where, off, buf, len);
272 48
        assert(l <= maxlen);
273
274 48
        if (l == 0) {
275 0
                switch (errno) {
276 0
                case EINVAL: WRONG(where); break;
277 0
                case EAGAIN: err = "NULL"; break;
278 0
                case EFAULT: err = "off limit"; break;
279 0
                default: err = "unknown error"; break;
280
                }
281 0
                VRT_fail(ctx, "workspace_dump: %s", err);
282 0
                return (NULL);
283
        }
284
285 48
        p = WS_Copy(ctx->ws, buf, (int)l);
286 48
        if (p == NULL) {
287 0
                VRT_fail(ctx, "workspace_dump: copy failed");
288 0
                return (NULL);
289
        }
290 48
        return (VRT_blob(ctx, "workspace_dump", p, l, 0xd000d000));
291 48
}
292
293
/*--------------------------------------------------------------------*/
294
295
VCL_INT v_matchproto_(td_vtc_typesize)
296 100
vmod_typesize(VRT_CTX, VCL_STRING s)
297
{
298 100
        size_t i = 0, l, a, p = 0;
299
300 100
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
301 100
        AN(s);
302 100
        AN(*s);
303
304 392
        for (; *s; s++) {
305 296
                switch (*s) {
306
#define VTC_TYPESIZE(c, t) case c: l = sizeof(t); break;
307 44
                VTC_TYPESIZE('c', char)
308 12
                VTC_TYPESIZE('d', double)
309 4
                VTC_TYPESIZE('f', float)
310 12
                VTC_TYPESIZE('i', int)
311 4
                VTC_TYPESIZE('j', intmax_t)
312 4
                VTC_TYPESIZE('l', long)
313 4
                VTC_TYPESIZE('o', off_t)
314 64
                VTC_TYPESIZE('p', void *)
315 12
                VTC_TYPESIZE('s', short)
316 60
                VTC_TYPESIZE('u', unsigned)
317 72
                VTC_TYPESIZE('z', size_t)
318
#undef VTC_TYPESIZE
319 4
                default:        return (-1);
320
                }
321 292
                if (l > p)
322 168
                        p = l;
323 292
                a = i % l;
324 292
                if (a != 0)
325 76
                        i += (l - a); /* align */
326 292
                i += l;
327 292
        }
328 96
        AN(p);
329 96
        a = i % p;
330 96
        if (a != 0)
331 24
                i += (p - a); /* pad */
332 96
        return ((VCL_INT)i);
333 100
}
334
335
/*--------------------------------------------------------------------*/
336
337
#define BLOB_VMOD_PROXY_HEADER_TYPE     0xc8f34f78
338
339
VCL_BLOB v_matchproto_(td_vtc_proxy_header)
340 8
vmod_proxy_header(VRT_CTX, VCL_ENUM venum, VCL_IP client, VCL_IP server,
341
    VCL_STRING authority)
342
{
343
        struct vsb *vsb;
344
        const void *h;
345
        int version;
346
        size_t l;
347
348 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
349
350 8
        if (venum == VENUM(v1))
351 4
                version = 1;
352 4
        else if (venum == VENUM(v2))
353 4
                version = 2;
354
        else
355 0
                WRONG(venum);
356
357 8
        vsb = VSB_new_auto();
358 8
        AN(vsb);
359 8
        VRT_Format_Proxy(vsb, version, client, server, authority);
360 8
        l = VSB_len(vsb);
361 8
        h = WS_Copy(ctx->ws, VSB_data(vsb), l);
362 8
        VSB_destroy(&vsb);
363
364 8
        if (h == NULL) {
365 0
                VRT_fail(ctx, "proxy_header: out of workspace");
366 0
                return (NULL);
367
        }
368
369 8
        return (VRT_blob(ctx, "proxy_header", h, l,
370
            BLOB_VMOD_PROXY_HEADER_TYPE));
371 8
}
372
373
// ref vsl.c
374
struct vsl_tag2enum {
375
        const char      *string;
376
        enum VSL_tag_e  tag;
377
};
378
379
static struct vsl_tag2enum vsl_tag2enum[SLT__MAX] = {
380
#define SLTM(name,flags,sdesc,ldesc) [SLT_ ## name] = { \
381
                .string = #name,                                \
382
                .tag = SLT_ ## name                             \
383
        },
384
#include "tbl/vsl_tags.h"
385
};
386
387
static int
388 236856
vsl_tagcmp(const void *aa, const void *bb)
389
{
390 236856
        const struct vsl_tag2enum *a = aa, *b = bb;
391
392
        // ref vsl2rst.c ptag_cmp
393 236856
        if (a->string == NULL && b->string != NULL)
394 536
                return (1);
395 236320
        else if (a->string != NULL && b->string == NULL)
396 25824
                return (-1);
397 210496
        else if (a->string == NULL && b->string == NULL)
398 45292
                return (0);
399 165204
        return (strcmp(a->string, b->string));
400 236856
}
401
402
/*lint -esym(528, init_vsl_tag2enum) */
403
static void __attribute__((constructor))
404 268
init_vsl_tag2enum(void)
405
{
406 268
        qsort(vsl_tag2enum, SLT__MAX, sizeof *vsl_tag2enum, vsl_tagcmp);
407 268
}
408
409
410
VCL_VOID
411 244
vmod_vsl(VRT_CTX, VCL_INT id, VCL_STRING tag_s, VCL_ENUM side, VCL_STRANDS s)
412
{
413
        struct vsl_tag2enum *te, key;
414
        vxid_t vxid;
415
416 244
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
417
418 244
        key.string = tag_s;
419 244
        te = bsearch(&key, vsl_tag2enum, SLT__MAX,
420
            sizeof *vsl_tag2enum, vsl_tagcmp);
421
422 244
        if (te == NULL) {
423 0
                VRT_fail(ctx, "No such tag: %s", tag_s);
424 0
                return;
425
        }
426
427 244
        if (id < 0 || id > VRT_INTEGER_MAX) {
428 0
                VRT_fail(ctx, "id out of bounds");
429 0
                return;
430
        }
431
432 244
        vxid.vxid = id & VSL_IDENTMASK;
433 244
        if (side == VENUM(c))
434 236
                vxid.vxid |= VSL_CLIENTMARKER;
435 8
        else if (side == VENUM(b))
436 8
                vxid.vxid |= VSL_BACKENDMARKER;
437
        else
438 0
                WRONG("side");
439
440 244
        VSLs(te->tag, vxid, s);
441 244
}
442
443
static void
444 236
vsl_line(VRT_CTX, char *str)
445
{
446
        VCL_INT id;
447
        VCL_ENUM side;
448
        VCL_STRANDS s;
449 236
        const char *tag, *delim = " \t\r\n";
450
        char *e, *save;
451
452 236
        if (*str == '*') {
453
                // varnishtest
454 220
                str = strstr(str, "vsl|");
455 220
                if (str == NULL)
456 0
                        return;
457 220
                str += 4;
458 220
        }
459 236
        if ((str = strtok_r(str, delim, &save)) == NULL)
460 16
                return;
461 220
        id = strtoll(str, &e, 10);
462 220
        if (e == str)
463 0
                return;
464
465 220
        if ((str = strtok_r(NULL, delim, &save)) == NULL)
466 0
                return;
467 220
        tag = str;
468
469 220
        if ((str = strtok_r(NULL, delim, &save)) == NULL)
470 0
                return;
471 220
        if (*str == 'c')
472 212
                side = VENUM(c);
473 8
        else if (*str == 'b')
474 8
                side = VENUM(b);
475
        else
476 0
                return;
477
478 220
        str = strtok_r(NULL, "\r\n", &save);
479 220
        s = TOSTRAND(str);
480 220
        if (str == NULL)
481 32
                s = vrt_null_strands;
482
483 220
        vmod_vsl(ctx, id, tag, side, s);
484 236
}
485
486
VCL_VOID
487 8
vmod_vsl_replay(VRT_CTX, VCL_STRANDS s)
488
{
489
        struct vsb cp[1];
490
        const char *p, *pp;
491
        size_t l;
492 8
        int i, err = 0;
493
494 8
        if (s == NULL || s->n == 0)
495 0
                return;
496
497 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
498 8
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
499 8
        WS_VSB_new(cp, ctx->ws);
500
501 16
        for (i = 0; i < s->n; i++) {
502 8
                p = s->p[i];
503 8
                if (p == NULL || *p == '\0')
504 0
                        continue;
505 8
                pp = strpbrk(p, "\r\n");
506 236
                while (pp != NULL) {
507 228
                        l = pp - p;
508 228
                        if (VSB_bcat(cp, p, l) || VSB_finish(cp)) {
509 0
                                err = 1;
510 0
                                break;
511
                        }
512 228
                        vsl_line(ctx, VSB_data(cp));
513 228
                        VSB_clear(cp);
514 228
                        p = pp + 1;
515 228
                        pp = strpbrk(p, "\r\n");
516
                }
517 8
                if (err || VSB_cat(cp, p)) {
518 0
                        err = 1;
519 0
                        break;
520
                }
521 8
        }
522 8
        if (err || VSB_finish(cp)) {
523 0
                AZ(WS_VSB_finish(cp, ctx->ws, NULL));
524 0
                VRT_fail(ctx, "out of workspace");
525 0
                return;
526
        }
527 8
        vsl_line(ctx, VSB_data(cp));
528 8
        VSB_clear(cp);
529 8
        AN(WS_VSB_finish(cp, ctx->ws, NULL));
530 8
}