blob: 437a9086c20299d4ef4cd91a5db229c0092052f2 [file] [log] [blame]
William Lallemand41db4602017-10-30 11:15:51 +01001/*
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 Lallemand41db4602017-10-30 11:15:51 +010013#include <eb32tree.h>
William Lallemandf528fff2017-11-23 19:43:17 +010014#include <import/sha1.h>
William Lallemand41db4602017-10-30 11:15:51 +010015
William Lallemand75d93292017-11-21 20:01:24 +010016#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010017#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010018#include <types/filters.h>
19#include <types/proxy.h>
20#include <types/shctx.h>
21
William Lallemand41db4602017-10-30 11:15:51 +010022#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010023#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010024#include <proto/proxy.h>
25#include <proto/hdr_idx.h>
26#include <proto/filters.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020027#include <proto/http_rules.h>
William Lallemand41db4602017-10-30 11:15:51 +010028#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 Lallemand41db4602017-10-30 11:15:51 +010034
35#include <common/cfgparse.h>
36#include <common/hash.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010037#include <common/initcall.h>
William Lallemand41db4602017-10-30 11:15:51 +010038
39/* flt_cache_store */
Christopher Faulet1f672c52018-12-03 14:30:41 +010040#define CACHE_F_LEGACY_HTTP 0x00000001 /* The cache is used to store raw HTTP
41 * messages (legacy implementation) */
42#define CACHE_F_HTX 0x00000002 /* The cache is used to store HTX messages */
William Lallemand41db4602017-10-30 11:15:51 +010043
44static const char *cache_store_flt_id = "cache store filter";
45
46struct applet http_cache_applet;
47
48struct flt_ops cache_ops;
49
50struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010052 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 unsigned int maxage; /* max-age */
54 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020055 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010056 char id[33]; /* cache name */
Christopher Faulet1f672c52018-12-03 14:30:41 +010057 unsigned int flags; /* CACHE_F_* */
William Lallemand41db4602017-10-30 11:15:51 +010058};
59
60/*
61 * cache ctx for filters
62 */
63struct cache_st {
64 int hdrs_len;
65 struct shared_block *first_block;
66};
67
68struct cache_entry {
69 unsigned int latest_validation; /* latest validation date */
70 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020071 unsigned int age; /* Origin server "Age" header value */
72 unsigned int eoh; /* Origin server end of headers offset. */
William Lallemand41db4602017-10-30 11:15:51 +010073 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010074 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010075 unsigned char data[0];
76};
77
78#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010079#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010080
81static struct list caches = LIST_HEAD_INIT(caches);
82static struct cache *tmp_cache_config = NULL;
83
Willy Tarreau8ceae722018-11-26 11:58:30 +010084DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
85
William Lallemandf528fff2017-11-23 19:43:17 +010086struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010087{
88 struct eb32_node *node;
89 struct cache_entry *entry;
90
William Lallemandf528fff2017-11-23 19:43:17 +010091 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010092 if (!node)
93 return NULL;
94
95 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010096
97 /* if that's not the right node */
98 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
99 return NULL;
100
William Lallemand08727662017-11-21 20:01:27 +0100101 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100102 return entry;
William Lallemand08727662017-11-21 20:01:27 +0100103 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100104 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100105 entry->eb.key = 0;
106 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100107 return NULL;
108
109}
110
111static inline struct shared_context *shctx_ptr(struct cache *cache)
112{
113 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
114}
115
William Lallemand77c11972017-10-31 20:43:01 +0100116static inline struct shared_block *block_ptr(struct cache_entry *entry)
117{
118 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
119}
120
121
122
William Lallemand41db4602017-10-30 11:15:51 +0100123static int
124cache_store_init(struct proxy *px, struct flt_conf *f1conf)
125{
126 return 0;
127}
128
William Lallemand4da3f8a2017-10-31 14:33:34 +0100129static int
130cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
131{
132 if (!(chn->flags & CF_ISRESP))
133 return 1;
134
135 if (filter->ctx == NULL) {
136 struct cache_st *st;
137
Willy Tarreaubafbe012017-11-24 17:34:44 +0100138 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100139 if (st == NULL)
140 return -1;
141
142 st->hdrs_len = 0;
143 st->first_block = NULL;
144 filter->ctx = st;
145 }
146
William Lallemand4da3f8a2017-10-31 14:33:34 +0100147 return 1;
148}
149
150static int
William Lallemand49dc0482017-11-24 14:33:54 +0100151cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
152{
153 struct cache_st *st = filter->ctx;
154 struct cache *cache = filter->config->conf;
155 struct shared_context *shctx = shctx_ptr(cache);
156
157 if (!(chn->flags & CF_ISRESP))
158 return 1;
159
160 /* Everything should be released in the http_end filter, but we need to do it
161 * there too, in case of errors */
162
163 if (st && st->first_block) {
164
165 shctx_lock(shctx);
166 shctx_row_dec_hot(shctx, st->first_block);
167 shctx_unlock(shctx);
168
169 }
170 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100171 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100172 filter->ctx = NULL;
173 }
174
175 return 1;
176}
177
178
179static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100180cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
181{
182 struct cache_st *st = filter->ctx;
183
William Lallemand4da3f8a2017-10-31 14:33:34 +0100184 if (!(msg->chn->flags & CF_ISRESP) || !st)
185 return 1;
186
Christopher Faulet67658c92018-12-06 21:59:39 +0100187 if (st->first_block) {
188 register_data_filter(s, msg->chn, filter);
189 st->hdrs_len = msg->sov;
190 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100191 return 1;
192}
193
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200194static inline void disable_cache_entry(struct cache_st *st,
195 struct filter *filter, struct shared_context *shctx)
196{
197 struct cache_entry *object;
198
199 object = (struct cache_entry *)st->first_block->data;
200 filter->ctx = NULL; /* disable cache */
201 shctx_lock(shctx);
202 shctx_row_dec_hot(shctx, st->first_block);
203 object->eb.key = 0;
204 shctx_unlock(shctx);
205 pool_free(pool_head_cache_st, st);
206}
207
William Lallemand4da3f8a2017-10-31 14:33:34 +0100208static int
209cache_store_http_forward_data(struct stream *s, struct filter *filter,
210 struct http_msg *msg, unsigned int len)
211{
212 struct cache_st *st = filter->ctx;
213 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
214 int ret;
215
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200216 ret = 0;
217
William Lallemand4da3f8a2017-10-31 14:33:34 +0100218 /*
219 * We need to skip the HTTP headers first, because we saved them in the
220 * http-response action.
221 */
Christopher Faulet67658c92018-12-06 21:59:39 +0100222 if (!(msg->chn->flags & CF_ISRESP) || !st) {
223 /* should never happen */
224 unregister_data_filter(s, msg->chn, filter);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100225 return len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100226 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100227
228 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800229 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100230 ret = len;
231 }
William Lallemand10935bc2017-11-14 14:39:23 +0100232 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100233 /* Forward part of headers */
234 ret = len;
235 st->hdrs_len -= len;
236 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100237 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100238 /* Forward data */
Christopher Faulet67658c92018-12-06 21:59:39 +0100239 if (st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200240 int to_append, append;
241 struct shared_block *fb;
242
243 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
244
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200245 shctx_lock(shctx);
246 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
247 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100248 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200249 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100250 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200251 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100252 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200253 shctx_unlock(shctx);
254
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200255 /* Skip remaining headers to fill the cache */
256 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200257 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
258 (unsigned char *)ci_head(msg->chn), to_append);
259 ret = st->hdrs_len + to_append - append;
260 /* Rewind the buffer to forward all data */
261 c_rew(msg->chn, st->hdrs_len);
262 st->hdrs_len = 0;
Christopher Faulet67658c92018-12-06 21:59:39 +0100263 if (ret < 0) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200264 disable_cache_entry(st, filter, shctx);
Christopher Faulet67658c92018-12-06 21:59:39 +0100265 unregister_data_filter(s, msg->chn, filter);
266 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100267 }
Christopher Faulet67658c92018-12-06 21:59:39 +0100268 else {
269 /* should never happen */
270 unregister_data_filter(s, msg->chn, filter);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200271 ret = len;
Christopher Faulet67658c92018-12-06 21:59:39 +0100272 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100273 }
274
275 if ((ret != len) ||
276 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
277 task_wakeup(s->task, TASK_WOKEN_MSG);
278
279 return ret;
280}
281
282static int
283cache_store_http_end(struct stream *s, struct filter *filter,
284 struct http_msg *msg)
285{
286 struct cache_st *st = filter->ctx;
287 struct cache *cache = filter->config->conf;
288 struct shared_context *shctx = shctx_ptr(cache);
289 struct cache_entry *object;
290
291 if (!(msg->chn->flags & CF_ISRESP))
292 return 1;
293
294 if (st && st->first_block) {
295
296 object = (struct cache_entry *)st->first_block->data;
297
298 /* does not need to test if the insertion worked, if it
299 * doesn't, the blocks will be reused anyway */
300
301 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100302 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
303 object->eb.key = 0;
304 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100305 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100306 shctx_row_dec_hot(shctx, st->first_block);
307 shctx_unlock(shctx);
308
309 }
310 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100311 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100312 filter->ctx = NULL;
313 }
314
315 return 1;
316}
317
318 /*
319 * This intends to be used when checking HTTP headers for some
320 * word=value directive. Return a pointer to the first character of value, if
321 * the word was not found or if there wasn't any value assigned ot it return NULL
322 */
323char *directive_value(const char *sample, int slen, const char *word, int wlen)
324{
325 int st = 0;
326
327 if (slen < wlen)
328 return 0;
329
330 while (wlen) {
331 char c = *sample ^ *word;
332 if (c && c != ('A' ^ 'a'))
333 return NULL;
334 sample++;
335 word++;
336 slen--;
337 wlen--;
338 }
339
340 while (slen) {
341 if (st == 0) {
342 if (*sample != '=')
343 return NULL;
344 sample++;
345 slen--;
346 st = 1;
347 continue;
348 } else {
349 return (char *)sample;
350 }
351 }
352
353 return NULL;
354}
355
356/*
357 * Return the maxage in seconds of an HTTP response.
358 * Compute the maxage using either:
359 * - the assigned max-age of the cache
360 * - the s-maxage directive
361 * - the max-age directive
362 * - (Expires - Data) headers
363 * - the default-max-age of the cache
364 *
365 */
William Lallemand49b44532017-11-24 18:53:43 +0100366int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100367{
368 struct http_txn *txn = s->txn;
369 struct hdr_ctx ctx;
370
371 int smaxage = -1;
372 int maxage = -1;
373
374
William Lallemand4da3f8a2017-10-31 14:33:34 +0100375 ctx.idx = 0;
376
377 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200378 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100379 char *directive = ctx.line + ctx.val;
380 char *value;
381
382 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
383 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200384 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100385
386 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
387 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200388 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100389 }
390
391 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
392 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200393 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100394
395 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
396 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200397 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100398 }
399 }
400
401 /* TODO: Expires - Data */
402
403
404 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100405 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100406
407 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100408 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100409
William Lallemand49b44532017-11-24 18:53:43 +0100410 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100411
412}
413
414
William Lallemanda400a3a2017-11-20 19:13:12 +0100415static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
416{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200417 struct cache_entry *object = (struct cache_entry *)block->data;
418
419 if (first == block && object->eb.key)
420 eb32_delete(&object->eb);
421 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100422}
423
William Lallemand41db4602017-10-30 11:15:51 +0100424/*
425 * This fonction will store the headers of the response in a buffer and then
426 * register a filter to store the data
427 */
428enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
429 struct session *sess, struct stream *s, int flags)
430{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200431 unsigned int age;
432 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100433 struct http_txn *txn = s->txn;
434 struct http_msg *msg = &txn->rsp;
435 struct filter *filter;
436 struct hdr_ctx ctx;
437 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100438 struct cache *cache = (struct cache *)rule->arg.act.p[0];
439 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100440 struct cache_entry *object;
441
442
443 /* Don't cache if the response came from a cache */
444 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
445 s->target == &http_cache_applet.obj_type) {
446 goto out;
447 }
448
449 /* cache only HTTP/1.1 */
450 if (!(txn->req.flags & HTTP_MSGF_VER_11))
451 goto out;
452
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200453 /* Do not cache too big objects. */
454 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
455 msg->sov + msg->body_len > shctx->max_obj_size)
456 goto out;
457
William Lallemand4da3f8a2017-10-31 14:33:34 +0100458 /* cache only GET method */
459 if (txn->meth != HTTP_METH_GET)
460 goto out;
461
462 /* cache only 200 status code */
463 if (txn->status != 200)
464 goto out;
465
466 /* Does not manage Vary at the moment. We will need a secondary key later for that */
467 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200468 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100469 goto out;
470
Willy Tarreaufaf29092017-12-21 15:59:17 +0100471 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100472
Willy Tarreaud4569d12017-12-22 18:03:04 +0100473 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100474 goto out;
475
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200476 age = 0;
477 ctx.idx = 0;
478 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
479 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
480 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
481 hdr_age = CACHE_ENTRY_MAX_AGE;
482 age = hdr_age;
483 }
484 http_remove_header2(msg, &txn->hdr_idx, &ctx);
485 }
486
William Lallemand4da3f8a2017-10-31 14:33:34 +0100487 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200488 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100489 if (!first) {
490 shctx_unlock(shctx);
491 goto out;
492 }
493 shctx_unlock(shctx);
494
Willy Tarreau1093a452018-04-06 19:02:25 +0200495 /* the received memory is not initialized, we need at least to mark
496 * the object as not indexed yet.
497 */
498 object = (struct cache_entry *)first->data;
499 object->eb.node.leaf_p = NULL;
500 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200501 object->age = age;
502 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200503
William Lallemand4da3f8a2017-10-31 14:33:34 +0100504 /* reserve space for the cache_entry structure */
505 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200506 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100507 /* cache the headers in a http action because it allows to chose what
508 * to cache, for example you might want to cache a response before
509 * modifying some HTTP headers, or on the contrary after modifying
510 * those headers.
511 */
512
513 /* does not need to be locked because it's in the "hot" list,
514 * copy the headers */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200515 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100516 goto out;
517
518 /* register the buffer in the filter ctx for filling it with data*/
519 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
520 list_for_each_entry(filter, &s->strm_flt.filters, list) {
521 if (filter->config->id == cache_store_flt_id &&
522 filter->config->conf == rule->arg.act.p[0]) {
523 if (filter->ctx) {
524 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100525 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100526
527 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100528
William Lallemandf528fff2017-11-23 19:43:17 +0100529 object->eb.key = (*(unsigned int *)&txn->cache_hash);
530 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100531 /* Insert the node later on caching success */
532
533 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100534
535 old = entry_exist(cache, txn->cache_hash);
536 if (old) {
537 eb32_delete(&old->eb);
538 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100539 }
540 shctx_unlock(shctx);
541
542 /* store latest value and expiration time */
543 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100544 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100545 }
546 return ACT_RET_CONT;
547 }
548 }
549 }
550
551out:
552 /* if does not cache */
553 if (first) {
554 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100555 first->len = 0;
556 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100557 shctx_row_dec_hot(shctx, first);
558 shctx_unlock(shctx);
559 }
560
William Lallemand41db4602017-10-30 11:15:51 +0100561 return ACT_RET_CONT;
562}
563
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200564#define HTTP_CACHE_INIT 0 /* Initial state. */
565#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
566#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
567#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100568
William Lallemandecb73b12017-11-24 14:33:55 +0100569static void http_cache_applet_release(struct appctx *appctx)
570{
571 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
572 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
573 struct shared_block *first = block_ptr(cache_ptr);
574
575 shctx_lock(shctx_ptr(cache));
576 shctx_row_dec_hot(shctx_ptr(cache), first);
577 shctx_unlock(shctx_ptr(cache));
578}
579
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200580/*
581 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800582 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200583 * data in the channel.
584 *
585 * Returns the number of bytes inserted if succeeded, 0 if failed.
586 */
587static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
588{
589 unsigned int age;
590
591 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
592 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
593 age = CACHE_ENTRY_MAX_AGE;
594
595 chunk_reset(&trash);
596 chunk_printf(&trash, "Age: %u", age);
597
598 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
599}
600
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200601static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100602{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200603 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100604 struct stream_interface *si = appctx->owner;
605 struct channel *res = si_ic(si);
606 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +0100607 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200608 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
609 struct shared_block *blk, *next = appctx->ctx.cache.next;
610 int offset;
611
612 total = 0;
613 offset = 0;
614
615 if (!next) {
616 offset = sizeof(struct cache_entry);
617 next = block_ptr(cache_ptr);
618 }
619
620 blk = next;
621 list_for_each_entry_from(blk, &shctx->hot, list) {
622 int sz;
623
624 if (len <= 0)
625 break;
626
627 sz = MIN(len, shctx->block_size - offset);
628
629 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
630 if (unlikely(offset))
631 offset = 0;
632 if (ret <= 0) {
633 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100634 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200635 break;
636 }
637 return -1;
638 }
639
640 total += sz;
641 len -= sz;
642 }
643 appctx->ctx.cache.next = blk;
644
645 return total;
646}
647
648static void http_cache_io_handler(struct appctx *appctx)
649{
650 struct stream_interface *si = appctx->owner;
651 struct channel *res = si_ic(si);
652 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +0100653 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200654 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100655
656 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
657 goto out;
658
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800659 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200660 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100661 /* buf.size==0 means we failed to get a buffer and were
662 * already subscribed to a wait list to get a buffer.
663 */
William Lallemand77c11972017-10-31 20:43:01 +0100664 goto out;
665 }
666
667 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
668 appctx->st0 = HTTP_CACHE_END;
669
670 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200671 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200672 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100673
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200674 if (len > 0) {
675 int ret;
676
677 ret = cache_channel_row_data_get(appctx, len);
678 if (ret == -1)
679 appctx->st0 = HTTP_CACHE_END;
680 else
681 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200682 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
683 cache_channel_append_age_header(cache_ptr, res))
684 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200685 }
686 else {
687 *sent = 0;
688 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +0100689 }
William Lallemand77c11972017-10-31 20:43:01 +0100690 }
691
692 if (appctx->st0 == HTTP_CACHE_FWD) {
693 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200694 co_skip(si_oc(si), co_data(si_oc(si))); // NOTE: when disabled does not repport the correct status code
William Lallemand77c11972017-10-31 20:43:01 +0100695 res->flags |= CF_READ_NULL;
696 si_shutr(si);
697 }
698
699 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
700 si_shutw(si);
701out:
702 ;
703}
704
William Lallemand41db4602017-10-30 11:15:51 +0100705enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
706 struct act_rule *rule, char **err)
707{
708 struct flt_conf *fconf;
709 int cur_arg = *orig_arg;
710 rule->action = ACT_CUSTOM;
711 rule->action_ptr = http_action_store_cache;
712
713 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
714 memprintf(err, "expects a cache name");
715 return ACT_RET_PRS_ERR;
716 }
717
718 /* check if a cache filter was already registered with this cache
719 * name, if that's the case, must use it. */
720 list_for_each_entry(fconf, &proxy->filter_configs, list) {
721 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
722 rule->arg.act.p[0] = fconf->conf;
723 (*orig_arg)++;
724 /* filter already registered */
725 return ACT_RET_PRS_OK;
726 }
727 }
728
729 rule->arg.act.p[0] = strdup(args[cur_arg]);
730 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100731 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100732 err++;
733 goto err;
734 }
735 /* register a filter to fill the cache buffer */
736 fconf = calloc(1, sizeof(*fconf));
737 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 ha_alert("config: %s '%s': out of memory\n",
739 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100740 err++;
741 goto err;
742 }
743 fconf->id = cache_store_flt_id;
744 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
745 fconf->ops = &cache_ops;
746 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
747
748 (*orig_arg)++;
749
750 return ACT_RET_PRS_OK;
751
752err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100753 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100754}
755
William Lallemandf528fff2017-11-23 19:43:17 +0100756/* This produces a sha1 hash of the concatenation of the first
757 * occurrence of the Host header followed by the path component if it
758 * begins with a slash ('/'). */
759int sha1_hosturi(struct http_txn *txn)
760{
761 struct hdr_ctx ctx;
762
763 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200764 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100765 char *path;
766 char *end;
767 trash = get_trash_chunk();
768
769 /* retrive the host */
770 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200771 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100772 return 0;
773 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
774
775 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200776 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200777 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100778 if (!path)
779 return 0;
780 chunk_strncat(trash, path, end - path);
781
782 /* hash everything */
783 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100785 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
786
787 return 1;
788}
789
790
William Lallemand41db4602017-10-30 11:15:51 +0100791
792enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
793 struct session *sess, struct stream *s, int flags)
794{
William Lallemand77c11972017-10-31 20:43:01 +0100795
William Lallemand77c11972017-10-31 20:43:01 +0100796 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100797 struct cache *cache = (struct cache *)rule->arg.act.p[0];
798
Willy Tarreau504455c2017-12-22 17:47:35 +0100799 check_request_for_cacheability(s, &s->req);
800 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
801 return ACT_RET_CONT;
802
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100803 if (!sha1_hosturi(s->txn))
804 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100805
Willy Tarreau504455c2017-12-22 17:47:35 +0100806 if (s->txn->flags & TX_CACHE_IGNORE)
807 return ACT_RET_CONT;
808
William Lallemanda400a3a2017-11-20 19:13:12 +0100809 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100810 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100811 if (res) {
812 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100813 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
814 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100815 s->target = &http_cache_applet.obj_type;
816 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
817 appctx->st0 = HTTP_CACHE_INIT;
818 appctx->rule = rule;
819 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200820 appctx->ctx.cache.next = NULL;
821 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100822 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100823 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100824 shctx_lock(shctx_ptr(cache));
825 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
826 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100827 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100828 }
829 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100830 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100831 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100832}
833
834
835enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
836 struct act_rule *rule, char **err)
837{
838 int cur_arg = *orig_arg;
839
840 rule->action = ACT_CUSTOM;
841 rule->action_ptr = http_action_req_cache_use;
842
843 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
844 memprintf(err, "expects a cache name");
845 return ACT_RET_PRS_ERR;
846 }
847
848 rule->arg.act.p[0] = strdup(args[cur_arg]);
849 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100850 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100851 err++;
852 goto err;
853 }
854
855 (*orig_arg)++;
856 return ACT_RET_PRS_OK;
857
858err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100859 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100860
861}
862
863int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
864{
865 int err_code = 0;
866
867 if (strcmp(args[0], "cache") == 0) { /* new cache section */
868
869 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100870 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
871 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100872 err_code |= ERR_ALERT | ERR_ABORT;
873 goto out;
874 }
875
876 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
877 err_code |= ERR_ABORT;
878 goto out;
879 }
880
881 if (tmp_cache_config == NULL) {
882 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
883 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100884 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100885 err_code |= ERR_ALERT | ERR_ABORT;
886 goto out;
887 }
888
889 strlcpy2(tmp_cache_config->id, args[1], 33);
890 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100891 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
892 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100893 err_code |= ERR_WARN;
894 }
William Lallemand49b44532017-11-24 18:53:43 +0100895 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100896 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200897 tmp_cache_config->maxobjsz = 0;
Christopher Faulet1f672c52018-12-03 14:30:41 +0100898 tmp_cache_config->flags = 0;
William Lallemand41db4602017-10-30 11:15:51 +0100899 }
900 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200901 unsigned long int maxsize;
902 char *err;
William Lallemand41db4602017-10-30 11:15:51 +0100903
904 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
905 err_code |= ERR_ABORT;
906 goto out;
907 }
908
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200909 maxsize = strtoul(args[1], &err, 10);
910 if (err == args[1] || *err != '\0') {
911 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
912 file, linenum, args[1]);
913 err_code |= ERR_ABORT;
914 goto out;
915 }
916
917 if (maxsize > (UINT_MAX >> 20)) {
918 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
919 file, linenum, args[1], UINT_MAX >> 20);
920 err_code |= ERR_ABORT;
921 goto out;
922 }
923
William Lallemand41db4602017-10-30 11:15:51 +0100924 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200925 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +0100926 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +0100927 } else if (strcmp(args[0], "max-age") == 0) {
928 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
929 err_code |= ERR_ABORT;
930 goto out;
931 }
932
933 if (!*args[1]) {
934 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
935 file, linenum, args[0]);
936 err_code |= ERR_WARN;
937 }
938
939 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200940 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200941 unsigned int maxobjsz;
942 char *err;
943
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200944 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
945 err_code |= ERR_ABORT;
946 goto out;
947 }
948
949 if (!*args[1]) {
950 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
951 file, linenum, args[0]);
952 err_code |= ERR_WARN;
953 }
954
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200955 maxobjsz = strtoul(args[1], &err, 10);
956 if (err == args[1] || *err != '\0') {
957 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
958 file, linenum, args[1]);
959 err_code |= ERR_ABORT;
960 goto out;
961 }
962 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200963 }
964 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100965 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100966 err_code |= ERR_ALERT | ERR_FATAL;
967 goto out;
968 }
969out:
970 return err_code;
971}
972
973/* once the cache section is parsed */
974
975int cfg_post_parse_section_cache()
976{
977 struct shared_context *shctx;
978 int err_code = 0;
979 int ret_shctx;
980
981 if (tmp_cache_config) {
982 struct cache *cache;
983
984 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100985 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100986 err_code |= ERR_FATAL | ERR_ALERT;
987 goto out;
988 }
989
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200990 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200991 /* Default max. file size is a 256th of the cache size. */
992 tmp_cache_config->maxobjsz =
993 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200994 }
995 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
996 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
997 err_code |= ERR_FATAL | ERR_ALERT;
998 goto out;
999 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +02001000
1001 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
1002 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +01001003
Frédéric Lécaillebc584492018-10-25 20:18:59 +02001004 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +01001005 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +01001006 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001007 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001008 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +01001009
1010 err_code |= ERR_FATAL | ERR_ALERT;
1011 goto out;
1012 }
William Lallemanda400a3a2017-11-20 19:13:12 +01001013 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001014 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1015 cache = (struct cache *)shctx->data;
1016 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001017 LIST_ADDQ(&caches, &cache->list);
1018 }
1019out:
1020 free(tmp_cache_config);
1021 tmp_cache_config = NULL;
1022 return err_code;
1023
1024}
1025
1026/*
1027 * Resolve the cache name to a pointer once the file is completely read.
1028 */
1029int cfg_cache_postparser()
1030{
1031 struct act_rule *hresrule, *hrqrule;
1032 void *cache_ptr;
1033 struct cache *cache;
1034 struct proxy *curproxy = NULL;
1035 int err = 0;
1036 struct flt_conf *fconf;
1037
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001038 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +01001039
1040 /* resolve the http response cache name to a ptr in the action rule */
1041 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
1042 if (hresrule->action != ACT_CUSTOM ||
1043 hresrule->action_ptr != http_action_store_cache)
1044 continue;
1045
1046 cache_ptr = hresrule->arg.act.p[0];
1047
1048 list_for_each_entry(cache, &caches, list) {
1049 if (!strcmp(cache->id, cache_ptr)) {
1050 /* don't free there, it's still used in the filter conf */
1051 cache_ptr = cache;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001052 cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX)
1053 ? CACHE_F_HTX
1054 : CACHE_F_LEGACY_HTTP);
William Lallemand41db4602017-10-30 11:15:51 +01001055 break;
1056 }
1057 }
1058
1059 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001060 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
1061 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001062 err++;
1063 }
1064
1065 hresrule->arg.act.p[0] = cache_ptr;
1066 }
1067
1068 /* resolve the http request cache name to a ptr in the action rule */
1069 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
1070 if (hrqrule->action != ACT_CUSTOM ||
1071 hrqrule->action_ptr != http_action_req_cache_use)
1072 continue;
1073
1074 cache_ptr = hrqrule->arg.act.p[0];
1075
1076 list_for_each_entry(cache, &caches, list) {
1077 if (!strcmp(cache->id, cache_ptr)) {
1078 free(cache_ptr);
1079 cache_ptr = cache;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001080 cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX)
1081 ? CACHE_F_HTX
1082 : CACHE_F_LEGACY_HTTP);
William Lallemand41db4602017-10-30 11:15:51 +01001083 break;
1084 }
1085 }
1086
1087 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001088 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
1089 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001090 err++;
1091 }
1092
1093 hrqrule->arg.act.p[0] = cache_ptr;
1094 }
1095
1096 /* resolve the cache name to a ptr in the filter config */
1097 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1098
William Lallemand9c54c532017-11-02 16:38:42 +01001099 if (fconf->id != cache_store_flt_id)
1100 continue;
1101
William Lallemand41db4602017-10-30 11:15:51 +01001102 cache_ptr = fconf->conf;
1103
1104 list_for_each_entry(cache, &caches, list) {
1105 if (!strcmp(cache->id, cache_ptr)) {
1106 /* there can be only one filter per cache, so we free it there */
1107 free(cache_ptr);
1108 cache_ptr = cache;
Christopher Faulet1f672c52018-12-03 14:30:41 +01001109 cache->flags |= ((curproxy->options2 & PR_O2_USE_HTX)
1110 ? CACHE_F_HTX
1111 : CACHE_F_LEGACY_HTTP);
William Lallemand41db4602017-10-30 11:15:51 +01001112 break;
1113 }
1114 }
1115
1116 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001117 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1118 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001119 err++;
1120 }
1121 fconf->conf = cache_ptr;
1122 }
1123 }
Christopher Faulet1f672c52018-12-03 14:30:41 +01001124
1125 /* Check if the cache is used by HTX and legacy HTTP proxies in same
1126 * time
1127 */
1128 list_for_each_entry(cache, &caches, list) {
1129 if ((cache->flags & (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) == (CACHE_F_HTX|CACHE_F_LEGACY_HTTP)) {
1130 ha_alert("Cache '%s': cannot be used by HTX and legacy HTTP proxies in same time.\n",
1131 cache->id);
1132 err++;
1133 }
1134 }
1135
William Lallemand41db4602017-10-30 11:15:51 +01001136 return err;
1137}
1138
1139
1140struct flt_ops cache_ops = {
1141 .init = cache_store_init,
1142
William Lallemand4da3f8a2017-10-31 14:33:34 +01001143 /* Handle channels activity */
1144 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001145 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001146
1147 /* Filter HTTP requests and responses */
1148 .http_headers = cache_store_http_headers,
1149 .http_end = cache_store_http_end,
1150
1151 .http_forward_data = cache_store_http_forward_data,
1152
William Lallemand41db4602017-10-30 11:15:51 +01001153};
1154
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001155static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001156{
1157 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1158 return 1;
1159
1160 return 0;
1161}
1162
1163static int cli_io_handler_show_cache(struct appctx *appctx)
1164{
1165 struct cache* cache = appctx->ctx.cli.p0;
1166 struct stream_interface *si = appctx->owner;
1167
William Lallemand1f49a362017-11-21 20:01:26 +01001168 if (cache == NULL) {
1169 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1170 }
1171
1172 list_for_each_entry_from(cache, &caches, list) {
1173 struct eb32_node *node = NULL;
1174 unsigned int next_key;
1175 struct cache_entry *entry;
1176
William Lallemand1f49a362017-11-21 20:01:26 +01001177 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001178 if (!next_key) {
1179 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1180 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001181 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001182 return 0;
1183 }
1184 }
William Lallemand1f49a362017-11-21 20:01:26 +01001185
1186 appctx->ctx.cli.p0 = cache;
1187
1188 while (1) {
1189
1190 shctx_lock(shctx_ptr(cache));
1191 node = eb32_lookup_ge(&cache->entries, next_key);
1192 if (!node) {
1193 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001194 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001195 break;
1196 }
1197
1198 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001199 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 Lallemand1f49a362017-11-21 20:01:26 +01001200
1201 next_key = node->key + 1;
1202 appctx->ctx.cli.i0 = next_key;
1203
1204 shctx_unlock(shctx_ptr(cache));
1205
1206 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001207 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001208 return 0;
1209 }
1210 }
1211
1212 }
1213
1214 return 1;
1215
1216}
1217
1218static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001219 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1220 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001221}};
1222
Willy Tarreau0108d902018-11-25 19:14:37 +01001223INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001224
William Lallemand41db4602017-10-30 11:15:51 +01001225static struct action_kw_list http_res_actions = {
1226 .kw = {
1227 { "cache-store", parse_cache_store },
1228 { NULL, NULL }
1229 }
1230};
1231
Willy Tarreau0108d902018-11-25 19:14:37 +01001232INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1233
William Lallemand41db4602017-10-30 11:15:51 +01001234static struct action_kw_list http_req_actions = {
1235 .kw = {
1236 { "cache-use", parse_cache_use },
1237 { NULL, NULL }
1238 }
1239};
1240
Willy Tarreau0108d902018-11-25 19:14:37 +01001241INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1242
William Lallemand41db4602017-10-30 11:15:51 +01001243struct applet http_cache_applet = {
1244 .obj_type = OBJ_TYPE_APPLET,
1245 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001246 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001247 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001248};
1249
Willy Tarreaue6552512018-11-26 11:33:13 +01001250/* config parsers for this section */
1251REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1252REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);