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