Coverage Report

Created: 2026-02-14 06:03

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