1#ifndef _CACHE_H_ 2#define _CACHE_H_ 3 4#include <stdint.h> 5#include <stddef.h> 6#include "hash.h" 7#include "date.h" 8 9/* cache features */ 10enum { 11 NO_FEATURES = 0, 12 13 TIMER_FEATURE = 0, 14 TIMER = (1 << TIMER_FEATURE), 15 16 __CACHE_MAX_FEATURE 17}; 18#define CACHE_MAX_FEATURE __CACHE_MAX_FEATURE 19 20enum { 21 C_OBJ_NONE = 0, /* not in the cache */ 22 C_OBJ_NEW, /* just added to the cache */ 23 C_OBJ_ALIVE, /* in the cache, alive */ 24 C_OBJ_DEAD, /* still in the cache, but dead */ 25 C_OBJ_MAX 26}; 27 28struct cache; 29struct cache_object { 30 struct hashtable_node hashnode; 31 void *ptr; 32 struct cache *cache; 33 int status; 34 int refcnt; 35 long lifetime; 36 long lastupdate; 37 char data[0]; 38}; 39 40struct cache_feature { 41 size_t size; 42 void (*add)(struct cache_object *obj, void *data); 43 void (*update)(struct cache_object *obj, void *data); 44 void (*destroy)(struct cache_object *obj, void *data); 45 int (*dump)(struct cache_object *obj, void *data, char *buf, int type); 46}; 47 48extern struct cache_feature timer_feature; 49 50#define CACHE_MAX_NAMELEN 32 51 52enum cache_type { 53 CACHE_T_NONE = 0, 54 CACHE_T_CT, 55 CACHE_T_EXP, 56 CACHE_T_MAX 57}; 58 59struct cache { 60 char name[CACHE_MAX_NAMELEN]; 61 enum cache_type type; 62 struct hashtable *h; 63 64 unsigned int num_features; 65 struct cache_feature **features; 66 unsigned int feature_type[CACHE_MAX_FEATURE]; 67 unsigned int *feature_offset; 68 struct cache_ops *ops; 69 struct cache_extra *extra; 70 unsigned int extra_offset; 71 size_t object_size; 72 73 /* statistics */ 74 struct { 75 uint32_t active; 76 77 uint32_t add_ok; 78 uint32_t del_ok; 79 uint32_t upd_ok; 80 81 uint32_t add_fail; 82 uint32_t del_fail; 83 uint32_t upd_fail; 84 85 uint32_t add_fail_enomem; 86 uint32_t add_fail_enospc; 87 uint32_t del_fail_enoent; 88 uint32_t upd_fail_enoent; 89 90 uint32_t commit_ok; 91 uint32_t commit_fail; 92 93 uint32_t flush; 94 95 uint32_t objects; 96 } stats; 97}; 98 99struct cache_extra { 100 unsigned int size; 101 102 void (*add)(struct cache_object *obj, void *data); 103 void (*update)(struct cache_object *obj, void *data); 104 void (*destroy)(struct cache_object *obj, void *data); 105}; 106 107struct nfct_handle; 108 109/* cache options depends on the object type: conntrack or expectation. */ 110struct cache_ops { 111 /* hashing and comparison of objects. */ 112 uint32_t (*hash)(const void *data, const struct hashtable *table); 113 int (*cmp)(const void *data1, const void *data2); 114 115 /* object allocation, copy and release. */ 116 void *(*alloc)(void); 117 void (*copy)(void *dst, void *src, unsigned int flags); 118 void (*free)(void *ptr); 119 120 /* dump and commit. */ 121 int (*dump_step)(void *data1, void *n); 122 int (*commit)(struct cache *c, struct nfct_handle *h, int clientfd); 123 124 /* build network message from object. */ 125 struct nethdr *(*build_msg)(const struct cache_object *obj, int type); 126}; 127 128/* templates to configure conntrack caching. */ 129extern struct cache_ops cache_sync_internal_ct_ops; 130extern struct cache_ops cache_sync_external_ct_ops; 131extern struct cache_ops cache_stats_ct_ops; 132/* templates to configure expectation caching. */ 133extern struct cache_ops cache_sync_internal_exp_ops; 134extern struct cache_ops cache_sync_external_exp_ops; 135 136struct nf_conntrack; 137 138struct cache *cache_create(const char *name, enum cache_type type, unsigned int features, struct cache_extra *extra, struct cache_ops *ops); 139void cache_destroy(struct cache *e); 140 141struct cache_object *cache_object_new(struct cache *c, void *ptr); 142void cache_object_free(struct cache_object *obj); 143void cache_object_get(struct cache_object *obj); 144int cache_object_put(struct cache_object *obj); 145void cache_object_set_status(struct cache_object *obj, int status); 146 147int cache_add(struct cache *c, struct cache_object *obj, int id); 148void cache_update(struct cache *c, struct cache_object *obj, int id, void *ptr); 149struct cache_object *cache_update_force(struct cache *c, void *ptr); 150void cache_del(struct cache *c, struct cache_object *obj); 151struct cache_object *cache_find(struct cache *c, void *ptr, int *pos); 152void cache_stats(const struct cache *c, int fd); 153void cache_stats_extended(const struct cache *c, int fd); 154void *cache_get_extra(struct cache_object *); 155void cache_iterate(struct cache *c, void *data, int (*iterate)(void *data1, void *data2)); 156void cache_iterate_limit(struct cache *c, void *data, uint32_t from, uint32_t steps, int (*iterate)(void *data1, void *data2)); 157 158/* iterators */ 159struct nfct_handle; 160 161struct __dump_container { 162 int fd; 163 int type; 164}; 165 166void cache_dump(struct cache *c, int fd, int type); 167 168struct __commit_container { 169 struct nfct_handle *h; 170 struct cache *c; 171}; 172 173int cache_commit(struct cache *c, struct nfct_handle *h, int clientfd); 174void cache_flush(struct cache *c); 175void cache_bulk(struct cache *c); 176 177#endif 178