blob: 6988b15bec9237cba8b26af8b81bfdfc12ee8c82 [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>
37
38/* flt_cache_store */
39
40static const char *cache_store_flt_id = "cache store filter";
41
Willy Tarreaubafbe012017-11-24 17:34:44 +010042static struct pool_head *pool_head_cache_st = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +010043
William Lallemand41db4602017-10-30 11:15:51 +010044struct applet http_cache_applet;
45
46struct flt_ops cache_ops;
47
48struct cache {
Willy Tarreaufd5efb52017-11-26 08:54:31 +010049 struct list list; /* cache linked list */
William Lallemand41db4602017-10-30 11:15:51 +010050 struct eb_root entries; /* head of cache entries based on keys */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010051 unsigned int maxage; /* max-age */
52 unsigned int maxblocks;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +020053 unsigned int maxobjsz; /* max-object-size (in bytes) */
Willy Tarreaufd5efb52017-11-26 08:54:31 +010054 char id[33]; /* cache name */
William Lallemand41db4602017-10-30 11:15:51 +010055};
56
57/*
58 * cache ctx for filters
59 */
60struct cache_st {
61 int hdrs_len;
62 struct shared_block *first_block;
63};
64
65struct cache_entry {
66 unsigned int latest_validation; /* latest validation date */
67 unsigned int expire; /* expiration date */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020068 unsigned int age; /* Origin server "Age" header value */
69 unsigned int eoh; /* Origin server end of headers offset. */
William Lallemand41db4602017-10-30 11:15:51 +010070 struct eb32_node eb; /* ebtree node used to hold the cache object */
William Lallemandf528fff2017-11-23 19:43:17 +010071 char hash[20];
William Lallemand41db4602017-10-30 11:15:51 +010072 unsigned char data[0];
73};
74
75#define CACHE_BLOCKSIZE 1024
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +020076#define CACHE_ENTRY_MAX_AGE 2147483648
William Lallemand41db4602017-10-30 11:15:51 +010077
78static struct list caches = LIST_HEAD_INIT(caches);
79static struct cache *tmp_cache_config = NULL;
80
William Lallemandf528fff2017-11-23 19:43:17 +010081struct cache_entry *entry_exist(struct cache *cache, char *hash)
William Lallemand4da3f8a2017-10-31 14:33:34 +010082{
83 struct eb32_node *node;
84 struct cache_entry *entry;
85
William Lallemandf528fff2017-11-23 19:43:17 +010086 node = eb32_lookup(&cache->entries, (*(unsigned int *)hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +010087 if (!node)
88 return NULL;
89
90 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemandf528fff2017-11-23 19:43:17 +010091
92 /* if that's not the right node */
93 if (memcmp(entry->hash, hash, sizeof(entry->hash)))
94 return NULL;
95
William Lallemand08727662017-11-21 20:01:27 +010096 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010097 return entry;
William Lallemand08727662017-11-21 20:01:27 +010098 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010099 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +0100100 entry->eb.key = 0;
101 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100102 return NULL;
103
104}
105
106static inline struct shared_context *shctx_ptr(struct cache *cache)
107{
108 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
109}
110
William Lallemand77c11972017-10-31 20:43:01 +0100111static inline struct shared_block *block_ptr(struct cache_entry *entry)
112{
113 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
114}
115
116
117
William Lallemand41db4602017-10-30 11:15:51 +0100118static int
119cache_store_init(struct proxy *px, struct flt_conf *f1conf)
120{
121 return 0;
122}
123
William Lallemand4da3f8a2017-10-31 14:33:34 +0100124static int
125cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
126{
127 if (!(chn->flags & CF_ISRESP))
128 return 1;
129
130 if (filter->ctx == NULL) {
131 struct cache_st *st;
132
Willy Tarreaubafbe012017-11-24 17:34:44 +0100133 st = pool_alloc_dirty(pool_head_cache_st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100134 if (st == NULL)
135 return -1;
136
137 st->hdrs_len = 0;
138 st->first_block = NULL;
139 filter->ctx = st;
140 }
141
142 register_data_filter(s, chn, filter);
143
144 return 1;
145}
146
147static int
William Lallemand49dc0482017-11-24 14:33:54 +0100148cache_store_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
149{
150 struct cache_st *st = filter->ctx;
151 struct cache *cache = filter->config->conf;
152 struct shared_context *shctx = shctx_ptr(cache);
153
154 if (!(chn->flags & CF_ISRESP))
155 return 1;
156
157 /* Everything should be released in the http_end filter, but we need to do it
158 * there too, in case of errors */
159
160 if (st && st->first_block) {
161
162 shctx_lock(shctx);
163 shctx_row_dec_hot(shctx, st->first_block);
164 shctx_unlock(shctx);
165
166 }
167 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100168 pool_free(pool_head_cache_st, st);
William Lallemand49dc0482017-11-24 14:33:54 +0100169 filter->ctx = NULL;
170 }
171
172 return 1;
173}
174
175
176static int
William Lallemand4da3f8a2017-10-31 14:33:34 +0100177cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
178{
179 struct cache_st *st = filter->ctx;
180
William Lallemand4da3f8a2017-10-31 14:33:34 +0100181 if (!(msg->chn->flags & CF_ISRESP) || !st)
182 return 1;
183
William Lallemand9d5f54d2017-11-14 14:39:22 +0100184 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100185
186 return 1;
187}
188
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200189static inline void disable_cache_entry(struct cache_st *st,
190 struct filter *filter, struct shared_context *shctx)
191{
192 struct cache_entry *object;
193
194 object = (struct cache_entry *)st->first_block->data;
195 filter->ctx = NULL; /* disable cache */
196 shctx_lock(shctx);
197 shctx_row_dec_hot(shctx, st->first_block);
198 object->eb.key = 0;
199 shctx_unlock(shctx);
200 pool_free(pool_head_cache_st, st);
201}
202
William Lallemand4da3f8a2017-10-31 14:33:34 +0100203static int
204cache_store_http_forward_data(struct stream *s, struct filter *filter,
205 struct http_msg *msg, unsigned int len)
206{
207 struct cache_st *st = filter->ctx;
208 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
209 int ret;
210
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200211 ret = 0;
212
William Lallemand4da3f8a2017-10-31 14:33:34 +0100213 /*
214 * We need to skip the HTTP headers first, because we saved them in the
215 * http-response action.
216 */
217 if (!(msg->chn->flags & CF_ISRESP) || !st)
218 return len;
219
220 if (!len) {
221 /* Nothing to foward */
222 ret = len;
223 }
William Lallemand10935bc2017-11-14 14:39:23 +0100224 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100225 /* Forward part of headers */
226 ret = len;
227 st->hdrs_len -= len;
228 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100229 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100230 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100231 if (filter->ctx && st->first_block) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200232 int to_append, append;
233 struct shared_block *fb;
234
235 to_append = MIN(ci_contig_data(msg->chn), len - st->hdrs_len);
236
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200237 shctx_lock(shctx);
238 fb = shctx_row_reserve_hot(shctx, st->first_block, to_append);
239 if (!fb) {
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100240 shctx_unlock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200241 disable_cache_entry(st, filter, shctx);
242 return len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100243 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200244 shctx_unlock(shctx);
245
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200246 /* Skip remaining headers to fill the cache */
247 c_adv(msg->chn, st->hdrs_len);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200248 append = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append,
249 (unsigned char *)ci_head(msg->chn), to_append);
250 ret = st->hdrs_len + to_append - append;
251 /* Rewind the buffer to forward all data */
252 c_rew(msg->chn, st->hdrs_len);
253 st->hdrs_len = 0;
254 if (ret < 0)
255 disable_cache_entry(st, filter, shctx);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100256 }
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200257 else
258 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100259 }
260
261 if ((ret != len) ||
262 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
263 task_wakeup(s->task, TASK_WOKEN_MSG);
264
265 return ret;
266}
267
268static int
269cache_store_http_end(struct stream *s, struct filter *filter,
270 struct http_msg *msg)
271{
272 struct cache_st *st = filter->ctx;
273 struct cache *cache = filter->config->conf;
274 struct shared_context *shctx = shctx_ptr(cache);
275 struct cache_entry *object;
276
277 if (!(msg->chn->flags & CF_ISRESP))
278 return 1;
279
280 if (st && st->first_block) {
281
282 object = (struct cache_entry *)st->first_block->data;
283
284 /* does not need to test if the insertion worked, if it
285 * doesn't, the blocks will be reused anyway */
286
287 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100288 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
289 object->eb.key = 0;
290 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100291 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100292 shctx_row_dec_hot(shctx, st->first_block);
293 shctx_unlock(shctx);
294
295 }
296 if (st) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100297 pool_free(pool_head_cache_st, st);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100298 filter->ctx = NULL;
299 }
300
301 return 1;
302}
303
304 /*
305 * This intends to be used when checking HTTP headers for some
306 * word=value directive. Return a pointer to the first character of value, if
307 * the word was not found or if there wasn't any value assigned ot it return NULL
308 */
309char *directive_value(const char *sample, int slen, const char *word, int wlen)
310{
311 int st = 0;
312
313 if (slen < wlen)
314 return 0;
315
316 while (wlen) {
317 char c = *sample ^ *word;
318 if (c && c != ('A' ^ 'a'))
319 return NULL;
320 sample++;
321 word++;
322 slen--;
323 wlen--;
324 }
325
326 while (slen) {
327 if (st == 0) {
328 if (*sample != '=')
329 return NULL;
330 sample++;
331 slen--;
332 st = 1;
333 continue;
334 } else {
335 return (char *)sample;
336 }
337 }
338
339 return NULL;
340}
341
342/*
343 * Return the maxage in seconds of an HTTP response.
344 * Compute the maxage using either:
345 * - the assigned max-age of the cache
346 * - the s-maxage directive
347 * - the max-age directive
348 * - (Expires - Data) headers
349 * - the default-max-age of the cache
350 *
351 */
William Lallemand49b44532017-11-24 18:53:43 +0100352int http_calc_maxage(struct stream *s, struct cache *cache)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100353{
354 struct http_txn *txn = s->txn;
355 struct hdr_ctx ctx;
356
357 int smaxage = -1;
358 int maxage = -1;
359
360
William Lallemand4da3f8a2017-10-31 14:33:34 +0100361 ctx.idx = 0;
362
363 /* loop on the Cache-Control values */
Willy Tarreau178b9872018-06-19 07:13:36 +0200364 while (http_find_header2("Cache-Control", 13, ci_head(&s->res), &txn->hdr_idx, &ctx)) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100365 char *directive = ctx.line + ctx.val;
366 char *value;
367
368 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
369 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200370 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100371
372 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
373 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200374 maxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100375 }
376
377 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
378 if (value) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200379 struct buffer *chk = get_trash_chunk();
William Lallemand4da3f8a2017-10-31 14:33:34 +0100380
381 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
382 chunk_strncat(chk, "", 1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200383 smaxage = atoi(chk->area);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100384 }
385 }
386
387 /* TODO: Expires - Data */
388
389
390 if (smaxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100391 return MIN(smaxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100392
393 if (maxage > 0)
William Lallemand49b44532017-11-24 18:53:43 +0100394 return MIN(maxage, cache->maxage);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100395
William Lallemand49b44532017-11-24 18:53:43 +0100396 return cache->maxage;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100397
398}
399
400
William Lallemanda400a3a2017-11-20 19:13:12 +0100401static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
402{
Willy Tarreau5bd37fa2018-04-04 20:17:03 +0200403 struct cache_entry *object = (struct cache_entry *)block->data;
404
405 if (first == block && object->eb.key)
406 eb32_delete(&object->eb);
407 object->eb.key = 0;
William Lallemanda400a3a2017-11-20 19:13:12 +0100408}
409
William Lallemand41db4602017-10-30 11:15:51 +0100410/*
411 * This fonction will store the headers of the response in a buffer and then
412 * register a filter to store the data
413 */
414enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
415 struct session *sess, struct stream *s, int flags)
416{
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200417 unsigned int age;
418 long long hdr_age;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100419 struct http_txn *txn = s->txn;
420 struct http_msg *msg = &txn->rsp;
421 struct filter *filter;
422 struct hdr_ctx ctx;
423 struct shared_block *first = NULL;
William Lallemand49b44532017-11-24 18:53:43 +0100424 struct cache *cache = (struct cache *)rule->arg.act.p[0];
425 struct shared_context *shctx = shctx_ptr(cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100426 struct cache_entry *object;
427
428
429 /* Don't cache if the response came from a cache */
430 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
431 s->target == &http_cache_applet.obj_type) {
432 goto out;
433 }
434
435 /* cache only HTTP/1.1 */
436 if (!(txn->req.flags & HTTP_MSGF_VER_11))
437 goto out;
438
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200439 /* Do not cache too big objects. */
440 if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 &&
441 msg->sov + msg->body_len > shctx->max_obj_size)
442 goto out;
443
William Lallemand4da3f8a2017-10-31 14:33:34 +0100444 /* cache only GET method */
445 if (txn->meth != HTTP_METH_GET)
446 goto out;
447
448 /* cache only 200 status code */
449 if (txn->status != 200)
450 goto out;
451
452 /* Does not manage Vary at the moment. We will need a secondary key later for that */
453 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200454 if (http_find_header2("Vary", 4, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100455 goto out;
456
Willy Tarreaufaf29092017-12-21 15:59:17 +0100457 check_response_for_cacheability(s, &s->res);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100458
Willy Tarreaud4569d12017-12-22 18:03:04 +0100459 if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100460 goto out;
461
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200462 age = 0;
463 ctx.idx = 0;
464 if (http_find_header2("Age", 3, ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
465 if (!strl2llrc(ctx.line + ctx.val, ctx.vlen, &hdr_age) && hdr_age > 0) {
466 if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE))
467 hdr_age = CACHE_ENTRY_MAX_AGE;
468 age = hdr_age;
469 }
470 http_remove_header2(msg, &txn->hdr_idx, &ctx);
471 }
472
William Lallemand4da3f8a2017-10-31 14:33:34 +0100473 shctx_lock(shctx);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200474 first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + msg->sov);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100475 if (!first) {
476 shctx_unlock(shctx);
477 goto out;
478 }
479 shctx_unlock(shctx);
480
Willy Tarreau1093a452018-04-06 19:02:25 +0200481 /* the received memory is not initialized, we need at least to mark
482 * the object as not indexed yet.
483 */
484 object = (struct cache_entry *)first->data;
485 object->eb.node.leaf_p = NULL;
486 object->eb.key = 0;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200487 object->age = age;
488 object->eoh = msg->eoh;
Willy Tarreau1093a452018-04-06 19:02:25 +0200489
William Lallemand4da3f8a2017-10-31 14:33:34 +0100490 /* reserve space for the cache_entry structure */
491 first->len = sizeof(struct cache_entry);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200492 first->last_append = NULL;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100493 /* cache the headers in a http action because it allows to chose what
494 * to cache, for example you might want to cache a response before
495 * modifying some HTTP headers, or on the contrary after modifying
496 * those headers.
497 */
498
499 /* does not need to be locked because it's in the "hot" list,
500 * copy the headers */
Frédéric Lécaille0bec8072018-10-22 17:55:57 +0200501 if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)ci_head(&s->res), msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100502 goto out;
503
504 /* register the buffer in the filter ctx for filling it with data*/
505 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
506 list_for_each_entry(filter, &s->strm_flt.filters, list) {
507 if (filter->config->id == cache_store_flt_id &&
508 filter->config->conf == rule->arg.act.p[0]) {
509 if (filter->ctx) {
510 struct cache_st *cache_ctx = filter->ctx;
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100511 struct cache_entry *old;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100512
513 cache_ctx->first_block = first;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100514
William Lallemandf528fff2017-11-23 19:43:17 +0100515 object->eb.key = (*(unsigned int *)&txn->cache_hash);
516 memcpy(object->hash, txn->cache_hash, sizeof(object->hash));
William Lallemand4da3f8a2017-10-31 14:33:34 +0100517 /* Insert the node later on caching success */
518
519 shctx_lock(shctx);
Willy Tarreauc9bd34c2017-12-22 17:42:46 +0100520
521 old = entry_exist(cache, txn->cache_hash);
522 if (old) {
523 eb32_delete(&old->eb);
524 old->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100525 }
526 shctx_unlock(shctx);
527
528 /* store latest value and expiration time */
529 object->latest_validation = now.tv_sec;
William Lallemand49b44532017-11-24 18:53:43 +0100530 object->expire = now.tv_sec + http_calc_maxage(s, cache);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100531 }
532 return ACT_RET_CONT;
533 }
534 }
535 }
536
537out:
538 /* if does not cache */
539 if (first) {
540 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100541 first->len = 0;
542 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100543 shctx_row_dec_hot(shctx, first);
544 shctx_unlock(shctx);
545 }
546
William Lallemand41db4602017-10-30 11:15:51 +0100547 return ACT_RET_CONT;
548}
549
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200550#define HTTP_CACHE_INIT 0 /* Initial state. */
551#define HTTP_CACHE_HEADER 1 /* Cache entry headers forwarded. */
552#define HTTP_CACHE_FWD 2 /* Cache entry completely forwarded. */
553#define HTTP_CACHE_END 3 /* Cache entry treatment terminated. */
William Lallemand77c11972017-10-31 20:43:01 +0100554
William Lallemandecb73b12017-11-24 14:33:55 +0100555static void http_cache_applet_release(struct appctx *appctx)
556{
557 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
558 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
559 struct shared_block *first = block_ptr(cache_ptr);
560
561 shctx_lock(shctx_ptr(cache));
562 shctx_row_dec_hot(shctx_ptr(cache), first);
563 shctx_unlock(shctx_ptr(cache));
564}
565
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200566/*
567 * Append an "Age" header into <chn> channel for this <ce> cache entry.
568 * This is the responsability of the caller to insure there is enough
569 * data in the channel.
570 *
571 * Returns the number of bytes inserted if succeeded, 0 if failed.
572 */
573static int cache_channel_append_age_header(struct cache_entry *ce, struct channel *chn)
574{
575 unsigned int age;
576
577 age = MAX(0, (int)(now.tv_sec - ce->latest_validation)) + ce->age;
578 if (unlikely(age > CACHE_ENTRY_MAX_AGE))
579 age = CACHE_ENTRY_MAX_AGE;
580
581 chunk_reset(&trash);
582 chunk_printf(&trash, "Age: %u", age);
583
584 return ci_insert_line2(chn, ce->eoh, trash.area, trash.data);
585}
586
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200587static int cache_channel_row_data_get(struct appctx *appctx, int len)
William Lallemand77c11972017-10-31 20:43:01 +0100588{
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200589 int ret, total;
William Lallemand77c11972017-10-31 20:43:01 +0100590 struct stream_interface *si = appctx->owner;
591 struct channel *res = si_ic(si);
592 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
William Lallemand77c11972017-10-31 20:43:01 +0100593 struct shared_context *shctx = shctx_ptr(cache);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200594 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
595 struct shared_block *blk, *next = appctx->ctx.cache.next;
596 int offset;
597
598 total = 0;
599 offset = 0;
600
601 if (!next) {
602 offset = sizeof(struct cache_entry);
603 next = block_ptr(cache_ptr);
604 }
605
606 blk = next;
607 list_for_each_entry_from(blk, &shctx->hot, list) {
608 int sz;
609
610 if (len <= 0)
611 break;
612
613 sz = MIN(len, shctx->block_size - offset);
614
615 ret = ci_putblk(res, (const char *)blk->data + offset, sz);
616 if (unlikely(offset))
617 offset = 0;
618 if (ret <= 0) {
619 if (ret == -3 || ret == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100620 si_cant_put(si);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200621 break;
622 }
623 return -1;
624 }
625
626 total += sz;
627 len -= sz;
628 }
629 appctx->ctx.cache.next = blk;
630
631 return total;
632}
633
634static void http_cache_io_handler(struct appctx *appctx)
635{
636 struct stream_interface *si = appctx->owner;
637 struct channel *res = si_ic(si);
638 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
William Lallemand77c11972017-10-31 20:43:01 +0100639 struct shared_block *first = block_ptr(cache_ptr);
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200640 int *sent = &appctx->ctx.cache.sent;
William Lallemand77c11972017-10-31 20:43:01 +0100641
642 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
643 goto out;
644
645 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200646 if (res->buf.size == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100647 si_cant_put(si);
William Lallemand77c11972017-10-31 20:43:01 +0100648 goto out;
649 }
650
651 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
652 appctx->st0 = HTTP_CACHE_END;
653
654 /* buffer are aligned there, should be fine */
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200655 if (appctx->st0 == HTTP_CACHE_HEADER || appctx->st0 == HTTP_CACHE_INIT) {
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200656 int len = first->len - *sent - sizeof(struct cache_entry);
William Lallemand55e76742017-11-21 20:01:28 +0100657
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200658 if (len > 0) {
659 int ret;
660
661 ret = cache_channel_row_data_get(appctx, len);
662 if (ret == -1)
663 appctx->st0 = HTTP_CACHE_END;
664 else
665 *sent += ret;
Frédéric Lécaillee7a770c2018-10-26 14:29:22 +0200666 if (appctx->st0 == HTTP_CACHE_INIT && *sent > cache_ptr->eoh &&
667 cache_channel_append_age_header(cache_ptr, res))
668 appctx->st0 = HTTP_CACHE_HEADER;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200669 }
670 else {
671 *sent = 0;
672 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand77c11972017-10-31 20:43:01 +0100673 }
William Lallemand77c11972017-10-31 20:43:01 +0100674 }
675
676 if (appctx->st0 == HTTP_CACHE_FWD) {
677 /* eat the whole request */
Willy Tarreau178b9872018-06-19 07:13:36 +0200678 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 +0100679 res->flags |= CF_READ_NULL;
680 si_shutr(si);
681 }
682
683 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
684 si_shutw(si);
685out:
686 ;
687}
688
William Lallemand41db4602017-10-30 11:15:51 +0100689enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
690 struct act_rule *rule, char **err)
691{
692 struct flt_conf *fconf;
693 int cur_arg = *orig_arg;
694 rule->action = ACT_CUSTOM;
695 rule->action_ptr = http_action_store_cache;
696
697 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
698 memprintf(err, "expects a cache name");
699 return ACT_RET_PRS_ERR;
700 }
701
702 /* check if a cache filter was already registered with this cache
703 * name, if that's the case, must use it. */
704 list_for_each_entry(fconf, &proxy->filter_configs, list) {
705 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
706 rule->arg.act.p[0] = fconf->conf;
707 (*orig_arg)++;
708 /* filter already registered */
709 return ACT_RET_PRS_OK;
710 }
711 }
712
713 rule->arg.act.p[0] = strdup(args[cur_arg]);
714 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100715 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100716 err++;
717 goto err;
718 }
719 /* register a filter to fill the cache buffer */
720 fconf = calloc(1, sizeof(*fconf));
721 if (!fconf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100722 ha_alert("config: %s '%s': out of memory\n",
723 proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100724 err++;
725 goto err;
726 }
727 fconf->id = cache_store_flt_id;
728 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
729 fconf->ops = &cache_ops;
730 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
731
732 (*orig_arg)++;
733
734 return ACT_RET_PRS_OK;
735
736err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100737 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100738}
739
William Lallemandf528fff2017-11-23 19:43:17 +0100740/* This produces a sha1 hash of the concatenation of the first
741 * occurrence of the Host header followed by the path component if it
742 * begins with a slash ('/'). */
743int sha1_hosturi(struct http_txn *txn)
744{
745 struct hdr_ctx ctx;
746
747 blk_SHA_CTX sha1_ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +0200748 struct buffer *trash;
William Lallemandf528fff2017-11-23 19:43:17 +0100749 char *path;
750 char *end;
751 trash = get_trash_chunk();
752
753 /* retrive the host */
754 ctx.idx = 0;
Willy Tarreau178b9872018-06-19 07:13:36 +0200755 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx))
William Lallemandf528fff2017-11-23 19:43:17 +0100756 return 0;
757 chunk_strncat(trash, ctx.line + ctx.val, ctx.vlen);
758
759 /* now retrieve the path */
Willy Tarreau178b9872018-06-19 07:13:36 +0200760 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +0200761 path = http_txn_get_path(txn);
William Lallemandf528fff2017-11-23 19:43:17 +0100762 if (!path)
763 return 0;
764 chunk_strncat(trash, path, end - path);
765
766 /* hash everything */
767 blk_SHA1_Init(&sha1_ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 blk_SHA1_Update(&sha1_ctx, trash->area, trash->data);
William Lallemandf528fff2017-11-23 19:43:17 +0100769 blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx);
770
771 return 1;
772}
773
774
William Lallemand41db4602017-10-30 11:15:51 +0100775
776enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
777 struct session *sess, struct stream *s, int flags)
778{
William Lallemand77c11972017-10-31 20:43:01 +0100779
William Lallemand77c11972017-10-31 20:43:01 +0100780 struct cache_entry *res;
William Lallemand77c11972017-10-31 20:43:01 +0100781 struct cache *cache = (struct cache *)rule->arg.act.p[0];
782
Willy Tarreau504455c2017-12-22 17:47:35 +0100783 check_request_for_cacheability(s, &s->req);
784 if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE)
785 return ACT_RET_CONT;
786
Willy Tarreau7704b1e2017-12-22 16:32:43 +0100787 if (!sha1_hosturi(s->txn))
788 return ACT_RET_CONT;
William Lallemandf528fff2017-11-23 19:43:17 +0100789
Willy Tarreau504455c2017-12-22 17:47:35 +0100790 if (s->txn->flags & TX_CACHE_IGNORE)
791 return ACT_RET_CONT;
792
William Lallemanda400a3a2017-11-20 19:13:12 +0100793 shctx_lock(shctx_ptr(cache));
William Lallemandf528fff2017-11-23 19:43:17 +0100794 res = entry_exist(cache, s->txn->cache_hash);
William Lallemand77c11972017-10-31 20:43:01 +0100795 if (res) {
796 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100797 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
798 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100799 s->target = &http_cache_applet.obj_type;
800 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
801 appctx->st0 = HTTP_CACHE_INIT;
802 appctx->rule = rule;
803 appctx->ctx.cache.entry = res;
Frédéric Lécaille8df65ae2018-10-22 18:01:48 +0200804 appctx->ctx.cache.next = NULL;
805 appctx->ctx.cache.sent = 0;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100806 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100807 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100808 shctx_lock(shctx_ptr(cache));
809 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
810 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100811 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100812 }
813 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100814 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100815 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100816}
817
818
819enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
820 struct act_rule *rule, char **err)
821{
822 int cur_arg = *orig_arg;
823
824 rule->action = ACT_CUSTOM;
825 rule->action_ptr = http_action_req_cache_use;
826
827 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
828 memprintf(err, "expects a cache name");
829 return ACT_RET_PRS_ERR;
830 }
831
832 rule->arg.act.p[0] = strdup(args[cur_arg]);
833 if (!rule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100834 ha_alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
William Lallemand41db4602017-10-30 11:15:51 +0100835 err++;
836 goto err;
837 }
838
839 (*orig_arg)++;
840 return ACT_RET_PRS_OK;
841
842err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100843 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100844
845}
846
847int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
848{
849 int err_code = 0;
850
851 if (strcmp(args[0], "cache") == 0) { /* new cache section */
852
853 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100854 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
855 file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100856 err_code |= ERR_ALERT | ERR_ABORT;
857 goto out;
858 }
859
860 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
861 err_code |= ERR_ABORT;
862 goto out;
863 }
864
865 if (tmp_cache_config == NULL) {
866 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
867 if (!tmp_cache_config) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100868 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
William Lallemand41db4602017-10-30 11:15:51 +0100869 err_code |= ERR_ALERT | ERR_ABORT;
870 goto out;
871 }
872
873 strlcpy2(tmp_cache_config->id, args[1], 33);
874 if (strlen(args[1]) > 32) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100875 ha_warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
876 file, linenum, tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100877 err_code |= ERR_WARN;
878 }
William Lallemand49b44532017-11-24 18:53:43 +0100879 tmp_cache_config->maxage = 60;
William Lallemand41db4602017-10-30 11:15:51 +0100880 tmp_cache_config->maxblocks = 0;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200881 tmp_cache_config->maxobjsz = 0;
William Lallemand41db4602017-10-30 11:15:51 +0100882 }
883 } else if (strcmp(args[0], "total-max-size") == 0) {
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200884 unsigned long int maxsize;
885 char *err;
William Lallemand41db4602017-10-30 11:15:51 +0100886
887 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
888 err_code |= ERR_ABORT;
889 goto out;
890 }
891
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200892 maxsize = strtoul(args[1], &err, 10);
893 if (err == args[1] || *err != '\0') {
894 ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n",
895 file, linenum, args[1]);
896 err_code |= ERR_ABORT;
897 goto out;
898 }
899
900 if (maxsize > (UINT_MAX >> 20)) {
901 ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n",
902 file, linenum, args[1], UINT_MAX >> 20);
903 err_code |= ERR_ABORT;
904 goto out;
905 }
906
William Lallemand41db4602017-10-30 11:15:51 +0100907 /* size in megabytes */
Frédéric Lécailleb9b8b6b2018-10-25 20:17:45 +0200908 maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE;
William Lallemand41db4602017-10-30 11:15:51 +0100909 tmp_cache_config->maxblocks = maxsize;
William Lallemand49b44532017-11-24 18:53:43 +0100910 } else if (strcmp(args[0], "max-age") == 0) {
911 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
912 err_code |= ERR_ABORT;
913 goto out;
914 }
915
916 if (!*args[1]) {
917 ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n",
918 file, linenum, args[0]);
919 err_code |= ERR_WARN;
920 }
921
922 tmp_cache_config->maxage = atoi(args[1]);
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200923 } else if (strcmp(args[0], "max-object-size") == 0) {
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200924 unsigned int maxobjsz;
925 char *err;
926
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200927 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
928 err_code |= ERR_ABORT;
929 goto out;
930 }
931
932 if (!*args[1]) {
933 ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n",
934 file, linenum, args[0]);
935 err_code |= ERR_WARN;
936 }
937
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200938 maxobjsz = strtoul(args[1], &err, 10);
939 if (err == args[1] || *err != '\0') {
940 ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n",
941 file, linenum, args[1]);
942 err_code |= ERR_ABORT;
943 goto out;
944 }
945 tmp_cache_config->maxobjsz = maxobjsz;
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200946 }
947 else if (*args[0] != 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100948 ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
William Lallemand41db4602017-10-30 11:15:51 +0100949 err_code |= ERR_ALERT | ERR_FATAL;
950 goto out;
951 }
952out:
953 return err_code;
954}
955
956/* once the cache section is parsed */
957
958int cfg_post_parse_section_cache()
959{
960 struct shared_context *shctx;
961 int err_code = 0;
962 int ret_shctx;
963
964 if (tmp_cache_config) {
965 struct cache *cache;
966
967 if (tmp_cache_config->maxblocks <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100968 ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
William Lallemand41db4602017-10-30 11:15:51 +0100969 err_code |= ERR_FATAL | ERR_ALERT;
970 goto out;
971 }
972
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200973 if (!tmp_cache_config->maxobjsz) {
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200974 /* Default max. file size is a 256th of the cache size. */
975 tmp_cache_config->maxobjsz =
976 (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8;
Frédéric Lécaille4eba5442018-10-25 20:29:31 +0200977 }
978 else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) {
979 ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2);
980 err_code |= ERR_FATAL | ERR_ALERT;
981 goto out;
982 }
Frédéric Lécaillea2219f52018-10-22 16:59:13 +0200983
984 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE,
985 tmp_cache_config->maxobjsz, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100986
Frédéric Lécaillebc584492018-10-25 20:18:59 +0200987 if (ret_shctx <= 0) {
William Lallemand41db4602017-10-30 11:15:51 +0100988 if (ret_shctx == SHCTX_E_INIT_LOCK)
Christopher Faulet767a84b2017-11-24 16:50:31 +0100989 ha_alert("Unable to initialize the lock for the cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100990 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100991 ha_alert("Unable to allocate cache.\n");
William Lallemand41db4602017-10-30 11:15:51 +0100992
993 err_code |= ERR_FATAL | ERR_ALERT;
994 goto out;
995 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100996 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100997 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
998 cache = (struct cache *)shctx->data;
999 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +01001000 LIST_ADDQ(&caches, &cache->list);
1001 }
1002out:
1003 free(tmp_cache_config);
1004 tmp_cache_config = NULL;
1005 return err_code;
1006
1007}
1008
1009/*
1010 * Resolve the cache name to a pointer once the file is completely read.
1011 */
1012int cfg_cache_postparser()
1013{
1014 struct act_rule *hresrule, *hrqrule;
1015 void *cache_ptr;
1016 struct cache *cache;
1017 struct proxy *curproxy = NULL;
1018 int err = 0;
1019 struct flt_conf *fconf;
1020
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001021 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
William Lallemand41db4602017-10-30 11:15:51 +01001022
1023 /* resolve the http response cache name to a ptr in the action rule */
1024 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
1025 if (hresrule->action != ACT_CUSTOM ||
1026 hresrule->action_ptr != http_action_store_cache)
1027 continue;
1028
1029 cache_ptr = hresrule->arg.act.p[0];
1030
1031 list_for_each_entry(cache, &caches, list) {
1032 if (!strcmp(cache->id, cache_ptr)) {
1033 /* don't free there, it's still used in the filter conf */
1034 cache_ptr = cache;
1035 break;
1036 }
1037 }
1038
1039 if (cache_ptr == hresrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001040 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
1041 curproxy->id, (char *)hresrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001042 err++;
1043 }
1044
1045 hresrule->arg.act.p[0] = cache_ptr;
1046 }
1047
1048 /* resolve the http request cache name to a ptr in the action rule */
1049 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
1050 if (hrqrule->action != ACT_CUSTOM ||
1051 hrqrule->action_ptr != http_action_req_cache_use)
1052 continue;
1053
1054 cache_ptr = hrqrule->arg.act.p[0];
1055
1056 list_for_each_entry(cache, &caches, list) {
1057 if (!strcmp(cache->id, cache_ptr)) {
1058 free(cache_ptr);
1059 cache_ptr = cache;
1060 break;
1061 }
1062 }
1063
1064 if (cache_ptr == hrqrule->arg.act.p[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001065 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
1066 curproxy->id, (char *)hrqrule->arg.act.p[0]);
William Lallemand41db4602017-10-30 11:15:51 +01001067 err++;
1068 }
1069
1070 hrqrule->arg.act.p[0] = cache_ptr;
1071 }
1072
1073 /* resolve the cache name to a ptr in the filter config */
1074 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
1075
William Lallemand9c54c532017-11-02 16:38:42 +01001076 if (fconf->id != cache_store_flt_id)
1077 continue;
1078
William Lallemand41db4602017-10-30 11:15:51 +01001079 cache_ptr = fconf->conf;
1080
1081 list_for_each_entry(cache, &caches, list) {
1082 if (!strcmp(cache->id, cache_ptr)) {
1083 /* there can be only one filter per cache, so we free it there */
1084 free(cache_ptr);
1085 cache_ptr = cache;
1086 break;
1087 }
1088 }
1089
1090 if (cache_ptr == fconf->conf) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001091 ha_alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
1092 curproxy->id, (char *)fconf->conf);
William Lallemand41db4602017-10-30 11:15:51 +01001093 err++;
1094 }
1095 fconf->conf = cache_ptr;
1096 }
1097 }
1098 return err;
1099}
1100
1101
1102struct flt_ops cache_ops = {
1103 .init = cache_store_init,
1104
William Lallemand4da3f8a2017-10-31 14:33:34 +01001105 /* Handle channels activity */
1106 .channel_start_analyze = cache_store_chn_start_analyze,
William Lallemand49dc0482017-11-24 14:33:54 +01001107 .channel_end_analyze = cache_store_chn_end_analyze,
William Lallemand4da3f8a2017-10-31 14:33:34 +01001108
1109 /* Filter HTTP requests and responses */
1110 .http_headers = cache_store_http_headers,
1111 .http_end = cache_store_http_end,
1112
1113 .http_forward_data = cache_store_http_forward_data,
1114
William Lallemand41db4602017-10-30 11:15:51 +01001115};
1116
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001117static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand1f49a362017-11-21 20:01:26 +01001118{
1119 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1120 return 1;
1121
1122 return 0;
1123}
1124
1125static int cli_io_handler_show_cache(struct appctx *appctx)
1126{
1127 struct cache* cache = appctx->ctx.cli.p0;
1128 struct stream_interface *si = appctx->owner;
1129
William Lallemand1f49a362017-11-21 20:01:26 +01001130 if (cache == NULL) {
1131 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
1132 }
1133
1134 list_for_each_entry_from(cache, &caches, list) {
1135 struct eb32_node *node = NULL;
1136 unsigned int next_key;
1137 struct cache_entry *entry;
1138
William Lallemand1f49a362017-11-21 20:01:26 +01001139 next_key = appctx->ctx.cli.i0;
Willy Tarreauafe1de52018-04-04 11:56:43 +02001140 if (!next_key) {
1141 chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
1142 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001143 si_cant_put(si);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001144 return 0;
1145 }
1146 }
William Lallemand1f49a362017-11-21 20:01:26 +01001147
1148 appctx->ctx.cli.p0 = cache;
1149
1150 while (1) {
1151
1152 shctx_lock(shctx_ptr(cache));
1153 node = eb32_lookup_ge(&cache->entries, next_key);
1154 if (!node) {
1155 shctx_unlock(shctx_ptr(cache));
Willy Tarreauafe1de52018-04-04 11:56:43 +02001156 appctx->ctx.cli.i0 = 0;
William Lallemand1f49a362017-11-21 20:01:26 +01001157 break;
1158 }
1159
1160 entry = container_of(node, struct cache_entry, eb);
Willy Tarreauafe1de52018-04-04 11:56:43 +02001161 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 +01001162
1163 next_key = node->key + 1;
1164 appctx->ctx.cli.i0 = next_key;
1165
1166 shctx_unlock(shctx_ptr(cache));
1167
1168 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001169 si_cant_put(si);
William Lallemand1f49a362017-11-21 20:01:26 +01001170 return 0;
1171 }
1172 }
1173
1174 }
1175
1176 return 1;
1177
1178}
1179
1180static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +01001181 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
1182 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +01001183}};
1184
1185
William Lallemand41db4602017-10-30 11:15:51 +01001186static struct action_kw_list http_res_actions = {
1187 .kw = {
1188 { "cache-store", parse_cache_store },
1189 { NULL, NULL }
1190 }
1191};
1192
1193static struct action_kw_list http_req_actions = {
1194 .kw = {
1195 { "cache-use", parse_cache_use },
1196 { NULL, NULL }
1197 }
1198};
1199
1200struct applet http_cache_applet = {
1201 .obj_type = OBJ_TYPE_APPLET,
1202 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +01001203 .fct = http_cache_io_handler,
William Lallemandecb73b12017-11-24 14:33:55 +01001204 .release = http_cache_applet_release,
William Lallemand41db4602017-10-30 11:15:51 +01001205};
1206
1207__attribute__((constructor))
1208static void __cache_init(void)
1209{
1210 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
1211 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +01001212 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +01001213 http_res_keywords_register(&http_res_actions);
1214 http_req_keywords_register(&http_req_actions);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001215 pool_head_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +01001216}
1217