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
45
#include "vdef.h"
46
#include "vrt.h"
47
48
#define CACHE_H_INCLUDED        // After vrt.h include.
49
50
#include "miniobj.h"
51
#include "vas.h"
52
#include "vqueue.h"
53
#include "vtree.h"
54
55
#include "vapi/vsl_int.h"
56
57
/*--------------------------------------------------------------------*/
58
59
struct vxids {
60
        uint64_t        vxid;
61
};
62
63
typedef struct vxids vxid_t;
64
65
#define NO_VXID ((struct vxids){0})
66
#define IS_NO_VXID(x) ((x).vxid == 0)
67
#define VXID_TAG(x) ((uintmax_t)((x).vxid & (VSL_CLIENTMARKER|VSL_BACKENDMARKER)))
68
#define VXID(u) ((uintmax_t)((u.vxid) & VSL_IDENTMASK))
69
#define IS_SAME_VXID(x, y) ((x).vxid == (y).vxid)
70
71
/*--------------------------------------------------------------------*/
72
73
struct body_status {
74
        const char              *name;
75
        int                     nbr;
76
        int                     avail;
77
        int                     length_known;
78
};
79
80
#define BODYSTATUS(U, l, n, a, k) extern const struct body_status BS_##U[1];
81
#include "tbl/body_status.h"
82
83
typedef const struct body_status *body_status_t;
84
85
typedef const char *hdr_t;
86
87
/*--------------------------------------------------------------------*/
88
89
struct stream_close {
90
        unsigned                magic;
91
#define STREAM_CLOSE_MAGIC      0xc879c93d
92
        int                     idx;
93
        unsigned                is_err;
94
        const char              *name;
95
        const char              *desc;
96
};
97
    extern const struct stream_close SC_NULL[1];
98
#define SESS_CLOSE(nm, stat, err, desc) \
99
    extern const struct stream_close SC_##nm[1];
100
#include "tbl/sess_close.h"
101
102
103
/*--------------------------------------------------------------------
104
 * Indices into http->hd[]
105
 */
106
enum {
107
#define SLTH(tag, ind, req, resp, sdesc, ldesc) ind,
108
#include "tbl/vsl_tags_http.h"
109
};
110
111
/*--------------------------------------------------------------------*/
112
113
struct ban;
114
struct ban_proto;
115
struct cli;
116
struct http_conn;
117
struct listen_sock;
118
struct mempool;
119
struct objcore;
120
struct objhead;
121
struct pool;
122
struct req_step;
123
struct sess;
124
struct transport;
125
struct vcf;
126
struct VSC_lck;
127
struct VSC_main;
128
struct VSC_main_wrk;
129
struct worker;
130
struct worker_priv;
131
132
#define DIGEST_LEN              32
133
134
/*--------------------------------------------------------------------*/
135
136
struct lock { void *priv; };    // Opaque
137
138
/*--------------------------------------------------------------------
139
 * Workspace structure for quick memory allocation.
140
 */
141
142
#define WS_ID_SIZE 4
143
144
struct ws {
145
        unsigned                magic;
146
#define WS_MAGIC                0x35fac554
147
        char                    id[WS_ID_SIZE]; /* identity */
148
        char                    *s;             /* (S)tart of buffer */
149
        char                    *f;             /* (F)ree/front pointer */
150
        char                    *r;             /* (R)eserved length */
151
        char                    *e;             /* (E)nd of buffer */
152
};
153
154
/*--------------------------------------------------------------------
155
 *
156
 */
