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