varnish-cache/bin/varnishd/cache/cache.h
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
32
#ifdef VRT_H_INCLUDED
33
#  error "vrt.h included before cache.h - they are exclusive"
34
#endif
35
36
#ifdef CACHE_H_INCLUDED
37
#  error "cache.h included multiple times"
38
#endif
39
40
#include <math.h>
41
#include <pthread.h>
42
#include <stdarg.h>
43
#include <sys/types.h>
44
#include <sys/uio.h>
45
46
#include "vdef.h"
47
#include "vrt.h"
48
49
#define CACHE_H_INCLUDED        // After vrt.h include.
50
51
#include "miniobj.h"
52
#include "vas.h"
53
#include "vqueue.h"
54
#include "vtree.h"
55
56
#include "vapi/vsl_int.h"
57
58
/*--------------------------------------------------------------------*/
59
60
struct vxids {
61
        uint64_t        vxid;
62
};
63
64
typedef struct vxids vxid_t;
65
66
#define NO_VXID ((struct vxids){0})
67
#define IS_NO_VXID(x) ((x).vxid == 0)
68
#define VXID_TAG(x) ((uintmax_t)((x).vxid & (VSL_CLIENTMARKER|VSL_BACKENDMARKER)))
69
#define VXID(u) ((uintmax_t)((u.vxid) & VSL_IDENTMASK))
70
#define IS_SAME_VXID(x, y) ((x).vxid == (y).vxid)
71
72
/*--------------------------------------------------------------------*/
73
74
struct body_status {
75
        const char              *name;
76
        int                     nbr;
77
        int                     avail;
78
        int                     length_known;
79
};
80
81
#define BODYSTATUS(U, l, n, a, k) extern const struct body_status BS_##U[1];
82
#include "tbl/body_status.h"
83
84
typedef const struct body_status *body_status_t;
85
86
/*--------------------------------------------------------------------*/
87
88
struct stream_close {
89
        unsigned                magic;
90
#define STREAM_CLOSE_MAGIC      0xc879c93d
91
        int                     idx;
92
        unsigned                is_err;
93
        const char              *name;
94
        const char              *desc;
95
};
96
    extern const struct stream_close SC_NULL[1];
97
#define SESS_CLOSE(nm, stat, err, desc) \
98
    extern const struct stream_close SC_##nm[1];
99
#include "tbl/sess_close.h"
100
101
102
/*--------------------------------------------------------------------
103
 * Indices into http->hd[]
104
 */
105
enum {
106
#define SLTH(tag, ind, req, resp, sdesc, ldesc) ind,
107
#include "tbl/vsl_tags_http.h"
108
};
109
110
/*--------------------------------------------------------------------*/
111
112
struct ban;
113
struct ban_proto;
114
struct cli;
115
struct http_conn;
116
struct listen_sock;
117
struct mempool;
118
struct objcore;
119
struct objhead;
120
struct pool;
121
struct req_step;
122
struct sess;
123
struct transport;
124
struct vcf;
125
struct VSC_lck;
126
struct VSC_main;
127
struct VSC_main_wrk;
128
struct worker;
129
struct worker_priv;
130
131
#define DIGEST_LEN              32
132
133
/*--------------------------------------------------------------------*/
134
135
struct lock { void *priv; };    // Opaque
136
137
/*--------------------------------------------------------------------
138
 * Workspace structure for quick memory allocation.
139
 */
140
141
#define WS_ID_SIZE 4
142
143
struct ws {
144
        unsigned                magic;
145
#define WS_MAGIC                0x35fac554
146
        char                    id[WS_ID_SIZE]; /* identity */
147
        char                    *s;             /* (S)tart of buffer */
148
        char                    *f;             /* (F)ree/front pointer */
149
        char                    *r;             /* (R)eserved length */
150
        char                    *e;             /* (E)nd of buffer */
151
};
152
153
/*--------------------------------------------------------------------
154
 *
155
 */
156
157
struct http {
158
        unsigned                magic;
159
#define HTTP_MAGIC              0x6428b5c9
160
161
        uint16_t                shd;            /* Size of hd space */
162
        txt                     *hd;
163
        unsigned char           *hdf;
164
#define HDF_FILTER              (1 << 0)        /* Filtered by Connection */
165
166
        /* NB: ->nhd and below zeroed/initialized by http_Teardown */
167
        uint16_t                nhd;            /* Next free hd */
168
169
        enum VSL_tag_e          logtag;         /* Must be SLT_*Method */
170
        struct vsl_log          *vsl;
171
172
        struct ws               *ws;
173
        uint16_t                status;
174
        uint8_t                 protover;
175
};
176
177
/*--------------------------------------------------------------------*/
178
179
struct acct_req {
180
#define ACCT(foo)       uint64_t        foo;
181
#include "tbl/acct_fields_req.h"
182
};
183
184
/*--------------------------------------------------------------------*/
185
186
struct acct_bereq {
187
#define ACCT(foo)       uint64_t        foo;
188
#include "tbl/acct_fields_bereq.h"
189
};
190
191
/*--------------------------------------------------------------------*/
192
193
struct vsl_log {
194
        uint32_t                *wlb, *wlp, *wle;
195
        vxid_t                  wid;
196
        unsigned                wlr;
197
};
198
199
/*--------------------------------------------------------------------*/
200
201
VRBT_HEAD(vrt_privs, vrt_priv);
202
203
/* Worker pool stuff -------------------------------------------------*/
204
205
typedef void task_func_t(struct worker *wrk, void *priv);
206
207
struct pool_task {
208
        VTAILQ_ENTRY(pool_task)         list;
209
        task_func_t                     *func;
210
        void                            *priv;
211
};
212
213
/*
214
 * tasks are taken off the queues in this order
215
 *
216
 * TASK_QUEUE_{REQ|STR} are new req's (H1/H2), and subject to queue limit.
217
 *
218
 * TASK_QUEUE_RUSH is req's returning from waiting list
219
 *
220
 * NOTE: When changing the number of classes, update places marked with
221
 * TASK_QUEUE_RESERVE in params.h
222
 */
223
enum task_prio {
224
        TASK_QUEUE_BO,
225
        TASK_QUEUE_RUSH,
226
        TASK_QUEUE_REQ,
227
        TASK_QUEUE_STR,
228
        TASK_QUEUE_VCA,
229
        TASK_QUEUE_BG,
230
        TASK_QUEUE__END
231
};
232
233
#define TASK_QUEUE_HIGHEST_PRIORITY TASK_QUEUE_BO
234
#define TASK_QUEUE_RESERVE TASK_QUEUE_BG
235
#define TASK_QUEUE_LIMITED(prio) \
236
        (prio == TASK_QUEUE_REQ || prio == TASK_QUEUE_STR)
237
238
/*--------------------------------------------------------------------*/
239
240
struct worker {
241
        unsigned                magic;
242
#define WORKER_MAGIC            0x6391adcf
243
        int                     strangelove;
244
        struct worker_priv      *wpriv;
245
        struct pool             *pool;
246
        struct VSC_main_wrk     *stats;
247
        struct vsl_log          *vsl;           // borrowed from req/bo
248
249
        struct pool_task        task[1];
250
251
        vtim_real               lastused;
252
253
        pthread_cond_t          cond;
254
255
        struct ws               aws[1];
256
257
        unsigned                cur_method;
258
        unsigned                seen_methods;
259
260
        struct wrk_vpi          *vpi;
261
};
262
263
/* Stored object -----------------------------------------------------
264
 * This is just to encapsulate the fields owned by the stevedore
265
 */
266
267
struct storeobj {
268
        const struct stevedore  *stevedore;
269
        void                    *priv;
270
        uint64_t                priv2;
271
};
272
273
/* Busy Objcore structure --------------------------------------------
274
 *
275
 */
276
277
/*
278
 * The macro-states we expose outside the fetch code
279
 */
280
enum boc_state_e {
281
#define BOC_STATE(U, l)       BOS_##U,
282
#include "tbl/boc_state.h"
283
};
284
285
// cache_obj.h vai notify
286
struct vai_qe;
287
VSLIST_HEAD(vai_q_head, vai_qe);
288
289
struct boc {
290
        unsigned                magic;
291
#define BOC_MAGIC               0x70c98476
292
        unsigned                refcount;
293
        struct lock             mtx;
294
        pthread_cond_t          cond;
295
        void                    *stevedore_priv;
296
        enum boc_state_e        state;
297
        uint8_t                 *vary;
298
        uint64_t                fetched_so_far;
299
        uint64_t                delivered_so_far;
300
        uint64_t                transit_buffer;
301
        struct vai_q_head       vai_q_head;
302
};
303
304
/* Object core structure ---------------------------------------------
305
 * Objects have sideways references in the binary heap and the LRU list
306
 * and we want to avoid paging in a lot of objects just to move them up
307
 * or down the binheap or to move a unrelated object on the LRU list.
308
 * To avoid this we use a proxy object, objcore, to hold the relevant
309
 * housekeeping fields parts of an object.
310
 */
311
312
enum obj_attr {
313
#define OBJ_FIXATTR(U, l, s)    OA_##U,
314
#define OBJ_VARATTR(U, l)       OA_##U,
315
#define OBJ_AUXATTR(U, l)       OA_##U,
316
#include "tbl/obj_attr.h"
317
                                OA__MAX,
318
};
319
320
enum obj_flags {
321
#define OBJ_FLAG(U, l, v)       OF_##U = v,
322
#include "tbl/obj_attr.h"
323
};
324
325
enum oc_flags {
326
#define OC_FLAG(U, l, v)        OC_F_##U = v,
327
#include "tbl/oc_flags.h"
328
};
329
330
#define OC_F_TRANSIENT (OC_F_PRIVATE | OC_F_HFM | OC_F_HFP)
331
332
enum oc_exp_flags {
333
#define OC_EXP_FLAG(U, l, v)    OC_EF_##U = v,
334
#include "tbl/oc_exp_flags.h"
335
};
336
337
struct objcore {
338
        unsigned                magic;
339
#define OBJCORE_MAGIC           0x4d301302
340
        int                     refcnt;
341
        struct storeobj         stobj[1];
342
        struct objhead          *objhead;
343
        struct boc              *boc;
344
        vtim_real               timer_when;
345
        VCL_INT                 hits;
346
347
348
        vtim_real               t_origin;
349
        float                   ttl;
350
        float                   grace;
351
        float                   keep;
352
353
        uint8_t                 flags;
354
355
        uint8_t                 exp_flags;
356
357
        uint16_t                oa_present;
358
359
        unsigned                timer_idx;      // XXX 4Gobj limit
360
        unsigned                waitinglist_gen;
361
        vtim_real               last_lru;
362
        VTAILQ_ENTRY(objcore)   hsh_list;
363
        VTAILQ_ENTRY(objcore)   lru_list;
364
        VTAILQ_ENTRY(objcore)   ban_list;
365
        VSTAILQ_ENTRY(objcore)  exp_list;
366
        struct ban              *ban;
367
};
368
369
/* Busy Object structure ---------------------------------------------
370
 *
371
 * The busyobj structure captures the aspects of an object related to,
372
 * and while it is being fetched from the backend.
373
 *
374
 * One of these aspects will be how much has been fetched, which
375
 * streaming delivery will make use of.
376
 */
377
378
enum director_state_e {
379
        DIR_S_NULL = 0,
380
        DIR_S_HDRS = 1,
381
        DIR_S_BODY = 2,
382
};
383
384
struct busyobj {
385
        unsigned                magic;
386
#define BUSYOBJ_MAGIC           0x23b95567
387
388
        char                    *end;
389
390
        unsigned                max_retries;
391
        unsigned                retries;
392
        struct req              *req;
393
        struct sess             *sp;
394
        struct worker           *wrk;
395
396
        /* beresp.body */
397
        struct vfp_ctx          *vfc;
398
        const char              *vfp_filter_list;
399
        /* bereq.body */
400
        const char              *vdp_filter_list;
401
402
        struct ws               ws[1];
403
        uintptr_t               ws_bo;
404
        struct http             *bereq0;
405
        struct http             *bereq;
406
        struct http             *beresp;
407
        struct objcore          *bereq_body;
408
        struct objcore          *stale_oc;
409
        struct objcore          *fetch_objcore;
410
411
        const char              *no_retry;
412
413
        struct http_conn        *htc;
414
415
        struct pool_task        fetch_task[1];
416
417
        const char              *err_reason;
418
        enum director_state_e   director_state;
419
        uint16_t                err_code;
420
421
#define BERESP_FLAG(l, r, w, f, d) unsigned     l:1;
422
#define BEREQ_FLAG(l, r, w, d) BERESP_FLAG(l, r, w, 0, d)
423
#include "tbl/bereq_flags.h"
424
#include "tbl/beresp_flags.h"
425
426
427
        /* Timeouts */
428
        vtim_dur                connect_timeout;
429
        vtim_dur                first_byte_timeout;
430
        vtim_dur                between_bytes_timeout;
431
        vtim_dur                task_deadline;
432
433
        /* Timers */
434
        vtim_real               t_first;        /* First timestamp logged */
435
        vtim_real               t_resp;         /* response received */
436
        vtim_real               t_prev;         /* Previous timestamp logged */
437
438
        /* Acct */
439
        struct acct_bereq       acct;
440
441
        const struct stevedore  *storage;
442
        const struct director   *director_req;
443
        const struct director   *director_resp;
444
        struct vcl              *vcl;
445
446
        struct vsl_log          vsl[1];
447
448
        uint8_t                 digest[DIGEST_LEN];
449
        struct vrt_privs        privs[1];
450
451
        const char              *client_identity;
452
};
453
454
#define BUSYOBJ_TMO(bo, pfx, tmo)                                       \
455
        (isnan((bo)->tmo) ? cache_param->pfx##tmo : (bo)->tmo)
456
457
458
/*--------------------------------------------------------------------*/
459
460
struct reqtop {
461
        unsigned                magic;
462
#define REQTOP_MAGIC            0x57fbda52
463
        struct req              *topreq;
464
        struct vcl              *vcl0;
465
        struct vrt_privs        privs[1];
466
};
467
468
struct req {
469
        unsigned                magic;
470
#define REQ_MAGIC               0xfb4abf6d
471
472
        unsigned                esi_level;
473
        body_status_t           req_body_status;
474
        stream_close_t          doclose;
475
        unsigned                restarts;
476
        unsigned                max_restarts;
477
        unsigned                waitinglist_gen;
478
479
        const struct req_step   *req_step;
480
        struct reqtop           *top;   /* esi_level == 0 request */
481
482
        uint16_t                err_code;
483
#define REQ_FLAG(l, r, w, d) unsigned   l:1;
484
#include "tbl/req_flags.h"
485
486
        const char              *err_reason;
487
488
        struct sess             *sp;
489
        struct worker           *wrk;
490
        struct pool_task        task[1];
491
492
        const struct transport  *transport;
493
        void                    *transport_priv;
494
495
        VTAILQ_ENTRY(req)       w_list;
496
497
        struct objcore          *body_oc;
498
499
        /* Built Vary string == workspace reservation */
500
        uint8_t                 *vary_b;
501
        uint8_t                 *vary_e;
502
503
        uint8_t                 digest[DIGEST_LEN];
504
505
        vtim_dur                d_ttl;
506
        vtim_dur                d_grace;
507
508
        const struct stevedore  *storage;
509
510
        const struct director   *director_hint;
511
        struct vcl              *vcl;
512
513
        uintptr_t               ws_req;         /* WS above request data */
514
515
        /* Timestamps */
516
        vtim_real               t_first;        /* First timestamp logged */
517
        vtim_real               t_prev;         /* Previous timestamp logged */
518
        vtim_real               t_req;          /* Headers complete */
519
        vtim_real               t_resp;         /* Entry to last deliver/synth */
520
521
        struct http_conn        *htc;
522
        struct vfp_ctx          *vfc;
523
        const char              *client_identity;
524
525
        /* HTTP request */
526
        struct http             *http;
527
        struct http             *http0;
528
529
        /* HTTP response */
530
        struct http             *resp;
531
        intmax_t                resp_len;
532
533
        struct ws               ws[1];
534
        struct objcore          *objcore;
535
        struct objcore          *stale_oc;
536
        struct boc              *boc;           /* valid during cnt_transmit */
537
538
        /* resp.body */
539
        struct vdp_ctx          *vdc;
540
        const char              *vdp_filter_list;
541
        /* req.body */
542
        const char              *vfp_filter_list;
543
544
        /* Transaction VSL buffer */
545
        struct vsl_log          vsl[1];
546
547
        /* Temporary accounting */
548
        struct acct_req         acct;
549
550
        struct vrt_privs        privs[1];
551
552
        struct vcf              *vcf;
553
};
554
555
#define IS_TOPREQ(req) ((req)->top->topreq == (req))
556
557
/*--------------------------------------------------------------------
558
 * Struct sess is a high memory-load structure because sessions typically
559
 * hang around the waiter for relatively long time.
560
 *
561
 * The size goal for struct sess + struct memitem is <512 bytes
562
 *
563
 * Getting down to the next relevant size (<256 bytes because of how malloc
564
 * works, is not realistic without a lot of code changes.
565
 */
566
567
enum sess_attr {
568
#define SESS_ATTR(UP, low, typ, len)    SA_##UP,
569
#include "tbl/sess_attr.h"
570
        SA_LAST
571
};
572
573
struct sess {
574
        unsigned                magic;
575
#define SESS_MAGIC              0x2c2f9c5a
576
577
        uint16_t                sattr[SA_LAST];
578
        struct listen_sock      *listen_sock;
579
        int                     refcnt;
580
        int                     fd;
581
        vxid_t                  vxid;
582
583
        struct lock             mtx;
584
585
        struct pool             *pool;
586
587
        struct ws               ws[1];
588
589
        vtim_real               t_open;         /* fd accepted */
590
        vtim_real               t_idle;         /* fd accepted or resp sent */
591
        vtim_dur                timeout_idle;
592
        vtim_dur                timeout_linger;
593
        vtim_dur                send_timeout;
594
        vtim_dur                idle_send_timeout;
595
};
596
597
#define SESS_TMO(sp, tmo)                                       \
598
        (isnan((sp)->tmo) ? cache_param->tmo : (sp)->tmo)
599
600
/* Prototypes etc ----------------------------------------------------*/
601
602
603
/* cache_ban.c */
604
605
/* for constructing bans */
606
struct ban_proto *BAN_Build(void);
607
const char *BAN_AddTest(struct ban_proto *,
608
    const char *, const char *, const char *);
609
const char *BAN_Commit(struct ban_proto *b);
610
void BAN_Abandon(struct ban_proto *b);
611
612
/* cache_cli.c [CLI] */
613
extern pthread_t cli_thread;
614
#define IS_CLI() (pthread_equal(pthread_self(), cli_thread))
615
#define ASSERT_CLI() do {assert(IS_CLI());} while (0)
616
617
/* cache_http.c */
618
unsigned HTTP_estimate(unsigned nhttp);
619
void HTTP_Clone(struct http *to, const struct http * const fm);
620
void HTTP_Dup(struct http *to, const struct http * const fm);
621
struct http *HTTP_create(void *p, uint16_t nhttp, unsigned);
622
const char *http_Status2Reason(unsigned, const char **);
623
int http_IsHdr(const txt *hh, hdr_t hdr);
624
unsigned http_EstimateWS(const struct http *fm, unsigned how);
625
void http_PutResponse(struct http *to, const char *proto, uint16_t status,
626
    const char *response);
627
void http_FilterReq(struct http *to, const struct http *fm, unsigned how);
628
void HTTP_Encode(const struct http *fm, uint8_t *, unsigned len, unsigned how);
629
int HTTP_Decode(struct http *to, const uint8_t *fm);
630
void http_ForceHeader(struct http *to, hdr_t, const char *val);
631
void http_AppendHeader(struct http *to, hdr_t, const char *val);
632
void http_PrintfHeader(struct http *to, const char *fmt, ...)
633
    v_printflike_(2, 3);
634
void http_TimeHeader(struct http *to, const char *fmt, vtim_real now);
635
const char * http_ViaHeader(void);
636
void http_Proto(struct http *to);
637
void http_SetHeader(struct http *to, const char *header);
638
void http_SetH(struct http *to, unsigned n, const char *header);
639
void http_ForceField(struct http *to, unsigned n, const char *t);
640
void HTTP_Setup(struct http *, struct ws *, struct vsl_log *, enum VSL_tag_e);
641
void http_Teardown(struct http *ht);
642
int http_GetHdr(const struct http *hp, hdr_t, const char **ptr);
643
int http_GetHdrToken(const struct http *hp, hdr_t,
644
    const char *token, const char **pb, const char **pe);
645
int http_GetHdrField(const struct http *hp, hdr_t,
646
    const char *field, const char **ptr);
647
double http_GetHdrQ(const struct http *hp, hdr_t, const char *field);
648
ssize_t http_GetContentLength(const struct http *hp);
649
ssize_t http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi);
650
const char * http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi,
651
    ssize_t len);