157
158
struct http {
159
        unsigned                magic;
160
#define HTTP_MAGIC              0x6428b5c9
161
162
        uint16_t                shd;            /* Size of hd space */
163
        txt                     *hd;
164
        unsigned char           *hdf;
165
#define HDF_FILTER              (1 << 0)        /* Filtered by Connection */
166
167
        /* NB: ->nhd and below zeroed/initialized by http_Teardown */
168
        uint16_t                nhd;            /* Next free hd */
169
170
        enum VSL_tag_e          logtag;         /* Must be SLT_*Method */
171
        struct vsl_log          *vsl;
172
173
        struct ws               *ws;
174
        uint16_t                status;
175
        uint8_t                 protover;
176
};
177
178
/*--------------------------------------------------------------------*/
179
180
struct acct_req {
181
#define ACCT(foo)       uint64_t        foo;
182
#include "tbl/acct_fields_req.h"
183
};
184
185
/*--------------------------------------------------------------------*/
186
187
struct acct_bereq {
188
#define ACCT(foo)       uint64_t        foo;
189
#include "tbl/acct_fields_bereq.h"
190
};
191
192
/*--------------------------------------------------------------------*/
193
194
struct vsl_log {
195
        uint32_t                *wlb, *wlp, *wle;
196
        unsigned                wlr;
197
        vxid_t                  wid;
198
};
199
200
/*--------------------------------------------------------------------*/
201
202
VRBT_HEAD(vrt_privs, vrt_priv);
203
204
/* Worker pool stuff -------------------------------------------------*/
205
206
typedef void task_func_t(struct worker *wrk, void *priv);
207
208
struct pool_task {
209
        VTAILQ_ENTRY(pool_task)         list;
210
        task_func_t                     *func;
211
        void                            *priv;
212
};
213
214
/*
215
 * tasks are taken off the queues in this order
216
 *
217
 * TASK_QUEUE_{REQ|STR} are new req's (H1/H2), and subject to queue limit.
218
 *
219
 * TASK_QUEUE_RUSH is req's returning from waiting list
220
 *
221
 * NOTE: When changing the number of classes, update places marked with
222
 * TASK_QUEUE_RESERVE in params.h
223
 */
224
enum task_prio {
225
        TASK_QUEUE_BO,
226
        TASK_QUEUE_RUSH,
227
        TASK_QUEUE_REQ,
228
        TASK_QUEUE_STR,
229
        TASK_QUEUE_VCA,
230
        TASK_QUEUE_BG,
231
        TASK_QUEUE__END
232
};
233
234
#define TASK_QUEUE_HIGHEST_PRIORITY TASK_QUEUE_BO
235
#define TASK_QUEUE_RESERVE TASK_QUEUE_BG
236
#define TASK_QUEUE_LIMITED(prio) \
237
        (prio == TASK_QUEUE_REQ || prio == TASK_QUEUE_STR)
238
239
/*--------------------------------------------------------------------*/
240
241
struct worker {
242
        unsigned                magic;
243
#define WORKER_MAGIC            0x6391adcf
244
        int                     strangelove;
245
        struct worker_priv      *wpriv;
246
        struct pool             *pool;
247
        struct VSC_main_wrk     *stats;
248
        struct vsl_log          *vsl;           // borrowed from req/bo
249
250
        struct pool_task        task[1];
251
252
        vtim_real               lastused;
253
254
        struct v1l              *v1l;
255
256
        pthread_cond_t          cond;
257
258
        struct ws               aws[1];
259
260
        unsigned                cur_method;
261
        unsigned                seen_methods;
262
263
        struct wrk_vpi          *vpi;
264
};
265
266
/* Stored object -----------------------------------------------------
267
 * This is just to encapsulate the fields owned by the stevedore
268
 */
269
270
struct storeobj {
271
        const struct stevedore  *stevedore;
272
        void                    *priv;
273
        uint64_t                priv2;
274
};
275
276
/* Busy Objcore structure --------------------------------------------
277
 *
278
 */
279
280
/*
281
 * The macro-states we expose outside the fetch code
282
 */
283
enum boc_state_e {
284
#define BOC_STATE(U, l)       BOS_##U,
285
#include "tbl/boc_state.h"
286
};
287
288
struct boc {
289
        unsigned                magic;
290
#define BOC_MAGIC               0x70c98476
291
        unsigned                refcount;
292
        struct lock             mtx;
293
        pthread_cond_t          cond;
294
        void                    *stevedore_priv;
295
        enum boc_state_e        state;
296
        uint8_t                 *vary;
297
        uint64_t                fetched_so_far;
298
        uint64_t                delivered_so_far;
299
        uint64_t                transit_buffer;
300
};
301
302
/* Object core structure ---------------------------------------------
303
 * Objects have sideways references in the binary heap and the LRU list
304
 * and we want to avoid paging in a lot of objects just to move them up
305
 * or down the binheap or to move a unrelated object on the LRU list.
306
 * To avoid this we use a proxy object, objcore, to hold the relevant
307
 * housekeeping fields parts of an object.
308
 */
