Commit 4a6f25ab authored by Marek Vavruša's avatar Marek Vavruša

lib/resolve: use begin layer for resolution start

this provides a useful callback for per-request operations that can’t wait until the query is completed (e.g. blocking or logging started queries)
parent 9cd8ce56
......@@ -351,7 +351,9 @@ static int finish(knot_layer_t *ctx) { return KNOT_STATE_NOOP; }
/* Set resolution context and parameters. */
static int begin(knot_layer_t *ctx, void *module_param)
{
ctx->data = module_param;
if (ctx->state & (KNOT_STATE_DONE|KNOT_STATE_FAIL)) {
return ctx->state;
}
return reset(ctx);
}
......
......@@ -31,12 +31,6 @@ static inline uint8_t get_tag(knot_pkt_t *pkt)
return knot_pkt_has_dnssec(pkt) ? KR_CACHE_SEC : KR_CACHE_PKT;
}
static int begin(knot_layer_t *ctx, void *module_param)
{
ctx->data = module_param;
return ctx->state;
}
static uint32_t limit_ttl(uint32_t ttl)
{
/* @todo Configurable limit */
......@@ -217,7 +211,6 @@ static int stash(knot_layer_t *ctx, knot_pkt_t *pkt)
const knot_layer_api_t *pktcache_layer(struct kr_module *module)
{
static const knot_layer_api_t _layer = {
.begin = &begin,
.produce = &peek,
.consume = &stash
};
......
......@@ -28,12 +28,6 @@
#define DEBUG_MSG(fmt...) QRDEBUG(kr_rplan_current(rplan), " rc ", fmt)
#define DEFAULT_MINTTL (5) /* Short-time "no data" retention to avoid bursts */
static int begin(knot_layer_t *ctx, void *module_param)
{
ctx->data = module_param;
return ctx->state;
}
/** Record is expiring if it has less than 1% TTL (or less than 5s) */
static inline bool is_expiring(const knot_rrset_t *rr, uint32_t drift)
{
......@@ -312,7 +306,6 @@ static int stash(knot_layer_t *ctx, knot_pkt_t *pkt)
const knot_layer_api_t *rrcache_layer(struct kr_module *module)
{
static const knot_layer_api_t _layer = {
.begin = &begin,
.produce = &peek,
.consume = &stash
};
......
......@@ -32,9 +32,11 @@
#define ITERATE_LAYERS(req, func, ...) \
for (unsigned i = 0; i < (req)->ctx->modules->len; ++i) { \
struct kr_module *mod = (req)->ctx->modules->at[i]; \
if (mod->layer) { \
if (mod->layer ) { \
struct knot_layer layer = {.state = (req)->state, .api = mod->layer(mod), .data = (req)}; \
(req)->state = (func)(&layer, ##__VA_ARGS__); \
if (layer.api && layer.api->func) { \
(req)->state = layer.api->func(&layer, ##__VA_ARGS__); \
} \
} \
}
......@@ -368,7 +370,8 @@ int kr_resolve_query(struct kr_request *request, const knot_dname_t *qname, uint
knot_wire_set_rcode(answer->wire, KNOT_RCODE_NOERROR);
/* Expect answer */
return KNOT_STATE_PRODUCE;
ITERATE_LAYERS(request, begin, request);
return request->state;
}
int kr_resolve_consume(struct kr_request *request, knot_pkt_t *packet)
......@@ -404,7 +407,7 @@ int kr_resolve_consume(struct kr_request *request, knot_pkt_t *packet)
if (qname_raw && qry->secret != 0) {
randomized_qname_case(qname_raw, qry->secret);
}
ITERATE_LAYERS(request, knot_layer_consume, packet);
ITERATE_LAYERS(request, consume, packet);
}
/* Resolution failed, invalidate current NS. */
......@@ -427,7 +430,7 @@ int kr_resolve_consume(struct kr_request *request, knot_pkt_t *packet)
qry->flags &= ~(QUERY_CACHED|QUERY_TCP);
}
ITERATE_LAYERS(request, knot_layer_reset);
ITERATE_LAYERS(request, reset);
return kr_rplan_empty(&request->rplan) ? KNOT_STATE_DONE : KNOT_STATE_PRODUCE;
}
......@@ -450,12 +453,12 @@ int kr_resolve_produce(struct kr_request *request, struct sockaddr **dst, int *t
#endif
/* Resolve current query and produce dependent or finish */
ITERATE_LAYERS(request, knot_layer_produce, packet);
ITERATE_LAYERS(request, produce, packet);
if (request->state != KNOT_STATE_FAIL && knot_wire_get_qr(packet->wire)) {
/* Produced an answer, consume it. */
qry->secret = 0;
request->state = KNOT_STATE_CONSUME;
ITERATE_LAYERS(request, knot_layer_consume, packet);
ITERATE_LAYERS(request, consume, packet);
}
switch(request->state) {
case KNOT_STATE_FAIL: return request->state; break;
......@@ -465,7 +468,7 @@ int kr_resolve_produce(struct kr_request *request, struct sockaddr **dst, int *t
if (qry->flags & QUERY_RESOLVED) {
kr_rplan_pop(rplan, qry);
}
ITERATE_LAYERS(request, knot_layer_reset);
ITERATE_LAYERS(request, reset);
return kr_rplan_empty(rplan) ? KNOT_STATE_DONE : KNOT_STATE_PRODUCE;
}
......@@ -498,7 +501,7 @@ int kr_resolve_produce(struct kr_request *request, struct sockaddr **dst, int *t
kr_nsrep_elect(qry, request->ctx);
if (qry->ns.score > KR_NS_MAX_SCORE) {
DEBUG_MSG("=> no valid NS left\n");
ITERATE_LAYERS(request, knot_layer_reset);
ITERATE_LAYERS(request, reset);
kr_rplan_pop(rplan, qry);
return KNOT_STATE_PRODUCE;
}
......@@ -510,7 +513,7 @@ int kr_resolve_produce(struct kr_request *request, struct sockaddr **dst, int *t
qry->flags &= ~(QUERY_AWAIT_IPV6|QUERY_AWAIT_IPV4|QUERY_TCP);
goto ns_election; /* Must try different NS */
}
ITERATE_LAYERS(request, knot_layer_reset);
ITERATE_LAYERS(request, reset);
return KNOT_STATE_PRODUCE;
}
......@@ -552,7 +555,7 @@ int kr_resolve_finish(struct kr_request *request, int state)
knot_wire_set_rcode(answer->wire, KNOT_RCODE_SERVFAIL);
}
}
ITERATE_LAYERS(request, knot_layer_finish);
ITERATE_LAYERS(request, finish);
/* Clean up. */
kr_rplan_deinit(&request->rplan);
return KNOT_STATE_DONE;
......
......@@ -89,12 +89,6 @@ static inline void stat_const_add(struct stat_data *data, enum const_metric key,
const_metrics[key].val += incr;
}
static int begin(knot_layer_t *ctx, void *module_param)
{
ctx->data = module_param;
return ctx->state;
}
static int collect_answer(struct stat_data *data, knot_pkt_t *pkt)
{
stat_const_add(data, metric_answer_total, 1);
......@@ -341,7 +335,6 @@ static char* clear_expiring(void *env, struct kr_module *module, const char *arg
const knot_layer_api_t *stats_layer(struct kr_module *module)
{
static knot_layer_api_t _layer = {
.begin = &begin,
.finish = &collect,
};
/* Store module reference */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment