Commit 22e477e6 authored by Marek Vavruša's avatar Marek Vavruša

processing: mm is pointer, updated api, tests

parent c6394a0a
......@@ -28,7 +28,7 @@ static void answer_data_init(knot_process_t *ctx, void *module_param)
/* Initialize persistent data. */
struct answer_data *data = ANSWER_DATA(ctx);
memset(data, 0, sizeof(struct answer_data));
data->mm = &ctx->mm;
data->mm = ctx->mm;
data->param = module_param;
}
......@@ -47,7 +47,7 @@ static int process_answer_begin(knot_process_t *ctx, void *module_param)
/* Initialize context. */
assert(ctx);
ctx->type = NS_PROC_ANSWER_ID;
ctx->data = mm_alloc(&ctx->mm, sizeof(struct answer_data));
ctx->data = mm_alloc(ctx->mm, sizeof(struct answer_data));
/* Initialize persistent data. */
answer_data_init(ctx, module_param);
......@@ -79,7 +79,7 @@ static int process_answer_reset(knot_process_t *ctx)
static int process_answer_finish(knot_process_t *ctx)
{
process_answer_reset(ctx);
mm_free(&ctx->mm, ctx->data);
mm_free(ctx->mm, ctx->data);
ctx->data = NULL;
return NS_PROC_NOOP;
......
......@@ -26,7 +26,7 @@ static void query_data_init(knot_process_t *ctx, void *module_param)
/* Initialize persistent data. */
struct query_data *data = QUERY_DATA(ctx);
memset(data, 0, sizeof(struct query_data));
data->mm = &ctx->mm;
data->mm = ctx->mm;
data->param = (struct process_query_param*)module_param;
/* Initialize lists. */
......@@ -39,7 +39,7 @@ static int process_query_begin(knot_process_t *ctx, void *module_param)
/* Initialize context. */
assert(ctx);
ctx->type = NS_PROC_QUERY_ID;
ctx->data = mm_alloc(&ctx->mm, sizeof(struct query_data));
ctx->data = mm_alloc(ctx->mm, sizeof(struct query_data));
/* Initialize persistent data. */
query_data_init(ctx, module_param);
......@@ -74,7 +74,7 @@ static int process_query_reset(knot_process_t *ctx)
static int process_query_finish(knot_process_t *ctx)
{
process_query_reset(ctx);
ctx->mm.free(ctx->data);
mm_free(ctx->mm, ctx->data);
ctx->data = NULL;
return NS_PROC_NOOP;
......
......@@ -137,13 +137,13 @@ static int tcp_handle(tcp_context_t *tcp, int fd,
knot_process_begin(&tcp->query_ctx, &param, NS_PROC_QUERY);
/* Input packet. */
int state = knot_process_in(rx->iov_base, rx->iov_len, &tcp->query_ctx);
int state = knot_process_in(&tcp->query_ctx, rx->iov_base, rx->iov_len);
/* Resolve until NOOP or finished. */
ret = KNOT_EOK;
while (state & (NS_PROC_FULL|NS_PROC_FAIL)) {
uint16_t tx_len = tx->iov_len;
state = knot_process_out(tx->iov_base, &tx_len, &tcp->query_ctx);
state = knot_process_out(&tcp->query_ctx, tx->iov_base, &tx_len);
/* If it has response, send it. */
if (tx_len > 0) {
......@@ -188,7 +188,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->query_ctx.mm->ctx);
if (ret == KNOT_EOK) {
/* Update socket activity timer. */
......@@ -302,12 +302,14 @@ int tcp_master(dthread_t *thread)
tcp_context_t tcp;
memset(&tcp, 0, sizeof(tcp_context_t));
/* Create big enough memory cushion. */
mm_ctx_t mm;
mm_ctx_mempool(&mm, 4 * sizeof(knot_pkt_t));
/* Create TCP answering context. */
tcp.server = handler->server;
tcp.thread_id = handler->thread_id[dt_get_id(thread)];
/* Create big enough memory cushion. */
mm_ctx_mempool(&tcp.query_ctx.mm, 4 * sizeof(knot_pkt_t));
tcp.query_ctx.mm = &mm;
/* Prepare structures for bound sockets. */
fdset_init(&tcp.set, list_size(&conf()->ifaces) + CONFIG_XFERS);
......@@ -366,7 +368,7 @@ int tcp_master(dthread_t *thread)
finish:
free(tcp.iov[0].iov_base);
free(tcp.iov[1].iov_base);
mp_delete(tcp.query_ctx.mm.ctx);
mp_delete(mm.ctx);
fdset_clear(&tcp.set);
ref_release(ref);
......
......@@ -136,18 +136,18 @@ void udp_handle(udp_context_t *udp, int fd, struct sockaddr_storage *ss,
knot_process_begin(&udp->query_ctx, &param, NS_PROC_QUERY);
/* Input packet. */
int state = knot_process_in(rx->iov_base, rx->iov_len, &udp->query_ctx);
int state = knot_process_in(&udp->query_ctx, rx->iov_base, rx->iov_len);
/* Process answer. */
uint16_t tx_len = tx->iov_len;
if (state == NS_PROC_FULL) {
state = knot_process_out(tx->iov_base, &tx_len, &udp->query_ctx);
state = knot_process_out(&udp->query_ctx, tx->iov_base, &tx_len);
}
/* Process error response (if failed). */
if (state == NS_PROC_FAIL) {
tx_len = tx->iov_len; /* Reset size. */
state = knot_process_out(tx->iov_base, &tx_len, &udp->query_ctx);
state = knot_process_out(&udp->query_ctx, tx->iov_base, &tx_len);
}
/* Send response only if finished successfuly. */
......@@ -505,7 +505,9 @@ int udp_master(dthread_t *thread)
udp.thread_id = handler->thread_id[thr_id];
/* Create big enough memory cushion. */
mm_ctx_mempool(&udp.query_ctx.mm, 4 * sizeof(knot_pkt_t));
mm_ctx_t mm;
mm_ctx_mempool(&mm, 4 * sizeof(knot_pkt_t));
udp.query_ctx.mm = &mm;
/* Chose select as epoll/kqueue has larger overhead for a
* single or handful of sockets. */
......@@ -550,7 +552,7 @@ int udp_master(dthread_t *thread)
if ((rcvd = _udp_recv(fd, rq)) > 0) {
_udp_handle(&udp, rq);
/* Flush allocated memory. */
mp_flush(udp.query_ctx.mm.ctx);
mp_flush(mm.ctx);
_udp_send(rq);
udp_pps_sample(rcvd, thr_id);
}
......@@ -560,7 +562,7 @@ int udp_master(dthread_t *thread)
_udp_deinit(rq);
forget_ifaces(ref, &fds, maxfd);
mp_delete(udp.query_ctx.mm.ctx);
mp_delete(mm.ctx);
return KNOT_EOK;
}
......
......@@ -75,14 +75,14 @@ int knot_process_finish(knot_process_t *ctx)
return ctx->state;
}
int knot_process_in(const uint8_t *wire, uint16_t wire_len, knot_process_t *ctx)
int knot_process_in(knot_process_t *ctx, const uint8_t *wire, uint16_t wire_len)
{
/* Only if expecting data. */
if (ctx->state != NS_PROC_MORE) {
return NS_PROC_NOOP;
}
knot_pkt_t *pkt = knot_pkt_new((uint8_t *)wire, wire_len, &ctx->mm);
knot_pkt_t *pkt = knot_pkt_new((uint8_t *)wire, wire_len, ctx->mm);
knot_pkt_parse(pkt, 0);
ctx->state = ctx->module->in(pkt, ctx);
......@@ -90,9 +90,9 @@ int knot_process_in(const uint8_t *wire, uint16_t wire_len, knot_process_t *ctx)
return ctx->state;
}
int knot_process_out(uint8_t *wire, uint16_t *wire_len, knot_process_t *ctx)
int knot_process_out(knot_process_t *ctx, uint8_t *wire, uint16_t *wire_len)
{
knot_pkt_t *pkt = knot_pkt_new(wire, *wire_len, &ctx->mm);
knot_pkt_t *pkt = knot_pkt_new(wire, *wire_len, ctx->mm);
switch (ctx->state) {
case NS_PROC_FULL: ctx->state = ctx->module->out(pkt, ctx); break;
......
......@@ -52,7 +52,7 @@ typedef struct knot_process_context
{
uint16_t state; /* Bitmap of enum knot_process_state. */
uint16_t type; /* Module identifier. */
mm_ctx_t mm; /* Processing memory context. */
mm_ctx_t *mm; /* Processing memory context. */
/* Module specific. */
void *data;
......@@ -117,7 +117,7 @@ int knot_process_finish(knot_process_t *ctx);
* \param ctx Context.
* \return (module specific state)
*/
int knot_process_in(const uint8_t *wire, uint16_t wire_len, knot_process_t *ctx);
int knot_process_in(knot_process_t *ctx, const uint8_t *wire, uint16_t wire_len);
/*!
* \brief Write out output from packet processing.
......@@ -129,6 +129,6 @@ int knot_process_in(const uint8_t *wire, uint16_t wire_len, knot_process_t *ctx)
* \param ctx Context.
* \return (module specific state)
*/
int knot_process_out(uint8_t *wire, uint16_t *wire_len, knot_process_t *ctx);
int knot_process_out(knot_process_t *ctx, uint8_t *wire, uint16_t *wire_len);
/*! @} */
......@@ -114,7 +114,7 @@ static int request_send(struct knot_request *request, const struct timeval *time
wire = query->wire;
wire_len = query->size;
} else {
request->state = knot_process_out(wire, &wire_len, &request->process);
request->state = knot_process_out(&request->process, wire, &wire_len);
}
/* Send query. */
......@@ -223,12 +223,7 @@ int knot_requestor_enqueue(struct knot_requestor *requestor, struct knot_request
/* Form a pending request. */
request->data.fd = fd;
if (requestor->mm != NULL) {
memcpy(&request->process.mm, requestor->mm, sizeof(mm_ctx_t));
} else {
mm_ctx_init(&request->process.mm);
}
request->process.mm = requestor->mm;
request->state = knot_process_begin(&request->process, param, requestor->module);
/* We have a query to be sent. */
......@@ -271,7 +266,7 @@ static int exec_request(struct knot_request *last, struct timeval *timeout)
return rcvd;
}
last->state = knot_process_in(last->pkt_buf, rcvd, &last->process);
last->state = knot_process_in(&last->process, last->pkt_buf, rcvd);
}
/* Expect complete request. */
......
......@@ -31,7 +31,7 @@
#define TEST_EXEC(expect, info) {\
pkt->parsed = pkt->size; /* Simulate parsed packet. */ \
int state = knot_process_in(pkt->wire, pkt->size, proc); \
int state = knot_process_in(proc, pkt->wire, pkt->size); \
is_int((expect), state, "proc_answer: " info); \
}
......@@ -59,7 +59,7 @@ static void test_specific(knot_pkt_t *pkt, knot_process_t *proc, struct process_
{
/* Set specific SOA query. */
uint16_t query_id = 0xBEEF;
knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MIN_PKTSIZE, &proc->mm);
knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MIN_PKTSIZE, proc->mm);
assert(query);
knot_pkt_put_question(query, ROOT_DNAME, KNOT_CLASS_IN, KNOT_RRTYPE_SOA);
knot_wire_set_id(query->wire, query_id);
......@@ -79,7 +79,7 @@ static void test_specific(knot_pkt_t *pkt, knot_process_t *proc, struct process_
static void test_inclass(knot_pkt_t *pkt, knot_process_t *proc, struct process_answer_param *param)
{
/* Set specific SOA query. */
knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MIN_PKTSIZE, &proc->mm);
knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MIN_PKTSIZE, proc->mm);
assert(query);
knot_pkt_put_question(query, ROOT_DNAME, KNOT_CLASS_IN, KNOT_RRTYPE_SOA);
param->query = query;
......@@ -110,13 +110,16 @@ int main(int argc, char *argv[])
plan(3 + TEST_COUNT);
/* Create processing context. */
mm_ctx_t mm;
mm_ctx_mempool(&mm, sizeof(knot_pkt_t));
knot_process_t proc;
memset(&proc, 0, sizeof(knot_process_t));
mm_ctx_mempool(&proc.mm, sizeof(knot_pkt_t));
proc.mm = &mm;
/* Create fake server environment. */
server_t server;
int ret = create_fake_server(&server, &proc.mm);
int ret = create_fake_server(&server, proc.mm);
ok(ret == KNOT_EOK, "proc_answer: fake server initialization");
/* Prepare. */
......@@ -126,7 +129,7 @@ int main(int argc, char *argv[])
struct process_answer_param param = {0};
param.remote = &remote;
param.zone = knot_zonedb_find(server.zone_db, ROOT_DNAME);
knot_pkt_t *pkt = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, &proc.mm);
knot_pkt_t *pkt = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, proc.mm);
/* Begin processing. */
int state = knot_process_begin(&proc, &param, NS_PROC_ANSWER);
......@@ -151,7 +154,7 @@ int main(int argc, char *argv[])
ok(state == NS_PROC_NOOP, "proc_answer: processing end" );
/* Cleanup. */
mp_delete((struct mempool *)proc.mm.ctx);
mp_delete(mm.ctx);
server_deinit(&server);
conf_free(conf());
......
......@@ -49,16 +49,16 @@ static void exec_query(knot_process_t *query_ctx, const char *name,
uint8_t answer[KNOT_WIRE_MAX_PKTSIZE];
/* Input packet. */
int state = knot_process_in(query, query_len, query_ctx);
int state = knot_process_in(query_ctx, query, query_len);
ok(state & (NS_PROC_FULL|NS_PROC_FAIL), "ns: process %s query", name);
/* Create answer. */
state = knot_process_out(answer, &answer_len, query_ctx);
state = knot_process_out(query_ctx, answer, &answer_len);
if (state & NS_PROC_FAIL) {
/* Allow 1 generic error response. */
answer_len = KNOT_WIRE_MAX_PKTSIZE;
state = knot_process_out(answer, &answer_len, query_ctx);
state = knot_process_out(query_ctx, answer, &answer_len);
}
ok(state == NS_PROC_DONE, "ns: answer %s query", name);
......@@ -76,20 +76,23 @@ int main(int argc, char *argv[])
{
plan(8*6 + 4); /* exec_query = 6 TAP tests */
mm_ctx_t mm;
mm_ctx_mempool(&mm, sizeof(knot_pkt_t));
/* Create processing context. */
knot_process_t proc;
memset(&proc, 0, sizeof(knot_process_t));
mm_ctx_mempool(&proc.mm, sizeof(knot_pkt_t));
proc.mm = &mm;
/* Create fake server environment. */
server_t server;
int ret = create_fake_server(&server, &proc.mm);
int ret = create_fake_server(&server, proc.mm);
ok(ret == KNOT_EOK, "ns: fake server initialization");
zone_t *zone = knot_zonedb_find(server.zone_db, ROOT_DNAME);
/* Prepare. */
knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, &proc.mm);
knot_pkt_t *query = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, proc.mm);
/* Create query processing parameter. */
struct sockaddr_storage ss;
......@@ -154,13 +157,13 @@ int main(int argc, char *argv[])
knot_process_reset(&proc);
knot_pkt_clear(query);
knot_pkt_put_question(query, ROOT_DNAME, KNOT_CLASS_IN, KNOT_RRTYPE_SOA);
int state = knot_process_in(query->wire, KNOT_WIRE_HEADER_SIZE - 1, &proc);
int state = knot_process_in(&proc, query->wire, KNOT_WIRE_HEADER_SIZE - 1);
ok(state == NS_PROC_NOOP, "ns: IN/less-than-header query ignored");
/* Query processor (response, ignore). */
knot_process_reset(&proc);
knot_wire_set_qr(query->wire);
state = knot_process_in(query->wire, query->size, &proc);
state = knot_process_in(&proc, query->wire, query->size);
ok(state == NS_PROC_NOOP, "ns: IN/less-than-header query ignored");
/* Finish. */
......@@ -168,7 +171,7 @@ int main(int argc, char *argv[])
ok(state == NS_PROC_NOOP, "ns: processing end" );
/* Cleanup. */
mp_delete((struct mempool *)proc.mm.ctx);
mp_delete((struct mempool *)mm.ctx);
server_deinit(&server);
return 0;
......
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