309
310
enum obj_attr {
311
#define OBJ_FIXATTR(U, l, s)    OA_##U,
312
#define OBJ_VARATTR(U, l)       OA_##U,
313
#define OBJ_AUXATTR(U, l)       OA_##U,
314
#include "tbl/obj_attr.h"
315
                                OA__MAX,
316
};
317
318
enum obj_flags {
319
#define OBJ_FLAG(U, l, v)       OF_##U = v,
320
#include "tbl/obj_attr.h"
321
};
322
323
enum oc_flags {
324
#define OC_FLAG(U, l, v)        OC_F_##U = v,
325
#include "tbl/oc_flags.h"
326
};
327
328
#define OC_F_TRANSIENT (OC_F_PRIVATE | OC_F_HFM | OC_F_HFP)
329
330
enum oc_exp_flags {
331
#define OC_EXP_FLAG(U, l, v)    OC_EF_##U = v,
332
#include "tbl/oc_exp_flags.h"
333
};
334
335
struct objcore {
336
        unsigned                magic;
337
#define OBJCORE_MAGIC           0x4d301302
338
        int                     refcnt;
339
        struct storeobj         stobj[1];
340
        struct objhead          *objhead;
341
        struct boc              *boc;
342
        vtim_real               timer_when;
343
        VCL_INT                 hits;
344
345
346
        vtim_real               t_origin;
347
        float                   ttl;
348
        float                   grace;
349
        float                   keep;
350
351
        uint8_t                 flags;
352
353
        uint8_t                 exp_flags;
354
355
        uint16_t                oa_present;
356
357
        unsigned                timer_idx;      // XXX 4Gobj limit
358
        vtim_real               last_lru;
359
        VTAILQ_ENTRY(objcore)   hsh_list;
360
        VTAILQ_ENTRY(objcore)   lru_list;
361
        VTAILQ_ENTRY(objcore)   ban_list;
362
        VSTAILQ_ENTRY(objcore)  exp_list;
363
        struct ban              *ban;
364
};
365
366
/* Busy Object structure ---------------------------------------------
367
 *
368
 * The busyobj structure captures the aspects of an object related to,
369
 * and while it is being fetched from the backend.
370
 *
371
 * One of these aspects will be how much has been fetched, which
372
 * streaming delivery will make use of.
373
 */
