Commit 5ac02e0b authored by Marek Vavruša's avatar Marek Vavruša

udp/tcp: moved to overlay, fixed multiple parse call in layers

parent 9b8a3907
......@@ -41,10 +41,11 @@
#include "knot/nameserver/process_query.h"
#include "libknot/dnssec/crypto.h"
#include "libknot/dnssec/random.h"
#include "libknot/processing/overlay.h"
/*! \brief TCP context data. */
typedef struct tcp_context {
knot_layer_t query_ctx; /*!< Query processing context. */
struct knot_overlay overlay;/*!< Query processing overlay. */
server_t *server; /*!< Name server structure. */
struct iovec iov[2]; /*!< TX/RX buffers. */
unsigned client_threshold; /*!< Index of first TCP client. */
......@@ -134,23 +135,24 @@ static int tcp_handle(tcp_context_t *tcp, int fd,
}
/* Create packets. */
mm_ctx_t *mm = tcp->query_ctx.mm;
knot_pkt_t *query = knot_pkt_new(rx->iov_base, rx->iov_len, mm);
mm_ctx_t *mm = tcp->overlay.mm;
knot_pkt_t *ans = knot_pkt_new(tx->iov_base, tx->iov_len, mm);
knot_pkt_t *query = knot_pkt_new(rx->iov_base, rx->iov_len, mm);
/* Create query processing context. */
knot_layer_begin(&tcp->query_ctx, NS_PROC_QUERY, &param);
/* Initialize processing overlay. */
knot_overlay_init(&tcp->overlay, mm);
knot_overlay_add(&tcp->overlay, NS_PROC_QUERY, &param);
/* Input packet. */
int state = knot_layer_in(&tcp->query_ctx, query);
int state = knot_overlay_in(&tcp->overlay, query);
/* Resolve until NOOP or finished. */
ret = KNOT_EOK;
while (state & (NS_PROC_FULL|NS_PROC_FAIL)) {
state = knot_layer_out(&tcp->query_ctx, ans);
state = knot_overlay_out(&tcp->overlay, ans);
/* If it has response, send it. */
if (ans->size > 0) {
/* Send, if response generation passed and wasn't ignored. */
if (ans->size > 0 && !(state & (NS_PROC_FAIL|NS_PROC_NOOP))) {
if (tcp_send_msg(fd, ans->wire, ans->size) != ans->size) {
ret = KNOT_ECONNREFUSED;
break;
......@@ -159,7 +161,8 @@ static int tcp_handle(tcp_context_t *tcp, int fd,
}
/* Reset after processing. */
knot_layer_finish(&tcp->query_ctx);
knot_overlay_finish(&tcp->overlay);
knot_overlay_deinit(&tcp->overlay);
/* Cleanup. */
knot_pkt_free(&query);
......@@ -235,7 +238,7 @@ static int tcp_event_serve(tcp_context_t *tcp, unsigned i)
int ret = tcp_handle(tcp, fd, &tcp->iov[0], &tcp->iov[1]);
/* Flush per-query memory. */
mp_flush(tcp->query_ctx.mm->ctx);
mp_flush(tcp->overlay.mm->ctx);
if (ret == KNOT_EOK) {
/* Update socket activity timer. */
......@@ -316,7 +319,7 @@ int tcp_master(dthread_t *thread)
/* Create TCP answering context. */
tcp.server = handler->server;
tcp.thread_id = handler->thread_id[dt_get_id(thread)];
tcp.query_ctx.mm = &mm;
tcp.overlay.mm = &mm;
/* Prepare structures for bound sockets. */
fdset_init(&tcp.set, list_size(&conf()->ifaces) + CONFIG_XFERS);
......
......@@ -46,7 +46,7 @@
#include "libknot/consts.h"
#include "libknot/packet/pkt.h"
#include "libknot/dnssec/crypto.h"
#include "libknot/processing/layer.h"
#include "libknot/processing/overlay.h"
/* Buffer identifiers. */
enum {
......@@ -57,9 +57,9 @@ enum {
/*! \brief UDP context data. */
typedef struct udp_context {
knot_layer_t layer; /*!< Query processing context. */
server_t *server; /*!< Name server structure. */
unsigned thread_id; /*!< Thread identifier. */
struct knot_overlay overlay; /*!< Query processing overlay. */
server_t *server; /*!< Name server structure. */
unsigned thread_id; /*!< Thread identifier. */
} udp_context_t;
/* FD_COPY macro compat. */
......@@ -133,19 +133,20 @@ void udp_handle(udp_context_t *udp, int fd, struct sockaddr_storage *ss,
}
/* Create packets. */
mm_ctx_t *mm = udp->layer.mm;
mm_ctx_t *mm = udp->overlay.mm;
knot_pkt_t *query = knot_pkt_new(rx->iov_base, rx->iov_len, mm);
knot_pkt_t *ans = knot_pkt_new(tx->iov_base, tx->iov_len, mm);
/* Create query processing context. */
knot_layer_begin(&udp->layer, NS_PROC_QUERY, &param);
knot_overlay_init(&udp->overlay, mm);
knot_overlay_add(&udp->overlay, NS_PROC_QUERY, &param);
/* Input packet. */
int state = knot_layer_in(&udp->layer, query);
int state = knot_overlay_in(&udp->overlay, query);
/* Process answer. */
while (state & (NS_PROC_FULL|NS_PROC_FAIL)) {
state = knot_layer_out(&udp->layer, ans);
state = knot_overlay_out(&udp->overlay, ans);
}
/* Send response only if finished successfuly. */
......@@ -155,8 +156,9 @@ void udp_handle(udp_context_t *udp, int fd, struct sockaddr_storage *ss,
tx->iov_len = 0;
}
/* Reset context. */
knot_layer_finish(&udp->layer);
/* Reset after processing. */
knot_overlay_finish(&udp->overlay);
knot_overlay_deinit(&udp->overlay);
/* Cleanup. */
knot_pkt_free(&query);
......@@ -509,7 +511,7 @@ int udp_master(dthread_t *thread)
/* Create big enough memory cushion. */
mm_ctx_t mm;
mm_ctx_mempool(&mm, 4 * sizeof(knot_pkt_t));
udp.layer.mm = &mm;
udp.overlay.mm = &mm;
/* Chose select as epoll/kqueue has larger overhead for a
* single or handful of sockets. */
......
......@@ -78,8 +78,6 @@ int knot_layer_in(knot_layer_t *ctx, knot_pkt_t *pkt)
return ctx->state;
}
knot_pkt_parse(pkt, 0);
LAYER_CALL(ctx, in, pkt);
dbg_ns("%s -> %s\n", __func__, LAYER_STATE_STR(ctx->state));
return ctx->state;
......
......@@ -26,8 +26,8 @@ void knot_overlay_deinit(struct knot_overlay *overlay)
}
}
int knot_overlay_add(struct knot_overlay *overlay, void *module_param,
const knot_layer_api_t *module)
int knot_overlay_add(struct knot_overlay *overlay, const knot_layer_api_t *module,
void *module_param)
{
struct knot_layer *layer = mm_alloc(overlay->mm, sizeof(struct knot_layer));
if (layer == NULL) {
......@@ -57,6 +57,7 @@ int knot_overlay_finish(struct knot_overlay *overlay)
int knot_overlay_in(struct knot_overlay *overlay, knot_pkt_t *pkt)
{
knot_pkt_parse(pkt, 0);
ITERATE_LAYERS(overlay, knot_layer_in, pkt);
}
......
......@@ -51,26 +51,26 @@ void knot_overlay_deinit(struct knot_overlay *overlay);
/*!
* \brief Add an overlay on top of the list and begin execution.
* \fn knot_process_begin
* \fn knot_layer_begin
*
* \param overlay
* \param module_param
* \param module
* \param module_param
*
* \return KNOT_EOK or an error
*/
int knot_overlay_add(struct knot_overlay *overlay, void *module_param,
const knot_layer_api_t *module);
int knot_overlay_add(struct knot_overlay *overlay, const knot_layer_api_t *module,
void *module_param);
/*!
* \brief Reset processing layers.
* \fn knot_process_reset
* \fn knot_layer_reset
*/
int knot_overlay_reset(struct knot_overlay *overlay);
/*!
* \brief Finish processing layers.
* \fn knot_process_finish
* \fn knot_layer_finish
*/
int knot_overlay_finish(struct knot_overlay *overlay);
......
......@@ -182,7 +182,7 @@ bool knot_requestor_finished(struct knot_requestor *requestor)
int knot_requestor_overlay(struct knot_requestor *requestor,
const knot_layer_api_t *proc, void *param)
{
return knot_overlay_add(&requestor->overlay, param, proc);
return knot_overlay_add(&requestor->overlay, proc, param);
}
int knot_requestor_enqueue(struct knot_requestor *requestor, struct knot_request *request)
......
......@@ -80,8 +80,8 @@ int main(int argc, char *argv[])
knot_overlay_init(&overlay, &mm);
/* Add FSMs. */
knot_overlay_add(&overlay, NULL, &fsm1_module);
knot_overlay_add(&overlay, NULL, &fsm2_module);
knot_overlay_add(&overlay, &fsm1_module, NULL);
knot_overlay_add(&overlay, &fsm2_module, NULL);
/* Run the sequence. */
int state = knot_overlay_in(&overlay, buf);
......
......@@ -31,7 +31,7 @@
knot_pkt_clear(pkt)
#define TEST_EXEC(expect, info) {\
pkt->parsed = pkt->size; /* Simulate parsed packet. */ \
knot_pkt_parse(pkt, 0); \
int state = knot_layer_in(proc, pkt); \
is_int((expect), state, "proc_answer: " info); \
}
......
......@@ -49,6 +49,7 @@ static void exec_query(knot_layer_t *query_ctx, const char *name,
assert(answer);
/* Input packet. */
knot_pkt_parse(query, 0);
int state = knot_layer_in(query_ctx, query);
ok(state & (NS_PROC_FULL|NS_PROC_FAIL), "ns: process %s query", name);
......
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