652
uint16_t http_GetStatus(const struct http *hp);
653
int http_IsStatus(const struct http *hp, int);
654
void http_SetStatus(struct http *to, uint16_t status, const char *reason);
655
const char *http_GetMethod(const struct http *hp);
656
int http_HdrIs(const struct http *hp, hdr_t, const char *val);
657
void http_CopyHome(const struct http *hp);
658
void http_Unset(struct http *hp, hdr_t);
659
unsigned http_CountHdr(const struct http *hp, hdr_t);
660
void http_CollectHdr(struct http *hp, hdr_t);
661
void http_CollectHdrSep(struct http *hp, hdr_t, const char *sep);
662
void http_VSL_log(const struct http *hp);
663
void HTTP_Merge(struct worker *, struct objcore *, struct http *to);
664
uint16_t HTTP_GetStatusPack(struct worker *, struct objcore *oc);
665
int HTTP_IterHdrPack(struct worker *, struct objcore *, const char **);
666
#define HTTP_FOREACH_PACK(wrk, oc, ptr) \
667
         for ((ptr) = NULL; HTTP_IterHdrPack(wrk, oc, &(ptr));)
668
const char *HTTP_GetHdrPack(struct worker *, struct objcore *, hdr_t);
669
stream_close_t http_DoConnection(struct http *hp, stream_close_t sc_close);
670
int http_IsFiltered(const struct http *hp, unsigned u, unsigned how);
671
672
#define HTTPH_R_PASS            (1 << 0)        /* Request (c->b) in pass mode */
673
#define HTTPH_R_FETCH           (1 << 1)        /* Request (c->b) for fetch */
674
#define HTTPH_A_INS             (1 << 2)        /* Response (b->o) for insert */
675
#define HTTPH_A_PASS            (1 << 3)        /* Response (b->o) for pass */
676
#define HTTPH_C_SPECIFIC        (1 << 4)        /* Connection-specific */
677
678
#define HTTPH(a, b, c) extern hdr_t b;
679
#include "tbl/http_headers.h"
680
681
extern hdr_t H__Status;
682
extern hdr_t H__Proto;
683
extern hdr_t H__Reason;
684
685
// rfc7233,l,1207,1208
686
#define http_tok_eq(s1, s2)             (!vct_casecmp(s1, s2))
687
#define http_tok_at(s1, s2, l)          (!vct_caselencmp(s1, s2, l))
688
#define http_ctok_at(s, cs)             (!vct_caselencmp(s, cs, sizeof(cs) - 1))
689
690
// rfc7230,l,1037,1038
691
#define http_scheme_at(str, tok)        http_ctok_at(str, #tok "://")
692
693
// rfc7230,l,1144,1144
694
// rfc7231,l,1156,1158
695
#define http_method_eq(str, tok)        (!strcmp(str, #tok))
696
697
// rfc7230,l,1222,1222
698
// rfc7230,l,2848,2848
699
// rfc7231,l,3883,3885
700
// rfc7234,l,1339,1340
701
// rfc7234,l,1418,1419
702
#define http_hdr_eq(s1, s2)             http_tok_eq(s1, s2)
703
#define http_hdr_at(s1, s2, l)          http_tok_at(s1, s2, l)
704
705
// rfc7230,l,1952,1952
706
// rfc7231,l,604,604
707
#define http_coding_eq(str, tok)        http_tok_eq(str, #tok)
708
709
// rfc7231,l,1864,1864
710
#define http_expect_eq(str, tok)        http_tok_eq(str, #tok)
711
712
// rfc7233,l,1207,1208
713
#define http_range_at(str, tok, l)      http_tok_at(str, #tok, l)
714
715
/* cache_lck.c */
716
717
/* Internal functions, call only through macros below */
718
void Lck__Lock(struct lock *lck, const char *p,  int l);
719
void Lck__Unlock(struct lock *lck, const char *p,  int l);
720
int Lck__Trylock(struct lock *lck, const char *p,  int l);
721
void Lck__New(struct lock *lck, struct VSC_lck *, const char *);
722
int Lck__Held(const struct lock *lck);
723
int Lck__Owned(const struct lock *lck);
724
extern pthread_mutexattr_t mtxattr_errorcheck;
725
726
/* public interface: */
727
void Lck_Delete(struct lock *lck);
728
int Lck_CondWaitUntil(pthread_cond_t *, struct lock *, vtim_real when);
729
int Lck_CondWait(pthread_cond_t *, struct lock *);
730
int Lck_CondWaitTimeout(pthread_cond_t *, struct lock *, vtim_dur timeout);
731
732
#define Lck_New(a, b) Lck__New(a, b, #b)
733
#define Lck_Lock(a) Lck__Lock(a, __func__, __LINE__)
734
#define Lck_Unlock(a) Lck__Unlock(a, __func__, __LINE__)
735
#define Lck_Trylock(a) Lck__Trylock(a, __func__, __LINE__)
736
#define Lck_AssertHeld(a)               \
737
        do {                            \
738
                assert(Lck__Held(a));   \
739
                assert(Lck__Owned(a));  \
740
        } while (0)
