worker.h 3.52 KB
Newer Older
Marek Vavruša's avatar
Marek Vavruša committed
1
/*  Copyright (C) 2014 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 14 15
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
16 17 18

#pragma once

19
#include "daemon/engine.h"
20
#include "lib/generic/array.h"
21
#include "lib/generic/map.h"
22

23 24 25
/** @internal Number of request within timeout window. */
#define MAX_PENDING (KR_NSREP_MAXADDR + (KR_NSREP_MAXADDR / 2))

26
/** @cond internal Freelist of available mempools. */
27
typedef array_t(void *) mp_freelist_t;
28

29 30
/**
 * Query resolution worker.
31
 */
32
struct worker_ctx {
33 34
	struct engine *engine;
	uv_loop_t *loop;
35 36
	int id;
	int count;
37
	unsigned tcp_pipeline_max;
38 39 40 41 42
#if __linux__
	uint8_t wire_buf[RECVMMSG_BATCH * KNOT_WIRE_MAX_PKTSIZE];
#else
	uint8_t wire_buf[KNOT_WIRE_MAX_PKTSIZE];
#endif
43 44 45 46
	struct {
		size_t concurrent;
		size_t udp;
		size_t tcp;
47 48
		size_t ipv4;
		size_t ipv6;
49 50 51
		size_t queries;
		size_t dropped;
		size_t timeout;
52
	} stats;
53
	map_t outgoing;
54
	map_t outstanding;
55 56 57
	mp_freelist_t pool_mp;
	mp_freelist_t pool_ioreq;
	mp_freelist_t pool_sessions;
58
	knot_mm_t pkt_pool;
59 60
};

61 62 63
/* Worker callback */
typedef void (*worker_cb_t)(struct worker_ctx *worker, struct kr_request *req, void *baton);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/** @internal Query resolution task. */
struct qr_task
{
	struct kr_request req;
	struct worker_ctx *worker;
	struct session *session;
	knot_pkt_t *pktbuf;
	array_t(struct qr_task *) waiting;
	uv_handle_t *pending[MAX_PENDING];
	uint16_t pending_count;
	uint16_t addrlist_count;
	uint16_t addrlist_turn;
	uint16_t timeouts;
	uint16_t iter_count;
	uint16_t bytes_remaining;
	struct sockaddr *addrlist;
	uv_timer_t *timeout;
	worker_cb_t on_complete;
	void *baton;
	struct {
		union {
			struct sockaddr_in ip4;
			struct sockaddr_in6 ip6;
		} addr;
88 89 90 91
		union {
			struct sockaddr_in ip4;
			struct sockaddr_in6 ip6;
		} dst_addr;
92 93 94 95 96 97 98 99
		uv_handle_t *handle;
	} source;
	uint32_t refs;
	bool finished : 1;
	bool leading  : 1;
};
/* @endcond */

100
/**
101 102
 * Process incoming packet (query or answer to subrequest).
 * @return 0 or an error code
103
 */
104
int worker_submit(struct worker_ctx *worker, uv_handle_t *handle, knot_pkt_t *query, const struct sockaddr* addr);
105

106
/**
107
 * Process incoming DNS/TCP message fragment(s).
108 109
 * If the fragment contains only a partial message, it is buffered.
 * If the fragment contains a complete query or completes current fragment, execute it.
110
 * @return 0 or an error code
111
 */
112 113 114 115 116 117 118 119
int worker_process_tcp(struct worker_ctx *worker, uv_handle_t *handle, const uint8_t *msg, ssize_t len);

/**
 * End current DNS/TCP session, this disassociates pending tasks from this session
 * which may be freely closed afterwards.
 */
int worker_end_tcp(struct worker_ctx *worker, uv_handle_t *handle);

120

121 122 123 124
/**
 * Schedule query for resolution.
 * @return 0 or an error code
 */
125
int worker_resolve(struct worker_ctx *worker, knot_pkt_t *query, unsigned options, worker_cb_t on_complete, void *baton);
126

127 128 129 130 131
/** Reserve worker buffers */
int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen);

/** Collect worker mempools */
void worker_reclaim(struct worker_ctx *worker);