summaryrefslogtreecommitdiff
path: root/include/cache.h
blob: 371170d2a4c0d83470f5b7e9850d068d4dd4e7de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef _CACHE_H_
#define _CACHE_H_

#include <stdint.h>
#include <stddef.h>
#include "hash.h"

/* cache features */
enum {
	NO_FEATURES = 0,

	TIMER_FEATURE = 0,
	TIMER = (1 << TIMER_FEATURE),

	WRITE_THROUGH_FEATURE = 1,
	WRITE_THROUGH = (1 << WRITE_THROUGH_FEATURE),

	__CACHE_MAX_FEATURE
};
#define CACHE_MAX_FEATURE __CACHE_MAX_FEATURE

enum {
	C_OBJ_NONE = 0,		/* not in the cache */
	C_OBJ_NEW,		/* just added to the cache */
	C_OBJ_ALIVE,		/* in the cache, alive */
	C_OBJ_DEAD		/* still in the cache, but dead */
};

struct cache;
struct cache_object {
	struct	hashtable_node hashnode;
	struct	nf_conntrack *ct;
	struct	cache *cache;
	int	status;
	int	refcnt;
	long	lifetime;
	long	lastupdate;
	char	data[0];
};

struct cache_feature {
	size_t size;
	void (*add)(struct cache_object *obj, void *data);
	void (*update)(struct cache_object *obj, void *data);
	void (*destroy)(struct cache_object *obj, void *data);
	int  (*dump)(struct cache_object *obj, void *data, char *buf, int type);
};

extern struct cache_feature lifetime_feature;
extern struct cache_feature timer_feature;
extern struct cache_feature writethrough_feature;

#define CACHE_MAX_NAMELEN 32

struct cache {
	char name[CACHE_MAX_NAMELEN];
	struct hashtable *h;

	unsigned int num_features;
	struct cache_feature **features;
	unsigned int feature_type[CACHE_MAX_FEATURE];
	unsigned int *feature_offset;
	struct cache_extra *extra;
	unsigned int extra_offset;
	size_t object_size;

        /* statistics */
	struct {
		uint32_t	active;
	
		uint32_t	add_ok;
		uint32_t	del_ok;
		uint32_t	upd_ok;
		
		uint32_t	add_fail;
		uint32_t	del_fail;
		uint32_t	upd_fail;

		uint32_t	add_fail_enomem;
		uint32_t	add_fail_enospc;
		uint32_t	del_fail_enoent;
		uint32_t	upd_fail_enoent;

		uint32_t	commit_ok;
		uint32_t	commit_fail;

		uint32_t	flush;

		uint32_t	objects;
	} stats;
};

struct cache_extra {
	unsigned int size;

	void (*add)(struct cache_object *obj, void *data);
	void (*update)(struct cache_object *obj, void *data);
	void (*destroy)(struct cache_object *obj, void *data);
};

struct nf_conntrack;

struct cache *cache_create(const char *name, unsigned int features, struct cache_extra *extra);
void cache_destroy(struct cache *e);

struct cache_object *cache_object_new(struct cache *c, struct nf_conntrack *ct);
void cache_object_free(struct cache_object *obj);
void cache_object_get(struct cache_object *obj);
int cache_object_put(struct cache_object *obj);
void cache_object_set_status(struct cache_object *obj, int status);

int cache_add(struct cache *c, struct cache_object *obj, int id);
void cache_update(struct cache *c, struct cache_object *obj, int id, struct nf_conntrack *ct);
struct cache_object *cache_update_force(struct cache *c, struct nf_conntrack *ct);
void cache_del(struct cache *c, struct cache_object *obj);
struct cache_object *cache_find(struct cache *c, struct nf_conntrack *ct, int *pos);
void cache_stats(const struct cache *c, int fd);
void cache_stats_extended(const struct cache *c, int fd);
struct cache_object *cache_data_get_object(struct cache *c, void *data);
void *cache_get_extra(struct cache *, void *);
void cache_iterate(struct cache *c, void *data, int (*iterate)(void *data1, void *data2));

/* iterators */
void cache_dump(struct cache *c, int fd, int type);
void cache_commit(struct cache *c);
void cache_flush(struct cache *c);
void cache_bulk(struct cache *c);

#endif