741
742
struct VSC_lck *Lck_CreateClass(struct vsc_seg **, const char *);
743
void Lck_DestroyClass(struct vsc_seg **);
744
745
#define LOCK(nam) extern struct VSC_lck *lck_##nam;
746
#include "tbl/locks.h"
747
748
/* cache_obj.c */
749
750
int ObjHasAttr(struct worker *, struct objcore *, enum obj_attr);
751
const void *ObjGetAttr(struct worker *, struct objcore *, enum obj_attr,
752
    ssize_t *len);
753
754
typedef int objiterate_f(void *priv, unsigned flush,
755
    const void *ptr, ssize_t len);
756
#define OBJ_ITER_FLUSH  0x01
757
#define OBJ_ITER_END    0x02
758
759
int ObjIterate(struct worker *, struct objcore *,
760
    void *priv, objiterate_f *func, int final);
761
762
vxid_t ObjGetXID(struct worker *, struct objcore *);
763
uint64_t ObjGetLen(struct worker *, struct objcore *);
764
int ObjGetDouble(struct worker *, struct objcore *, enum obj_attr, double *);
765
int ObjGetU64(struct worker *, struct objcore *, enum obj_attr, uint64_t *);
766
int ObjCheckFlag(struct worker *, struct objcore *, enum obj_flags of);
767
768
/*====================================================================
769
 * ObjVAI...(): Asynchronous Iteration
770
 *
771
 * see comments in cache_obj.c for usage
772
 */