374
375
enum director_state_e {
376
        DIR_S_NULL = 0,
377
        DIR_S_HDRS = 1,
378
        DIR_S_BODY = 2,
379
};
380
381
struct busyobj {
382
        unsigned                magic;
383
#define BUSYOBJ_MAGIC           0x23b95567
384
385
        char                    *end;
386
387
        /*
388
         * All fields from retries and down are zeroed when the busyobj
389
         * is recycled.
390
         */
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
#define BERESP_FLAG(l, r, w, f, d) unsigned     l:1;
418
#define BEREQ_FLAG(l, r, w, d) BERESP_FLAG(l, r, w, 0, d)
419
#include "tbl/bereq_flags.h"
420
#include "tbl/beresp_flags.h"
421
422
        /* Timeouts */
423
        vtim_dur                connect_timeout;
424
        vtim_dur                first_byte_timeout;
425
        vtim_dur                between_bytes_timeout;
426
        vtim_dur                task_deadline;
427
428
        /* Timers */
429
        vtim_real               t_first;        /* First timestamp logged */
430
        vtim_real               t_resp;         /* response received */
431
        vtim_real               t_prev;         /* Previous timestamp logged */
432
433
        /* Acct */
434
        struct acct_bereq       acct;
435
436
        const struct stevedore  *storage;
437
        const struct director   *director_req;
438
        const struct director   *director_resp;
439
        enum director_state_e   director_state;
440
        struct vcl              *vcl;
441
442
        struct vsl_log          vsl[1];
443
444
        uint8_t                 digest[DIGEST_LEN];
445
        struct vrt_privs        privs[1];
446
447
        uint16_t                err_code;
448
        const char              *err_reason;
449
450
        const char              *client_identity;
451
};
452
453
#define BUSYOBJ_TMO(bo, pfx, tmo)                                       \
454
        (isnan((bo)->tmo) ? cache_param->pfx##tmo : (bo)->tmo)
455
456
457
/*--------------------------------------------------------------------*/
458
459
struct reqtop {
460
        unsigned                magic;
461
#define REQTOP_MAGIC            0x57fbda52
462
        struct req              *topreq;
463
        struct vcl              *vcl0;
464
        struct vrt_privs        privs[1];
465
};
466
467
struct req {
468
        unsigned                magic;
469
#define REQ_MAGIC               0xfb4abf6d
470
471
        body_status_t           req_body_status;
472
        stream_close_t          doclose;
473
        unsigned                restarts;
474
        unsigned                esi_level;
475
476
        /* Delivery mode */
477
        unsigned                res_mode;
478
#define RES_ESI                 (1<<4)
479
#define RES_PIPE                (1<<7)
480
481
        const struct req_step   *req_step;
482
        struct reqtop           *top;   /* esi_level == 0 request */
483
484
#define REQ_FLAG(l, r, w, d) unsigned   l:1;
485
#include "tbl/req_flags.h"
486
487
        uint16_t                err_code;
488
        const char              *err_reason;
489
490
        struct sess             *sp;
491
        struct worker           *wrk;
492
        struct pool_task        task[1];
493
494
        const struct transport  *transport;
495
        void                    *transport_priv;
496
497
        VTAILQ_ENTRY(req)       w_list;
498
499
        struct objcore          *body_oc;
500
501
        /* The busy objhead we sleep on */
502
        struct objhead          *hash_objhead;
503
504
        /* Built Vary string == workspace reservation */
505
        uint8_t                 *vary_b;
506
        uint8_t                 *vary_e;
507
508
        uint8_t                 digest[DIGEST_LEN];
509
510
        vtim_dur                d_ttl;
511
        vtim_dur                d_grace;
512
513
        const struct stevedore  *storage;
514
515
        const struct director   *director_hint;
516
        struct vcl              *vcl;
517
518
        uintptr_t               ws_req;         /* WS above request data */
519
520
        /* Timestamps */
521
        vtim_real               t_first;        /* First timestamp logged */
522
        vtim_real               t_prev;         /* Previous timestamp logged */
523
        vtim_real               t_req;          /* Headers complete */
524
        vtim_real               t_resp;         /* Entry to last deliver/synth */
525
526
        struct http_conn        *htc;
527
        struct vfp_ctx          *vfc;
528
        const char              *client_identity;
529
530
        /* HTTP request */
531
        struct http             *http;
532
        struct http             *http0;
533
534
        /* HTTP response */
535
        struct http             *resp;
536
        intmax_t                resp_len;
537
538
        struct ws               ws[1];
539
        struct objcore          *objcore;
540
        struct objcore          *stale_oc;
541
542
        /* resp.body */
543
        struct vdp_ctx          *vdc;
544
        const char              *vdp_filter_list;
545
        /* req.body */
546
        const char              *vfp_filter_list;
547
548
        /* Transaction VSL buffer */
549
        struct vsl_log          vsl[1];
550
551
        /* Temporary accounting */
552
        struct acct_req         acct;
553
554
        struct vrt_privs        privs[1];
555
556
        struct vcf              *vcf;
557
};
558
559
#define IS_TOPREQ(req) ((req)->top->topreq == (req))
560
561
/*--------------------------------------------------------------------
562
 * Struct sess is a high memory-load structure because sessions typically
563
 * hang around the waiter for relatively long time.
564
 *
565
 * The size goal for struct sess + struct memitem is <512 bytes
566
 *
567
 * Getting down to the next relevant size (<256 bytes because of how malloc
568
 * works, is not realistic without a lot of code changes.
569
 */
570
571
enum sess_attr {
572
#define SESS_ATTR(UP, low, typ, len)    SA_##UP,
573
#include "tbl/sess_attr.h"
574
        SA_LAST
575
};
576
577
struct sess {
578
        unsigned                magic;
579
#define SESS_MAGIC              0x2c2f9c5a
580
581
        uint16_t                sattr[SA_LAST];
582
        struct listen_sock      *listen_sock;
583
        int                     refcnt;
584
        int                     fd;
585
        vxid_t                  vxid;
586
587
        struct lock             mtx;
588
589
        struct pool             *pool;
590
591
        struct ws               ws[1];
592
593
        vtim_real               t_open;         /* fd accepted */
594
        vtim_real               t_idle;         /* fd accepted or resp sent */
595
        vtim_dur                timeout_idle;
596
        vtim_dur                timeout_linger;
597
        vtim_dur                send_timeout;
598
        vtim_dur                idle_send_timeout;
599
};
600
601
#define SESS_TMO(sp, tmo)                                       \
602
        (isnan((sp)->tmo) ? cache_param->tmo : (sp)->tmo)
603
604
/* Prototypes etc ----------------------------------------------------*/
605
606
607
/* cache_ban.c */
608
609
/* for constructing bans */
610
struct ban_proto *BAN_Build(void);
611
const char *BAN_AddTest(struct ban_proto *,
612
    const char *, const char *, const char *);
613
const char *BAN_Commit(struct ban_proto *b);
614
void BAN_Abandon(struct ban_proto *b);
615
616
/* cache_cli.c [CLI] */
617
extern pthread_t cli_thread;
618
#define IS_CLI() (pthread_equal(pthread_self(), cli_thread))
619
#define ASSERT_CLI() do {assert(IS_CLI());} while (0)
620
621
/* cache_http.c */
622
unsigned HTTP_estimate(unsigned nhttp);
623
void HTTP_Clone(struct http *to, const struct http * const fm);
624
void HTTP_Dup(struct http *to, const struct http * const fm);
625
struct http *HTTP_create(void *p, uint16_t nhttp, unsigned);
626
const char *http_Status2Reason(unsigned, const char **);
627
int http_IsHdr(const txt *hh, hdr_t hdr);
628
unsigned http_EstimateWS(const struct http *fm, unsigned how);
629
void http_PutResponse(struct http *to, const char *proto, uint16_t status,
630
    const char *response);
631
void http_FilterReq(struct http *to, const struct http *fm, unsigned how);
632
void HTTP_Encode(const struct http *fm, uint8_t *, unsigned len, unsigned how);
633
int HTTP_Decode(struct http *to, const uint8_t *fm);
634
void http_ForceHeader(struct http *to, hdr_t, const char *val);
635
void http_AppendHeader(struct http *to, hdr_t, const char *val);
636
void http_PrintfHeader(struct http *to, const char *fmt, ...)
637
    v_printflike_(2, 3);
638
void http_TimeHeader(struct http *to, const char *fmt, vtim_real now);
639
const char * http_ViaHeader(void);
640
void http_Proto(struct http *to);
641
void http_SetHeader(struct http *to, const char *header);
642
void http_SetH(struct http *to, unsigned n, const char *header);
643
void http_ForceField(struct http *to, unsigned n, const char *t);
644
void HTTP_Setup(struct http *, struct ws *, struct vsl_log *, enum VSL_tag_e);
645
void http_Teardown(struct http *ht);
646
int http_GetHdr(const struct http *hp, hdr_t, const char **ptr);
647
int http_GetHdrToken(const struct http *hp, hdr_t,
648
    const char *token, const char **pb, const char **pe);
649
int http_GetHdrField(const struct http *hp, hdr_t,
650
    const char *field, const char **ptr);
651
double http_GetHdrQ(const struct http *hp, hdr_t, const char *field);
652
ssize_t http_GetContentLength(const struct http *hp);
653
ssize_t http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi);
654
const char * http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi,
655
    ssize_t len);
