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