2 * Copyright (c) 2022 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <http_static/http_cache.h>
17 #include <vppinfra/bihash_template.c>
18 #include <vppinfra/unix.h>
19 #include <vlib/vlib.h>
22 hss_cache_lock (hss_cache_t *hc)
24 clib_spinlock_lock (&hc->cache_lock);
28 hss_cache_unlock (hss_cache_t *hc)
30 clib_spinlock_unlock (&hc->cache_lock);
33 /** \brief Sanity-check the forward and reverse LRU lists
36 lru_validate (hss_cache_t *hc)
42 hss_cache_entry_t *ce;
44 last_timestamp = 1e70;
45 for (i = 1, index = hc->first_index; index != ~0;)
47 ce = pool_elt_at_index (hc->cache_pool, index);
48 /* Timestamps should be smaller (older) as we walk the fwd list */
49 if (ce->last_used > last_timestamp)
51 clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
52 i, ce->last_used, last_timestamp);
54 index = ce->next_index;
55 last_timestamp = ce->last_used;
60 for (i = 1, index = hc->last_index; index != ~0;)
62 ce = pool_elt_at_index (hc->cache_pool, index);
63 /* Timestamps should be larger (newer) as we walk the rev list */
64 if (ce->last_used < last_timestamp)
66 clib_warning ("%d[%d]: last used %.6f, last_timestamp %.6f", index,
67 i, ce->last_used, last_timestamp);
69 index = ce->prev_index;
70 last_timestamp = ce->last_used;
76 /** \brief Remove a data cache entry from the LRU lists
79 lru_remove (hss_cache_t *hc, hss_cache_entry_t *ce)
81 hss_cache_entry_t *next_ep, *prev_ep;
86 ce_index = ce - hc->cache_pool;
88 /* Deal with list heads */
89 if (ce_index == hc->first_index)
90 hc->first_index = ce->next_index;
91 if (ce_index == hc->last_index)
92 hc->last_index = ce->prev_index;
95 if (ce->next_index != ~0)
97 next_ep = pool_elt_at_index (hc->cache_pool, ce->next_index);
98 next_ep->prev_index = ce->prev_index;
101 if (ce->prev_index != ~0)
103 prev_ep = pool_elt_at_index (hc->cache_pool, ce->prev_index);
104 prev_ep->next_index = ce->next_index;
109 /** \brief Add an entry to the LRU lists, tag w/ supplied timestamp
112 lru_add (hss_cache_t *hc, hss_cache_entry_t *ce, f64 now)
114 hss_cache_entry_t *next_ce;
119 ce_index = ce - hc->cache_pool;
122 * Re-add at the head of the forward LRU list,
123 * tail of the reverse LRU list
125 if (hc->first_index != ~0)
127 next_ce = pool_elt_at_index (hc->cache_pool, hc->first_index);
128 next_ce->prev_index = ce_index;
133 /* ep now the new head of the LRU forward list */
134 ce->next_index = hc->first_index;
135 hc->first_index = ce_index;
137 /* single session case: also the tail of the reverse LRU list */
138 if (hc->last_index == ~0)
139 hc->last_index = ce_index;
145 /** \brief Remove and re-add a cache entry from/to the LRU lists
148 lru_update (hss_cache_t *hc, hss_cache_entry_t *ep, f64 now)
151 lru_add (hc, ep, now);
155 hss_cache_attach_entry (hss_cache_t *hc, u32 ce_index, u8 **data,
158 hss_cache_entry_t *ce;
160 /* Expect ce_index to be validated outside */
161 ce = pool_elt_at_index (hc->cache_pool, ce_index);
164 *data_len = vec_len (ce->data);
166 /* Update the cache entry, mark it in-use */
167 lru_update (hc, ce, vlib_time_now (vlib_get_main ()));
169 if (hc->debug_level > 1)
170 clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
173 /** \brief Detach cache entry from session
176 hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index)
178 hss_cache_entry_t *ce;
182 ce = pool_elt_at_index (hc->cache_pool, ce_index);
185 if (hc->debug_level > 1)
186 clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
188 hss_cache_unlock (hc);
192 hss_cache_lookup (hss_cache_t *hc, u8 *path)
194 BVT (clib_bihash_kv) kv;
200 /* Value updated only if lookup succeeds */
201 rv = BV (clib_bihash_search) (&hc->name_to_data, &kv, &kv);
202 ASSERT (!rv || kv.value == ~0);
204 if (hc->debug_level > 1)
205 clib_warning ("lookup '%s' %s", kv.key, kv.value == ~0 ? "fail" : "found");
211 hss_cache_lookup_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
216 /* Make sure nobody removes the entry while we look it up */
219 ce_index = hss_cache_lookup (hc, path);
221 hss_cache_attach_entry (hc, ce_index, data, data_len);
223 hss_cache_unlock (hc);
229 hss_cache_do_evictions (hss_cache_t *hc)
231 BVT (clib_bihash_kv) kv;
232 hss_cache_entry_t *ce;
235 free_index = hc->last_index;
237 while (free_index != ~0)
240 ce = pool_elt_at_index (hc->cache_pool, free_index);
241 /* Which could be in use... */
244 if (hc->debug_level > 1)
245 clib_warning ("index %d in use refcnt %d", free_index, ce->inuse);
247 free_index = ce->prev_index;
248 kv.key = (u64) (ce->filename);
250 if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
253 clib_warning ("LRU delete '%s' FAILED!", ce->filename);
255 else if (hc->debug_level > 1)
256 clib_warning ("LRU delete '%s' ok", ce->filename);
259 hc->cache_size -= vec_len (ce->data);
260 hc->cache_evictions++;
261 vec_free (ce->filename);
264 if (hc->debug_level > 1)
265 clib_warning ("pool put index %d", ce - hc->cache_pool);
267 pool_put (hc->cache_pool, ce);
268 if (hc->cache_size < hc->cache_limit)
274 hss_cache_add_and_attach (hss_cache_t *hc, u8 *path, u8 **data, u64 *data_len)
276 BVT (clib_bihash_kv) kv;
277 hss_cache_entry_t *ce;
284 /* Need to recycle one (or more cache) entries? */
285 if (hc->cache_size > hc->cache_limit)
286 hss_cache_do_evictions (hc);
289 error = clib_file_contents ((char *) path, &file_data);
292 clib_warning ("Error reading '%s'", path);
293 clib_error_report (error);
297 /* Create a cache entry for it */
298 pool_get_zero (hc->cache_pool, ce);
299 ce->filename = vec_dup (path);
300 ce->data = file_data;
302 /* Attach cache entry without additional lock */
305 *data_len = vec_len (file_data);
306 lru_add (hc, ce, vlib_time_now (vlib_get_main ()));
308 hc->cache_size += vec_len (ce->data);
309 ce_index = ce - hc->cache_pool;
311 if (hc->debug_level > 1)
312 clib_warning ("index %d refcnt now %d", ce_index, ce->inuse);
314 /* Add to the lookup table */
316 kv.key = (u64) vec_dup (path);
319 if (hc->debug_level > 1)
320 clib_warning ("add '%s' value %lld", kv.key, kv.value);
322 if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 1 /* is_add */) < 0)
324 clib_warning ("BUG: add failed!");
327 hss_cache_unlock (hc);
333 hss_cache_clear (hss_cache_t *hc)
335 u32 free_index, busy_items = 0;
336 hss_cache_entry_t *ce;
337 BVT (clib_bihash_kv) kv;
341 /* Walk the LRU list to find active entries */
342 free_index = hc->last_index;
343 while (free_index != ~0)
345 ce = pool_elt_at_index (hc->cache_pool, free_index);
346 free_index = ce->prev_index;
347 /* Which could be in use... */
351 free_index = ce->next_index;
354 kv.key = (u64) (ce->filename);
356 if (BV (clib_bihash_add_del) (&hc->name_to_data, &kv, 0 /* is_add */) <
359 clib_warning ("BUG: cache clear delete '%s' FAILED!", ce->filename);
363 hc->cache_size -= vec_len (ce->data);
364 hc->cache_evictions++;
365 vec_free (ce->filename);
367 if (hc->debug_level > 1)
368 clib_warning ("pool put index %d", ce - hc->cache_pool);
369 pool_put (hc->cache_pool, ce);
370 free_index = hc->last_index;
373 hss_cache_unlock (hc);
379 hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level)
381 clib_spinlock_init (&hc->cache_lock);
383 /* Init path-to-cache hash table */
384 BV (clib_bihash_init) (&hc->name_to_data, "http cache", 128, 32 << 20);
386 hc->cache_limit = cache_size;
387 hc->debug_level = debug_level;
388 hc->first_index = hc->last_index = ~0;
391 /** \brief format a file cache entry
394 format_hss_cache_entry (u8 *s, va_list *args)
396 hss_cache_entry_t *ep = va_arg (*args, hss_cache_entry_t *);
397 f64 now = va_arg (*args, f64);
402 s = format (s, "%40s%12s%20s", "File", "Size", "Age");
405 s = format (s, "%40s%12lld%20.2f", ep->filename, vec_len (ep->data),
406 now - ep->last_used);
411 format_hss_cache (u8 *s, va_list *args)
413 hss_cache_t *hc = va_arg (*args, hss_cache_t *);
414 u32 verbose = va_arg (*args, u32);
415 hss_cache_entry_t *ce;
422 s = format (s, "cache size %lld bytes, limit %lld bytes, evictions %lld",
423 hc->cache_size, hc->cache_limit, hc->cache_evictions);
427 vm = vlib_get_main ();
428 now = vlib_time_now (vm);
430 s = format (s, "%U", format_hss_cache_entry, 0 /* header */, now);
432 for (index = hc->first_index; index != ~0;)
434 ce = pool_elt_at_index (hc->cache_pool, index);
435 index = ce->next_index;
436 s = format (s, "%U", format_hss_cache_entry, ce, now);
439 s = format (s, "%40s%12lld", "Total Size", hc->cache_size);
445 * fd.io coding-style-patch-verification: ON
448 * eval: (c-set-style "gnu")