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