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