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