William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Cache management |
| 3 | * |
| 4 | * Copyright 2017 HAProxy Technologies |
| 5 | * William Lallemand <wlallemand@haproxy.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 13 | #include <eb32tree.h> |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 14 | #include <import/sha1.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 15 | |
William Lallemand | 75d9329 | 2017-11-21 20:01:24 +0100 | [diff] [blame] | 16 | #include <types/action.h> |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 17 | #include <types/cli.h> |
William Lallemand | 75d9329 | 2017-11-21 20:01:24 +0100 | [diff] [blame] | 18 | #include <types/filters.h> |
| 19 | #include <types/proxy.h> |
| 20 | #include <types/shctx.h> |
| 21 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 22 | #include <proto/channel.h> |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 23 | #include <proto/cli.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 24 | #include <proto/proxy.h> |
| 25 | #include <proto/hdr_idx.h> |
| 26 | #include <proto/filters.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 27 | #include <proto/http_rules.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 28 | #include <proto/proto_http.h> |
| 29 | #include <proto/log.h> |
| 30 | #include <proto/stream.h> |
| 31 | #include <proto/stream_interface.h> |
| 32 | #include <proto/shctx.h> |
| 33 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 34 | |
| 35 | #include <common/cfgparse.h> |
| 36 | #include <common/hash.h> |
| 37 | |
| 38 | /* flt_cache_store */ |
| 39 | |
| 40 | static const char *cache_store_flt_id = "cache store filter"; |
| 41 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 42 | static struct pool_head *pool_head_cache_st = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 43 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 44 | struct applet http_cache_applet; |
| 45 | |
| 46 | struct flt_ops cache_ops; |
| 47 | |
| 48 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 49 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 50 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 51 | unsigned int maxage; /* max-age */ |
| 52 | unsigned int maxblocks; |
| 53 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * cache ctx for filters |
| 58 | */ |
| 59 | struct cache_st { |
| 60 | int hdrs_len; |
| 61 | struct shared_block *first_block; |
| 62 | }; |
| 63 | |
| 64 | struct cache_entry { |
| 65 | unsigned int latest_validation; /* latest validation date */ |
| 66 | unsigned int expire; /* expiration date */ |
| 67 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 68 | char hash[20]; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 69 | unsigned char data[0]; |
| 70 | }; |
| 71 | |
| 72 | #define CACHE_BLOCKSIZE 1024 |
| 73 | |
| 74 | static struct list caches = LIST_HEAD_INIT(caches); |
| 75 | static struct cache *tmp_cache_config = NULL; |
| 76 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 77 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 78 | { |
| 79 | struct eb32_node *node; |
| 80 | struct cache_entry *entry; |
| 81 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 82 | node = eb32_lookup(&cache->entries, (*(unsigned int *)hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 83 | if (!node) |
| 84 | return NULL; |
| 85 | |
| 86 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 87 | |
| 88 | /* if that's not the right node */ |
| 89 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 90 | return NULL; |
| 91 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 92 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 93 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 94 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 95 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 96 | entry->eb.key = 0; |
| 97 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 98 | return NULL; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 103 | { |
| 104 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 105 | } |
| 106 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 107 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 108 | { |
| 109 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 114 | static int |
| 115 | cache_store_init(struct proxy *px, struct flt_conf *f1conf) |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 120 | static int |
| 121 | cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 122 | { |
| 123 | if (!(chn->flags & CF_ISRESP)) |
| 124 | return 1; |
| 125 | |
| 126 | if (filter->ctx == NULL) { |
| 127 | struct cache_st *st; |
| 128 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 129 | st = pool_alloc_dirty(pool_head_cache_st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 130 | if (st == NULL) |
| 131 | return -1; |
| 132 | |
| 133 | st->hdrs_len = 0; |
| 134 | st->first_block = NULL; |
| 135 | filter->ctx = st; |
| 136 | } |
| 137 | |
| 138 | register_data_filter(s, chn, filter); |
| 139 | |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | static int |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 144 | cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 145 | { |
| 146 | struct cache_st *st = filter->ctx; |
| 147 | struct cache *cache = filter->config->conf; |
| 148 | struct shared_context *shctx = shctx_ptr(cache); |
| 149 | |
| 150 | if (!(chn->flags & CF_ISRESP)) |
| 151 | return 1; |
| 152 | |
| 153 | /* Everything should be released in the http_end filter, but we need to do it |
| 154 | * there too, in case of errors */ |
| 155 | |
| 156 | if (st && st->first_block) { |
| 157 | |
| 158 | shctx_lock(shctx); |
| 159 | shctx_row_dec_hot(shctx, st->first_block); |
| 160 | shctx_unlock(shctx); |
| 161 | |
| 162 | } |
| 163 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 164 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 165 | filter->ctx = NULL; |
| 166 | } |
| 167 | |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 173 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 174 | { |
| 175 | struct cache_st *st = filter->ctx; |
| 176 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 177 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 178 | return 1; |
| 179 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 180 | st->hdrs_len = msg->sov; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 181 | |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | static int |
| 186 | cache_store_http_forward_data(struct stream *s, struct filter *filter, |
| 187 | struct http_msg *msg, unsigned int len) |
| 188 | { |
| 189 | struct cache_st *st = filter->ctx; |
| 190 | struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 191 | struct cache_entry *object; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 192 | int ret; |
| 193 | |
| 194 | /* |
| 195 | * We need to skip the HTTP headers first, because we saved them in the |
| 196 | * http-response action. |
| 197 | */ |
| 198 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 199 | return len; |
| 200 | |
| 201 | if (!len) { |
| 202 | /* Nothing to foward */ |
| 203 | ret = len; |
| 204 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 205 | else if (st->hdrs_len >= len) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 206 | /* Forward part of headers */ |
| 207 | ret = len; |
| 208 | st->hdrs_len -= len; |
| 209 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 210 | else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 211 | /* Forward data */ |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 212 | if (filter->ctx && st->first_block) { |
| 213 | /* disable buffering if too much data (never greater than a buffer size */ |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 214 | if (len - st->hdrs_len > global.tune.bufsize - global.tune.maxrewrite - st->first_block->len) { |
William Lallemand | e1533f5 | 2017-11-14 14:39:24 +0100 | [diff] [blame] | 215 | disable_cache: |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 216 | object = (struct cache_entry *)st->first_block->data; |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 217 | filter->ctx = NULL; /* disable cache */ |
| 218 | shctx_lock(shctx); |
| 219 | shctx_row_dec_hot(shctx, st->first_block); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 220 | object->eb.key = 0; |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 221 | shctx_unlock(shctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 222 | pool_free(pool_head_cache_st, st); |
Olivier Houchard | cd2867a | 2017-11-01 13:58:21 +0100 | [diff] [blame] | 223 | } else { |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 224 | /* Skip remaining headers to fill the cache */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 225 | c_adv(msg->chn, st->hdrs_len); |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 226 | ret = shctx_row_data_append(shctx, |
| 227 | st->first_block, |
Willy Tarreau | dda2e41 | 2018-06-07 18:08:04 +0200 | [diff] [blame] | 228 | (unsigned char *)ci_head(msg->chn), |
Willy Tarreau | 7194d3c | 2018-06-06 16:55:45 +0200 | [diff] [blame] | 229 | MIN(ci_contig_data(msg->chn), len - st->hdrs_len)); |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 230 | /* Rewind the buffer to forward all data */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 231 | c_rew(msg->chn, st->hdrs_len); |
William Lallemand | bcd9101 | 2017-11-28 11:33:02 +0100 | [diff] [blame] | 232 | st->hdrs_len = 0; |
William Lallemand | e1533f5 | 2017-11-14 14:39:24 +0100 | [diff] [blame] | 233 | if (ret) |
| 234 | goto disable_cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
William Lallemand | 10935bc | 2017-11-14 14:39:23 +0100 | [diff] [blame] | 237 | ret = len; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if ((ret != len) || |
| 241 | (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret)) |
| 242 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static int |
| 248 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 249 | struct http_msg *msg) |
| 250 | { |
| 251 | struct cache_st *st = filter->ctx; |
| 252 | struct cache *cache = filter->config->conf; |
| 253 | struct shared_context *shctx = shctx_ptr(cache); |
| 254 | struct cache_entry *object; |
| 255 | |
| 256 | if (!(msg->chn->flags & CF_ISRESP)) |
| 257 | return 1; |
| 258 | |
| 259 | if (st && st->first_block) { |
| 260 | |
| 261 | object = (struct cache_entry *)st->first_block->data; |
| 262 | |
| 263 | /* does not need to test if the insertion worked, if it |
| 264 | * doesn't, the blocks will be reused anyway */ |
| 265 | |
| 266 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 267 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 268 | object->eb.key = 0; |
| 269 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 270 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 271 | shctx_row_dec_hot(shctx, st->first_block); |
| 272 | shctx_unlock(shctx); |
| 273 | |
| 274 | } |
| 275 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 276 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 277 | filter->ctx = NULL; |
| 278 | } |
| 279 | |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * This intends to be used when checking HTTP headers for some |
| 285 | * word=value directive. Return a pointer to the first character of value, if |
| 286 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 287 | */ |
| 288 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 289 | { |
| 290 | int st = 0; |
| 291 | |
| 292 | if (slen < wlen) |
| 293 | return 0; |
| 294 | |
| 295 | while (wlen) { |
| 296 | char c = *sample ^ *word; |
| 297 | if (c && c != ('A' ^ 'a')) |
| 298 | return NULL; |
| 299 | sample++; |
| 300 | word++; |
| 301 | slen--; |
| 302 | wlen--; |
| 303 | } |
| 304 | |
| 305 | while (slen) { |
| 306 | if (st == 0) { |
| 307 | if (*sample != '=') |
| 308 | return NULL; |
| 309 | sample++; |
| 310 | slen--; |
| 311 | st = 1; |
| 312 | continue; |
| 313 | } else { |
| 314 | return (char *)sample; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return NULL; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Return the maxage in seconds of an HTTP response. |
| 323 | * Compute the maxage using either: |
| 324 | * - the assigned max-age of the cache |
| 325 | * - the s-maxage directive |
| 326 | * - the max-age directive |
| 327 | * - (Expires - Data) headers |
| 328 | * - the default-max-age of the cache |
| 329 | * |
| 330 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 331 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 332 | { |
| 333 | struct http_txn *txn = s->txn; |
| 334 | struct hdr_ctx ctx; |
| 335 | |
| 336 | int smaxage = -1; |
| 337 | int maxage = -1; |
| 338 | |
| 339 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 340 | ctx.idx = 0; |
| 341 | |
| 342 | /* loop on the Cache-Control values */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 343 | while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 344 | char *directive = ctx.line + ctx.val; |
| 345 | char *value; |
| 346 | |
| 347 | value = directive_value(directive, ctx.vlen, "s-maxage", 8); |
| 348 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 349 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 350 | |
| 351 | chunk_strncat(chk, value, ctx.vlen - 8 + 1); |
| 352 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 353 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7); |
| 357 | if (value) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 358 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 359 | |
| 360 | chunk_strncat(chk, value, ctx.vlen - 7 + 1); |
| 361 | chunk_strncat(chk, "", 1); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 362 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /* TODO: Expires - Data */ |
| 367 | |
| 368 | |
| 369 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 370 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 371 | |
| 372 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 373 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 374 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 375 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 376 | |
| 377 | } |
| 378 | |
| 379 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 380 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 381 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 382 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 383 | |
| 384 | if (first == block && object->eb.key) |
| 385 | eb32_delete(&object->eb); |
| 386 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 387 | } |
| 388 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 389 | /* |
| 390 | * This fonction will store the headers of the response in a buffer and then |
| 391 | * register a filter to store the data |
| 392 | */ |
| 393 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
| 394 | struct session *sess, struct stream *s, int flags) |
| 395 | { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 396 | struct http_txn *txn = s->txn; |
| 397 | struct http_msg *msg = &txn->rsp; |
| 398 | struct filter *filter; |
| 399 | struct hdr_ctx ctx; |
| 400 | struct shared_block *first = NULL; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 401 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 402 | struct shared_context *shctx = shctx_ptr(cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 403 | struct cache_entry *object; |
| 404 | |
| 405 | |
| 406 | /* Don't cache if the response came from a cache */ |
| 407 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 408 | s->target == &http_cache_applet.obj_type) { |
| 409 | goto out; |
| 410 | } |
| 411 | |
| 412 | /* cache only HTTP/1.1 */ |
| 413 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 414 | goto out; |
| 415 | |
William Lallemand | 18f133a | 2017-11-08 11:25:15 +0100 | [diff] [blame] | 416 | /* does not cache if Content-Length unknown */ |
| 417 | if (!(msg->flags & HTTP_MSGF_CNT_LEN)) |
| 418 | goto out; |
| 419 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 420 | /* cache only GET method */ |
| 421 | if (txn->meth != HTTP_METH_GET) |
| 422 | goto out; |
| 423 | |
| 424 | /* cache only 200 status code */ |
| 425 | if (txn->status != 200) |
| 426 | goto out; |
| 427 | |
| 428 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 429 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 430 | if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 431 | goto out; |
| 432 | |
Willy Tarreau | faf2909 | 2017-12-21 15:59:17 +0100 | [diff] [blame] | 433 | check_response_for_cacheability(s, &s->res); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 434 | |
Willy Tarreau | d4569d1 | 2017-12-22 18:03:04 +0100 | [diff] [blame] | 435 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 436 | goto out; |
| 437 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 438 | if ((msg->sov + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite)) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 439 | goto out; |
| 440 | |
| 441 | shctx_lock(shctx); |
| 442 | |
William Lallemand | 9d5f54d | 2017-11-14 14:39:22 +0100 | [diff] [blame] | 443 | first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->sov + msg->body_len); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 444 | if (!first) { |
| 445 | shctx_unlock(shctx); |
| 446 | goto out; |
| 447 | } |
| 448 | shctx_unlock(shctx); |
| 449 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 450 | /* the received memory is not initialized, we need at least to mark |
| 451 | * the object as not indexed yet. |
| 452 | */ |
| 453 | object = (struct cache_entry *)first->data; |
| 454 | object->eb.node.leaf_p = NULL; |
| 455 | object->eb.key = 0; |
| 456 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 457 | /* reserve space for the cache_entry structure */ |
| 458 | first->len = sizeof(struct cache_entry); |
| 459 | |
| 460 | /* cache the headers in a http action because it allows to chose what |
| 461 | * to cache, for example you might want to cache a response before |
| 462 | * modifying some HTTP headers, or on the contrary after modifying |
| 463 | * those headers. |
| 464 | */ |
| 465 | |
| 466 | /* does not need to be locked because it's in the "hot" list, |
| 467 | * copy the headers */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 468 | if (shctx_row_data_append(shctx, first, (unsigned char *)ci_head(&s->res), msg->sov) < 0) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 469 | goto out; |
| 470 | |
| 471 | /* register the buffer in the filter ctx for filling it with data*/ |
| 472 | if (!LIST_ISEMPTY(&s->strm_flt.filters)) { |
| 473 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 474 | if (filter->config->id == cache_store_flt_id && |
| 475 | filter->config->conf == rule->arg.act.p[0]) { |
| 476 | if (filter->ctx) { |
| 477 | struct cache_st *cache_ctx = filter->ctx; |
Willy Tarreau | c9bd34c | 2017-12-22 17:42:46 +0100 | [diff] [blame] | 478 | struct cache_entry *old; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 479 | |
| 480 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 481 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 482 | object->eb.key = (*(unsigned int *)&txn->cache_hash); |
| 483 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 484 | /* Insert the node later on caching success */ |
| 485 | |
| 486 | shctx_lock(shctx); |
Willy Tarreau | c9bd34c | 2017-12-22 17:42:46 +0100 | [diff] [blame] | 487 | |
| 488 | old = entry_exist(cache, txn->cache_hash); |
| 489 | if (old) { |
| 490 | eb32_delete(&old->eb); |
| 491 | old->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 492 | } |
| 493 | shctx_unlock(shctx); |
| 494 | |
| 495 | /* store latest value and expiration time */ |
| 496 | object->latest_validation = now.tv_sec; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 497 | object->expire = now.tv_sec + http_calc_maxage(s, cache); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 498 | } |
| 499 | return ACT_RET_CONT; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | out: |
| 505 | /* if does not cache */ |
| 506 | if (first) { |
| 507 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 508 | first->len = 0; |
| 509 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 510 | shctx_row_dec_hot(shctx, first); |
| 511 | shctx_unlock(shctx); |
| 512 | } |
| 513 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 514 | return ACT_RET_CONT; |
| 515 | } |
| 516 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 517 | #define HTTP_CACHE_INIT 0 |
| 518 | #define HTTP_CACHE_FWD 1 |
| 519 | #define HTTP_CACHE_END 2 |
| 520 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 521 | static void http_cache_applet_release(struct appctx *appctx) |
| 522 | { |
| 523 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
| 524 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 525 | struct shared_block *first = block_ptr(cache_ptr); |
| 526 | |
| 527 | shctx_lock(shctx_ptr(cache)); |
| 528 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 529 | shctx_unlock(shctx_ptr(cache)); |
| 530 | } |
| 531 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 532 | static void http_cache_io_handler(struct appctx *appctx) |
| 533 | { |
| 534 | struct stream_interface *si = appctx->owner; |
| 535 | struct channel *res = si_ic(si); |
| 536 | struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0]; |
| 537 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 538 | struct shared_context *shctx = shctx_ptr(cache); |
| 539 | struct shared_block *first = block_ptr(cache_ptr); |
| 540 | |
| 541 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 542 | goto out; |
| 543 | |
| 544 | /* Check if the input buffer is avalaible. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 545 | if (res->buf.size == 0) { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 546 | si_applet_cant_put(si); |
| 547 | goto out; |
| 548 | } |
| 549 | |
| 550 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 551 | appctx->st0 = HTTP_CACHE_END; |
| 552 | |
| 553 | /* buffer are aligned there, should be fine */ |
| 554 | if (appctx->st0 == HTTP_CACHE_INIT) { |
| 555 | int len = first->len - sizeof(struct cache_entry); |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 556 | if ((shctx_row_data_get(shctx, first, (unsigned char *)ci_tail(res), sizeof(struct cache_entry), len)) != 0) { |
William Lallemand | a71cd1d | 2017-11-24 18:53:42 +0100 | [diff] [blame] | 557 | /* should never get there, because at the moment, a |
| 558 | * cache object can never be bigger than a buffer */ |
| 559 | abort(); |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 560 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 561 | si_applet_cant_put(si); |
| 562 | goto out; |
| 563 | } |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 564 | b_add(&res->buf, len); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 565 | res->total += len; |
| 566 | appctx->st0 = HTTP_CACHE_FWD; |
| 567 | } |
| 568 | |
| 569 | if (appctx->st0 == HTTP_CACHE_FWD) { |
| 570 | /* eat the whole request */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 571 | co_skip(si_oc(si), co_data(si_oc(si))); // NOTE: when disabled does not repport the correct status code |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 572 | res->flags |= CF_READ_NULL; |
| 573 | si_shutr(si); |
| 574 | } |
| 575 | |
| 576 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 577 | si_shutw(si); |
| 578 | out: |
| 579 | ; |
| 580 | } |
| 581 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 582 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 583 | struct act_rule *rule, char **err) |
| 584 | { |
| 585 | struct flt_conf *fconf; |
| 586 | int cur_arg = *orig_arg; |
| 587 | rule->action = ACT_CUSTOM; |
| 588 | rule->action_ptr = http_action_store_cache; |
| 589 | |
| 590 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 591 | memprintf(err, "expects a cache name"); |
| 592 | return ACT_RET_PRS_ERR; |
| 593 | } |
| 594 | |
| 595 | /* check if a cache filter was already registered with this cache |
| 596 | * name, if that's the case, must use it. */ |
| 597 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 598 | if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) { |
| 599 | rule->arg.act.p[0] = fconf->conf; |
| 600 | (*orig_arg)++; |
| 601 | /* filter already registered */ |
| 602 | return ACT_RET_PRS_OK; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 607 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 608 | ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 609 | err++; |
| 610 | goto err; |
| 611 | } |
| 612 | /* register a filter to fill the cache buffer */ |
| 613 | fconf = calloc(1, sizeof(*fconf)); |
| 614 | if (!fconf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 615 | ha_alert("config: %s '%s': out of memory\n", |
| 616 | proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 617 | err++; |
| 618 | goto err; |
| 619 | } |
| 620 | fconf->id = cache_store_flt_id; |
| 621 | fconf->conf = rule->arg.act.p[0]; /* store the proxy name */ |
| 622 | fconf->ops = &cache_ops; |
| 623 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 624 | |
| 625 | (*orig_arg)++; |
| 626 | |
| 627 | return ACT_RET_PRS_OK; |
| 628 | |
| 629 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 630 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 631 | } |
| 632 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 633 | /* This produces a sha1 hash of the concatenation of the first |
| 634 | * occurrence of the Host header followed by the path component if it |
| 635 | * begins with a slash ('/'). */ |
| 636 | int sha1_hosturi(struct http_txn *txn) |
| 637 | { |
| 638 | struct hdr_ctx ctx; |
| 639 | |
| 640 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 641 | struct buffer *trash; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 642 | char *path; |
| 643 | char *end; |
| 644 | trash = get_trash_chunk(); |
| 645 | |
| 646 | /* retrive the host */ |
| 647 | ctx.idx = 0; |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 648 | if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 649 | return 0; |
| 650 | chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen); |
| 651 | |
| 652 | /* now retrieve the path */ |
Willy Tarreau | 178b987 | 2018-06-19 07:13:36 +0200 | [diff] [blame] | 653 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 654 | path = http_txn_get_path(txn); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 655 | if (!path) |
| 656 | return 0; |
| 657 | chunk_strncat(trash, path, end - path); |
| 658 | |
| 659 | /* hash everything */ |
| 660 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 661 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 662 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 663 | |
| 664 | return 1; |
| 665 | } |
| 666 | |
| 667 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 668 | |
| 669 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 670 | struct session *sess, struct stream *s, int flags) |
| 671 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 672 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 673 | struct cache_entry *res; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 674 | struct cache *cache = (struct cache *)rule->arg.act.p[0]; |
| 675 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 676 | check_request_for_cacheability(s, &s->req); |
| 677 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 678 | return ACT_RET_CONT; |
| 679 | |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 680 | if (!sha1_hosturi(s->txn)) |
| 681 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 682 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 683 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 684 | return ACT_RET_CONT; |
| 685 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 686 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 687 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 688 | if (res) { |
| 689 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 690 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 691 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 692 | s->target = &http_cache_applet.obj_type; |
| 693 | if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
| 694 | appctx->st0 = HTTP_CACHE_INIT; |
| 695 | appctx->rule = rule; |
| 696 | appctx->ctx.cache.entry = res; |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 697 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 698 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 699 | shctx_lock(shctx_ptr(cache)); |
| 700 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 701 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 702 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 703 | } |
| 704 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 705 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 706 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
| 710 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 711 | struct act_rule *rule, char **err) |
| 712 | { |
| 713 | int cur_arg = *orig_arg; |
| 714 | |
| 715 | rule->action = ACT_CUSTOM; |
| 716 | rule->action_ptr = http_action_req_cache_use; |
| 717 | |
| 718 | if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 719 | memprintf(err, "expects a cache name"); |
| 720 | return ACT_RET_PRS_ERR; |
| 721 | } |
| 722 | |
| 723 | rule->arg.act.p[0] = strdup(args[cur_arg]); |
| 724 | if (!rule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 725 | ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 726 | err++; |
| 727 | goto err; |
| 728 | } |
| 729 | |
| 730 | (*orig_arg)++; |
| 731 | return ACT_RET_PRS_OK; |
| 732 | |
| 733 | err: |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 734 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 735 | |
| 736 | } |
| 737 | |
| 738 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 739 | { |
| 740 | int err_code = 0; |
| 741 | |
| 742 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 743 | |
| 744 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 745 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 746 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 747 | err_code |= ERR_ALERT | ERR_ABORT; |
| 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 752 | err_code |= ERR_ABORT; |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | if (tmp_cache_config == NULL) { |
| 757 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 758 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 759 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 760 | err_code |= ERR_ALERT | ERR_ABORT; |
| 761 | goto out; |
| 762 | } |
| 763 | |
| 764 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 765 | if (strlen(args[1]) > 32) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 766 | ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n", |
| 767 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 768 | err_code |= ERR_WARN; |
| 769 | } |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 770 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 771 | tmp_cache_config->maxblocks = 0; |
| 772 | } |
| 773 | } else if (strcmp(args[0], "total-max-size") == 0) { |
| 774 | int maxsize; |
| 775 | |
| 776 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 777 | err_code |= ERR_ABORT; |
| 778 | goto out; |
| 779 | } |
| 780 | |
| 781 | /* size in megabytes */ |
| 782 | maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE; |
| 783 | tmp_cache_config->maxblocks = maxsize; |
| 784 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 785 | } else if (strcmp(args[0], "max-age") == 0) { |
| 786 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 787 | err_code |= ERR_ABORT; |
| 788 | goto out; |
| 789 | } |
| 790 | |
| 791 | if (!*args[1]) { |
| 792 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 793 | file, linenum, args[0]); |
| 794 | err_code |= ERR_WARN; |
| 795 | } |
| 796 | |
| 797 | tmp_cache_config->maxage = atoi(args[1]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 798 | } else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 799 | 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] | 800 | err_code |= ERR_ALERT | ERR_FATAL; |
| 801 | goto out; |
| 802 | } |
| 803 | out: |
| 804 | return err_code; |
| 805 | } |
| 806 | |
| 807 | /* once the cache section is parsed */ |
| 808 | |
| 809 | int cfg_post_parse_section_cache() |
| 810 | { |
| 811 | struct shared_context *shctx; |
| 812 | int err_code = 0; |
| 813 | int ret_shctx; |
| 814 | |
| 815 | if (tmp_cache_config) { |
| 816 | struct cache *cache; |
| 817 | |
| 818 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 819 | 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] | 820 | err_code |= ERR_FATAL | ERR_ALERT; |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 825 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 826 | if (ret_shctx < 0) { |
| 827 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 828 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 829 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 830 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 831 | |
| 832 | err_code |= ERR_FATAL | ERR_ALERT; |
| 833 | goto out; |
| 834 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 835 | shctx->free_block = cache_free_blocks; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 836 | memcpy(shctx->data, tmp_cache_config, sizeof(struct cache)); |
| 837 | cache = (struct cache *)shctx->data; |
| 838 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 839 | LIST_ADDQ(&caches, &cache->list); |
| 840 | } |
| 841 | out: |
| 842 | free(tmp_cache_config); |
| 843 | tmp_cache_config = NULL; |
| 844 | return err_code; |
| 845 | |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Resolve the cache name to a pointer once the file is completely read. |
| 850 | */ |
| 851 | int cfg_cache_postparser() |
| 852 | { |
| 853 | struct act_rule *hresrule, *hrqrule; |
| 854 | void *cache_ptr; |
| 855 | struct cache *cache; |
| 856 | struct proxy *curproxy = NULL; |
| 857 | int err = 0; |
| 858 | struct flt_conf *fconf; |
| 859 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 860 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 861 | |
| 862 | /* resolve the http response cache name to a ptr in the action rule */ |
| 863 | list_for_each_entry(hresrule, &curproxy->http_res_rules, list) { |
| 864 | if (hresrule->action != ACT_CUSTOM || |
| 865 | hresrule->action_ptr != http_action_store_cache) |
| 866 | continue; |
| 867 | |
| 868 | cache_ptr = hresrule->arg.act.p[0]; |
| 869 | |
| 870 | list_for_each_entry(cache, &caches, list) { |
| 871 | if (!strcmp(cache->id, cache_ptr)) { |
| 872 | /* don't free there, it's still used in the filter conf */ |
| 873 | cache_ptr = cache; |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if (cache_ptr == hresrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 879 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n", |
| 880 | curproxy->id, (char *)hresrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 881 | err++; |
| 882 | } |
| 883 | |
| 884 | hresrule->arg.act.p[0] = cache_ptr; |
| 885 | } |
| 886 | |
| 887 | /* resolve the http request cache name to a ptr in the action rule */ |
| 888 | list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) { |
| 889 | if (hrqrule->action != ACT_CUSTOM || |
| 890 | hrqrule->action_ptr != http_action_req_cache_use) |
| 891 | continue; |
| 892 | |
| 893 | cache_ptr = hrqrule->arg.act.p[0]; |
| 894 | |
| 895 | list_for_each_entry(cache, &caches, list) { |
| 896 | if (!strcmp(cache->id, cache_ptr)) { |
| 897 | free(cache_ptr); |
| 898 | cache_ptr = cache; |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | if (cache_ptr == hrqrule->arg.act.p[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 904 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n", |
| 905 | curproxy->id, (char *)hrqrule->arg.act.p[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 906 | err++; |
| 907 | } |
| 908 | |
| 909 | hrqrule->arg.act.p[0] = cache_ptr; |
| 910 | } |
| 911 | |
| 912 | /* resolve the cache name to a ptr in the filter config */ |
| 913 | list_for_each_entry(fconf, &curproxy->filter_configs, list) { |
| 914 | |
William Lallemand | 9c54c53 | 2017-11-02 16:38:42 +0100 | [diff] [blame] | 915 | if (fconf->id != cache_store_flt_id) |
| 916 | continue; |
| 917 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 918 | cache_ptr = fconf->conf; |
| 919 | |
| 920 | list_for_each_entry(cache, &caches, list) { |
| 921 | if (!strcmp(cache->id, cache_ptr)) { |
| 922 | /* there can be only one filter per cache, so we free it there */ |
| 923 | free(cache_ptr); |
| 924 | cache_ptr = cache; |
| 925 | break; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | if (cache_ptr == fconf->conf) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 930 | ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 931 | curproxy->id, (char *)fconf->conf); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 932 | err++; |
| 933 | } |
| 934 | fconf->conf = cache_ptr; |
| 935 | } |
| 936 | } |
| 937 | return err; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | struct flt_ops cache_ops = { |
| 942 | .init = cache_store_init, |
| 943 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 944 | /* Handle channels activity */ |
| 945 | .channel_start_analyze = cache_store_chn_start_analyze, |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 946 | .channel_end_analyze = cache_store_chn_end_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 947 | |
| 948 | /* Filter HTTP requests and responses */ |
| 949 | .http_headers = cache_store_http_headers, |
| 950 | .http_end = cache_store_http_end, |
| 951 | |
| 952 | .http_forward_data = cache_store_http_forward_data, |
| 953 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 954 | }; |
| 955 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 956 | 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] | 957 | { |
| 958 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 959 | return 1; |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 965 | { |
| 966 | struct cache* cache = appctx->ctx.cli.p0; |
| 967 | struct stream_interface *si = appctx->owner; |
| 968 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 969 | if (cache == NULL) { |
| 970 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 971 | } |
| 972 | |
| 973 | list_for_each_entry_from(cache, &caches, list) { |
| 974 | struct eb32_node *node = NULL; |
| 975 | unsigned int next_key; |
| 976 | struct cache_entry *entry; |
| 977 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 978 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 979 | if (!next_key) { |
| 980 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 981 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 982 | si_applet_cant_put(si); |
| 983 | return 0; |
| 984 | } |
| 985 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 986 | |
| 987 | appctx->ctx.cli.p0 = cache; |
| 988 | |
| 989 | while (1) { |
| 990 | |
| 991 | shctx_lock(shctx_ptr(cache)); |
| 992 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 993 | if (!node) { |
| 994 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 995 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | |
| 999 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1000 | chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, (*(unsigned int *)entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1001 | |
| 1002 | next_key = node->key + 1; |
| 1003 | appctx->ctx.cli.i0 = next_key; |
| 1004 | |
| 1005 | shctx_unlock(shctx_ptr(cache)); |
| 1006 | |
| 1007 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 1008 | si_applet_cant_put(si); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | } |
| 1014 | |
| 1015 | return 1; |
| 1016 | |
| 1017 | } |
| 1018 | |
| 1019 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1020 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1021 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1022 | }}; |
| 1023 | |
| 1024 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1025 | static struct action_kw_list http_res_actions = { |
| 1026 | .kw = { |
| 1027 | { "cache-store", parse_cache_store }, |
| 1028 | { NULL, NULL } |
| 1029 | } |
| 1030 | }; |
| 1031 | |
| 1032 | static struct action_kw_list http_req_actions = { |
| 1033 | .kw = { |
| 1034 | { "cache-use", parse_cache_use }, |
| 1035 | { NULL, NULL } |
| 1036 | } |
| 1037 | }; |
| 1038 | |
| 1039 | struct applet http_cache_applet = { |
| 1040 | .obj_type = OBJ_TYPE_APPLET, |
| 1041 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1042 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1043 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1044 | }; |
| 1045 | |
| 1046 | __attribute__((constructor)) |
| 1047 | static void __cache_init(void) |
| 1048 | { |
| 1049 | cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
| 1050 | cfg_register_postparser("cache", cfg_cache_postparser); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1051 | cli_register_kw(&cli_kws); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1052 | http_res_keywords_register(&http_res_actions); |
| 1053 | http_req_keywords_register(&http_req_actions); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1054 | pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |