Code Coverage: vinyl-cache/bin/vinyld/cache/cache_ban.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
 * Ban processing
31
 *
32
 * A ban consists of a number of conditions (or tests), all of which must be
33
 * satisfied.  Here are some potential bans we could support:
34
 *
35
 *      req.url == "/foo"
36
 *      req.url ~ ".iso" && obj.size > 10MB
37
 *      req.http.host ~ "web1.com" && obj.http.set-cookie ~ "USER=29293"
38
 *
39
 * We make the "&&" mandatory from the start, leaving the syntax space
40
 * for latter handling of "||" as well.
41
 *
42
 * Bans are compiled into bytestrings as follows:
43
 *      8 bytes - double: timestamp             XXX: Byteorder ?
44
 *      4 bytes - be32: length
45
 *      1 byte - flags: 0x01: BAN_F_{REQ|OBJ|COMPLETED}
46
 *      N tests
47
 * A test have this form:
48
 *      1 byte - arg (see ban_vars.h col 3 "BANS_ARG_XXX")
49
 *      (n bytes) - http header name, canonical encoding
50
 *      lump - comparison arg
51
 *      1 byte - operation (BANS_OPER_)
52
 *      (lump) - compiled regexp
53
 * A lump is:
54
 *      4 bytes - be32: length
55
 *      n bytes - content
56
 *
57
 */
58
59
/*--------------------------------------------------------------------
60
 * BAN string defines & magic markers
61
 */
62
63 2116
#define BANS_TIMESTAMP          0
64 8832
#define BANS_LENGTH             8
65 2451
#define BANS_FLAGS              12
66 4997
#define BANS_HEAD_LEN           16
67
68 3551
#define BANS_FLAG_REQ           (1<<0)
69 1295
#define BANS_FLAG_OBJ           (1<<1)
70 6981
#define BANS_FLAG_COMPLETED     (1<<2)
71 1957
#define BANS_FLAG_HTTP          (1<<3)
72 149
#define BANS_FLAG_DURATION      (1<<4)
73 170
#define BANS_FLAG_NODEDUP       (1<<5)
74 119
#define BANS_FLAG_BOOL          (1<<6)
75
76 112113
#define BANS_OPER_EQ            0x10
77 111882
#define BANS_OPER_OFF_          BANS_OPER_EQ
78 33
#define BANS_OPER_NEQ           0x11
79 614
#define BANS_OPER_MATCH         0x12
80 215
#define BANS_OPER_NMATCH        0x13
81 8
#define BANS_OPER_GT            0x14
82 0
#define BANS_OPER_GTE           0x15
83 3
#define BANS_OPER_LT            0x16
84 111149
#define BANS_OPER_LTE           0x17
85 111148
#define BANS_OPER_LIM_          (BANS_OPER_LTE + 1)
86
87 416
#define BAN_OPERIDX(x) ((x) - BANS_OPER_OFF_)
88 111148
#define BAN_OPERARRSZ  (BANS_OPER_LIM_ - BANS_OPER_OFF_)
89 259
#define ASSERT_BAN_OPER(x) assert((x) >= BANS_OPER_OFF_ && (x) < BANS_OPER_LIM_)
90
91 40300
#define BANS_ARG_URL            0x18
92 40273
#define BANS_ARG_OFF_           BANS_ARG_URL
93 1323
#define BANS_ARG_REQHTTP        0x19
94 628
#define BANS_ARG_OBJHTTP        0x1a
95 14
#define BANS_ARG_OBJSTATUS      0x1b
96 1319
#define BANS_ARG_OBJTTL         0x1c
97 5
#define BANS_ARG_OBJAGE         0x1d
98 3
#define BANS_ARG_OBJGRACE       0x1e
99 7
#define BANS_ARG_OBJKEEP        0x1f
100 329
#define BANS_ARG_OBJLASTHIT     0x20
101 740
#define BANS_ARG_OBJHFM         0x21
102 730
#define BANS_ARG_OBJHFP         0x22
103 26867
#define BANS_ARG_OBJUNCACHEABLE 0x23
104 26507
#define BANS_ARG_LIM            (BANS_ARG_OBJUNCACHEABLE + 1)
105
106 26078
#define BAN_ARGIDX(x) ((x) - BANS_ARG_OFF_)
107 13169
#define BAN_ARGARRSZ  (BANS_ARG_LIM - BANS_ARG_OFF_)
108 12571
#define ASSERT_BAN_ARG(x) assert((x) >= BANS_ARG_OFF_ && (x) < BANS_ARG_LIM)
109
110
// has an arg1_spec (BANS_FLAG_HTTP at build time)
111
#define BANS_HAS_ARG1_SPEC(arg) \
112 657
        ((arg) == BANS_ARG_REQHTTP ||   \
113 657
         (arg) == BANS_ARG_OBJHTTP)
114
115
// has an arg2_spec (regex)
116
#define BANS_HAS_ARG2_SPEC(oper)        \
117 295
        ((oper) == BANS_OPER_MATCH ||   \
118 295
         (oper) == BANS_OPER_NMATCH)
119
120
// has an arg2_double (BANS_FLAG_DURATION at build time)
121
#define BANS_HAS_ARG2_DOUBLE(arg)       \
122 657
        ((arg) >= BANS_ARG_OBJTTL &&    \
123 657
         (arg) <= BANS_ARG_OBJLASTHIT)
124
125
// has an arg2_bool (BANS_FLAG_BOOL at build time)
126
#define BANS_HAS_ARG2_BOOL(arg) \
127 365
        ((arg) == BANS_ARG_OBJHFM ||    \
128 365
         (arg) == BANS_ARG_OBJHFP ||    \
129 365
         (arg) == BANS_ARG_OBJUNCACHEABLE)
130
131
/*--------------------------------------------------------------------*/
132
133
struct ban {
134
        unsigned                magic;
135
#define BAN_MAGIC               0x700b08ea
136
        unsigned                flags;          /* BANS_FLAG_* */
137
        VTAILQ_ENTRY(ban)       list;
138
        VTAILQ_ENTRY(ban)       l_list;
139
        int64_t                 refcount;
140
141
        VTAILQ_HEAD(,objcore)   objcore;
142
        uint8_t                 *spec;
143
};
144
145
VTAILQ_HEAD(banhead_s,ban);
146
147
bgthread_t ban_lurker;
148
extern struct lock ban_mtx;
149
extern int ban_shutdown;
150
extern struct banhead_s ban_head;
151
extern struct ban * volatile ban_start;
152
extern pthread_cond_t   ban_lurker_cond;
153
extern uint64_t bans_persisted_bytes;
154
extern uint64_t bans_persisted_fragmentation;
155
extern const char * const ban_oper[BAN_OPERARRSZ + 1];
156
157
void ban_mark_completed(struct ban *);
158
unsigned ban_len(const uint8_t *banspec);
159
void ban_info_new(const uint8_t *ban, unsigned len);
160
void ban_info_drop(const uint8_t *ban, unsigned len);
161
162
int ban_evaluate(struct worker *wrk, const uint8_t *bs, struct objcore *oc,
163
    const struct http *reqhttp, unsigned *tests);
164
vtim_real ban_time(const uint8_t *banspec);
165
int ban_equal(const uint8_t *bs1, const uint8_t *bs2);
166
void BAN_Free(struct ban *b);
167
void ban_kick_lurker(void);
168
169
// cache_ban_idx.c
170
struct ban * BANIDX_lookup(vtim_real);
171
void BANIDX_fini(void);