blob: 1bf996b5c06c56bb7e8cdd7fe259d053f27610c8 [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 */
40
41static const char *cache_store_flt_id = "cache store filter";
42
43struct applet http_cache_applet;
44
45struct flt_ops cache_ops;
46
47struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010048 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010049 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010050 unsigned int maxage; /* max-age */
51 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020052 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010053 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010054};
55
56/*
57 * cache ctx for filters
58 */
59struct cache_st {
60 int hdrs_len;
61 struct shared_block *first_block;
62};
63
64struct cache_entry {
65 unsigned int latest_validation; /* latest validation date */
66 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020067 unsigned int age; /* Origin server "Age" header value */
68 unsigned int eoh; /* Origin server end of headers offset. */
William Lallemand41db4602017-10-30 11:15:51 +010069 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010070 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010071 unsigned char data[0];
72};
73
74#define CACHE_BLOCKSIZE 1024
Willy Tarreau96062a12018-11-11 14:00:28 +010075#define CACHE_ENTRY_MAX_AGE 2147483648U
William Lallemand41db4602017-10-30 11:15:51 +010076
77static struct list caches = LIST_HEAD_INIT(caches);
78static struct cache *tmp_cache_config = NULL;
79
Willy Tarreau8ceae722018-11-26 11:58:30 +010080DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st));
81
William Lallemandf528fff2017-11-23 19:43:17 +010082struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010083{
84 struct eb32_node *node;
85 struct cache_entry *entry;
86
William Lallemandf528fff2017-11-23 19:43:17 +010087 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010088 if (!node)
89 return NULL;
90
91 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010092
93 /* if that's not the right node */
94 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
95 return NULL;
96
William Lallemand08727662017-11-21 20:01:27 +010097 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010098 return entry;
William Lallemand08727662017-11-21 20:01:27 +010099 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100100 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100101 entry->eb.key = 0;
102 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100103 return NULL;
104
105}
106
107static inline struct shared_context *shctx_ptr(struct cache *cache)
108{
109 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
110}
111
William Lallemand77c11972017-10-31 20:43:01 +0100112static inline struct shared_block *block_ptr(struct cache_entry *entry)
113{
114 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
115}
116
117
118
William Lallemand41db4602017-10-30 11:15:51 +0100119static int
120cache_store_init(struct proxy *px, struct flt_conf *f1conf)
121{
122 return 0;
123}
124
William Lallemand4da3f8a2017-10-31 14:33:34 +0100125static int
126cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
127{
128 if (!(chn->flags & CF_ISRESP))
129 return 1;
130
131 if (filter->ctx == NULL) {
132 struct cache_st *st;
133
Willy Tarreaubafbe012017-11-24 17:34:44 +0100134 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100135 if (st == NULL)
136 return -1;
137
138 st->hdrs_len = 0;
139 st->first_block = NULL;
140 filter->ctx = st;
141 }
142
143 register_data_filter(s, chn, filter);
144
145 return 1;
146}
147
148static int
William Lallemand49dc0482017-11-24 14:33:54 +0100149cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
150{
151 struct cache_st *st = filter->ctx;
152 struct cache *cache = filter->config->conf;
153 struct shared_context *shctx = shctx_ptr(cache);
154
155 if (!(chn->flags & CF_ISRESP))
156 return 1;
157
158 /* Everything should be released in the http_end filter, but we need to do it
159 * there too, in case of errors */
160
161 if (st && st->first_block) {
162
163 shctx_lock(shctx);
164 shctx_row_dec_hot(shctx, st->first_block);
165 shctx_unlock(shctx);
166
167 }
168 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100169 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100170 filter->ctx = NULL;
171 }
172
173 return 1;
174}
175
176
177static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100178cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
179{
180 struct cache_st *st = filter->ctx;
181
William Lallemand4da3f8a2017-10-31 14:33:34 +0100182 if (!(msg->chn->flags & CF_ISRESP) || !st)
183 return 1;
184
William Lallemand9d5f54d2017-11-14 14:39:22 +0100185 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100186
187 return 1;
188}
189
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200190static inline void disable_cache_entry(struct cache_st *st,
191 struct filter *filter, struct shared_context *shctx)
192{
193 struct cache_entry *object;
194
195 object = (struct cache_entry *)st->first_block->data;
196 filter->ctx = NULL; /* disable cache */
197 shctx_lock(shctx);
198 shctx_row_dec_hot(shctx, st->first_block);
199 object->eb.key = 0;
200 shctx_unlock(shctx);
201 pool_free(pool_head_cache_st, st);
202}
203
William Lallemand4da3f8a2017-10-31 14:33:34 +0100204static int
205cache_store_http_forward_data(struct stream *s, struct filter *filter,
206 struct http_msg *msg, unsigned int len)
207{
208 struct cache_st *st = filter->ctx;
209 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
210 int ret;
211
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200212 ret = 0;
213
William Lallemand4da3f8a2017-10-31 14:33:34 +0100214 /*
215 * We need to skip the HTTP headers first, because we saved them in the
216 * http-response action.
217 */
218 if (!(msg->chn->flags & CF_ISRESP) || !st)
219 return len;
220
221 if (!len) {
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800222 /* Nothing to forward */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100223 ret = len;
224 }
William Lallemand10935bc2017-11-14 14:39:23 +0100225 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100226 /* Forward part of headers */
227 ret = len;
228 st->hdrs_len -= len;
229 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100230 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100231 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100232 if (filter->ctx && st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200233 int to_append, append;
234 struct shared_block *fb;
235
236 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
237
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200238 shctx_lock(shctx);
239 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
240 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100241 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200242 disable_cache_entry(st, filter, shctx);
243 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100244 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200245 shctx_unlock(shctx);
246
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200247 /* Skip remaining headers to fill the cache */
248 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200249 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
250 (unsigned char *)ci_head(msg->chn), to_append);
251 ret = st->hdrs_len + to_append - append;
252 /* Rewind the buffer to forward all data */
253 c_rew(msg->chn, st->hdrs_len);
254 st->hdrs_len = 0;
255 if (ret < 0)
256 disable_cache_entry(st, filter, shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100257 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200258 else
259 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100260 }
261
262 if ((ret != len) ||
263 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
264 task_wakeup(s->task, TASK_WOKEN_MSG);
265
266 return ret;
267}
268
269static int
270cache_store_http_end(struct stream *s, struct filter *filter,
271 struct http_msg *msg)
272{
273 struct cache_st *st = filter->ctx;
274 struct cache *cache = filter->config->conf;
275 struct shared_context *shctx = shctx_ptr(cache);
276 struct cache_entry *object;
277
278 if (!(msg->chn->flags & CF_ISRESP))
279 return 1;
280
281 if (st && st->first_block) {
282
283 object = (struct cache_entry *)st->first_block->data;
284
285 /* does not need to test if the insertion worked, if it
286 * doesn't, the blocks will be reused anyway */
287
288 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100289 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
290 object->eb.key = 0;
291 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100292 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100293 shctx_row_dec_hot(shctx, st->first_block);
294 shctx_unlock(shctx);
295
296 }
297 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100298 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100299 filter->ctx = NULL;
300 }
301
302 return 1;
303}
304
305 /*
306 * This intends to be used when checking HTTP headers for some
307 * word=value directive. Return a pointer to the first character of value, if
308 * the word was not found or if there wasn't any value assigned ot it return NULL
309 */
310char *directive_value(const char *sample, int slen, const char *word, int wlen)
311{
312 int st = 0;
313
314 if (slen < wlen)
315 return 0;
316
317 while (wlen) {
318 char c = *sample ^ *word;
319 if (c && c != ('A' ^ 'a'))
320 return NULL;
321 sample++;
322 word++;
323 slen--;
324 wlen--;
325 }
326
327 while (slen) {
328 if (st == 0) {
329 if (*sample != '=')
330 return NULL;
331 sample++;
332 slen--;
333 st = 1;
334 continue;
335 } else {
336 return (char *)sample;
337 }
338 }
339
340 return NULL;
341}
342
343/*
344 * Return the maxage in seconds of an HTTP response.
345 * Compute the maxage using either:
346 * - the assigned max-age of the cache
347 * - the s-maxage directive
348 * - the max-age directive
349 * - (Expires - Data) headers
350 * - the default-max-age of the cache
351 *
352 */
William Lallemand49b44532017-11-24 18:53:43 +0100353int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100354{
355 struct http_txn *txn = s->txn;
356 struct hdr_ctx ctx;
357
358 int smaxage = -1;
359 int maxage = -1;
360
361
William Lallemand4da3f8a2017-10-31 14:33:34 +0100362 ctx.idx = 0;
363
364 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200365 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100366 char *directive = ctx.line + ctx.val;
367 char *value;
368
369 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
370 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200371 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100372
373 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
374 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200375 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100376 }
377
378 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
379 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200380 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100381
382 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
383 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200384 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100385 }
386 }
387
388 /* TODO: Expires - Data */
389
390
391 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100392 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100393
394 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100395 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100396
William Lallemand49b44532017-11-24 18:53:43 +0100397 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100398
399}
400
401
William Lallemanda400a3a2017-11-20 19:13:12 +0100402static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
403{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200404 struct cache_entry *object = (struct cache_entry *)block->data;
405
406 if (first == block && object->eb.key)
407 eb32_delete(&object->eb);
408 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100409}
410
William Lallemand41db4602017-10-30 11:15:51 +0100411/*
412 * This fonction will store the headers of the response in a buffer and then
413 * register a filter to store the data
414 */
415enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
416 struct session *sess, struct stream *s, int flags)
417{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200418 unsigned int age;
419 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100420 struct http_txn *txn = s->txn;
421 struct http_msg *msg = &txn->rsp;
422 struct filter *filter;
423 struct hdr_ctx ctx;
424 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100425 struct cache *cache = (struct cache *)rule->arg.act.p[0];
426 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100427 struct cache_entry *object;
428
429
430 /* Don't cache if the response came from a cache */
431 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
432 s->target == &http_cache_applet.obj_type) {
433 goto out;
434 }
435
436 /* cache only HTTP/1.1 */
437 if (!(txn->req.flags & HTTP_MSGF_VER_11))
438 goto out;
439
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200440 /* Do not cache too big objects. */
441 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
442 msg->sov + msg->body_len > shctx->max_obj_size)
443 goto out;
444
William Lallemand4da3f8a2017-10-31 14:33:34 +0100445 /* cache only GET method */
446 if (txn->meth != HTTP_METH_GET)
447 goto out;
448
449 /* cache only 200 status code */
450 if (txn->status != 200)
451 goto out;
452
453 /* Does not manage Vary at the moment. We will need a secondary key later for that */
454 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200455 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100456 goto out;
457
Willy Tarreaufaf29092017-12-21 15:59:17 +0100458 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100459
Willy Tarreaud4569d12017-12-22 18:03:04 +0100460 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100461 goto out;
462
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200463 age = 0;
464 ctx.idx = 0;
465 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
466 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
467 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
468 hdr_age = CACHE_ENTRY_MAX_AGE;
469 age = hdr_age;
470 }
471 http_remove_header2(msg, &txn->hdr_idx, &ctx);
472 }
473
William Lallemand4da3f8a2017-10-31 14:33:34 +0100474 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200475 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100476 if (!first) {
477 shctx_unlock(shctx);
478 goto out;
479 }
480 shctx_unlock(shctx);
481
Willy Tarreau1093a452018-04-06 19:02:25 +0200482 /* the received memory is not initialized, we need at least to mark
483 * the object as not indexed yet.
484 */
485 object = (struct cache_entry *)first->data;
486 object->eb.node.leaf_p = NULL;
487 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200488 object->age = age;
489 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200490
William Lallemand4da3f8a2017-10-31 14:33:34 +0100491 /* reserve space for the cache_entry structure */
492 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200493 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100494 /* cache the headers in a http action because it allows to chose what
495 * to cache, for example you might want to cache a response before
496 * modifying some HTTP headers, or on the contrary after modifying
497 * those headers.
498 */
499
500 /* does not need to be locked because it's in the "hot" list,
501 * copy the headers */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200502 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100503 goto out;
504
505 /* register the buffer in the filter ctx for filling it with data*/
506 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
507 list_for_each_entry(filter, &s->strm_flt.filters, list) {
508 if (filter->config->id == cache_store_flt_id &&
509 filter->config->conf == rule->arg.act.p[0]) {
510 if (filter->ctx) {
511 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100512 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100513
514 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100515
William Lallemandf528fff2017-11-23 19:43:17 +0100516 object->eb.key = (*(unsigned int *)&txn->cache_hash);
517 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100518 /* Insert the node later on caching success */
519
520 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100521
522 old = entry_exist(cache, txn->cache_hash);
523 if (old) {
524 eb32_delete(&old->eb);
525 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100526 }
527 shctx_unlock(shctx);
528
529 /* store latest value and expiration time */
530 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100531 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100532 }
533 return ACT_RET_CONT;
534 }
535 }
536 }
537
538out:
539 /* if does not cache */
540 if (first) {
541 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100542 first->len = 0;
543 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100544 shctx_row_dec_hot(shctx, first);
545 shctx_unlock(shctx);
546 }
547
William Lallemand41db4602017-10-30 11:15:51 +0100548 return ACT_RET_CONT;
549}
550
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200551#define HTTP_CACHE_INIT 0 /* Initial state. */
552#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
553#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
554#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100555
William Lallemandecb73b12017-11-24 14:33:55 +0100556static void http_cache_applet_release(struct appctx *appctx)
557{
558 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
559 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
560 struct shared_block *first = block_ptr(cache_ptr);
561
562 shctx_lock(shctx_ptr(cache));
563 shctx_row_dec_hot(shctx_ptr(cache), first);
564 shctx_unlock(shctx_ptr(cache));
565}
566
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200567/*
568 * Append an "Age" header into <chn> channel for this <ce> cache entry.
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800569 * This is the responsibility of the caller to insure there is enough
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200570 * data in the channel.
571 *
572 * Returns the number of bytes inserted if succeeded, 0 if failed.
573 */
574static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
575{
576 unsigned int age;
577
578 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
579 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
580 age = CACHE_ENTRY_MAX_AGE;
581
582 chunk_reset(&trash);
583 chunk_printf(&trash, "Age: %u", age);
584
585 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
586}
587
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200588static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100589{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200590 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100591 struct stream_interface *si = appctx->owner;
592 struct channel *res = si_ic(si);
593 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +0100594 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200595 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
596 struct shared_block *blk, *next = appctx->ctx.cache.next;
597 int offset;
598
599 total = 0;
600 offset = 0;
601
602 if (!next) {
603 offset = sizeof(struct cache_entry);
604 next = block_ptr(cache_ptr);
605 }
606
607 blk = next;
608 list_for_each_entry_from(blk, &shctx->hot, list) {
609 int sz;
610
611 if (len <= 0)
612 break;
613
614 sz = MIN(len, shctx->block_size - offset);
615
616 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
617 if (unlikely(offset))
618 offset = 0;
619 if (ret <= 0) {
620 if (ret == -3 || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +0100621 si_rx_room_blk(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200622 break;
623 }
624 return -1;
625 }
626
627 total += sz;
628 len -= sz;
629 }
630 appctx->ctx.cache.next = blk;
631
632 return total;
633}
634
635static void http_cache_io_handler(struct appctx *appctx)
636{
637 struct stream_interface *si = appctx->owner;
638 struct channel *res = si_ic(si);
639 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +0100640 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200641 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100642
643 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
644 goto out;
645
Joseph Herlant8dae5b32018-11-15 14:07:53 -0800646 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200647 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +0100648 /* buf.size==0 means we failed to get a buffer and were
649 * already subscribed to a wait list to get a buffer.
650 */
William Lallemand77c11972017-10-31 20:43:01 +0100651 goto out;
652 }
653
654 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
655 appctx->st0 = HTTP_CACHE_END;
656
657 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200658 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200659 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100660
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200661 if (len > 0) {
662 int ret;
663
664 ret = cache_channel_row_data_get(appctx, len);
665 if (ret == -1)
666 appctx->st0 = HTTP_CACHE_END;
667 else
668 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200669 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
670 cache_channel_append_age_header(cache_ptr, res))
671 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200672 }
673 else {
674 *sent = 0;
675 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +0100676 }
William Lallemand77c11972017-10-31 20:43:01 +0100677 }
678
679 if (appctx->st0 == HTTP_CACHE_FWD) {
680 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200681 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 +0100682 res->flags |= CF_READ_NULL;
683 si_shutr(si);
684 }
685
686 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
687 si_shutw(si);
688out:
689 ;
690}
691
William Lallemand41db4602017-10-30 11:15:51 +0100692enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
693 struct act_rule *rule, char **err)
694{
695 struct flt_conf *fconf;
696 int cur_arg = *orig_arg;
697 rule->action = ACT_CUSTOM;
698 rule->action_ptr = http_action_store_cache;
699
700 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
701 memprintf(err, "expects a cache name");
702 return ACT_RET_PRS_ERR;
703 }
704
705 /* check if a cache filter was already registered with this cache
706 * name, if that's the case, must use it. */
707 list_for_each_entry(fconf, &proxy->filter_configs, list) {
708 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
709 rule->arg.act.p[0] = fconf->conf;
710 (*orig_arg)++;
711 /* filter already registered */
712 return ACT_RET_PRS_OK;
713 }
714 }
715
716 rule->arg.act.p[0] = strdup(args[cur_arg]);
717 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100718 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100719 err++;
720 goto err;
721 }
722 /* register a filter to fill the cache buffer */
723 fconf = calloc(1, sizeof(*fconf));
724 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100725 ha_alert("config: %s '%s': out of memory\n",
726 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100727 err++;
728 goto err;
729 }
730 fconf->id = cache_store_flt_id;
731 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
732 fconf->ops = &cache_ops;
733 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
734
735 (*orig_arg)++;
736
737 return ACT_RET_PRS_OK;
738
739err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100740 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100741}
742
William Lallemandf528fff2017-11-23 19:43:17 +0100743/* This produces a sha1 hash of the concatenation of the first
744 * occurrence of the Host header followed by the path component if it
745 * begins with a slash ('/'). */
746int sha1_hosturi(struct http_txn *txn)
747{
748 struct hdr_ctx ctx;
749
750 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200751 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100752 char *path;
753 char *end;
754 trash = get_trash_chunk();
755
756 /* retrive the host */
757 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200758 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100759 return 0;
760 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
761
762 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200763 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200764 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100765 if (!path)
766 return 0;
767 chunk_strncat(trash, path, end - path);
768
769 /* hash everything */
770 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200771 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100772 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
773
774 return 1;
775}
776
777
William Lallemand41db4602017-10-30 11:15:51 +0100778
779enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
780 struct session *sess, struct stream *s, int flags)
781{
William Lallemand77c11972017-10-31 20:43:01 +0100782
William Lallemand77c11972017-10-31 20:43:01 +0100783 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100784 struct cache *cache = (struct cache *)rule->arg.act.p[0];
785
Willy Tarreau504455c2017-12-22 17:47:35 +0100786 check_request_for_cacheability(s, &s->req);
787 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
788 return ACT_RET_CONT;
789
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100790 if (!sha1_hosturi(s->txn))
791 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100792
Willy Tarreau504455c2017-12-22 17:47:35 +0100793 if (s->txn->flags & TX_CACHE_IGNORE)
794 return ACT_RET_CONT;
795
William Lallemanda400a3a2017-11-20 19:13:12 +0100796 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100797 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100798 if (res) {
799 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100800 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
801 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100802 s->target = &http_cache_applet.obj_type;
803 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
804 appctx->st0 = HTTP_CACHE_INIT;
805 appctx->rule = rule;
806 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200807 appctx->ctx.cache.next = NULL;
808 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100809 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100810 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100811 shctx_lock(shctx_ptr(cache));
812 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
813 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100814 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100815 }
816 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100817 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100818 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100819}
820
821
822enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
823 struct act_rule *rule, char **err)
824{
825 int cur_arg = *orig_arg;
826
827 rule->action = ACT_CUSTOM;
828 rule->action_ptr = http_action_req_cache_use;
829
830 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
831 memprintf(err, "expects a cache name");
832 return ACT_RET_PRS_ERR;
833 }
834
835 rule->arg.act.p[0] = strdup(args[cur_arg]);
836 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100837 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100838 err++;
839 goto err;
840 }
841
842 (*orig_arg)++;
843 return ACT_RET_PRS_OK;
844
845err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100846 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100847
848}
849
850int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
851{
852 int err_code = 0;
853
854 if (strcmp(args[0], "cache") == 0) { /* new cache section */
855
856 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100857 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
858 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100859 err_code |= ERR_ALERT | ERR_ABORT;
860 goto out;
861 }
862
863 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
864 err_code |= ERR_ABORT;
865 goto out;
866 }
867
868 if (tmp_cache_config == NULL) {
869 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
870 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100871 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100872 err_code |= ERR_ALERT | ERR_ABORT;
873 goto out;
874 }
875
876 strlcpy2(tmp_cache_config->id, args[1], 33);
877 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100878 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
879 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100880 err_code |= ERR_WARN;
881 }
William Lallemand49b44532017-11-24 18:53:43 +0100882 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100883 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200884 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +0100885 }
886 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200887 unsigned long int maxsize;
888 char *err;
William Lallemand41db4602017-10-30 11:15:51 +0100889
890 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
891 err_code |= ERR_ABORT;
892 goto out;
893 }
894
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200895 maxsize = strtoul(args[1], &err, 10);
896 if (err == args[1] || *err != '\0') {
897 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
898 file, linenum, args[1]);
899 err_code |= ERR_ABORT;
900 goto out;
901 }
902
903 if (maxsize > (UINT_MAX >> 20)) {
904 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
905 file, linenum, args[1], UINT_MAX >> 20);
906 err_code |= ERR_ABORT;
907 goto out;
908 }
909
William Lallemand41db4602017-10-30 11:15:51 +0100910 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200911 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +0100912 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +0100913 } else if (strcmp(args[0], "max-age") == 0) {
914 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
915 err_code |= ERR_ABORT;
916 goto out;
917 }
918
919 if (!*args[1]) {
920 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
921 file, linenum, args[0]);
922 err_code |= ERR_WARN;
923 }
924
925 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200926 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200927 unsigned int maxobjsz;
928 char *err;
929
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200930 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
931 err_code |= ERR_ABORT;
932 goto out;
933 }
934
935 if (!*args[1]) {
936 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
937 file, linenum, args[0]);
938 err_code |= ERR_WARN;
939 }
940
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200941 maxobjsz = strtoul(args[1], &err, 10);
942 if (err == args[1] || *err != '\0') {
943 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
944 file, linenum, args[1]);
945 err_code |= ERR_ABORT;
946 goto out;
947 }
948 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200949 }
950 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100951 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100952 err_code |= ERR_ALERT | ERR_FATAL;
953 goto out;
954 }
955out:
956 return err_code;
957}
958
959/* once the cache section is parsed */
960
961int cfg_post_parse_section_cache()
962{
963 struct shared_context *shctx;
964 int err_code = 0;
965 int ret_shctx;
966
967 if (tmp_cache_config) {
968 struct cache *cache;
969
970 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100971 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100972 err_code |= ERR_FATAL | ERR_ALERT;
973 goto out;
974 }
975
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200976 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200977 /* Default max. file size is a 256th of the cache size. */
978 tmp_cache_config->maxobjsz =
979 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200980 }
981 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
982 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
983 err_code |= ERR_FATAL | ERR_ALERT;
984 goto out;
985 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200986
987 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
988 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100989
Frédéric Lécaillebc584492018-10-25 20:18:59 +0200990 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100991 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100992 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100993 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100994 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100995
996 err_code |= ERR_FATAL | ERR_ALERT;
997 goto out;
998 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100999 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +01001000 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
1001 cache = (struct cache *)shctx->data;
1002 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001003 LIST_ADDQ(&caches, &cache->list);
1004 }
1005out:
1006 free(tmp_cache_config);
1007 tmp_cache_config = NULL;
1008 return err_code;
1009
1010}
1011
1012/*
1013 * Resolve the cache name to a pointer once the file is completely read.
1014 */
1015int cfg_cache_postparser()
1016{
1017 struct act_rule *hresrule, *hrqrule;
1018 void *cache_ptr;
1019 struct cache *cache;
1020 struct proxy *curproxy = NULL;
1021 int err = 0;
1022 struct flt_conf *fconf;
1023
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001024 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +01001025
1026 /* resolve the http response cache name to a ptr in the action rule */
1027 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
1028 if (hresrule->action != ACT_CUSTOM ||
1029 hresrule->action_ptr != http_action_store_cache)
1030 continue;
1031
1032 cache_ptr = hresrule->arg.act.p[0];
1033
1034 list_for_each_entry(cache, &caches, list) {
1035 if (!strcmp(cache->id, cache_ptr)) {
1036 /* don't free there, it's still used in the filter conf */
1037 cache_ptr = cache;
1038 break;
1039 }
1040 }
1041
1042 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001043 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
1044 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001045 err++;
1046 }
1047
1048 hresrule->arg.act.p[0] = cache_ptr;
1049 }
1050
1051 /* resolve the http request cache name to a ptr in the action rule */
1052 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
1053 if (hrqrule->action != ACT_CUSTOM ||
1054 hrqrule->action_ptr != http_action_req_cache_use)
1055 continue;
1056
1057 cache_ptr = hrqrule->arg.act.p[0];
1058
1059 list_for_each_entry(cache, &caches, list) {
1060 if (!strcmp(cache->id, cache_ptr)) {
1061 free(cache_ptr);
1062 cache_ptr = cache;
1063 break;
1064 }
1065 }
1066
1067 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001068 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
1069 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001070 err++;
1071 }
1072
1073 hrqrule->arg.act.p[0] = cache_ptr;
1074 }
1075
1076 /* resolve the cache name to a ptr in the filter config */
1077 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1078
William Lallemand9c54c532017-11-02 16:38:42 +01001079 if (fconf->id != cache_store_flt_id)
1080 continue;
1081
William Lallemand41db4602017-10-30 11:15:51 +01001082 cache_ptr = fconf->conf;
1083
1084 list_for_each_entry(cache, &caches, list) {
1085 if (!strcmp(cache->id, cache_ptr)) {
1086 /* there can be only one filter per cache, so we free it there */
1087 free(cache_ptr);
1088 cache_ptr = cache;
1089 break;
1090 }
1091 }
1092
1093 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001094 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1095 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001096 err++;
1097 }
1098 fconf->conf = cache_ptr;
1099 }
1100 }
1101 return err;
1102}
1103
1104
1105struct flt_ops cache_ops = {
1106 .init = cache_store_init,
1107
William Lallemand4da3f8a2017-10-31 14:33:34 +01001108 /* Handle channels activity */
1109 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001110 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001111
1112 /* Filter HTTP requests and responses */
1113 .http_headers = cache_store_http_headers,
1114 .http_end = cache_store_http_end,
1115
1116 .http_forward_data = cache_store_http_forward_data,
1117
William Lallemand41db4602017-10-30 11:15:51 +01001118};
1119
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001120static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001121{
1122 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1123 return 1;
1124
1125 return 0;
1126}
1127
1128static int cli_io_handler_show_cache(struct appctx *appctx)
1129{
1130 struct cache* cache = appctx->ctx.cli.p0;
1131 struct stream_interface *si = appctx->owner;
1132
William Lallemand1f49a362017-11-21 20:01:26 +01001133 if (cache == NULL) {
1134 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1135 }
1136
1137 list_for_each_entry_from(cache, &caches, list) {
1138 struct eb32_node *node = NULL;
1139 unsigned int next_key;
1140 struct cache_entry *entry;
1141
William Lallemand1f49a362017-11-21 20:01:26 +01001142 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001143 if (!next_key) {
1144 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1145 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001146 si_rx_room_blk(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001147 return 0;
1148 }
1149 }
William Lallemand1f49a362017-11-21 20:01:26 +01001150
1151 appctx->ctx.cli.p0 = cache;
1152
1153 while (1) {
1154
1155 shctx_lock(shctx_ptr(cache));
1156 node = eb32_lookup_ge(&cache->entries, next_key);
1157 if (!node) {
1158 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001159 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001160 break;
1161 }
1162
1163 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001164 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 +01001165
1166 next_key = node->key + 1;
1167 appctx->ctx.cli.i0 = next_key;
1168
1169 shctx_unlock(shctx_ptr(cache));
1170
1171 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001172 si_rx_room_blk(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001173 return 0;
1174 }
1175 }
1176
1177 }
1178
1179 return 1;
1180
1181}
1182
1183static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001184 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1185 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001186}};
1187
Willy Tarreau0108d902018-11-25 19:14:37 +01001188INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand1f49a362017-11-21 20:01:26 +01001189
William Lallemand41db4602017-10-30 11:15:51 +01001190static struct action_kw_list http_res_actions = {
1191 .kw = {
1192 { "cache-store", parse_cache_store },
1193 { NULL, NULL }
1194 }
1195};
1196
Willy Tarreau0108d902018-11-25 19:14:37 +01001197INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
1198
William Lallemand41db4602017-10-30 11:15:51 +01001199static struct action_kw_list http_req_actions = {
1200 .kw = {
1201 { "cache-use", parse_cache_use },
1202 { NULL, NULL }
1203 }
1204};
1205
Willy Tarreau0108d902018-11-25 19:14:37 +01001206INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1207
William Lallemand41db4602017-10-30 11:15:51 +01001208struct applet http_cache_applet = {
1209 .obj_type = OBJ_TYPE_APPLET,
1210 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001211 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001212 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001213};
1214
Willy Tarreaue6552512018-11-26 11:33:13 +01001215/* config parsers for this section */
1216REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1217REGISTER_CONFIG_POSTPARSER("cache", cfg_cache_postparser);