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