api.h 4.29 KB
Newer Older
1
/*  Copyright (C) 2014-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2

Marek Vavruša's avatar
Marek Vavruša committed
3 4 5 6
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
7

Marek Vavruša's avatar
Marek Vavruša committed
8 9 10 11
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
12

Marek Vavruša's avatar
Marek Vavruša committed
13
    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
Marek Vavruša's avatar
Marek Vavruša committed
15
 */
16 17 18

#pragma once

Vladimír Čunát's avatar
.  
Vladimír Čunát committed
19
#include <libknot/consts.h>
20
#include <libknot/rrset.h>
Vladimír Čunát's avatar
Vladimír Čunát committed
21
#include <sys/time.h>
22
#include "lib/cache/cdb_api.h"
23
#include "lib/defines.h"
24
#include "contrib/ucw/config.h" /*uint*/
25

26 27 28
/** When knot_pkt is passed from cache without ->wire, this is the ->size. */
static const size_t PKT_SIZE_NOWIRE = -1;

Vladimír Čunát's avatar
.  
Vladimír Čunát committed
29

Vladimír Čunát's avatar
Vladimír Čunát committed
30
#include "lib/module.h"
31 32 33
/* Prototypes for the 'cache' module implementation. */
int cache_peek(kr_layer_t *ctx, knot_pkt_t *pkt);
int cache_stash(kr_layer_t *ctx, knot_pkt_t *pkt);
34 35


36 37 38 39 40
/**
 * Cache structure, keeps API, instance and metadata.
 */
struct kr_cache
{
41
	knot_db_t *db;		      /**< Storage instance */
42
	const struct kr_cdb_api *api; /**< Storage engine */
43 44 45 46 47 48
	struct {
		uint32_t hit;         /**< Number of cache hits */
		uint32_t miss;        /**< Number of cache misses */
		uint32_t insert;      /**< Number of insertions */
		uint32_t delete;      /**< Number of deletions */
	} stats;
49

50
	uint32_t ttl_min, ttl_max; /**< TTL limits */
51 52 53 54

	/* A pair of stamps for detection of real-time shifts during runtime. */
	struct timeval checkpoint_walltime; /**< Wall time on the last check-point. */
	uint64_t checkpoint_monotime; /**< Monotonic milliseconds on the last check-point. */
55 56
};

57
/**
58
 * Open/create cache with provided storage options.
59
 * @param cache cache structure to be initialized
60 61 62
 * @param api   storage engine API
 * @param opts  storage-specific options (may be NULL for default)
 * @param mm    memory context.
63
 * @return 0 or an error code
64
 */
65
KR_EXPORT
66
int kr_cache_open(struct kr_cache *cache, const struct kr_cdb_api *api, struct kr_cdb_opts *opts, knot_mm_t *mm);
67

68 69 70
/**
 * Close persistent cache.
 * @note This doesn't clear the data, just closes the connection to the database.
71
 * @param cache structure
72
 */
73
KR_EXPORT
74
void kr_cache_close(struct kr_cache *cache);
75

76
/** Run after a row of operations to release transaction/lock if needed. */
77
KR_EXPORT
78
int kr_cache_sync(struct kr_cache *cache);
79

80
/**
81
 * Return true if cache is open and enabled.
82
 */
83 84 85 86
static inline bool kr_cache_is_open(struct kr_cache *cache)
{
	return cache->db != NULL;
}
87

88 89 90 91 92 93 94
/** (Re)set the time pair to the current values. */
static inline void kr_cache_make_checkpoint(struct kr_cache *cache)
{
	cache->checkpoint_monotime = kr_now();
	gettimeofday(&cache->checkpoint_walltime, NULL);
}

95 96 97 98 99 100 101 102 103 104 105
/**
 * Insert RRSet into cache, replacing any existing data.
 * @param cache cache structure
 * @param rr inserted RRSet
 * @param rrsig RRSIG for inserted RRSet (optional)
 * @param rank rank of the data
 * @param timestamp current time
 * @return 0 or an errcode
 */
KR_EXPORT
int kr_cache_insert_rr(struct kr_cache *cache, const knot_rrset_t *rr, const knot_rrset_t *rrsig, uint8_t rank, uint32_t timestamp);
106

107 108
/**
 * Clear all items from the cache.
109
 * @param cache cache structure
110 111
 * @return 0 or an errcode
 */
112
KR_EXPORT
113
int kr_cache_clear(struct kr_cache *cache);
Vladimír Čunát's avatar
.  
Vladimír Čunát committed
114 115 116 117 118 119 120 121 122


/* ** This interface is temporary. ** */

struct kr_cache_p {
	uint32_t time;	/**< The time of inception. */
	uint32_t ttl;	/**< TTL at inception moment.  Assuming it fits into int32_t ATM. */
	uint8_t  rank;	/**< See enum kr_rank */
	struct {
Vladimír Čunát's avatar
.  
Vladimír Čunát committed
123 124
		/* internal: pointer to eh struct */
		void *raw_data, *raw_bound;
Vladimír Čunát's avatar
.  
Vladimír Čunát committed
125 126 127 128 129
	};
};
KR_EXPORT
int kr_cache_peek_exact(struct kr_cache *cache, const knot_dname_t *name, uint16_t type,
			struct kr_cache_p *peek);
130
/* Parameters (qry, name, type) are used for timestamp and stale-serving decisions. */
Vladimír Čunát's avatar
.  
Vladimír Čunát committed
131
KR_EXPORT
132 133
int32_t kr_cache_ttl(const struct kr_cache_p *peek, const struct kr_query *qry,
		     const knot_dname_t *name, uint16_t type);
Vladimír Čunát's avatar
.  
Vladimír Čunát committed
134 135 136
/*TODO: reorder*/
KR_EXPORT
int kr_cache_materialize(knot_rdataset_t *dst, const struct kr_cache_p *ref,
137
			 knot_mm_t *pool);
Vladimír Čunát's avatar
.  
Vladimír Čunát committed
138 139