William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Cache management |
| 3 | * |
| 4 | * Copyright 2017 HAProxy Technologies |
| 5 | * William Lallemand <wlallemand@haproxy.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 13 | #include <eb32tree.h> |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 14 | #include <import/sha1.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 15 | |
William Lallemand | 75d9329 | 2017-11-21 20:01:24 +0100 | [diff] [blame] | 16 | #include <types/action.h> |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 17 | #include <types/cli.h> |
William Lallemand | 75d9329 | 2017-11-21 20:01:24 +0100 | [diff] [blame] | 18 | #include <types/filters.h> |
| 19 | #include <types/proxy.h> |
| 20 | #include <types/shctx.h> |
| 21 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 22 | #include <proto/channel.h> |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 23 | #include <proto/cli.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 24 | #include <proto/proxy.h> |
| 25 | #include <proto/hdr_idx.h> |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 26 | #include <proto/http_htx.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 27 | #include <proto/filters.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 28 | #include <proto/http_rules.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 29 | #include <proto/proto_http.h> |
| 30 | #include <proto/log.h> |
| 31 | #include <proto/stream.h> |
| 32 | #include <proto/stream_interface.h> |
| 33 | #include <proto/shctx.h> |
| 34 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 35 | |
| 36 | #include <common/cfgparse.h> |
| 37 | #include <common/hash.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 38 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 39 | #include <common/initcall.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 40 | |
| 41 | /* flt_cache_store */ |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 42 | #define CACHE_F_LEGACY_HTTP 0x00000001 /* The cache is used to store raw HTTP |
| 43 | * messages (legacy implementation) */ |
| 44 | #define CACHE_F_HTX 0x00000002 /* The cache is used to store HTX messages */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 45 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 46 | #define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 47 | * the filter keyword) */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 48 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 49 | const char *cache_store_flt_id = "cache store filter"; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 51 | extern struct applet http_cache_applet; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 52 | |
| 53 | struct flt_ops cache_ops; |
| 54 | |
| 55 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 56 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 57 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 58 | unsigned int maxage; /* max-age */ |
| 59 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 60 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 61 | char id[33]; /* cache name */ |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 62 | unsigned int flags; /* CACHE_F_* */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 65 | /* cache config for filters */ |
| 66 | struct cache_flt_conf { |
| 67 | union { |
| 68 | struct cache *cache; /* cache used by the filter */ |
| 69 | char *name; /* cache name used during conf parsing */ |
| 70 | } c; |
| 71 | unsigned int flags; /* CACHE_FLT_F_* */ |
| 72 | }; |
| 73 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 74 | /* |
| 75 | * cache ctx for filters |
| 76 | */ |
| 77 | struct cache_st { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 78 | int hdrs_len; // field used in legacy mode only |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 79 | struct shared_block *first_block; |
| 80 | }; |
| 81 | |
| 82 | struct cache_entry { |
| 83 | unsigned int latest_validation; /* latest validation date */ |
| 84 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 85 | unsigned int age; /* Origin server "Age" header value */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 86 | unsigned int eoh; /* Origin server end of headers offset. */ // field used in legacy mode only |
| 87 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 88 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 89 | char hash[20]; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 90 | unsigned char data[0]; |
| 91 | }; |
| 92 | |
| 93 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 94 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 95 | |
| 96 | static struct list caches = LIST_HEAD_INIT(caches); |
| 97 | static struct cache *tmp_cache_config = NULL; |
| 98 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 99 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 100 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 101 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 102 | { |
| 103 | struct eb32_node *node; |
| 104 | struct cache_entry *entry; |
| 105 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 106 | node = eb32_lookup(&cache->entries, (*(unsigned int *)hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 107 | if (!node) |
| 108 | return NULL; |
| 109 | |
| 110 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 111 | |
| 112 | /* if that's not the right node */ |
| 113 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 114 | return NULL; |
| 115 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 116 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 117 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 118 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 119 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 120 | entry->eb.key = 0; |
| 121 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 122 | return NULL; |
| 123 | |
| 124 | } |
| 125 | |
| 126 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 127 | { |
| 128 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 129 | } |
| 130 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 131 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 132 | { |
| 133 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 138 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 139 | cache_store_init(struct proxy *px, struct flt_conf *fconf) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 140 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 141 | fconf->flags |= FLT_CFG_FL_HTX; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 145 | static void |
| 146 | cache_store_deinit(struct proxy *px, struct flt_conf *fconf) |
| 147 | { |
| 148 | struct cache_flt_conf *cconf = fconf->conf; |
| 149 | |
| 150 | free(cconf); |
| 151 | } |
| 152 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 153 | static int |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 154 | cache_store_check(struct proxy *px, struct flt_conf *fconf) |
| 155 | { |
| 156 | struct cache_flt_conf *cconf = fconf->conf; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 157 | struct flt_conf *f; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 158 | struct cache *cache; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 159 | int comp = 0; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 160 | |
| 161 | /* resolve the cache name to a ptr in the filter config */ |
| 162 | list_for_each_entry(cache, &caches, list) { |
| 163 | if (!strcmp(cache->id, cconf->c.name)) { |
| 164 | /* there can be only one filter per cache, so we free it there */ |
| 165 | cache->flags |= ((px->options2 & PR_O2_USE_HTX) |
| 166 | ? CACHE_F_HTX |
| 167 | : CACHE_F_LEGACY_HTTP); |
| 168 | |
| 169 | free(cconf->c.name); |
| 170 | cconf->c.cache = cache; |
| 171 | goto found; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 176 | proxy_type_str(px), px->id, (char *)cconf->c.name); |
| 177 | return 1; |
| 178 | |
| 179 | found: |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 180 | /* Here <cache> points on the cache the filter must use and <cconf> |
| 181 | * points on the cache filter configuration. */ |
| 182 | |
| 183 | /* Check all filters for proxy <px> to know if the compression is |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 184 | * enabled and if it is after the cache. When the compression is before |
| 185 | * the cache, an error is returned. Also check if the cache filter must |
| 186 | * be explicitly declaired or not. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 187 | list_for_each_entry(f, &px->filter_configs, list) { |
| 188 | if (f == fconf) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 189 | /* The compression filter must be evaluated after the cache. */ |
| 190 | if (comp) { |
| 191 | ha_alert("config: %s '%s': unable to enable the compression filter before " |
| 192 | "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 193 | return 1; |
| 194 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 195 | } |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 196 | else if (f->id == http_comp_flt_id) { |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 197 | if (!(px->options2 & PR_O2_USE_HTX)) { |
| 198 | ha_alert("config: %s '%s' : compression and cache filters cannot be " |
| 199 | "both enabled on non HTX proxy.\n", |
| 200 | proxy_type_str(px), px->id); |
| 201 | return 1; |
| 202 | } |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 203 | comp = 1; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 204 | } |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 205 | else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 206 | /* Implicit declaration is only allowed with the |
| 207 | * compression. For other filters, an implicit |
| 208 | * declaration is required. */ |
| 209 | ha_alert("config: %s '%s': require an explicit filter declaration " |
| 210 | "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 211 | return 1; |
| 212 | } |
| 213 | |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 214 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 219 | cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 220 | { |
| 221 | if (!(chn->flags & CF_ISRESP)) |
| 222 | return 1; |
| 223 | |
| 224 | if (filter->ctx == NULL) { |
| 225 | struct cache_st *st; |
| 226 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 227 | st = pool_alloc_dirty(pool_head_cache_st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 228 | if (st == NULL) |
| 229 | return -1; |
| 230 | |
| 231 | st->hdrs_len = 0; |
| 232 | st->first_block = NULL; |
| 233 | filter->ctx = st; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 234 | |
| 235 | /* Register post-analyzer on AN_RES_WAIT_HTTP */ |
| 236 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 237 | } |
| 238 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | static int |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 243 | cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 244 | { |
| 245 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 246 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 247 | struct cache *cache = cconf->c.cache; |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 248 | struct shared_context *shctx = shctx_ptr(cache); |
| 249 | |
| 250 | if (!(chn->flags & CF_ISRESP)) |
| 251 | return 1; |
| 252 | |
| 253 | /* Everything should be released in the http_end filter, but we need to do it |
| 254 | * there too, in case of errors */ |
| 255 | |
| 256 | if (st && st->first_block) { |
| 257 | |
| 258 | shctx_lock(shctx); |
| 259 | shctx_row_dec_hot(shctx, st->first_block); |
| 260 | shctx_unlock(shctx); |
| 261 | |
| 262 | } |
| 263 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 264 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 265 | filter->ctx = NULL; |
| 266 | } |
| 267 | |
| 268 | return 1; |
| 269 | } |
| 270 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 271 | static int |
| 272 | cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn, |
| 273 | unsigned an_bit) |
| 274 | { |
| 275 | struct http_txn *txn = s->txn; |
| 276 | struct http_msg *msg = &txn->rsp; |
| 277 | struct cache_st *st = filter->ctx; |
| 278 | |
| 279 | if (an_bit != AN_RES_WAIT_HTTP) |
| 280 | goto end; |
| 281 | |
| 282 | /* Here we need to check if any compression filter precedes the cache |
| 283 | * filter. This is only possible when the compression is configured in |
| 284 | * the frontend while the cache filter is configured on the |
| 285 | * backend. This case cannot be detected during HAProxy startup. So in |
| 286 | * such cases, the cache is disabled. |
| 287 | */ |
| 288 | if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) { |
| 289 | pool_free(pool_head_cache_st, st); |
| 290 | filter->ctx = NULL; |
| 291 | } |
| 292 | |
| 293 | end: |
| 294 | return 1; |
| 295 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 296 | |
| 297 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 298 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 299 | { |
| 300 | struct cache_st *st = filter->ctx; |
| 301 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 302 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 303 | return 1; |
| 304 | |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 305 | if (st->first_block) { |
| 306 | register_data_filter(s, msg->chn, filter); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 307 | if (!IS_HTX_STRM(s)) |
| 308 | st->hdrs_len = msg->sov; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 309 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 313 | static inline void disable_cache_entry(struct cache_st *st, |
| 314 | struct filter *filter, struct shared_context *shctx) |
| 315 | { |
| 316 | struct cache_entry *object; |
| 317 | |
| 318 | object = (struct cache_entry *)st->first_block->data; |
| 319 | filter->ctx = NULL; /* disable cache */ |
| 320 | shctx_lock(shctx); |
| 321 | shctx_row_dec_hot(shctx, st->first_block); |
| 322 | object->eb.key = 0; |
| 323 | shctx_unlock(shctx); |
| 324 | pool_free(pool_head_cache_st, st); |
| 325 | } |
| 326 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 327 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 328 | cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 329 | unsigned int offset, unsigned int len) |
| 330 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 331 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 332 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 333 | struct cache_st *st = filter->ctx; |
| 334 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 335 | struct htx_blk *blk; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 336 | struct shared_block *fb; |
| 337 | unsigned int orig_len, to_forward; |
| 338 | int ret; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 339 | |
| 340 | if (!len) |
| 341 | return len; |
| 342 | |
| 343 | if (!st->first_block) { |
| 344 | unregister_data_filter(s, msg->chn, filter); |
| 345 | return len; |
| 346 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 347 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 348 | chunk_reset(&trash); |
| 349 | orig_len = len; |
| 350 | to_forward = 0; |
Christopher Faulet | ee847d4 | 2019-05-23 11:55:33 +0200 | [diff] [blame] | 351 | for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 352 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 353 | uint32_t info, sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 354 | struct ist v; |
| 355 | |
Christopher Faulet | ee847d4 | 2019-05-23 11:55:33 +0200 | [diff] [blame] | 356 | if (offset >= sz) { |
| 357 | offset -= sz; |
| 358 | continue; |
| 359 | } |
| 360 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 361 | switch (type) { |
| 362 | case HTX_BLK_UNUSED: |
| 363 | break; |
| 364 | |
| 365 | case HTX_BLK_DATA: |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 366 | v = htx_get_blk_value(htx, blk); |
| 367 | v.ptr += offset; |
| 368 | v.len -= offset; |
| 369 | if (v.len > len) |
| 370 | v.len = len; |
| 371 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 372 | info = (type << 28) + v.len; |
| 373 | chunk_memcat(&trash, (char *)&info, sizeof(info)); |
| 374 | chunk_memcat(&trash, v.ptr, v.len); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 375 | to_forward += v.len; |
| 376 | len -= v.len; |
| 377 | break; |
| 378 | |
| 379 | default: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 380 | /* Here offset must always be 0 because only |
| 381 | * DATA blocks can be partially transferred. */ |
| 382 | if (offset) |
| 383 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 384 | if (sz > len) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 385 | goto end; |
| 386 | |
| 387 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 388 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 389 | to_forward += sz; |
| 390 | len -= sz; |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | offset = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 395 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 396 | |
| 397 | end: |
| 398 | shctx_lock(shctx); |
| 399 | fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data); |
| 400 | if (!fb) { |
| 401 | shctx_unlock(shctx); |
| 402 | goto no_cache; |
| 403 | } |
| 404 | shctx_unlock(shctx); |
| 405 | |
| 406 | ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 407 | (unsigned char *)b_head(&trash), b_data(&trash)); |
| 408 | if (ret < 0) |
| 409 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 410 | |
| 411 | return to_forward; |
| 412 | |
| 413 | no_cache: |
| 414 | disable_cache_entry(st, filter, shctx); |
| 415 | unregister_data_filter(s, msg->chn, filter); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 416 | return orig_len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 420 | cache_store_http_forward_data(struct stream *s, struct filter *filter, |
| 421 | struct http_msg *msg, unsigned int len) |
| 422 | { |
| 423 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 424 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 425 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 426 | int ret; |
| 427 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 428 | ret = 0; |
| 429 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 430 | /* |
| 431 | * We need to skip the HTTP headers first, because we saved them in the |
| 432 | * http-response action. |
| 433 | */ |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 434 | if (!(msg->chn->flags & CF_ISRESP) || !st) { |
| 435 | /* should never happen */ |
| 436 | unregister_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 437 | return len; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 438 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 439 | |
| 440 | if (!len) { |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 441 | /* Nothing to forward */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 442 | ret = len; |
| 443 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 444 | else if (st->hdrs_len >= len) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 445 | /* Forward part of headers */ |
| 446 | ret = len; |
| 447 | st->hdrs_len -= len; |
| 448 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 449 | else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 450 | /* Forward data */ |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 451 | if (st->first_block) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 452 | int to_append, append; |
| 453 | struct shared_block *fb; |
| 454 | |
| 455 | to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len); |
| 456 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 457 | shctx_lock(shctx); |
| 458 | fb = shctx_row_reserve_hot(shctx, st->first_block, to_append); |
| 459 | if (!fb) { |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 460 | shctx_unlock(shctx); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 461 | disable_cache_entry(st, filter, shctx); |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 462 | unregister_data_filter(s, msg->chn, filter); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 463 | return len; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 464 | } |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 465 | shctx_unlock(shctx); |
| 466 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 467 | /* Skip remaining headers to fill the cache */ |
| 468 | c_adv(msg->chn, st->hdrs_len); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 469 | append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 470 | (unsigned char *)ci_head(msg->chn), to_append); |
| 471 | ret = st->hdrs_len + to_append - append; |
| 472 | /* Rewind the buffer to forward all data */ |
| 473 | c_rew(msg->chn, st->hdrs_len); |
| 474 | st->hdrs_len = 0; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 475 | if (ret < 0) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 476 | disable_cache_entry(st, filter, shctx); |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 477 | unregister_data_filter(s, msg->chn, filter); |
| 478 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 479 | } |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 480 | else { |
| 481 | /* should never happen */ |
| 482 | unregister_data_filter(s, msg->chn, filter); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 483 | ret = len; |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 484 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if ((ret != len) || |
| 488 | (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret)) |
| 489 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | static int |
| 495 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 496 | struct http_msg *msg) |
| 497 | { |
| 498 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 499 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 500 | struct cache *cache = cconf->c.cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 501 | struct shared_context *shctx = shctx_ptr(cache); |
| 502 | struct cache_entry *object; |
| 503 | |
| 504 | if (!(msg->chn->flags & CF_ISRESP)) |
| 505 | return 1; |
| 506 | |
| 507 | if (st && st->first_block) { |
| 508 | |
| 509 | object = (struct cache_entry *)st->first_block->data; |
| 510 | |
| 511 | /* does not need to test if the insertion worked, if it |
| 512 | * doesn't, the blocks will be reused anyway */ |
| 513 | |
| 514 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 515 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 516 | object->eb.key = 0; |
| 517 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 518 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 519 | shctx_row_dec_hot(shctx, st->first_block); |
| 520 | shctx_unlock(shctx); |
| 521 | |
| 522 | } |
| 523 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 524 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 525 | filter->ctx = NULL; |
| 526 | } |
| 527 | |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * This intends to be used when checking HTTP headers for some |
| 533 | * word=value directive. Return a pointer to the first character of value, if |
| 534 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 535 | */ |
| 536 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 537 | { |
| 538 | int st = 0; |
| 539 | |
| 540 | if (slen < wlen) |
| 541 | return 0; |
| 542 | |
| 543 | while (wlen) { |
| 544 | char c = *sample ^ *word; |
| 545 | if (c && c != ('A' ^ 'a')) |
| 546 | return NULL; |
| 547 | sample++; |
| 548 | word++; |
| 549 | slen--; |
| 550 | wlen--; |
| 551 | } |
| 552 | |
| 553 | while (slen) { |
| 554 | if (st == 0) { |
| 555 | if (*sample != '=') |
| 556 | return NULL; |
| 557 | sample++; |
| 558 | slen--; |
| 559 | st = 1; |
| 560 | continue; |
| 561 | } else { |
| 562 | return (char *)sample; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | return NULL; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Return the maxage in seconds of an HTTP response. |
| 571 | * Compute the maxage using either: |
| 572 | * - the assigned max-age of the cache |
| 573 | * - the s-maxage directive |
| 574 | * - the max-age directive |
| 575 | * - (Expires - Data) headers |
| 576 | * - the default-max-age of the cache |
| 577 | * |
| 578 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 579 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 580 | { |
| 581 | struct http_txn *txn = s->txn; |
| 582 | struct hdr_ctx ctx; |
| 583 | |
| 584 | int smaxage = -1; |
| 585 | int maxage = -1; |
| 586 | |
| 587 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 588 | ctx.idx = 0; |
| 589 | |
| 590 | /* loop on the Cache-Control values */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 591 | while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 592 | char *directive = ctx.line + ctx.val; |
| 593 | char *value; |
| 594 | |
| 595 | value = directive_value(directive, ctx.vlen, "s-maxage", 8); |
| 596 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 597 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 598 | |
| 599 | chunk_strncat(chk, value, ctx.vlen - 8 + 1); |
| 600 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 601 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); |
| 605 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 606 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 607 | |
| 608 | chunk_strncat(chk, value, ctx.vlen - 7 + 1); |
| 609 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 610 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
| 614 | /* TODO: Expires - Data */ |
| 615 | |
| 616 | |
| 617 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 618 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 619 | |
| 620 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 621 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 622 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 623 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 624 | |
| 625 | } |
| 626 | |
| 627 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 628 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 629 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 630 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 631 | |
| 632 | if (first == block && object->eb.key) |
| 633 | eb32_delete(&object->eb); |
| 634 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 635 | } |
| 636 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 637 | /* |
| 638 | * This fonction will store the headers of the response in a buffer and then |
| 639 | * register a filter to store the data |
| 640 | */ |
| 641 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 642 | struct session *sess, struct stream *s, int flags) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 643 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 644 | unsigned int age; |
| 645 | long long hdr_age; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 646 | struct http_txn *txn = s->txn; |
| 647 | struct http_msg *msg = &txn->rsp; |
| 648 | struct filter *filter; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 649 | struct shared_block *first = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 650 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 651 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 652 | struct cache_st *cache_ctx = NULL; |
| 653 | struct cache_entry *object, *old; |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 654 | unsigned int key = *(unsigned int *)txn->cache_hash; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 655 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 656 | /* Don't cache if the response came from a cache */ |
| 657 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 658 | s->target == &http_cache_applet.obj_type) { |
| 659 | goto out; |
| 660 | } |
| 661 | |
| 662 | /* cache only HTTP/1.1 */ |
| 663 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 664 | goto out; |
| 665 | |
| 666 | /* cache only GET method */ |
| 667 | if (txn->meth != HTTP_METH_GET) |
| 668 | goto out; |
| 669 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 670 | /* cache key was not computed */ |
| 671 | if (!key) |
| 672 | goto out; |
| 673 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 674 | /* cache only 200 status code */ |
| 675 | if (txn->status != 200) |
| 676 | goto out; |
| 677 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 678 | /* Find the corresponding filter instance for the current stream */ |
| 679 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 680 | if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) { |
| 681 | /* No filter ctx, don't cache anything */ |
| 682 | if (!filter->ctx) |
| 683 | goto out; |
| 684 | cache_ctx = filter->ctx; |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* from there, cache_ctx is always defined */ |
| 690 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 691 | if (IS_HTX_STRM(s)) { |
| 692 | struct htx *htx = htxbuf(&s->res.buf); |
| 693 | struct http_hdr_ctx ctx; |
| 694 | int32_t pos; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 695 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 696 | /* Do not cache too big objects. */ |
| 697 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 698 | htx->data + htx->extra > shctx->max_obj_size) |
| 699 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 700 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 701 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 702 | ctx.blk = NULL; |
| 703 | if (http_find_header(htx, ist("Vary"), &ctx, 0)) |
| 704 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 705 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 706 | htx_check_response_for_cacheability(s, &s->res); |
| 707 | |
| 708 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
| 709 | goto out; |
| 710 | |
| 711 | age = 0; |
| 712 | ctx.blk = NULL; |
| 713 | if (http_find_header(htx, ist("Age"), &ctx, 0)) { |
| 714 | if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) { |
| 715 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 716 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 717 | age = hdr_age; |
| 718 | } |
| 719 | http_remove_header(htx, &ctx); |
| 720 | } |
| 721 | |
| 722 | chunk_reset(&trash); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 723 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 724 | struct htx_blk *blk = htx_get_blk(htx, pos); |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 725 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 726 | uint32_t sz = htx_get_blksz(blk); |
| 727 | |
| 728 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 729 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 730 | if (type == HTX_BLK_EOH) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 731 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | else { |
| 735 | struct hdr_ctx ctx; |
| 736 | |
| 737 | /* Do not cache too big objects. */ |
| 738 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 739 | msg->sov + msg->body_len > shctx->max_obj_size) |
| 740 | goto out; |
| 741 | |
| 742 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 743 | ctx.idx = 0; |
| 744 | if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) |
| 745 | goto out; |
| 746 | |
| 747 | check_response_for_cacheability(s, &s->res); |
| 748 | |
| 749 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
| 750 | goto out; |
| 751 | |
| 752 | age = 0; |
| 753 | ctx.idx = 0; |
| 754 | if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
| 755 | if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) { |
| 756 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 757 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 758 | age = hdr_age; |
| 759 | } |
| 760 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 761 | } |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 762 | } |
| 763 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 764 | shctx_lock(shctx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 765 | if (IS_HTX_STRM(s)) |
| 766 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data); |
| 767 | else |
| 768 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 769 | if (!first) { |
| 770 | shctx_unlock(shctx); |
| 771 | goto out; |
| 772 | } |
| 773 | shctx_unlock(shctx); |
| 774 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 775 | /* the received memory is not initialized, we need at least to mark |
| 776 | * the object as not indexed yet. |
| 777 | */ |
| 778 | object = (struct cache_entry *)first->data; |
| 779 | object->eb.node.leaf_p = NULL; |
| 780 | object->eb.key = 0; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 781 | object->age = age; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 782 | if (!IS_HTX_STRM(s)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 783 | object->eoh = msg->eoh; |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 784 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 785 | /* reserve space for the cache_entry structure */ |
| 786 | first->len = sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 787 | first->last_append = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 788 | /* cache the headers in a http action because it allows to chose what |
| 789 | * to cache, for example you might want to cache a response before |
| 790 | * modifying some HTTP headers, or on the contrary after modifying |
| 791 | * those headers. |
| 792 | */ |
| 793 | |
| 794 | /* does not need to be locked because it's in the "hot" list, |
| 795 | * copy the headers */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 796 | if (IS_HTX_STRM(s)) { |
| 797 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0) |
| 798 | goto out; |
| 799 | } |
| 800 | else { |
| 801 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0) |
| 802 | goto out; |
| 803 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 804 | |
| 805 | /* register the buffer in the filter ctx for filling it with data*/ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 806 | if (cache_ctx) { |
| 807 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 808 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 809 | object->eb.key = key; |
| 810 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 811 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
| 812 | /* Insert the node later on caching success */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 813 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 814 | shctx_lock(shctx); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 815 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 816 | old = entry_exist(cconf->c.cache, txn->cache_hash); |
| 817 | if (old) { |
| 818 | eb32_delete(&old->eb); |
| 819 | old->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 820 | } |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 821 | shctx_unlock(shctx); |
| 822 | |
| 823 | /* store latest value and expiration time */ |
| 824 | object->latest_validation = now.tv_sec; |
| 825 | object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache); |
| 826 | return ACT_RET_CONT; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | out: |
| 830 | /* if does not cache */ |
| 831 | if (first) { |
| 832 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 833 | first->len = 0; |
| 834 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 835 | shctx_row_dec_hot(shctx, first); |
| 836 | shctx_unlock(shctx); |
| 837 | } |
| 838 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 839 | return ACT_RET_CONT; |
| 840 | } |
| 841 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 842 | #define HTTP_CACHE_INIT 0 /* Initial state. */ |
| 843 | #define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */ |
| 844 | #define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */ |
| 845 | #define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 846 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 847 | #define HTX_CACHE_INIT 0 /* Initial state. */ |
| 848 | #define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */ |
| 849 | #define HTX_CACHE_DATA 2 /* Cache entry data forwarding */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 850 | #define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */ |
| 851 | #define HTX_CACHE_END 4 /* Cache entry treatment terminated */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 852 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 853 | static void http_cache_applet_release(struct appctx *appctx) |
| 854 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 855 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 856 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 857 | struct cache *cache = cconf->c.cache; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 858 | struct shared_block *first = block_ptr(cache_ptr); |
| 859 | |
| 860 | shctx_lock(shctx_ptr(cache)); |
| 861 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 862 | shctx_unlock(shctx_ptr(cache)); |
| 863 | } |
| 864 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 865 | |
| 866 | static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type, |
| 867 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 868 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 869 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 870 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 871 | struct htx_blk *blk; |
| 872 | unsigned int max, total; |
| 873 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 874 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 875 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 876 | if (!max) |
| 877 | return 0; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 878 | blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 879 | ? (info & 0xff) + ((info >> 8) & 0xfffff) |
| 880 | : info & 0xfffffff); |
| 881 | if (blksz > max) |
| 882 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 883 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 884 | blk = htx_add_blk(htx, type, blksz); |
| 885 | if (!blk) |
| 886 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 887 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 888 | blk->info = info; |
| 889 | total = 4; |
| 890 | while (blksz) { |
| 891 | max = MIN(blksz, shctx->block_size - offset); |
| 892 | memcpy(htx_get_blk_ptr(htx, blk), (const char *)shblk->data + offset, max); |
| 893 | offset += max; |
| 894 | blksz -= max; |
| 895 | total += max; |
| 896 | if (blksz || offset == shctx->block_size) { |
| 897 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 898 | offset = 0; |
| 899 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 900 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 901 | appctx->ctx.cache.offset = offset; |
| 902 | appctx->ctx.cache.next = shblk; |
| 903 | appctx->ctx.cache.sent += total; |
| 904 | return total; |
| 905 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 906 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 907 | static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx, |
| 908 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
| 909 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 910 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 911 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 912 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
| 913 | unsigned int max, total, rem_data; |
| 914 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 915 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 916 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 917 | if (!max) |
| 918 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 919 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 920 | rem_data = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 921 | if (appctx->ctx.cache.rem_data) { |
| 922 | blksz = appctx->ctx.cache.rem_data; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 923 | total = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 924 | } |
| 925 | else { |
| 926 | blksz = (info & 0xfffffff); |
| 927 | total = 4; |
| 928 | } |
| 929 | if (blksz > max) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 930 | rem_data = blksz - max; |
| 931 | blksz = max; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 932 | } |
| 933 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 934 | while (blksz) { |
| 935 | size_t sz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 936 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 937 | max = MIN(blksz, shctx->block_size - offset); |
| 938 | sz = htx_add_data(htx, ist2(shblk->data + offset, max)); |
| 939 | offset += sz; |
| 940 | blksz -= sz; |
| 941 | total += sz; |
| 942 | if (sz < max) |
| 943 | break; |
| 944 | if (blksz || offset == shctx->block_size) { |
| 945 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 946 | offset = 0; |
| 947 | } |
| 948 | } |
| 949 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 950 | appctx->ctx.cache.offset = offset; |
| 951 | appctx->ctx.cache.next = shblk; |
| 952 | appctx->ctx.cache.sent += total; |
| 953 | appctx->ctx.cache.rem_data = rem_data + blksz; |
| 954 | return total; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 957 | static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len, |
| 958 | enum htx_blk_type mark) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 959 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 960 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 961 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 962 | struct shared_block *shblk; |
| 963 | unsigned int offset, sz; |
| 964 | unsigned int ret, total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 965 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 966 | while (len) { |
| 967 | enum htx_blk_type type; |
| 968 | uint32_t info; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 969 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 970 | shblk = appctx->ctx.cache.next; |
| 971 | offset = appctx->ctx.cache.offset; |
| 972 | if (appctx->ctx.cache.rem_data) { |
| 973 | type = HTX_BLK_DATA; |
| 974 | info = 0; |
| 975 | goto add_data_blk; |
| 976 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 977 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 978 | /* Get info of the next HTX block. May be splitted on 2 shblk */ |
| 979 | sz = MIN(4, shctx->block_size - offset); |
| 980 | memcpy((char *)&info, (const char *)shblk->data + offset, sz); |
| 981 | offset += sz; |
| 982 | if (sz < 4) { |
| 983 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 984 | memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz); |
| 985 | offset = (4 - sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 986 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 987 | |
| 988 | /* Get payload of the next HTX block and insert it. */ |
| 989 | type = (info >> 28); |
| 990 | if (type != HTX_BLK_DATA) |
| 991 | ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset); |
| 992 | else { |
| 993 | add_data_blk: |
| 994 | ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 997 | if (!ret) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 998 | break; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 999 | total += ret; |
| 1000 | len -= ret; |
| 1001 | |
| 1002 | if (appctx->ctx.cache.rem_data || type == mark) |
| 1003 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1004 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1005 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1006 | return total; |
| 1007 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1008 | |
| 1009 | static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) |
| 1010 | { |
| 1011 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1012 | unsigned int age; |
| 1013 | char *end; |
| 1014 | |
| 1015 | chunk_reset(&trash); |
| 1016 | age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age; |
| 1017 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 1018 | age = CACHE_ENTRY_MAX_AGE; |
| 1019 | end = ultoa_o(age, b_head(&trash), b_size(&trash)); |
| 1020 | b_set_data(&trash, end - b_head(&trash)); |
| 1021 | if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash)))) |
| 1022 | return 0; |
| 1023 | return 1; |
| 1024 | } |
| 1025 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1026 | static void htx_cache_io_handler(struct appctx *appctx) |
| 1027 | { |
| 1028 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1029 | struct shared_block *first = block_ptr(cache_ptr); |
| 1030 | struct stream_interface *si = appctx->owner; |
| 1031 | struct channel *req = si_oc(si); |
| 1032 | struct channel *res = si_ic(si); |
| 1033 | struct htx *req_htx, *res_htx; |
| 1034 | struct buffer *errmsg; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1035 | unsigned int len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1036 | size_t ret, total = 0; |
| 1037 | |
| 1038 | res_htx = htxbuf(&res->buf); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1039 | total = res_htx->data; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 1042 | goto out; |
| 1043 | |
| 1044 | /* Check if the input buffer is avalaible. */ |
| 1045 | if (!b_size(&res->buf)) { |
| 1046 | si_rx_room_blk(si); |
| 1047 | goto out; |
| 1048 | } |
| 1049 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 1050 | if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW)) |
Willy Tarreau | 273e964 | 2018-12-16 00:35:15 +0100 | [diff] [blame] | 1051 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1052 | |
| 1053 | if (appctx->st0 == HTX_CACHE_INIT) { |
| 1054 | appctx->ctx.cache.next = block_ptr(cache_ptr); |
| 1055 | appctx->ctx.cache.offset = sizeof(*cache_ptr); |
| 1056 | appctx->ctx.cache.sent = 0; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1057 | appctx->ctx.cache.rem_data = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1058 | appctx->st0 = HTX_CACHE_HEADER; |
| 1059 | } |
| 1060 | |
| 1061 | if (appctx->st0 == HTX_CACHE_HEADER) { |
| 1062 | /* Headers must be dump at once. Otherwise it is an error */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1063 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1064 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH); |
| 1065 | if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) || |
| 1066 | !htx_cache_add_age_hdr(appctx, res_htx)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1067 | goto error; |
| 1068 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1069 | /* Skip response body for HEAD requests */ |
| 1070 | if (si_strm(si)->txn->meth == HTTP_METH_HEAD) |
Christopher Faulet | f0dd037 | 2019-02-25 11:08:34 +0100 | [diff] [blame] | 1071 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1072 | else |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1073 | appctx->st0 = HTX_CACHE_DATA; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | if (appctx->st0 == HTX_CACHE_DATA) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1077 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1078 | if (len) { |
| 1079 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM); |
| 1080 | if (ret < len) { |
| 1081 | si_rx_room_blk(si); |
| 1082 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1083 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1084 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1085 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | if (appctx->st0 == HTX_CACHE_EOM) { |
| 1089 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 1090 | si_rx_room_blk(si); |
| 1091 | goto out; |
| 1092 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1093 | appctx->st0 = HTX_CACHE_END; |
| 1094 | } |
| 1095 | |
| 1096 | end: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1097 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1098 | res->flags |= CF_READ_NULL; |
| 1099 | si_shutr(si); |
| 1100 | } |
| 1101 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1102 | out: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1103 | total = res_htx->data - total; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1104 | if (total) |
| 1105 | channel_add_input(res, total); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1106 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* eat the whole request */ |
| 1109 | if (co_data(req)) { |
| 1110 | req_htx = htx_from_buf(&req->buf); |
| 1111 | co_htx_skip(req, req_htx, co_data(req)); |
| 1112 | htx_to_buf(req_htx, &req->buf); |
| 1113 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1114 | return; |
| 1115 | |
| 1116 | error: |
| 1117 | /* Sent and HTTP error 500 */ |
| 1118 | b_reset(&res->buf); |
| 1119 | errmsg = &htx_err_chunks[HTTP_ERR_500]; |
| 1120 | res->buf.data = b_data(errmsg); |
| 1121 | memcpy(res->buf.area, b_head(errmsg), b_data(errmsg)); |
| 1122 | res_htx = htx_from_buf(&res->buf); |
| 1123 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1124 | total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1125 | appctx->st0 = HTX_CACHE_END; |
| 1126 | goto end; |
| 1127 | } |
| 1128 | |
| 1129 | |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 1130 | /* |
| 1131 | * Append an "Age" header into <chn> channel for this <ce> cache entry. |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 1132 | * This is the responsibility of the caller to insure there is enough |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 1133 | * data in the channel. |
| 1134 | * |
| 1135 | * Returns the number of bytes inserted if succeeded, 0 if failed. |
| 1136 | */ |
| 1137 | static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn) |
| 1138 | { |
| 1139 | unsigned int age; |
| 1140 | |
| 1141 | age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age; |
| 1142 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 1143 | age = CACHE_ENTRY_MAX_AGE; |
| 1144 | |
| 1145 | chunk_reset(&trash); |
| 1146 | chunk_printf(&trash, "Age: %u", age); |
| 1147 | |
| 1148 | return ci_insert_line2(chn, ce->eoh, trash.area, trash.data); |
| 1149 | } |
| 1150 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1151 | static int cache_channel_row_data_get(struct appctx *appctx, int len) |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1152 | { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1153 | int ret, total; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1154 | struct stream_interface *si = appctx->owner; |
| 1155 | struct channel *res = si_ic(si); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1156 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 1157 | struct cache *cache = cconf->c.cache; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1158 | struct shared_context *shctx = shctx_ptr(cache); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1159 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 1160 | struct shared_block *blk, *next = appctx->ctx.cache.next; |
| 1161 | int offset; |
| 1162 | |
| 1163 | total = 0; |
| 1164 | offset = 0; |
| 1165 | |
| 1166 | if (!next) { |
| 1167 | offset = sizeof(struct cache_entry); |
| 1168 | next = block_ptr(cache_ptr); |
| 1169 | } |
| 1170 | |
| 1171 | blk = next; |
| 1172 | list_for_each_entry_from(blk, &shctx->hot, list) { |
| 1173 | int sz; |
| 1174 | |
| 1175 | if (len <= 0) |
| 1176 | break; |
| 1177 | |
| 1178 | sz = MIN(len, shctx->block_size - offset); |
| 1179 | |
| 1180 | ret = ci_putblk(res, (const char *)blk->data + offset, sz); |
| 1181 | if (unlikely(offset)) |
| 1182 | offset = 0; |
| 1183 | if (ret <= 0) { |
| 1184 | if (ret == -3 || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1185 | si_rx_room_blk(si); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | } |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
| 1191 | total += sz; |
| 1192 | len -= sz; |
| 1193 | } |
| 1194 | appctx->ctx.cache.next = blk; |
| 1195 | |
| 1196 | return total; |
| 1197 | } |
| 1198 | |
| 1199 | static void http_cache_io_handler(struct appctx *appctx) |
| 1200 | { |
| 1201 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1202 | struct stream *s = si_strm(si); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1203 | struct channel *res = si_ic(si); |
| 1204 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1205 | struct shared_block *first = block_ptr(cache_ptr); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1206 | unsigned int *sent = &appctx->ctx.cache.sent; |
| 1207 | |
| 1208 | if (IS_HTX_STRM(s)) |
| 1209 | return htx_cache_io_handler(appctx); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1210 | |
| 1211 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 1212 | goto out; |
| 1213 | |
Joseph Herlant | 8dae5b3 | 2018-11-15 14:07:53 -0800 | [diff] [blame] | 1214 | /* Check if the input buffer is available. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1215 | if (res->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 1216 | /* buf.size==0 means we failed to get a buffer and were |
| 1217 | * already subscribed to a wait list to get a buffer. |
| 1218 | */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1219 | goto out; |
| 1220 | } |
| 1221 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 1222 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1223 | appctx->st0 = HTTP_CACHE_END; |
| 1224 | |
| 1225 | /* buffer are aligned there, should be fine */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 1226 | if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) { |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1227 | int len = first->len - *sent - sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1228 | if (len > 0) { |
| 1229 | int ret; |
| 1230 | |
| 1231 | ret = cache_channel_row_data_get(appctx, len); |
| 1232 | if (ret == -1) |
| 1233 | appctx->st0 = HTTP_CACHE_END; |
| 1234 | else |
| 1235 | *sent += ret; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 1236 | if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh && |
| 1237 | cache_channel_append_age_header(cache_ptr, res)) |
| 1238 | appctx->st0 = HTTP_CACHE_HEADER; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1239 | else if (ret == len) { |
| 1240 | *sent = 0; |
| 1241 | appctx->st0 = HTTP_CACHE_FWD; |
| 1242 | } |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1243 | } |
| 1244 | else { |
| 1245 | *sent = 0; |
| 1246 | appctx->st0 = HTTP_CACHE_FWD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1247 | } |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1250 | if (appctx->st0 == HTTP_CACHE_FWD) |
| 1251 | appctx->st0 = HTTP_CACHE_END; |
| 1252 | |
| 1253 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTTP_CACHE_END) { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1254 | res->flags |= CF_READ_NULL; |
| 1255 | si_shutr(si); |
| 1256 | } |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1257 | out: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1258 | /* eat the whole request */ |
| 1259 | if (co_data(si_oc(si))) |
| 1260 | co_skip(si_oc(si), co_data(si_oc(si))); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1261 | } |
| 1262 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1263 | static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1264 | { |
| 1265 | struct flt_conf *fconf; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1266 | struct cache_flt_conf *cconf = NULL; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1267 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1268 | if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1269 | memprintf(err, "expects a cache name"); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1270 | goto err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | /* check if a cache filter was already registered with this cache |
| 1274 | * name, if that's the case, must use it. */ |
| 1275 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1276 | if (fconf->id == cache_store_flt_id) { |
| 1277 | cconf = fconf->conf; |
| 1278 | if (cconf && !strcmp((char *)cconf->c.name, name)) { |
| 1279 | rule->arg.act.p[0] = cconf; |
| 1280 | return 1; |
| 1281 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1285 | /* Create the filter cache config */ |
| 1286 | cconf = calloc(1, sizeof(*cconf)); |
| 1287 | if (!cconf) { |
| 1288 | memprintf(err, "out of memory\n"); |
| 1289 | goto err; |
| 1290 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1291 | cconf->flags = CACHE_FLT_F_IMPLICIT_DECL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1292 | cconf->c.name = strdup(name); |
| 1293 | if (!cconf->c.name) { |
| 1294 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1295 | goto err; |
| 1296 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1297 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1298 | /* register a filter to fill the cache buffer */ |
| 1299 | fconf = calloc(1, sizeof(*fconf)); |
| 1300 | if (!fconf) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1301 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1302 | goto err; |
| 1303 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1304 | fconf->id = cache_store_flt_id; |
| 1305 | fconf->conf = cconf; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1306 | fconf->ops = &cache_ops; |
| 1307 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 1308 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1309 | rule->arg.act.p[0] = cconf; |
| 1310 | return 1; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1311 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1312 | err: |
| 1313 | free(cconf); |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 1318 | struct act_rule *rule, char **err) |
| 1319 | { |
| 1320 | rule->action = ACT_CUSTOM; |
| 1321 | rule->action_ptr = http_action_store_cache; |
| 1322 | |
| 1323 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
| 1324 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1325 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1326 | (*orig_arg)++; |
| 1327 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1330 | /* This produces a sha1 hash of the concatenation of the first |
| 1331 | * occurrence of the Host header followed by the path component if it |
| 1332 | * begins with a slash ('/'). */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1333 | int sha1_hosturi(struct stream *s) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1334 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1335 | struct http_txn *txn = s->txn; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1336 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1337 | struct buffer *trash; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1338 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1339 | trash = get_trash_chunk(); |
| 1340 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1341 | if (IS_HTX_STRM(s)) { |
| 1342 | struct htx *htx = htxbuf(&s->req.buf); |
| 1343 | struct htx_sl *sl; |
| 1344 | struct http_hdr_ctx ctx; |
| 1345 | struct ist path; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1346 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1347 | ctx.blk = NULL; |
| 1348 | if (!http_find_header(htx, ist("Host"), &ctx, 0)) |
| 1349 | return 0; |
| 1350 | chunk_memcat(trash, ctx.value.ptr, ctx.value.len); |
| 1351 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1352 | sl = http_get_stline(htx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1353 | path = http_get_path(htx_sl_req_uri(sl)); |
| 1354 | if (!path.ptr) |
| 1355 | return 0; |
| 1356 | chunk_memcat(trash, path.ptr, path.len); |
| 1357 | } |
| 1358 | else { |
| 1359 | struct hdr_ctx ctx; |
| 1360 | char *path; |
| 1361 | char *end; |
| 1362 | |
| 1363 | /* retrive the host */ |
| 1364 | ctx.idx = 0; |
| 1365 | if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) |
| 1366 | return 0; |
| 1367 | chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen); |
| 1368 | |
| 1369 | /* now retrieve the path */ |
| 1370 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 1371 | path = http_txn_get_path(txn); |
| 1372 | if (!path) |
| 1373 | return 0; |
| 1374 | chunk_strncat(trash, path, end - path); |
| 1375 | } |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1376 | |
| 1377 | /* hash everything */ |
| 1378 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1379 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1380 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 1381 | |
| 1382 | return 1; |
| 1383 | } |
| 1384 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1385 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 1386 | struct session *sess, struct stream *s, int flags) |
| 1387 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1388 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1389 | struct http_txn *txn = s->txn; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1390 | struct cache_entry *res; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1391 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 1392 | struct cache *cache = cconf->c.cache; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1393 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1394 | /* Ignore cache for HTTP/1.0 requests and for requests other than GET |
| 1395 | * and HEAD */ |
| 1396 | if (!(txn->req.flags & HTTP_MSGF_VER_11) || |
| 1397 | (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)) |
| 1398 | txn->flags |= TX_CACHE_IGNORE; |
| 1399 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1400 | if (IS_HTX_STRM(s)) |
| 1401 | htx_check_request_for_cacheability(s, &s->req); |
| 1402 | else |
| 1403 | check_request_for_cacheability(s, &s->req); |
| 1404 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1405 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 1406 | return ACT_RET_CONT; |
| 1407 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1408 | if (!sha1_hosturi(s)) |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 1409 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1410 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1411 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 1412 | return ACT_RET_CONT; |
| 1413 | |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1414 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1415 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1416 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1417 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1418 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1419 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1420 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1421 | if (res) { |
| 1422 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1423 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 1424 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1425 | s->target = &http_cache_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1426 | if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1427 | appctx->st0 = HTTP_CACHE_INIT; |
| 1428 | appctx->rule = rule; |
| 1429 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1430 | appctx->ctx.cache.next = NULL; |
| 1431 | appctx->ctx.cache.sent = 0; |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1432 | |
| 1433 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1434 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1435 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1436 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1437 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1438 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1439 | shctx_lock(shctx_ptr(cache)); |
| 1440 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 1441 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1442 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1443 | } |
| 1444 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1445 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1446 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 1451 | struct act_rule *rule, char **err) |
| 1452 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1453 | rule->action = ACT_CUSTOM; |
| 1454 | rule->action_ptr = http_action_req_cache_use; |
| 1455 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1456 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1457 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1458 | |
| 1459 | (*orig_arg)++; |
| 1460 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 1464 | { |
| 1465 | int err_code = 0; |
| 1466 | |
| 1467 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 1468 | |
| 1469 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1470 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 1471 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1472 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1473 | goto out; |
| 1474 | } |
| 1475 | |
| 1476 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1477 | err_code |= ERR_ABORT; |
| 1478 | goto out; |
| 1479 | } |
| 1480 | |
| 1481 | if (tmp_cache_config == NULL) { |
| 1482 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 1483 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1484 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1485 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1486 | goto out; |
| 1487 | } |
| 1488 | |
| 1489 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 1490 | if (strlen(args[1]) > 32) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1491 | ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n", |
| 1492 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1493 | err_code |= ERR_WARN; |
| 1494 | } |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1495 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1496 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1497 | tmp_cache_config->maxobjsz = 0; |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 1498 | tmp_cache_config->flags = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1499 | } |
| 1500 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1501 | unsigned long int maxsize; |
| 1502 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1503 | |
| 1504 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1505 | err_code |= ERR_ABORT; |
| 1506 | goto out; |
| 1507 | } |
| 1508 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1509 | maxsize = strtoul(args[1], &err, 10); |
| 1510 | if (err == args[1] || *err != '\0') { |
| 1511 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 1512 | file, linenum, args[1]); |
| 1513 | err_code |= ERR_ABORT; |
| 1514 | goto out; |
| 1515 | } |
| 1516 | |
| 1517 | if (maxsize > (UINT_MAX >> 20)) { |
| 1518 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 1519 | file, linenum, args[1], UINT_MAX >> 20); |
| 1520 | err_code |= ERR_ABORT; |
| 1521 | goto out; |
| 1522 | } |
| 1523 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1524 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1525 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1526 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1527 | } else if (strcmp(args[0], "max-age") == 0) { |
| 1528 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1529 | err_code |= ERR_ABORT; |
| 1530 | goto out; |
| 1531 | } |
| 1532 | |
| 1533 | if (!*args[1]) { |
| 1534 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 1535 | file, linenum, args[0]); |
| 1536 | err_code |= ERR_WARN; |
| 1537 | } |
| 1538 | |
| 1539 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1540 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1541 | unsigned int maxobjsz; |
| 1542 | char *err; |
| 1543 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1544 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1545 | err_code |= ERR_ABORT; |
| 1546 | goto out; |
| 1547 | } |
| 1548 | |
| 1549 | if (!*args[1]) { |
| 1550 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 1551 | file, linenum, args[0]); |
| 1552 | err_code |= ERR_WARN; |
| 1553 | } |
| 1554 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1555 | maxobjsz = strtoul(args[1], &err, 10); |
| 1556 | if (err == args[1] || *err != '\0') { |
| 1557 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 1558 | file, linenum, args[1]); |
| 1559 | err_code |= ERR_ABORT; |
| 1560 | goto out; |
| 1561 | } |
| 1562 | tmp_cache_config->maxobjsz = maxobjsz; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1563 | } |
| 1564 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1565 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1566 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1567 | goto out; |
| 1568 | } |
| 1569 | out: |
| 1570 | return err_code; |
| 1571 | } |
| 1572 | |
| 1573 | /* once the cache section is parsed */ |
| 1574 | |
| 1575 | int cfg_post_parse_section_cache() |
| 1576 | { |
| 1577 | struct shared_context *shctx; |
| 1578 | int err_code = 0; |
| 1579 | int ret_shctx; |
| 1580 | |
| 1581 | if (tmp_cache_config) { |
| 1582 | struct cache *cache; |
| 1583 | |
| 1584 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1585 | ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1586 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1587 | goto out; |
| 1588 | } |
| 1589 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1590 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1591 | /* Default max. file size is a 256th of the cache size. */ |
| 1592 | tmp_cache_config->maxobjsz = |
| 1593 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1594 | } |
| 1595 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 1596 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 1597 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1598 | goto out; |
| 1599 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1600 | |
| 1601 | ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 1602 | tmp_cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1603 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 1604 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1605 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1606 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1607 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1608 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1609 | |
| 1610 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1611 | goto out; |
| 1612 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1613 | shctx->free_block = cache_free_blocks; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1614 | memcpy(shctx->data, tmp_cache_config, sizeof(struct cache)); |
| 1615 | cache = (struct cache *)shctx->data; |
| 1616 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1617 | LIST_ADDQ(&caches, &cache->list); |
| 1618 | } |
| 1619 | out: |
| 1620 | free(tmp_cache_config); |
| 1621 | tmp_cache_config = NULL; |
| 1622 | return err_code; |
| 1623 | |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | * Resolve the cache name to a pointer once the file is completely read. |
| 1628 | */ |
| 1629 | int cfg_cache_postparser() |
| 1630 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1631 | struct cache *cache; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1632 | int err = 0; |
Christopher Faulet | 1f672c5 | 2018-12-03 14:30:41 +0100 | [diff] [blame] | 1633 | |
| 1634 | /* Check if the cache is used by HTX and legacy HTTP proxies in same |
| 1635 | * time |
| 1636 | */ |
| 1637 | list_for_each_entry(cache, &caches, list) { |
| 1638 | if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) { |
| 1639 | ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n", |
| 1640 | cache->id); |
| 1641 | err++; |
| 1642 | } |
| 1643 | } |
| 1644 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1645 | return err; |
| 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | struct flt_ops cache_ops = { |
| 1650 | .init = cache_store_init, |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1651 | .check = cache_store_check, |
| 1652 | .deinit = cache_store_deinit, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1653 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1654 | /* Handle channels activity */ |
| 1655 | .channel_start_analyze = cache_store_chn_start_analyze, |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 1656 | .channel_end_analyze = cache_store_chn_end_analyze, |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1657 | .channel_post_analyze = cache_store_post_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* Filter HTTP requests and responses */ |
| 1660 | .http_headers = cache_store_http_headers, |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1661 | .http_payload = cache_store_http_payload, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1662 | .http_end = cache_store_http_end, |
| 1663 | |
| 1664 | .http_forward_data = cache_store_http_forward_data, |
| 1665 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1666 | }; |
| 1667 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1668 | |
| 1669 | |
| 1670 | static int |
| 1671 | parse_cache_flt(char **args, int *cur_arg, struct proxy *px, |
| 1672 | struct flt_conf *fconf, char **err, void *private) |
| 1673 | { |
| 1674 | struct flt_conf *f, *back; |
Willy Tarreau | a73da1e | 2018-12-14 10:19:28 +0100 | [diff] [blame] | 1675 | struct cache_flt_conf *cconf = NULL; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1676 | char *name = NULL; |
| 1677 | int pos = *cur_arg; |
| 1678 | |
| 1679 | /* Get the cache filter name*/ |
| 1680 | if (!strcmp(args[pos], "cache")) { |
| 1681 | if (!*args[pos + 1]) { |
| 1682 | memprintf(err, "%s : expects an <id> argument", args[pos]); |
| 1683 | goto error; |
| 1684 | } |
| 1685 | name = strdup(args[pos + 1]); |
| 1686 | if (!name) { |
| 1687 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 1688 | goto error; |
| 1689 | } |
| 1690 | pos += 2; |
| 1691 | } |
| 1692 | |
| 1693 | /* Check if an implicit filter with the same name already exists. If so, |
| 1694 | * we remove the implicit filter to use the explicit one. */ |
| 1695 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 1696 | if (f->id != cache_store_flt_id) |
| 1697 | continue; |
| 1698 | |
| 1699 | cconf = f->conf; |
| 1700 | if (strcmp(name, cconf->c.name)) { |
| 1701 | cconf = NULL; |
| 1702 | continue; |
| 1703 | } |
| 1704 | |
| 1705 | if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 1706 | cconf = NULL; |
| 1707 | memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'", |
| 1708 | px->id, name); |
| 1709 | return -1; |
| 1710 | } |
| 1711 | |
| 1712 | /* Remove the implicit filter. <cconf> is kept for the explicit one */ |
| 1713 | LIST_DEL(&f->list); |
| 1714 | free(f); |
| 1715 | free(name); |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | /* No implicit cache filter found, create configuration for the explicit one */ |
| 1720 | if (!cconf) { |
| 1721 | cconf = calloc(1, sizeof(*cconf)); |
| 1722 | if (!cconf) { |
| 1723 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 1724 | goto error; |
| 1725 | } |
| 1726 | cconf->c.name = name; |
| 1727 | } |
| 1728 | |
| 1729 | cconf->flags = 0; |
| 1730 | fconf->id = cache_store_flt_id; |
| 1731 | fconf->conf = cconf; |
| 1732 | fconf->ops = &cache_ops; |
| 1733 | |
| 1734 | *cur_arg = pos; |
| 1735 | return 0; |
| 1736 | |
| 1737 | error: |
| 1738 | free(name); |
| 1739 | free(cconf); |
| 1740 | return -1; |
| 1741 | } |
| 1742 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1743 | static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1744 | { |
| 1745 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1746 | return 1; |
| 1747 | |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 1752 | { |
| 1753 | struct cache* cache = appctx->ctx.cli.p0; |
| 1754 | struct stream_interface *si = appctx->owner; |
| 1755 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1756 | if (cache == NULL) { |
| 1757 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 1758 | } |
| 1759 | |
| 1760 | list_for_each_entry_from(cache, &caches, list) { |
| 1761 | struct eb32_node *node = NULL; |
| 1762 | unsigned int next_key; |
| 1763 | struct cache_entry *entry; |
| 1764 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1765 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1766 | if (!next_key) { |
| 1767 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 1768 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1769 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1770 | return 0; |
| 1771 | } |
| 1772 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1773 | |
| 1774 | appctx->ctx.cli.p0 = cache; |
| 1775 | |
| 1776 | while (1) { |
| 1777 | |
| 1778 | shctx_lock(shctx_ptr(cache)); |
| 1779 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 1780 | if (!node) { |
| 1781 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1782 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1783 | break; |
| 1784 | } |
| 1785 | |
| 1786 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1787 | chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, (*(unsigned int *)entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1788 | |
| 1789 | next_key = node->key + 1; |
| 1790 | appctx->ctx.cli.i0 = next_key; |
| 1791 | |
| 1792 | shctx_unlock(shctx_ptr(cache)); |
| 1793 | |
| 1794 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1795 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | } |
| 1801 | |
| 1802 | return 1; |
| 1803 | |
| 1804 | } |
| 1805 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1806 | /* Declare the filter parser for "cache" keyword */ |
| 1807 | static struct flt_kw_list filter_kws = { "CACHE", { }, { |
| 1808 | { "cache", parse_cache_flt, NULL }, |
| 1809 | { NULL, NULL, NULL }, |
| 1810 | } |
| 1811 | }; |
| 1812 | |
| 1813 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 1814 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1815 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1816 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1817 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1818 | }}; |
| 1819 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1820 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1821 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1822 | static struct action_kw_list http_res_actions = { |
| 1823 | .kw = { |
| 1824 | { "cache-store", parse_cache_store }, |
| 1825 | { NULL, NULL } |
| 1826 | } |
| 1827 | }; |
| 1828 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1829 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 1830 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1831 | static struct action_kw_list http_req_actions = { |
| 1832 | .kw = { |
| 1833 | { "cache-use", parse_cache_use }, |
| 1834 | { NULL, NULL } |
| 1835 | } |
| 1836 | }; |
| 1837 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1838 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 1839 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 1840 | struct applet http_cache_applet = { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1841 | .obj_type = OBJ_TYPE_APPLET, |
| 1842 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1843 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1844 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1845 | }; |
| 1846 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 1847 | /* config parsers for this section */ |
| 1848 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
| 1849 | REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser); |