656
uint16_t http_GetStatus(const struct http *hp);
657
int http_IsStatus(const struct http *hp, int);
658
void http_SetStatus(struct http *to, uint16_t status, const char *reason);
659
const char *http_GetMethod(const struct http *hp);
660
int http_HdrIs(const struct http *hp, hdr_t, const char *val);
661
void http_CopyHome(const struct http *hp);
662
void http_Unset(struct http *hp, hdr_t);
663
unsigned http_CountHdr(const struct http *hp, hdr_t);
664
void http_CollectHdr(struct http *hp, hdr_t);
665
void http_CollectHdrSep(struct http *hp, hdr_t, const char *sep);
666
void http_VSL_log(const struct http *hp);
667
void HTTP_Merge(struct worker *, struct objcore *, struct http *to);
668
uint16_t HTTP_GetStatusPack(struct worker *, struct objcore *oc);
669
int HTTP_IterHdrPack(struct worker *, struct objcore *, const char **);
670
#define HTTP_FOREACH_PACK(wrk, oc, ptr) \
671
         for ((ptr) = NULL; HTTP_IterHdrPack(wrk, oc, &(ptr));)
672
const char *HTTP_GetHdrPack(struct worker *, struct objcore *, hdr_t);
673
stream_close_t http_DoConnection(struct http *hp, stream_close_t sc_close);
674
int http_IsFiltered(const struct http *hp, unsigned u, unsigned how);
675
676
#define HTTPH_R_PASS            (1 << 0)        /* Request (c->b) in pass mode */
677
#define HTTPH_R_FETCH           (1 << 1)        /* Request (c->b) for fetch */
678
#define HTTPH_A_INS             (1 << 2)        /* Response (b->o) for insert */
679
#define HTTPH_A_PASS            (1 << 3)        /* Response (b->o) for pass */
680
#define HTTPH_C_SPECIFIC        (1 << 4)        /* Connection-specific */
681
682
#define HTTPH(a, b, c) extern char b[];
683
#include "tbl/http_headers.h"
684
685
extern const char H__Status[];
686
extern const char H__Proto[];
687
extern const char H__Reason[];
688
689
// rfc7233,l,1207,1208
690
#define http_tok_eq(s1, s2)             (!vct_casecmp(s1, s2))
691
#define http_tok_at(s1, s2, l)          (!vct_caselencmp(s1, s2, l))
692
#define http_ctok_at(s, cs)             (!vct_caselencmp(s, cs, sizeof(cs) - 1))
693
694
// rfc7230,l,1037,1038
695
#define http_scheme_at(str, tok)        http_ctok_at(str, #tok "://")
696
697
// rfc7230,l,1144,1144
698
// rfc7231,l,1156,1158
699
#define http_method_eq(str, tok)        (!strcmp(str, #tok))
700
701
// rfc7230,l,1222,1222
702
// rfc7230,l,2848,2848
703
// rfc7231,l,3883,3885
704
// rfc7234,l,1339,1340
705
// rfc7234,l,1418,1419
706
#define http_hdr_eq(s1, s2)             http_tok_eq(s1, s2)
707
#define http_hdr_at(s1, s2, l)          http_tok_at(s1, s2, l)
708
709
// rfc7230,l,1952,1952
710
// rfc7231,l,604,604
711
#define http_coding_eq(str, tok)        http_tok_eq(str, #tok)
712
713
// rfc7231,l,1864,1864
714
#define http_expect_eq(str, tok)        http_tok_eq(str, #tok)
715
716
// rfc7233,l,1207,1208
717
#define http_range_at(str, tok, l)      http_tok_at(str, #tok, l)
718
719
/* cache_lck.c */
720
721
/* Internal functions, call only through macros below */
722
void Lck__Lock(struct lock *lck, const char *p,  int l);
723
void Lck__Unlock(struct lock *lck, const char *p,  int l);
724
int Lck__Trylock(struct lock *lck, const char *p,  int l);
725
void Lck__New(struct lock *lck, struct VSC_lck *, const char *);
726
int Lck__Held(const struct lock *lck);
727
int Lck__Owned(const struct lock *lck);
728
extern pthread_mutexattr_t mtxattr_errorcheck;
729
730
/* public interface: */
731
void Lck_Delete(struct lock *lck);
732
int Lck_CondWaitUntil(pthread_cond_t *, struct lock *, vtim_real when);
733
int Lck_CondWait(pthread_cond_t *, struct lock *);
734
int Lck_CondWaitTimeout(pthread_cond_t *, struct lock *, vtim_dur timeout);
735
736
#define Lck_New(a, b) Lck__New(a, b, #b)
737
#define Lck_Lock(a) Lck__Lock(a, __func__, __LINE__)
738
#define Lck_Unlock(a) Lck__Unlock(a, __func__, __LINE__)
739
#define Lck_Trylock(a) Lck__Trylock(a, __func__, __LINE__)
740
#define Lck_AssertHeld(a)               \
741
        do {                            \
742
                assert(Lck__Held(a));   \
743
                assert(Lck__Owned(a));  \
744
        } while (0)
