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