773
774
typedef void *vai_hdl;
775
typedef void vai_notify_cb(vai_hdl, void *priv);
776
777
778
/*
779
 * VSCARAB: Varnish SCatter ARAy of Buffers:
780
 *
781
 * an array of viovs, elsewhere also called an siov or sarray
782
 */
783
struct viov {
784
        uint64_t        lease;
785
        struct iovec    iov;
786
};
787
788
struct vscarab {
789
        unsigned        magic;
790
#define VSCARAB_MAGIC   0x05ca7ab0
791
        unsigned        flags;
792
#define VSCARAB_F_END   1       // last viov is last overall
793
        unsigned        capacity;
794
        unsigned        used;
795
        struct viov     s[] v_counted_by_(capacity);
796
};
797
798
// VFLA: starting generic container-with-flexible-array-member macros
799
// aka "struct hack"
800
//
801
// type : struct name
802
// name : a pointer to struct type
803
// mag  : the magic value for this VFLA
804
// cptr : pointer to container struct (aka "head")
805
// fam  : member name of the flexible array member
806
// cap  : capacity
807
//
808
// common properties of all VFLAs:
809
// - are a miniobj (have magic as the first element)
810
// - capacity member is the fam capacity
811
// - used member is the number of fam elements used
812
//
813
// VFLA_SIZE ignores the cap == 0 case, we assert in _INIT
814
// offsetoff ref: https://gustedt.wordpress.com/2011/03/14/flexible-array-member/
815
//lint -emacro(413, VFLA_SIZE)
816
//lint -emacro(545, VFLA_SIZE) bsd offsetof() seems to be using &
817
#define VFLA_SIZE(type, fam, cap) (offsetof(struct type, fam) + \
818
        (cap) * sizeof(((struct type *)0)->fam[0]))
819
#define VFLA_INIT_(type, cptr, mag, fam, cap, save) do {        \
820
        unsigned save = (cap);                                  \
821
        AN(save);                                               \
822
        memset((cptr), 0, VFLA_SIZE(type, fam, save));          \
823
        (cptr)->magic = (mag);                                  \
824
        (cptr)->capacity = (save);                              \
825
} while (0)
826
#define VFLA_INIT(type, cptr, mag, fam, cap)                    \
827
        VFLA_INIT_(type, cptr, mag, fam, cap, VUNIQ_NAME(save))
828
// declare, allocate and initialize a local VFLA
829
// the additional VLA buf declaration avoids
830
// "Variable-sized object may not be initialized"
831
#define VFLA_LOCAL_(type, name, mag, fam, cap, bufname)                         \
832
        char bufname[VFLA_SIZE(type, fam, cap)];                                \
833
        struct type *name = (void *)bufname;                                    \
834
        VFLA_INIT(type, name, mag, fam, cap)
835
#define VFLA_LOCAL(type, name, mag, fam, cap)                                   \
836
        VFLA_LOCAL_(type, name, mag, fam, cap, VUNIQ_NAME(buf))
837
// malloc and initialize a VFLA
838
#define VFLA_ALLOC(type, name, mag, fam, cap)   do {                    \
839
        (name) = malloc(VFLA_SIZE(type, fam, cap));                     \
840
        if ((name) != NULL)                                             \
841
                VFLA_INIT(type, name, mag, fam, cap);                   \
842
} while(0)
843
#define VFLA_FOREACH(var, cptr, fam)                                            \
844
        for (var = &(cptr)->fam[0];                                             \
845
             (var = (var < &(cptr)->fam[(cptr)->used] ? var : NULL)) != NULL;   \
846
             var++)
847
// continue iterating after a break of a _FOREACH
848
#define VFLA_FOREACH_RESUME(var, cptr, fam)                                     \
849
        for (;                                                                  \
850
             var != NULL &&                                                     \
851
               (var = (var < &(cptr)->fam[(cptr)->used] ? var : NULL)) != NULL; \
852
             var++)
