blob: 349101520475f9339267b1c5bfd86d7e4d2f3766 [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>
14
William Lallemand75d93292017-11-21 20:01:24 +010015#include <types/action.h>
William Lallemand1f49a362017-11-21 20:01:26 +010016#include <types/cli.h>
William Lallemand75d93292017-11-21 20:01:24 +010017#include <types/filters.h>
18#include <types/proxy.h>
19#include <types/shctx.h>
20
William Lallemand41db4602017-10-30 11:15:51 +010021#include <proto/channel.h>
William Lallemand1f49a362017-11-21 20:01:26 +010022#include <proto/cli.h>
William Lallemand41db4602017-10-30 11:15:51 +010023#include <proto/proxy.h>
24#include <proto/hdr_idx.h>
25#include <proto/filters.h>
26#include <proto/proto_http.h>
27#include <proto/log.h>
28#include <proto/stream.h>
29#include <proto/stream_interface.h>
30#include <proto/shctx.h>
31
William Lallemand41db4602017-10-30 11:15:51 +010032
33#include <common/cfgparse.h>
34#include <common/hash.h>
35
36/* flt_cache_store */
37
38static const char *cache_store_flt_id = "cache store filter";
39
William Lallemand4da3f8a2017-10-31 14:33:34 +010040static struct pool_head *pool2_cache_st = NULL;
41
William Lallemand41db4602017-10-30 11:15:51 +010042struct applet http_cache_applet;
43
44struct flt_ops cache_ops;
45
46struct cache {
47 char id[33]; /* cache name */
48 unsigned int maxage; /* max-age */
49 unsigned int maxblocks;
50 struct list list; /* cache linked list */
51 struct eb_root entries; /* head of cache entries based on keys */
52};
53
54/*
55 * cache ctx for filters
56 */
57struct cache_st {
58 int hdrs_len;
59 struct shared_block *first_block;
60};
61
62struct cache_entry {
63 unsigned int latest_validation; /* latest validation date */
64 unsigned int expire; /* expiration date */
65 struct eb32_node eb; /* ebtree node used to hold the cache object */
66 unsigned char data[0];
67};
68
69#define CACHE_BLOCKSIZE 1024
70
71static struct list caches = LIST_HEAD_INIT(caches);
72static struct cache *tmp_cache_config = NULL;
73
William Lallemand4da3f8a2017-10-31 14:33:34 +010074struct cache_entry *entry_exist(struct cache *cache, struct cache_entry *new_entry)
75{
76 struct eb32_node *node;
77 struct cache_entry *entry;
78
79 node = eb32_lookup(&cache->entries, new_entry->eb.key);
80 if (!node)
81 return NULL;
82
83 entry = eb32_entry(node, struct cache_entry, eb);
William Lallemand08727662017-11-21 20:01:27 +010084 if (entry->expire > now.tv_sec) {
William Lallemand4da3f8a2017-10-31 14:33:34 +010085 return entry;
William Lallemand08727662017-11-21 20:01:27 +010086 } else {
William Lallemand4da3f8a2017-10-31 14:33:34 +010087 eb32_delete(node);
William Lallemand08727662017-11-21 20:01:27 +010088 entry->eb.key = 0;
89 }
William Lallemand4da3f8a2017-10-31 14:33:34 +010090 return NULL;
91
92}
93
94static inline struct shared_context *shctx_ptr(struct cache *cache)
95{
96 return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data);
97}
98
William Lallemand77c11972017-10-31 20:43:01 +010099static inline struct shared_block *block_ptr(struct cache_entry *entry)
100{
101 return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data);
102}
103
104
105
William Lallemand41db4602017-10-30 11:15:51 +0100106static int
107cache_store_init(struct proxy *px, struct flt_conf *f1conf)
108{
109 return 0;
110}
111
William Lallemand4da3f8a2017-10-31 14:33:34 +0100112static int
113cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
114{
115 if (!(chn->flags & CF_ISRESP))
116 return 1;
117
118 if (filter->ctx == NULL) {
119 struct cache_st *st;
120
121 st = pool_alloc_dirty(pool2_cache_st);
122 if (st == NULL)
123 return -1;
124
125 st->hdrs_len = 0;
126 st->first_block = NULL;
127 filter->ctx = st;
128 }
129
130 register_data_filter(s, chn, filter);
131
132 return 1;
133}
134
135static int
136cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
137{
138 struct cache_st *st = filter->ctx;
139
William Lallemand4da3f8a2017-10-31 14:33:34 +0100140 if (!(msg->chn->flags & CF_ISRESP) || !st)
141 return 1;
142
William Lallemand9d5f54d2017-11-14 14:39:22 +0100143 st->hdrs_len = msg->sov;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100144
145 return 1;
146}
147
148static int
149cache_store_http_forward_data(struct stream *s, struct filter *filter,
150 struct http_msg *msg, unsigned int len)
151{
152 struct cache_st *st = filter->ctx;
153 struct shared_context *shctx = shctx_ptr((struct cache *)filter->config->conf);
William Lallemand08727662017-11-21 20:01:27 +0100154 struct cache_entry *object;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100155 int ret;
156
157 /*
158 * We need to skip the HTTP headers first, because we saved them in the
159 * http-response action.
160 */
161 if (!(msg->chn->flags & CF_ISRESP) || !st)
162 return len;
163
164 if (!len) {
165 /* Nothing to foward */
166 ret = len;
167 }
William Lallemand10935bc2017-11-14 14:39:23 +0100168 else if (st->hdrs_len >= len) {
William Lallemand4da3f8a2017-10-31 14:33:34 +0100169 /* Forward part of headers */
170 ret = len;
171 st->hdrs_len -= len;
172 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100173 else {
William Lallemand10935bc2017-11-14 14:39:23 +0100174 /* Forward data */
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100175 if (filter->ctx && st->first_block) {
176 /* disable buffering if too much data (never greater than a buffer size */
William Lallemand10935bc2017-11-14 14:39:23 +0100177 if (len - st->hdrs_len > global.tune.bufsize - global.tune.maxrewrite - st->first_block->len) {
William Lallemande1533f52017-11-14 14:39:24 +0100178 disable_cache:
William Lallemand08727662017-11-21 20:01:27 +0100179 object = (struct cache_entry *)st->first_block->data;
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100180 filter->ctx = NULL; /* disable cache */
181 shctx_lock(shctx);
182 shctx_row_dec_hot(shctx, st->first_block);
William Lallemand08727662017-11-21 20:01:27 +0100183 object->eb.key = 0;
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100184 shctx_unlock(shctx);
185 pool_free2(pool2_cache_st, st);
Olivier Houchardcd2867a2017-11-01 13:58:21 +0100186 } else {
William Lallemand10935bc2017-11-14 14:39:23 +0100187 /* Skip remaining headers to fill the cache */
188 b_adv(msg->chn->buf, st->hdrs_len);
189 ret = shctx_row_data_append(shctx,
190 st->first_block,
191 (unsigned char *)bi_ptr(msg->chn->buf),
192 MIN(bi_contig_data(msg->chn->buf), len - st->hdrs_len));
193 /* Rewind the buffer to forward all data */
194 b_rew(msg->chn->buf, st->hdrs_len);
William Lallemande1533f52017-11-14 14:39:24 +0100195 if (ret)
196 goto disable_cache;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100197 }
198 }
William Lallemand10935bc2017-11-14 14:39:23 +0100199 ret = len;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100200 }
201
202 if ((ret != len) ||
203 (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret))
204 task_wakeup(s->task, TASK_WOKEN_MSG);
205
206 return ret;
207}
208
209static int
210cache_store_http_end(struct stream *s, struct filter *filter,
211 struct http_msg *msg)
212{
213 struct cache_st *st = filter->ctx;
214 struct cache *cache = filter->config->conf;
215 struct shared_context *shctx = shctx_ptr(cache);
216 struct cache_entry *object;
217
218 if (!(msg->chn->flags & CF_ISRESP))
219 return 1;
220
221 if (st && st->first_block) {
222
223 object = (struct cache_entry *)st->first_block->data;
224
225 /* does not need to test if the insertion worked, if it
226 * doesn't, the blocks will be reused anyway */
227
228 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100229 if (eb32_insert(&cache->entries, &object->eb) != &object->eb) {
230 object->eb.key = 0;
231 }
William Lallemand4da3f8a2017-10-31 14:33:34 +0100232 /* remove from the hotlist */
William Lallemand4da3f8a2017-10-31 14:33:34 +0100233 shctx_row_dec_hot(shctx, st->first_block);
234 shctx_unlock(shctx);
235
236 }
237 if (st) {
238 pool_free2(pool2_cache_st, st);
239 filter->ctx = NULL;
240 }
241
242 return 1;
243}
244
245 /*
246 * This intends to be used when checking HTTP headers for some
247 * word=value directive. Return a pointer to the first character of value, if
248 * the word was not found or if there wasn't any value assigned ot it return NULL
249 */
250char *directive_value(const char *sample, int slen, const char *word, int wlen)
251{
252 int st = 0;
253
254 if (slen < wlen)
255 return 0;
256
257 while (wlen) {
258 char c = *sample ^ *word;
259 if (c && c != ('A' ^ 'a'))
260 return NULL;
261 sample++;
262 word++;
263 slen--;
264 wlen--;
265 }
266
267 while (slen) {
268 if (st == 0) {
269 if (*sample != '=')
270 return NULL;
271 sample++;
272 slen--;
273 st = 1;
274 continue;
275 } else {
276 return (char *)sample;
277 }
278 }
279
280 return NULL;
281}
282
283/*
284 * Return the maxage in seconds of an HTTP response.
285 * Compute the maxage using either:
286 * - the assigned max-age of the cache
287 * - the s-maxage directive
288 * - the max-age directive
289 * - (Expires - Data) headers
290 * - the default-max-age of the cache
291 *
292 */
293int http_calc_maxage(struct stream *s)
294{
295 struct http_txn *txn = s->txn;
296 struct hdr_ctx ctx;
297
298 int smaxage = -1;
299 int maxage = -1;
300
301
302 /* TODO: forced maxage configuration */
303
304 ctx.idx = 0;
305
306 /* loop on the Cache-Control values */
307 while (http_find_header2("Cache-Control", 13, s->res.buf->p, &txn->hdr_idx, &ctx)) {
308 char *directive = ctx.line + ctx.val;
309 char *value;
310
311 value = directive_value(directive, ctx.vlen, "s-maxage", 8);
312 if (value) {
313 struct chunk *chk = get_trash_chunk();
314
315 chunk_strncat(chk, value, ctx.vlen - 8 + 1);
316 chunk_strncat(chk, "", 1);
317 maxage = atoi(chk->str);
318 }
319
320 value = directive_value(ctx.line + ctx.val, ctx.vlen, "max-age", 7);
321 if (value) {
322 struct chunk *chk = get_trash_chunk();
323
324 chunk_strncat(chk, value, ctx.vlen - 7 + 1);
325 chunk_strncat(chk, "", 1);
326 smaxage = atoi(chk->str);
327 }
328 }
329
330 /* TODO: Expires - Data */
331
332
333 if (smaxage > 0)
334 return smaxage;
335
336 if (maxage > 0)
337 return maxage;
338
339 /* TODO: return default value */
340
341 return 60;
342
343}
344
345
William Lallemanda400a3a2017-11-20 19:13:12 +0100346static void cache_free_blocks(struct shared_block *first, struct shared_block *block)
347{
348 if (first == block) {
349 struct cache_entry *object = (struct cache_entry *)first->data;
William Lallemand08727662017-11-21 20:01:27 +0100350 if (object->eb.key) {
351 eb32_delete(&object->eb);
352 object->eb.key = 0;
353 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100354 }
355}
356
William Lallemand41db4602017-10-30 11:15:51 +0100357/*
358 * This fonction will store the headers of the response in a buffer and then
359 * register a filter to store the data
360 */
361enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px,
362 struct session *sess, struct stream *s, int flags)
363{
William Lallemand4da3f8a2017-10-31 14:33:34 +0100364 struct http_txn *txn = s->txn;
365 struct http_msg *msg = &txn->rsp;
366 struct filter *filter;
367 struct hdr_ctx ctx;
368 struct shared_block *first = NULL;
369 struct shared_context *shctx = shctx_ptr((struct cache *)rule->arg.act.p[0]);
370 struct cache_entry *object;
371
372
373 /* Don't cache if the response came from a cache */
374 if ((obj_type(s->target) == OBJ_TYPE_APPLET) &&
375 s->target == &http_cache_applet.obj_type) {
376 goto out;
377 }
378
379 /* cache only HTTP/1.1 */
380 if (!(txn->req.flags & HTTP_MSGF_VER_11))
381 goto out;
382
William Lallemand18f133a2017-11-08 11:25:15 +0100383 /* does not cache if Content-Length unknown */
384 if (!(msg->flags & HTTP_MSGF_CNT_LEN))
385 goto out;
386
William Lallemand4da3f8a2017-10-31 14:33:34 +0100387 /* cache only GET method */
388 if (txn->meth != HTTP_METH_GET)
389 goto out;
390
391 /* cache only 200 status code */
392 if (txn->status != 200)
393 goto out;
394
395 /* Does not manage Vary at the moment. We will need a secondary key later for that */
396 ctx.idx = 0;
397 if (http_find_header2("Vary", 4, txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx))
398 goto out;
399
400 /* we need to put this flag before using check_response_for_cacheability */
401 txn->flags |= TX_CACHEABLE;
402
403 if (txn->status != 101)
404 check_response_for_cacheability(s, &s->res);
405
406 if (!(txn->flags & TX_CACHEABLE))
407 goto out;
408
William Lallemand9d5f54d2017-11-14 14:39:22 +0100409 if ((msg->sov + msg->body_len) > (global.tune.bufsize - global.tune.maxrewrite))
William Lallemand4da3f8a2017-10-31 14:33:34 +0100410 goto out;
411
412 shctx_lock(shctx);
413
William Lallemand9d5f54d2017-11-14 14:39:22 +0100414 first = shctx_row_reserve_hot(shctx, sizeof(struct cache_entry) + msg->sov + msg->body_len);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100415 if (!first) {
416 shctx_unlock(shctx);
417 goto out;
418 }
419 shctx_unlock(shctx);
420
421 /* reserve space for the cache_entry structure */
422 first->len = sizeof(struct cache_entry);
423
424 /* cache the headers in a http action because it allows to chose what
425 * to cache, for example you might want to cache a response before
426 * modifying some HTTP headers, or on the contrary after modifying
427 * those headers.
428 */
429
430 /* does not need to be locked because it's in the "hot" list,
431 * copy the headers */
William Lallemand9d5f54d2017-11-14 14:39:22 +0100432 if (shctx_row_data_append(shctx, first, (unsigned char *)s->res.buf->p, msg->sov) < 0)
William Lallemand4da3f8a2017-10-31 14:33:34 +0100433 goto out;
434
435 /* register the buffer in the filter ctx for filling it with data*/
436 if (!LIST_ISEMPTY(&s->strm_flt.filters)) {
437 list_for_each_entry(filter, &s->strm_flt.filters, list) {
438 if (filter->config->id == cache_store_flt_id &&
439 filter->config->conf == rule->arg.act.p[0]) {
440 if (filter->ctx) {
441 struct cache_st *cache_ctx = filter->ctx;
442
443 cache_ctx->first_block = first;
444 object = (struct cache_entry *)first->data;
445
446 object->eb.key = hash_djb2(txn->uri, strlen(txn->uri));
447 /* Insert the node later on caching success */
448
449 shctx_lock(shctx);
450 if (entry_exist((struct cache *)rule->arg.act.p[0], object)) {
451 shctx_unlock(shctx);
452 if (filter->ctx) {
William Lallemand08727662017-11-21 20:01:27 +0100453 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100454 pool_free2(pool2_cache_st, filter->ctx);
455 filter->ctx = NULL;
456 }
457 goto out;
458 }
459 shctx_unlock(shctx);
460
461 /* store latest value and expiration time */
462 object->latest_validation = now.tv_sec;
463 object->expire = now.tv_sec + http_calc_maxage(s);
464
465 }
466 return ACT_RET_CONT;
467 }
468 }
469 }
470
471out:
472 /* if does not cache */
473 if (first) {
William Lallemand08727662017-11-21 20:01:27 +0100474 object = (struct cache_entry *)first->data;
475
William Lallemand4da3f8a2017-10-31 14:33:34 +0100476 shctx_lock(shctx);
William Lallemand08727662017-11-21 20:01:27 +0100477 first->len = 0;
478 object->eb.key = 0;
William Lallemand4da3f8a2017-10-31 14:33:34 +0100479 shctx_row_dec_hot(shctx, first);
480 shctx_unlock(shctx);
481 }
482
William Lallemand41db4602017-10-30 11:15:51 +0100483 return ACT_RET_CONT;
484}
485
William Lallemand77c11972017-10-31 20:43:01 +0100486#define HTTP_CACHE_INIT 0
487#define HTTP_CACHE_FWD 1
488#define HTTP_CACHE_END 2
489
490static void http_cache_io_handler(struct appctx *appctx)
491{
492 struct stream_interface *si = appctx->owner;
493 struct channel *res = si_ic(si);
494 struct cache *cache = (struct cache *)appctx->rule->arg.act.p[0];
495 struct cache_entry *cache_ptr = appctx->ctx.cache.entry;
496 struct shared_context *shctx = shctx_ptr(cache);
497 struct shared_block *first = block_ptr(cache_ptr);
498
499 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
500 goto out;
501
502 /* Check if the input buffer is avalaible. */
503 if (res->buf->size == 0) {
504 si_applet_cant_put(si);
505 goto out;
506 }
507
508 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
509 appctx->st0 = HTTP_CACHE_END;
510
511 /* buffer are aligned there, should be fine */
512 if (appctx->st0 == HTTP_CACHE_INIT) {
513 int len = first->len - sizeof(struct cache_entry);
514 if ((shctx_row_data_get(shctx, first, (unsigned char *)bi_end(res->buf), sizeof(struct cache_entry), len)) != 0) {
515 fprintf(stderr, "cache error too big: %d\n", first->len - (int)sizeof(struct cache_entry));
William Lallemand55e76742017-11-21 20:01:28 +0100516
517 shctx_lock(shctx_ptr(cache));
518 shctx_row_dec_hot(shctx_ptr(cache), first);
519 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100520 si_applet_cant_put(si);
521 goto out;
522 }
523 res->buf->i += len;
524 res->total += len;
525 appctx->st0 = HTTP_CACHE_FWD;
William Lallemand55e76742017-11-21 20:01:28 +0100526 shctx_lock(shctx_ptr(cache));
527 shctx_row_dec_hot(shctx_ptr(cache), first);
528 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100529 }
530
531 if (appctx->st0 == HTTP_CACHE_FWD) {
532 /* eat the whole request */
533 co_skip(si_oc(si), si_ob(si)->o); // NOTE: when disabled does not repport the correct status code
534 res->flags |= CF_READ_NULL;
535 si_shutr(si);
536 }
537
538 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
539 si_shutw(si);
540out:
541 ;
542}
543
William Lallemand41db4602017-10-30 11:15:51 +0100544enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy,
545 struct act_rule *rule, char **err)
546{
547 struct flt_conf *fconf;
548 int cur_arg = *orig_arg;
549 rule->action = ACT_CUSTOM;
550 rule->action_ptr = http_action_store_cache;
551
552 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
553 memprintf(err, "expects a cache name");
554 return ACT_RET_PRS_ERR;
555 }
556
557 /* check if a cache filter was already registered with this cache
558 * name, if that's the case, must use it. */
559 list_for_each_entry(fconf, &proxy->filter_configs, list) {
560 if (fconf->id == cache_store_flt_id && !strcmp((char *)fconf->conf, args[cur_arg])) {
561 rule->arg.act.p[0] = fconf->conf;
562 (*orig_arg)++;
563 /* filter already registered */
564 return ACT_RET_PRS_OK;
565 }
566 }
567
568 rule->arg.act.p[0] = strdup(args[cur_arg]);
569 if (!rule->arg.act.p[0]) {
570 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
571 err++;
572 goto err;
573 }
574 /* register a filter to fill the cache buffer */
575 fconf = calloc(1, sizeof(*fconf));
576 if (!fconf) {
577 Alert("config: %s '%s': out of memory\n",
578 proxy_type_str(proxy), proxy->id);
579 err++;
580 goto err;
581 }
582 fconf->id = cache_store_flt_id;
583 fconf->conf = rule->arg.act.p[0]; /* store the proxy name */
584 fconf->ops = &cache_ops;
585 LIST_ADDQ(&proxy->filter_configs, &fconf->list);
586
587 (*orig_arg)++;
588
589 return ACT_RET_PRS_OK;
590
591err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100592 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100593}
594
595
596enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px,
597 struct session *sess, struct stream *s, int flags)
598{
William Lallemand77c11972017-10-31 20:43:01 +0100599
600 struct cache_entry search_entry;
601 struct cache_entry *res;
602
603 struct cache *cache = (struct cache *)rule->arg.act.p[0];
604
605 search_entry.eb.key = hash_djb2(s->txn->uri, strlen(s->txn->uri));
William Lallemanda400a3a2017-11-20 19:13:12 +0100606 shctx_lock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100607 res = entry_exist(cache, &search_entry);
608 if (res) {
609 struct appctx *appctx;
William Lallemanda400a3a2017-11-20 19:13:12 +0100610 shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res));
611 shctx_unlock(shctx_ptr(cache));
William Lallemand77c11972017-10-31 20:43:01 +0100612 s->target = &http_cache_applet.obj_type;
613 if ((appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
614 appctx->st0 = HTTP_CACHE_INIT;
615 appctx->rule = rule;
616 appctx->ctx.cache.entry = res;
Olivier Houchardfccf8402017-11-01 14:04:02 +0100617 return ACT_RET_CONT;
William Lallemand77c11972017-10-31 20:43:01 +0100618 } else {
William Lallemand55e76742017-11-21 20:01:28 +0100619 shctx_lock(shctx_ptr(cache));
620 shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res));
621 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100622 return ACT_RET_YIELD;
William Lallemand77c11972017-10-31 20:43:01 +0100623 }
624 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100625 shctx_unlock(shctx_ptr(cache));
Olivier Houchardfccf8402017-11-01 14:04:02 +0100626 return ACT_RET_CONT;
William Lallemand41db4602017-10-30 11:15:51 +0100627}
628
629
630enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy,
631 struct act_rule *rule, char **err)
632{
633 int cur_arg = *orig_arg;
634
635 rule->action = ACT_CUSTOM;
636 rule->action_ptr = http_action_req_cache_use;
637
638 if (!*args[cur_arg] || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
639 memprintf(err, "expects a cache name");
640 return ACT_RET_PRS_ERR;
641 }
642
643 rule->arg.act.p[0] = strdup(args[cur_arg]);
644 if (!rule->arg.act.p[0]) {
645 Alert("config: %s '%s': out of memory\n", proxy_type_str(proxy), proxy->id);
646 err++;
647 goto err;
648 }
649
650 (*orig_arg)++;
651 return ACT_RET_PRS_OK;
652
653err:
Olivier Houchardfccf8402017-11-01 14:04:02 +0100654 return ACT_RET_PRS_ERR;
William Lallemand41db4602017-10-30 11:15:51 +0100655
656}
657
658int cfg_parse_cache(const char *file, int linenum, char **args, int kwm)
659{
660 int err_code = 0;
661
662 if (strcmp(args[0], "cache") == 0) { /* new cache section */
663
664 if (!*args[1]) {
665 Alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
666 file, linenum, args[0]);
667 err_code |= ERR_ALERT | ERR_ABORT;
668 goto out;
669 }
670
671 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
672 err_code |= ERR_ABORT;
673 goto out;
674 }
675
676 if (tmp_cache_config == NULL) {
677 tmp_cache_config = calloc(1, sizeof(*tmp_cache_config));
678 if (!tmp_cache_config) {
679 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
680 err_code |= ERR_ALERT | ERR_ABORT;
681 goto out;
682 }
683
684 strlcpy2(tmp_cache_config->id, args[1], 33);
685 if (strlen(args[1]) > 32) {
686 Warning("parsing [%s:%d]: cache id is limited to 32 characters, truncate to '%s'.\n",
687 file, linenum, tmp_cache_config->id);
688 err_code |= ERR_WARN;
689 }
690
691 tmp_cache_config->maxblocks = 0;
692 }
693 } else if (strcmp(args[0], "total-max-size") == 0) {
694 int maxsize;
695
696 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
697 err_code |= ERR_ABORT;
698 goto out;
699 }
700
701 /* size in megabytes */
702 maxsize = atoi(args[1]) * 1024 * 1024 / CACHE_BLOCKSIZE;
703 tmp_cache_config->maxblocks = maxsize;
704
705 } else if (*args[0] != 0) {
706 Alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]);
707 err_code |= ERR_ALERT | ERR_FATAL;
708 goto out;
709 }
710out:
711 return err_code;
712}
713
714/* once the cache section is parsed */
715
716int cfg_post_parse_section_cache()
717{
718 struct shared_context *shctx;
719 int err_code = 0;
720 int ret_shctx;
721
722 if (tmp_cache_config) {
723 struct cache *cache;
724
725 if (tmp_cache_config->maxblocks <= 0) {
726 Alert("Size not specified for cache '%s'\n", tmp_cache_config->id);
727 err_code |= ERR_FATAL | ERR_ALERT;
728 goto out;
729 }
730
731 ret_shctx = shctx_init(&shctx, tmp_cache_config->maxblocks, CACHE_BLOCKSIZE, sizeof(struct cache), 1);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100732
William Lallemand41db4602017-10-30 11:15:51 +0100733 if (ret_shctx < 0) {
734 if (ret_shctx == SHCTX_E_INIT_LOCK)
735 Alert("Unable to initialize the lock for the cache.\n");
736 else
737 Alert("Unable to allocate cache.\n");
738
739 err_code |= ERR_FATAL | ERR_ALERT;
740 goto out;
741 }
William Lallemanda400a3a2017-11-20 19:13:12 +0100742 shctx->free_block = cache_free_blocks;
William Lallemand41db4602017-10-30 11:15:51 +0100743 memcpy(shctx->data, tmp_cache_config, sizeof(struct cache));
744 cache = (struct cache *)shctx->data;
745 cache->entries = EB_ROOT_UNIQUE;
William Lallemand41db4602017-10-30 11:15:51 +0100746 LIST_ADDQ(&caches, &cache->list);
747 }
748out:
749 free(tmp_cache_config);
750 tmp_cache_config = NULL;
751 return err_code;
752
753}
754
755/*
756 * Resolve the cache name to a pointer once the file is completely read.
757 */
758int cfg_cache_postparser()
759{
760 struct act_rule *hresrule, *hrqrule;
761 void *cache_ptr;
762 struct cache *cache;
763 struct proxy *curproxy = NULL;
764 int err = 0;
765 struct flt_conf *fconf;
766
767 for (curproxy = proxy; curproxy; curproxy = curproxy->next) {
768
769 /* resolve the http response cache name to a ptr in the action rule */
770 list_for_each_entry(hresrule, &curproxy->http_res_rules, list) {
771 if (hresrule->action != ACT_CUSTOM ||
772 hresrule->action_ptr != http_action_store_cache)
773 continue;
774
775 cache_ptr = hresrule->arg.act.p[0];
776
777 list_for_each_entry(cache, &caches, list) {
778 if (!strcmp(cache->id, cache_ptr)) {
779 /* don't free there, it's still used in the filter conf */
780 cache_ptr = cache;
781 break;
782 }
783 }
784
785 if (cache_ptr == hresrule->arg.act.p[0]) {
786 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-response cache-store rule.\n",
787 curproxy->id, (char *)hresrule->arg.act.p[0]);
788 err++;
789 }
790
791 hresrule->arg.act.p[0] = cache_ptr;
792 }
793
794 /* resolve the http request cache name to a ptr in the action rule */
795 list_for_each_entry(hrqrule, &curproxy->http_req_rules, list) {
796 if (hrqrule->action != ACT_CUSTOM ||
797 hrqrule->action_ptr != http_action_req_cache_use)
798 continue;
799
800 cache_ptr = hrqrule->arg.act.p[0];
801
802 list_for_each_entry(cache, &caches, list) {
803 if (!strcmp(cache->id, cache_ptr)) {
804 free(cache_ptr);
805 cache_ptr = cache;
806 break;
807 }
808 }
809
810 if (cache_ptr == hrqrule->arg.act.p[0]) {
811 Alert("Proxy '%s': unable to find the cache '%s' referenced by http-request cache-use rule.\n",
812 curproxy->id, (char *)hrqrule->arg.act.p[0]);
813 err++;
814 }
815
816 hrqrule->arg.act.p[0] = cache_ptr;
817 }
818
819 /* resolve the cache name to a ptr in the filter config */
820 list_for_each_entry(fconf, &curproxy->filter_configs, list) {
821
William Lallemand9c54c532017-11-02 16:38:42 +0100822 if (fconf->id != cache_store_flt_id)
823 continue;
824
William Lallemand41db4602017-10-30 11:15:51 +0100825 cache_ptr = fconf->conf;
826
827 list_for_each_entry(cache, &caches, list) {
828 if (!strcmp(cache->id, cache_ptr)) {
829 /* there can be only one filter per cache, so we free it there */
830 free(cache_ptr);
831 cache_ptr = cache;
832 break;
833 }
834 }
835
836 if (cache_ptr == fconf->conf) {
837 Alert("Proxy '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n",
838 curproxy->id, (char *)fconf->conf);
839 err++;
840 }
841 fconf->conf = cache_ptr;
842 }
843 }
844 return err;
845}
846
847
848struct flt_ops cache_ops = {
849 .init = cache_store_init,
850
William Lallemand4da3f8a2017-10-31 14:33:34 +0100851 /* Handle channels activity */
852 .channel_start_analyze = cache_store_chn_start_analyze,
853
854 /* Filter HTTP requests and responses */
855 .http_headers = cache_store_http_headers,
856 .http_end = cache_store_http_end,
857
858 .http_forward_data = cache_store_http_forward_data,
859
William Lallemand41db4602017-10-30 11:15:51 +0100860};
861
William Lallemand1f49a362017-11-21 20:01:26 +0100862static int cli_parse_show_cache(char **args, struct appctx *appctx, void *private)
863{
864 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
865 return 1;
866
867 return 0;
868}
869
870static int cli_io_handler_show_cache(struct appctx *appctx)
871{
872 struct cache* cache = appctx->ctx.cli.p0;
873 struct stream_interface *si = appctx->owner;
874
875 chunk_reset(&trash);
876
877
878 if (cache == NULL) {
879 cache = LIST_ELEM((caches).n, typeof(struct cache *), list);
880 }
881
882 list_for_each_entry_from(cache, &caches, list) {
883 struct eb32_node *node = NULL;
884 unsigned int next_key;
885 struct cache_entry *entry;
886
887 chunk_appendf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav);
888
889 next_key = appctx->ctx.cli.i0;
890
891 appctx->ctx.cli.p0 = cache;
892
893 while (1) {
894
895 shctx_lock(shctx_ptr(cache));
896 node = eb32_lookup_ge(&cache->entries, next_key);
897 if (!node) {
898 shctx_unlock(shctx_ptr(cache));
899 break;
900 }
901
902 entry = container_of(node, struct cache_entry, eb);
903 chunk_appendf(&trash, "%p (size: %u (%u blocks), refcount:%u, expire: %d)\n", entry, block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec);
904
905 next_key = node->key + 1;
906 appctx->ctx.cli.i0 = next_key;
907
908 shctx_unlock(shctx_ptr(cache));
909
910 if (ci_putchk(si_ic(si), &trash) == -1) {
911 si_applet_cant_put(si);
912 return 0;
913 }
914 }
915
916 }
917
918 return 1;
919
920}
921
922static struct cli_kw_list cli_kws = {{},{
William Lallemande899af82017-11-22 16:41:26 +0100923 { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL },
924 {{},}
William Lallemand1f49a362017-11-21 20:01:26 +0100925}};
926
927
William Lallemand41db4602017-10-30 11:15:51 +0100928static struct action_kw_list http_res_actions = {
929 .kw = {
930 { "cache-store", parse_cache_store },
931 { NULL, NULL }
932 }
933};
934
935static struct action_kw_list http_req_actions = {
936 .kw = {
937 { "cache-use", parse_cache_use },
938 { NULL, NULL }
939 }
940};
941
942struct applet http_cache_applet = {
943 .obj_type = OBJ_TYPE_APPLET,
944 .name = "<CACHE>", /* used for logging */
William Lallemand77c11972017-10-31 20:43:01 +0100945 .fct = http_cache_io_handler,
William Lallemand41db4602017-10-30 11:15:51 +0100946 .release = NULL,
947};
948
949__attribute__((constructor))
950static void __cache_init(void)
951{
952 cfg_register_section("cache", cfg_parse_cache, cfg_post_parse_section_cache);
953 cfg_register_postparser("cache", cfg_cache_postparser);
William Lallemand1f49a362017-11-21 20:01:26 +0100954 cli_register_kw(&cli_kws);
William Lallemand41db4602017-10-30 11:15:51 +0100955 http_res_keywords_register(&http_res_actions);
956 http_req_keywords_register(&http_req_actions);
William Lallemand4da3f8a2017-10-31 14:33:34 +0100957 pool2_cache_st = create_pool("cache_st", sizeof(struct cache_st), MEM_F_SHARED);
William Lallemand41db4602017-10-30 11:15:51 +0100958}
959