745
746
struct VSC_lck *Lck_CreateClass(struct vsc_seg **, const char *);
747
void Lck_DestroyClass(struct vsc_seg **);
748
749
#define LOCK(nam) extern struct VSC_lck *lck_##nam;
750
#include "tbl/locks.h"
751
752
/* cache_obj.c */
753
754
int ObjHasAttr(struct worker *, struct objcore *, enum obj_attr);
755
const void *ObjGetAttr(struct worker *, struct objcore *, enum obj_attr,
756
    ssize_t *len);
757
758
typedef int objiterate_f(void *priv, unsigned flush,
759
    const void *ptr, ssize_t len);
760
#define OBJ_ITER_FLUSH  0x01
761
#define OBJ_ITER_END    0x02
762
763
int ObjIterate(struct worker *, struct objcore *,
764
    void *priv, objiterate_f *func, int final);
765
766
vxid_t ObjGetXID(struct worker *, struct objcore *);
767
uint64_t ObjGetLen(struct worker *, struct objcore *);
768
int ObjGetDouble(struct worker *, struct objcore *, enum obj_attr, double *);
769
int ObjGetU64(struct worker *, struct objcore *, enum obj_attr, uint64_t *);
770
int ObjCheckFlag(struct worker *, struct objcore *, enum obj_flags of);
771
772
/* cache_req_body.c */
773
ssize_t VRB_Iterate(struct worker *, struct vsl_log *, struct req *,
774
    objiterate_f *func, void *priv);