853
#define VFLA_GET(cptr, fam) ((cptr)->used < (cptr)->capacity ? &(cptr)->fam[(cptr)->used++] : NULL)
854
// asserts sufficient capacity
855
#define VFLA_ADD(cptr, fam, val) do {                                           \
856
        assert((cptr)->used < (cptr)->capacity);                                \
857
        (cptr)->fam[(cptr)->used++] = (val);                                    \
858
} while(0)
859
860
#define VSCARAB_SIZE(cap) VFLA_SIZE(vscarab, s, cap)
861
#define VSCARAB_INIT(scarab, cap) VFLA_INIT(vscarab, scarab, VSCARAB_MAGIC, s, cap)
862
#define VSCARAB_LOCAL(scarab, cap) VFLA_LOCAL(vscarab, scarab, VSCARAB_MAGIC, s, cap)
863
#define VSCARAB_ALLOC(scarab, cap) VFLA_ALLOC(vscarab, scarab, VSCARAB_MAGIC, s, cap)
864
#define VSCARAB_FOREACH(var, scarab) VFLA_FOREACH(var, scarab, s)
865
#define VSCARAB_FOREACH_RESUME(var, scarab) VFLA_FOREACH_RESUME(var, scarab, s)
866
#define VSCARAB_GET(scarab) VFLA_GET(scarab, s)
867
#define VSCARAB_ADD(scarab, val) VFLA_ADD(scarab, s, val)
868
//lint -emacro(64, VSCARAB_ADD_IOV_NORET) weird flexelint bug?
869
#define VSCARAB_ADD_IOV_NORET(scarab, vec)                                      \
870
        VSCARAB_ADD(scarab, ((struct viov){.lease = VAI_LEASE_NORET, .iov = (vec)}))
871
#define VSCARAB_LAST(scarab) ((scarab)->used > 0 ?                              \
872
        &(scarab)->s[(scarab)->used - 1] : NULL)
873
874
#define VSCARAB_CHECK(scarab) do {                                              \
875
        CHECK_OBJ(scarab, VSCARAB_MAGIC);                                       \
876
        assert((scarab)->used <= (scarab)->capacity);                           \
877
} while(0)
878
879
#define VSCARAB_CHECK_NOTNULL(scarab) do {                                      \
880
        AN(scarab);                                                             \
881
        VSCARAB_CHECK(scarab);                                                  \
882
} while(0)
883
884
/*
885
 * VSCARET: Varnish SCatter Array Return
886
 *
887
 * an array of leases obtained from a vscarab
888
 */
889
890
struct vscaret {
891
        unsigned        magic;
892
#define VSCARET_MAGIC   0x9c1f3d7b
893
        unsigned        capacity;
894
        unsigned        used;
895
        uint64_t        lease[] v_counted_by_(capacity);
896
};
897
898
#define VSCARET_SIZE(cap) VFLA_SIZE(vscaret, lease, cap)
899
#define VSCARET_INIT(scaret, cap) VFLA_INIT(vscaret, scaret, VSCARET_MAGIC, lease, cap)
900
#define VSCARET_LOCAL(scaret, cap) VFLA_LOCAL(vscaret, scaret, VSCARET_MAGIC, lease, cap)
901
#define VSCARET_ALLOC(scaret, cap) VFLA_ALLOC(vscaret, scaret, VSCARET_MAGIC, lease, cap)
902
#define VSCARET_FOREACH(var, scaret) VFLA_FOREACH(var, scaret, lease)
903
#define VSCARET_GET(scaret) VFLA_GET(scaret, lease)
904
#define VSCARET_ADD(scaret, val) VFLA_ADD(scaret, lease, val)
905
906
#define VSCARET_CHECK(scaret) do {                                              \
907
        CHECK_OBJ(scaret, VSCARET_MAGIC);                                       \
908
        assert(scaret->used <= scaret->capacity);                               \
909
} while(0)
910
911
#define VSCARET_CHECK_NOTNULL(scaret) do {                                      \
912
        AN(scaret);                                                             \
913
        VSCARET_CHECK(scaret);                                                  \
914
} while(0)
915
916
/*
917
 * VSCARABs can contain leases which are not to be returned to storage, for
918
 * example static data or fragments of larger leases to be returned later. For
919
 * these cases, use this magic value as the lease. This is deliberately not 0 to
920
 * catch oversights.
921
 */
922
#define VAI_LEASE_NORET ((uint64_t)0x8)
923
924
vai_hdl ObjVAIinit(struct worker *, struct objcore *, struct ws *,
925
    vai_notify_cb *, void *);
926
int ObjVAIlease(struct worker *, vai_hdl, struct vscarab *);
927
int ObjVAIbuffer(struct worker *, vai_hdl, struct vscarab *);
928
void ObjVAIreturn(struct worker *, vai_hdl, struct vscaret *);
929
void ObjVAIfini(struct worker *, vai_hdl *);
930
931
/* cache_req_body.c */
932
ssize_t VRB_Iterate(struct worker *, struct vsl_log *, struct req *,
933
    objiterate_f *func, void *priv);
934
935
/* cache_session.c [SES] */
936
937
#define SESS_ATTR(UP, low, typ, len)                                    \
938
        int SES_Get_##low(const struct sess *sp, typ **dst);
