http_static: cleanup file handler and cache
[vpp.git] / src / plugins / http_static / http_cache.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef SRC_PLUGINS_HTTP_STATIC_HTTP_CACHE_H_
17 #define SRC_PLUGINS_HTTP_STATIC_HTTP_CACHE_H_
18
19 #include <vppinfra/bihash_vec8_8.h>
20
21 typedef struct hss_cache_entry_
22 {
23   /** Name of the file */
24   u8 *filename;
25   /** Contents of the file, as a u8 * vector */
26   u8 *data;
27   /** Last time the cache entry was used */
28   f64 last_used;
29   /** Cache LRU links */
30   u32 next_index;
31   u32 prev_index;
32   /** Reference count, so we don't recycle while referenced */
33   int inuse;
34 } hss_cache_entry_t;
35
36 typedef struct hss_cache_
37 {
38   /** Unified file data cache pool */
39   hss_cache_entry_t *cache_pool;
40   /** Hash table which maps file name to file data */
41   BVT (clib_bihash) name_to_data;
42
43   /** Session pool lock */
44   clib_spinlock_t cache_lock;
45
46   /** Current cache size */
47   u64 cache_size;
48   /** Max cache size in bytes */
49   u64 cache_limit;
50   /** Number of cache evictions */
51   u64 cache_evictions;
52
53   /** Cache LRU listheads */
54   u32 first_index;
55   u32 last_index;
56
57   u8 debug_level;
58 } hss_cache_t;
59
60 u32 hss_cache_lookup_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
61                                  u64 *data_len);
62 u32 hss_cache_add_and_attach (hss_cache_t *hc, u8 *path, u8 **data,
63                               u64 *data_len);
64 void hss_cache_detach_entry (hss_cache_t *hc, u32 ce_index);
65 u32 hss_cache_clear (hss_cache_t *hc);
66 void hss_cache_init (hss_cache_t *hc, uword cache_size, u8 debug_level);
67
68 u8 *format_hss_cache (u8 *s, va_list *args);
69
70 #endif /* SRC_PLUGINS_HTTP_STATIC_HTTP_CACHE_H_ */
71
72 /*
73  * fd.io coding-style-patch-verification: ON
74  *
75  * Local Variables:
76  * eval: (c-set-style "gnu")
77  * End:
78  */