775
776
/* cache_session.c [SES] */
777
778
#define SESS_ATTR(UP, low, typ, len)                                    \
779
        int SES_Get_##low(const struct sess *sp, typ **dst);
780
#include "tbl/sess_attr.h"
781
const char *SES_Get_String_Attr(const struct sess *sp, enum sess_attr a);
782
783
/* cache_shmlog.c */
784
void VSLv(enum VSL_tag_e tag, vxid_t vxid, const char *fmt, va_list va);
785
void VSL(enum VSL_tag_e tag, vxid_t vxid, const char *fmt, ...)
786
    v_printflike_(3, 4);
787
void VSLs(enum VSL_tag_e tag, vxid_t vxid, const struct strands *s);
788
void VSLbv(struct vsl_log *, enum VSL_tag_e tag, const char *fmt, va_list va);
789
void VSLb(struct vsl_log *, enum VSL_tag_e tag, const char *fmt, ...)
790
    v_printflike_(3, 4);
791
void VSLbt(struct vsl_log *, enum VSL_tag_e tag, txt t);
792
void VSLbs(struct vsl_log *, enum VSL_tag_e tag, const struct strands *s);
793
void VSLb_ts(struct vsl_log *, const char *event, vtim_real first,
794
    vtim_real *pprev, vtim_real now);