939
#include "tbl/sess_attr.h"
940
const char *SES_Get_String_Attr(const struct sess *sp, enum sess_attr a);
941
942
/* cache_shmlog.c */
943
void VSLv(enum VSL_tag_e tag, vxid_t vxid, const char *fmt, va_list va);
944
void VSL(enum VSL_tag_e tag, vxid_t vxid, const char *fmt, ...)
945
    v_printflike_(3, 4);
946
void VSLs(enum VSL_tag_e tag, vxid_t vxid, const struct strands *s);
947
void VSLbv(struct vsl_log *, enum VSL_tag_e tag, const char *fmt, va_list va);
948
void VSLb(struct vsl_log *, enum VSL_tag_e tag, const char *fmt, ...)
949
    v_printflike_(3, 4);
950
void VSLbt(struct vsl_log *, enum VSL_tag_e tag, txt t);
951
void VSLbs(struct vsl_log *, enum VSL_tag_e tag, const struct strands *s);
952
void VSLb_ts(struct vsl_log *, const char *event, vtim_real first,
953
    vtim_real *pprev, vtim_real now);
954
void VSLb_bin(struct vsl_log *, enum VSL_tag_e, ssize_t, const void*);
955
int VSL_tag_is_masked(enum VSL_tag_e tag);
956
957
static inline void
958
VSLb_ts_req(struct req *req, const char *event, vtim_real now)
959
{
960
961
        if (isnan(req->t_first) || req->t_first == 0.)
962
                req->t_first = req->t_prev = now;
963
        VSLb_ts(req->vsl, event, req->t_first, &req->t_prev, now);
964
}
965
966
static inline void
967
VSLb_ts_busyobj(struct busyobj *bo, const char *event, vtim_real now)
968
{
969
970
        if (isnan(bo->t_first) || bo->t_first == 0.)
971
                bo->t_first = bo->t_prev = now;
972
        VSLb_ts(bo->vsl, event, bo->t_first, &bo->t_prev, now);
973
}
974
975
/* cache_vcl.c */
976
const char *VCL_Name(const struct vcl *);
977
978
/* cache_wrk.c */
979
980
typedef void *bgthread_t(struct worker *, void *priv);
981
void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func,
982
    void *priv);
983
984
/* cache_ws.c */
985
void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);
986
987
unsigned WS_ReserveSize(struct ws *, unsigned);
988
unsigned WS_ReserveAll(struct ws *);
989
void WS_Release(struct ws *ws, unsigned bytes);
990
void WS_ReleaseP(struct ws *ws, const char *ptr);
991
void WS_Assert(const struct ws *ws);
992
void WS_Reset(struct ws *ws, uintptr_t);
993
void *WS_Alloc(struct ws *ws, unsigned bytes);
994
void *WS_Copy(struct ws *ws, const void *str, int len);
995
uintptr_t WS_Snapshot(struct ws *ws);
996
int WS_Allocated(const struct ws *ws, const void *ptr, ssize_t len);
997
unsigned WS_Dump(const struct ws *ws, char, size_t off, void *buf, size_t len);
998
999
static inline void *
1000 32
WS_Reservation(const struct ws *ws)
1001
{
1002
1003 32
        WS_Assert(ws);
1004 32
        AN(ws->r);
1005 32
        AN(ws->f);
1006 32
        return (ws->f);
1007
}
1008
1009
static inline unsigned
1010
WS_ReservationSize(const struct ws *ws)
1011
{
1012
1013
        AN(ws->r);
1014
        return (ws->r - ws->f);
1015
}
1016
1017
static inline unsigned
1018
WS_ReserveLumps(struct ws *ws, size_t sz)
1019
{
1020
1021
        AN(sz);
1022
        return (WS_ReserveAll(ws) / sz);
1023
}
1024
1025
/* cache_ws_common.c */
1026
void WS_MarkOverflow(struct ws *ws);
1027
int WS_Overflowed(const struct ws *ws);
1028
1029
const char *WS_Printf(struct ws *ws, const char *fmt, ...) v_printflike_(2, 3);
1030
1031
void WS_VSB_new(struct vsb *, struct ws *);
1032
char *WS_VSB_finish(struct vsb *, struct ws *, size_t *);
1033
1034
/* WS utility */
1035
#define WS_TASK_ALLOC_OBJ(ctx, ptr, magic) do {                 \
1036
        ptr = WS_Alloc((ctx)->ws, sizeof *(ptr));               \
1037
        if ((ptr) == NULL)                                      \
1038
                VRT_fail(ctx, "Out of workspace for " #magic);  \
1039
        else                                                    \
1040
                INIT_OBJ(ptr, magic);                           \
1041
} while(0)
1042
1043
/* cache_rfc2616.c */
1044
void RFC2616_Ttl(struct busyobj *, vtim_real now, vtim_real *t_origin,
1045
    float *ttl, float *grace, float *keep);
1046
unsigned RFC2616_Req_Gzip(const struct http *);
1047
int RFC2616_Do_Cond(const struct req *sp);
1048
void RFC2616_Weaken_Etag(struct http *hp);
1049
void RFC2616_Vary_AE(struct http *hp);
1050
const char * RFC2616_Strong_LM(const struct http *hp, struct worker *wrk,
1051
    struct objcore *oc);
1052
1053
/*
1054
 * We want to cache the most recent timestamp in wrk->lastused to avoid
1055
 * extra timestamps in cache_pool.c.  Hide this detail with a macro
1056
 */
1057
#define W_TIM_real(w) ((w)->lastused = VTIM_real())