795
void VSLb_bin(struct vsl_log *, enum VSL_tag_e, ssize_t, const void*);
796
int VSL_tag_is_masked(enum VSL_tag_e tag);
797
798
static inline void
799 660032
VSLb_ts_req(struct req *req, const char *event, vtim_real now)
800
{
801
802 660032
        if (isnan(req->t_first) || req->t_first == 0.)
803 14696
                req->t_first = req->t_prev = now;
804 659986
        VSLb_ts(req->vsl, event, req->t_first, &req->t_prev, now);
805 659986
}
806
807
static inline void
808 600219
VSLb_ts_busyobj(struct busyobj *bo, const char *event, vtim_real now)
809
{
810
811 600219
        if (isnan(bo->t_first) || bo->t_first == 0.)
812 89122
                bo->t_first = bo->t_prev = now;
813 600213
        VSLb_ts(bo->vsl, event, bo->t_first, &bo->t_prev, now);
814 600213
}
815
816
/* cache_vcl.c */
817
const char *VCL_Name(const struct vcl *);
818
819
/* cache_wrk.c */
820
821
typedef void *bgthread_t(struct worker *, void *priv);
822
void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func,
823
    void *priv);
824
825
/* cache_ws.c */
826
void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);
827
828
unsigned WS_ReserveSize(struct ws *, unsigned);
829
unsigned WS_ReserveAll(struct ws *);
830
void WS_Release(struct ws *ws, unsigned bytes);
831
void WS_ReleaseP(struct ws *ws, const char *ptr);
832
void WS_Assert(const struct ws *ws);
833
void WS_Reset(struct ws *ws, uintptr_t);
834
void *WS_Alloc(struct ws *ws, unsigned bytes);
835
void *WS_Copy(struct ws *ws, const void *str, int len);
836
uintptr_t WS_Snapshot(struct ws *ws);
837
int WS_Allocated(const struct ws *ws, const void *ptr, ssize_t len);
838
unsigned WS_Dump(const struct ws *ws, char, size_t off, void *buf, size_t len);
839
840
static inline void *
841 3857662
WS_Reservation(const struct ws *ws)
842
{
843
844 3857662
        WS_Assert(ws);
845 3857662
        AN(ws->r);
846 3857662
        AN(ws->f);
847 3857662
        return (ws->f);
848
}
849
850
static inline unsigned
851 933772
WS_ReservationSize(const struct ws *ws)
852
{
853
854 933772
        AN(ws->r);
855 933772
        return (ws->r - ws->f);
856
}
857
858
static inline unsigned
859 199259
WS_ReserveLumps(struct ws *ws, size_t sz)
860
{
861
862 199259
        AN(sz);
863 199259
        return (WS_ReserveAll(ws) / sz);
864
}
865
866
/* cache_ws_common.c */
867
void WS_MarkOverflow(struct ws *ws);
868
int WS_Overflowed(const struct ws *ws);
869
870
const char *WS_Printf(struct ws *ws, const char *fmt, ...) v_printflike_(2, 3);
871
872
void WS_VSB_new(struct vsb *, struct ws *);
873
char *WS_VSB_finish(struct vsb *, struct ws *, size_t *);
874
875
/* WS utility */
876
#define WS_TASK_ALLOC_OBJ(ctx, ptr, magic) do {                 \
877
        ptr = WS_Alloc((ctx)->ws, sizeof *(ptr));               \
878
        if ((ptr) == NULL)                                      \
879
                VRT_fail(ctx, "Out of workspace for " #magic);  \
880
        else                                                    \
881
                INIT_OBJ(ptr, magic);                           \
882
} while(0)
883
884
/* cache_rfc2616.c */
885
void RFC2616_Ttl(struct busyobj *, vtim_real now, vtim_real *t_origin,
886
    float *ttl, float *grace, float *keep);
887
unsigned RFC2616_Req_Gzip(const struct http *);
888
int RFC2616_Do_Cond(const struct req *sp);
889
void RFC2616_Weaken_Etag(struct http *hp);
890
void RFC2616_Vary_AE(struct http *hp);
891
const char * RFC2616_Strong_LM(const struct http *hp, struct worker *wrk,
892
    struct objcore *oc);
893
894
/*
895
 * We want to cache the most recent timestamp in wrk->lastused to avoid
896
 * extra timestamps in cache_pool.c.  Hide this detail with a macro
897
 */
898
#define W_TIM_real(w) ((w)->lastused = VTIM_real())